OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 #include "wtf/Vector.h" | 42 #include "wtf/Vector.h" |
43 #include "wtf/unicode/CharacterNames.h" | 43 #include "wtf/unicode/CharacterNames.h" |
44 | 44 |
45 #include <unicode/uscript.h> | 45 #include <unicode/uscript.h> |
46 | 46 |
47 namespace blink { | 47 namespace blink { |
48 | 48 |
49 class Font; | 49 class Font; |
50 class GlyphBuffer; | 50 class GlyphBuffer; |
51 class SimpleFontData; | 51 class SimpleFontData; |
| 52 class HarfBuzzShaper; |
| 53 |
| 54 class PLATFORM_EXPORT ShapeResult : public RefCounted<ShapeResult> { |
| 55 public: |
| 56 ShapeResult(): m_width(0), m_numGlyphs(0) { } |
| 57 ~ShapeResult(); |
| 58 |
| 59 float width() { return m_width; } |
| 60 FloatRect bounds() { return m_glyphBoundingBox; } |
| 61 float fillGlyphBuffer(GlyphBuffer*, float initialAdvance, unsigned from, |
| 62 unsigned to, unsigned runOffset = 0); |
| 63 float fillGlyphBufferForTextEmphasis(GlyphBuffer*, float initialAdvance, |
| 64 const TextRun&, const GlyphData*, unsigned from, unsigned to, |
| 65 unsigned runOffset = 0); |
| 66 FloatRect selectionRect(const FloatPoint&, int height, unsigned from, |
| 67 unsigned to); |
| 68 int offsetForPosition(float targetX); |
| 69 unsigned numCharacters() const { return m_numCharacters; } |
| 70 |
| 71 unsigned numberOfRunsForTesting() const; |
| 72 bool runInfoForTesting(unsigned runIndex, unsigned& startIndex, |
| 73 unsigned& numGlyphs, hb_script_t&); |
| 74 uint16_t glyphForTesting(unsigned runIndex, size_t glyphIndex); |
| 75 float advanceForTesting(unsigned runIndex, size_t glyphIndex); |
| 76 |
| 77 private: |
| 78 struct RunInfo; |
| 79 |
| 80 template<TextDirection> |
| 81 float fillGlyphBufferForRun(GlyphBuffer*, const RunInfo*, |
| 82 float initialAdvance, unsigned from, unsigned to, unsigned runOffset); |
| 83 float fillGlyphBufferForTextEmphasisRun(GlyphBuffer*, const RunInfo*, |
| 84 const TextRun&, const GlyphData*, float initialAdvance, unsigned from, |
| 85 unsigned to, unsigned runOffset); |
| 86 |
| 87 float m_width; |
| 88 FloatRect m_glyphBoundingBox; |
| 89 Vector<RunInfo*> m_runs; |
| 90 |
| 91 unsigned m_numCharacters; |
| 92 unsigned m_numGlyphs : 31; |
| 93 |
| 94 // Overall direction for the TextRun, dictates which order each individual |
| 95 // sub run (represented by RunInfo structs in the m_runs vector) can have a |
| 96 // different text direction. |
| 97 unsigned m_direction : 1; |
| 98 |
| 99 friend class HarfBuzzShaper; |
| 100 }; |
52 | 101 |
53 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { | 102 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { |
54 public: | 103 public: |
55 HarfBuzzShaper(const Font*, const TextRun&, const GlyphData* emphasisData =
nullptr, | 104 HarfBuzzShaper(const Font*, const TextRun&, |
56 HashSet<const SimpleFontData*>* fallbackFonts = nullptr, FloatRect* = nu
llptr); | 105 HashSet<const SimpleFontData*>* fallbackFonts = nullptr); |
57 | 106 PassRefPtr<ShapeResult> shapeResult(); |
58 void setDrawRange(int from, int to); | 107 ~HarfBuzzShaper() { } |
59 bool shape(GlyphBuffer* = 0); | |
60 float totalWidth() { return m_totalWidth; } | |
61 int offsetForPosition(float targetX); | |
62 FloatRect selectionRect(const FloatPoint&, int height, int from, int to); | |
63 | |
64 unsigned numberOfRunsForTesting() const | |
65 { | |
66 return m_harfBuzzRuns.size(); | |
67 } | |
68 | |
69 bool runInfoForTesting(unsigned runIndex, unsigned& startIndex, | |
70 unsigned& numGlyphs, hb_script_t& script) | |
71 { | |
72 if (runIndex < m_harfBuzzRuns.size()) { | |
73 startIndex = m_harfBuzzRuns[runIndex]->startIndex(); | |
74 numGlyphs = m_harfBuzzRuns[runIndex]->numGlyphs(); | |
75 script = m_harfBuzzRuns[runIndex]->script(); | |
76 return true; | |
77 } | |
78 return false; | |
79 } | |
80 | |
81 uint16_t glyphForTesting(unsigned runIndex, size_t glyphIndex) | |
82 { | |
83 return m_harfBuzzRuns[runIndex]->glyphData(glyphIndex).glyph; | |
84 } | |
85 | |
86 float advanceForTesting(unsigned runIndex, size_t glyphIndex) | |
87 { | |
88 return m_harfBuzzRuns[runIndex]->glyphData(glyphIndex).advance; | |
89 } | |
90 | 108 |
91 private: | 109 private: |
92 struct HarfBuzzRunGlyphData { | |
93 uint16_t glyph; | |
94 uint16_t characterIndex; | |
95 float advance; | |
96 FloatSize offset; | |
97 }; | |
98 | |
99 class PLATFORM_EXPORT HarfBuzzRun { | 110 class PLATFORM_EXPORT HarfBuzzRun { |
100 public: | 111 public: |
101 HarfBuzzRun(const HarfBuzzRun&); | 112 HarfBuzzRun(const HarfBuzzRun&); |
102 ~HarfBuzzRun(); | 113 ~HarfBuzzRun(); |
103 | 114 |
104 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) | 115 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) |
105 { | 116 { |
106 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); | 117 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); |
107 } | 118 } |
108 | 119 |
109 void applyShapeResult(hb_buffer_t*); | |
110 void setGlyphAndPositions(unsigned index, uint16_t glyphId, float advanc
e, float offsetX, float offsetY); | |
111 void setWidth(float width) { m_width = width; } | |
112 void addAdvance(unsigned index, float advance); | |
113 | |
114 int characterIndexForXPosition(float targetX); | |
115 float xPositionForOffset(unsigned offset); | |
116 | |
117 const SimpleFontData* fontData() { return m_fontData; } | 120 const SimpleFontData* fontData() { return m_fontData; } |
118 unsigned startIndex() const { return m_startIndex; } | 121 unsigned startIndex() const { return m_startIndex; } |
119 unsigned numCharacters() const { return m_numCharacters; } | 122 unsigned numCharacters() const { return m_numCharacters; } |
120 unsigned numGlyphs() const { return m_numGlyphs; } | 123 unsigned numGlyphs() const { return m_numGlyphs; } |
121 Vector<HarfBuzzRunGlyphData, 256>& glyphData() { return m_glyphData; } | 124 void setNumGlyphs(unsigned num) { m_numGlyphs = num; } |
122 HarfBuzzRunGlyphData& glyphData(size_t glyphIndex) { return m_glyphData[
glyphIndex]; } | |
123 size_t glyphToCharacterIndex(size_t i) const { return m_startIndex + m_g
lyphData[i].characterIndex; } | |
124 float width() { return m_width; } | |
125 hb_direction_t direction() { return m_direction; } | 125 hb_direction_t direction() { return m_direction; } |
126 bool rtl() { return m_direction == HB_DIRECTION_RTL; } | 126 bool rtl() { return m_direction == HB_DIRECTION_RTL; } |
127 hb_script_t script() { return m_script; } | 127 hb_script_t script() { return m_script; } |
128 | 128 |
129 private: | 129 private: |
130 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numChar
acters, hb_direction_t, hb_script_t); | 130 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numChar
acters, hb_direction_t, hb_script_t); |
131 | 131 |
132 const SimpleFontData* m_fontData; | 132 const SimpleFontData* m_fontData; |
133 unsigned m_startIndex; | 133 unsigned m_startIndex; |
134 size_t m_numCharacters; | 134 size_t m_numCharacters; |
135 unsigned m_numGlyphs; | 135 unsigned m_numGlyphs; |
136 hb_direction_t m_direction; | 136 hb_direction_t m_direction; |
137 hb_script_t m_script; | 137 hb_script_t m_script; |
138 Vector<HarfBuzzRunGlyphData, 256> m_glyphData; | |
139 float m_width; | |
140 }; | 138 }; |
141 | 139 |
142 float nextExpansionPerOpportunity(); | 140 float nextExpansionPerOpportunity(); |
143 // setPadding sets a number of pixels to be distributed across the TextRun. | |
144 // WebKit uses this to justify text. | |
145 void setExpansion(float); | 141 void setExpansion(float); |
146 | |
147 void setFontFeatures(); | 142 void setFontFeatures(); |
148 | 143 |
149 bool createHarfBuzzRuns(); | 144 bool createHarfBuzzRuns(); |
150 bool createHarfBuzzRunsForSingleCharacter(); | 145 bool createHarfBuzzRunsForSingleCharacter(); |
151 bool shapeHarfBuzzRuns(); | 146 bool shapeHarfBuzzRuns(ShapeResult*); |
152 bool fillGlyphBuffer(GlyphBuffer*); | 147 void shapeResult(ShapeResult*, unsigned, HarfBuzzRun*, hb_buffer_t*); |
153 float fillGlyphBufferFromHarfBuzzRun(GlyphBuffer*, HarfBuzzRun*, float initi
alAdvance); | 148 float adjustSpacing(ShapeResult::RunInfo*, size_t glyphIndex, unsigned curre
ntCharacterIndex, float& offsetX, float& totalAdvance); |
154 float fillGlyphBufferForTextEmphasis(GlyphBuffer*, HarfBuzzRun*, float initi
alAdvance); | |
155 void setGlyphPositionsForHarfBuzzRun(HarfBuzzRun*, hb_buffer_t*); | |
156 float adjustSpacing(HarfBuzzRun*, size_t glyphIndex, unsigned currentCharact
erIndex, float& offsetX, float& totalAdvance); | |
157 void addHarfBuzzRun(unsigned startCharacter, unsigned endCharacter, const Si
mpleFontData*, UScriptCode); | 149 void addHarfBuzzRun(unsigned startCharacter, unsigned endCharacter, const Si
mpleFontData*, UScriptCode); |
158 | 150 |
159 OwnPtr<UChar[]> m_normalizedBuffer; | 151 OwnPtr<UChar[]> m_normalizedBuffer; |
160 unsigned m_normalizedBufferLength; | 152 unsigned m_normalizedBufferLength; |
161 | 153 |
162 float m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word br
eak. | 154 float m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word br
eak. |
163 float m_letterSpacing; // Pixels to be added after each glyph. | 155 float m_letterSpacing; // Pixels to be added after each glyph. |
164 unsigned m_expansionOpportunityCount; | 156 unsigned m_expansionOpportunityCount; |
165 | 157 |
166 Vector<hb_feature_t, 4> m_features; | 158 Vector<hb_feature_t, 4> m_features; |
167 Vector<OwnPtr<HarfBuzzRun>, 16> m_harfBuzzRuns; | 159 Vector<OwnPtr<HarfBuzzRun>, 16> m_harfBuzzRuns; |
168 | |
169 int m_fromIndex; | |
170 int m_toIndex; | |
171 | |
172 float m_totalWidth; | |
173 | |
174 friend struct CachedShapingResults; | |
175 }; | 160 }; |
176 | 161 |
177 } // namespace blink | 162 } // namespace blink |
178 | 163 |
179 #endif // HarfBuzzShaper_h | 164 #endif // HarfBuzzShaper_h |
OLD | NEW |