1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef HarfBuzzShaper_h 32 #define HarfBuzzShaper_h 33 34 #include "hb.h" 35 #include "platform/geometry/FloatBoxExtent.h" 36 #include "platform/geometry/FloatPoint.h" 37 #include "platform/text/TextRun.h" 38 #include "wtf/HashSet.h" 39 #include "wtf/OwnPtr.h" 40 #include "wtf/PassOwnPtr.h" 41 #include "wtf/unicode/CharacterNames.h" 42 #include "wtf/Vector.h" 43 44 #include <unicode/uscript.h> 45 46 namespace blink { 47 48 class Font; 49 class GlyphBuffer; 50 class SimpleFontData; 51 52 class HarfBuzzShaper FINAL { 53 public: 54 enum ForTextEmphasisOrNot { 55 NotForTextEmphasis, 56 ForTextEmphasis 57 }; 58 59 HarfBuzzShaper(const Font*, const TextRun&, ForTextEmphasisOrNot = NotForTextEmphasis, HashSet<const SimpleFontData*>* fallbackFonts = 0); 60 61 void setDrawRange(int from, int to); 62 bool shape(GlyphBuffer* = 0); 63 FloatPoint adjustStartPoint(const FloatPoint&); 64 float totalWidth() { return m_totalWidth; } 65 int offsetForPosition(float targetX); 66 FloatRect selectionRect(const FloatPoint&, int height, int from, int to); 67 FloatBoxExtent glyphBoundingBox() const { return m_glyphBoundingBox; } 68 69 private: 70 class HarfBuzzRun { 71 public: 72 HarfBuzzRun(const HarfBuzzRun&); 73 ~HarfBuzzRun(); 74 75 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, unsigned startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t script) 76 { 77 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters, direction, script)); 78 } 79 80 void applyShapeResult(hb_buffer_t*); 81 void setGlyphAndPositions(unsigned index, uint16_t glyphId, float advance, float offsetX, float offsetY); 82 void setWidth(float width) { m_width = width; } 83 84 int characterIndexForXPosition(float targetX); 85 float xPositionForOffset(unsigned offset); 86 87 const SimpleFontData* fontData() { return m_fontData; } 88 unsigned startIndex() const { return m_startIndex; } 89 unsigned numCharacters() const { return m_numCharacters; } 90 unsigned numGlyphs() const { return m_numGlyphs; } 91 uint16_t* glyphs() { return &m_glyphs[0]; } 92 float* advances() { return &m_advances[0]; } 93 FloatPoint* offsets() { return &m_offsets[0]; } 94 bool hasGlyphToCharacterIndexes() const 95 { 96 return m_glyphToCharacterIndexes.size() > 0; 97 } 98 uint16_t* glyphToCharacterIndexes() 99 { 100 return &m_glyphToCharacterIndexes[0]; 101 } 102 float width() { return m_width; } 103 hb_direction_t direction() { return m_direction; } 104 bool rtl() { return m_direction == HB_DIRECTION_RTL; } 105 hb_script_t script() { return m_script; } 106 107 private: 108 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numCharacters, hb_direction_t, hb_script_t); 109 110 const SimpleFontData* m_fontData; 111 unsigned m_startIndex; 112 size_t m_numCharacters; 113 unsigned m_numGlyphs; 114 hb_direction_t m_direction; 115 hb_script_t m_script; 116 Vector<uint16_t, 256> m_glyphs; 117 Vector<float, 256> m_advances; 118 Vector<uint16_t, 256> m_glyphToCharacterIndexes; 119 Vector<FloatPoint, 256> m_offsets; 120 float m_width; 121 }; 122 123 int determineWordBreakSpacing(); 124 // setPadding sets a number of pixels to be distributed across the TextRun. 125 // WebKit uses this to justify text. 126 void setPadding(int); 127 128 void setFontFeatures(); 129 130 bool createHarfBuzzRuns(); 131 bool shapeHarfBuzzRuns(); 132 bool fillGlyphBuffer(GlyphBuffer*); 133 void fillGlyphBufferFromHarfBuzzRun(GlyphBuffer*, HarfBuzzRun*, FloatPoint& firstOffsetOfNextRun); 134 void fillGlyphBufferForTextEmphasis(GlyphBuffer*, HarfBuzzRun* currentRun); 135 void setGlyphPositionsForHarfBuzzRun(HarfBuzzRun*, hb_buffer_t*); 136 void addHarfBuzzRun(unsigned startCharacter, unsigned endCharacter, const SimpleFontData*, UScriptCode); 137 138 const Font* m_font; 139 OwnPtr<UChar[]> m_normalizedBuffer; 140 unsigned m_normalizedBufferLength; 141 const TextRun& m_run; 142 143 float m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word break. 144 float m_padding; // Pixels to be distributed over the line at word breaks. 145 float m_padPerWordBreak; // Pixels to be added to each word break. 146 float m_padError; // m_padPerWordBreak might have a fractional component. Since we only add a whole number of padding pixels at each word break we accumulate error. This is the number of pixels that we are behind so far. 147 float m_letterSpacing; // Pixels to be added after each glyph. 148 149 Vector<hb_feature_t, 4> m_features; 150 Vector<OwnPtr<HarfBuzzRun>, 16> m_harfBuzzRuns; 151 152 FloatPoint m_startOffset; 153 154 int m_fromIndex; 155 int m_toIndex; 156 157 ForTextEmphasisOrNot m_forTextEmphasis; 158 159 float m_totalWidth; 160 FloatBoxExtent m_glyphBoundingBox; 161 HashSet<const SimpleFontData*>* m_fallbackFonts; 162 163 friend struct CachedShapingResults; 164 }; 165 166 } // namespace blink 167 168 #endif // HarfBuzzShaper_h 169