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 int offsetForPosition(float targetX); |
| 64 unsigned numCharacters() const { return m_numCharacters; } |
| 65 |
| 66 static float fillGlyphBuffer(Vector<RefPtr<ShapeResult>>&, |
| 67 GlyphBuffer*, const TextRun&, unsigned from, unsigned to); |
| 68 static float fillGlyphBufferForTextEmphasis(Vector<RefPtr<ShapeResult>>&, |
| 69 GlyphBuffer*, const TextRun&, const GlyphData* emphasisData, |
| 70 unsigned from, unsigned to); |
| 71 static FloatRect selectionRect(Vector<RefPtr<ShapeResult>>&, |
| 72 TextDirection, float totalWidth, const FloatPoint&, int height, |
| 73 unsigned from, unsigned to); |
| 74 |
| 75 unsigned numberOfRunsForTesting() const; |
| 76 bool runInfoForTesting(unsigned runIndex, unsigned& startIndex, |
| 77 unsigned& numGlyphs, hb_script_t&); |
| 78 uint16_t glyphForTesting(unsigned runIndex, size_t glyphIndex); |
| 79 float advanceForTesting(unsigned runIndex, size_t glyphIndex); |
| 80 |
| 81 private: |
| 82 struct RunInfo; |
| 83 |
| 84 template<TextDirection> |
| 85 float fillGlyphBufferForRun(GlyphBuffer*, const RunInfo*, |
| 86 float initialAdvance, unsigned from, unsigned to, unsigned runOffset); |
| 87 |
| 88 float m_width; |
| 89 FloatRect m_glyphBoundingBox; |
| 90 Vector<RunInfo*> m_runs; |
| 91 |
| 92 unsigned m_numCharacters; |
| 93 unsigned m_numGlyphs : 31; |
| 94 |
| 95 // Overall direction for the TextRun, dictates which order each individual |
| 96 // sub run (represented by RunInfo structs in the m_runs vector) can have a |
| 97 // different text direction. |
| 98 unsigned m_direction : 1; |
| 99 |
| 100 friend class HarfBuzzShaper; |
| 101 }; |
52 | 102 |
53 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { | 103 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { |
54 public: | 104 public: |
55 HarfBuzzShaper(const Font*, const TextRun&, const GlyphData* emphasisData =
nullptr, | 105 HarfBuzzShaper(const Font*, const TextRun&, |
56 HashSet<const SimpleFontData*>* fallbackFonts = nullptr, FloatRect* = nu
llptr); | 106 HashSet<const SimpleFontData*>* fallbackFonts = nullptr); |
57 | 107 PassRefPtr<ShapeResult> shapeResult(); |
58 void setDrawRange(int from, int to); | 108 ~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 | 109 |
91 private: | 110 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 { | 111 class PLATFORM_EXPORT HarfBuzzRun { |
100 public: | 112 public: |
101 HarfBuzzRun(const HarfBuzzRun&); | 113 HarfBuzzRun(const HarfBuzzRun&); |
102 ~HarfBuzzRun(); | 114 ~HarfBuzzRun(); |
103 | 115 |
104 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) | 116 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) |
105 { | 117 { |
106 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); | 118 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); |
107 } | 119 } |
108 | 120 |
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; } | 121 const SimpleFontData* fontData() { return m_fontData; } |
118 unsigned startIndex() const { return m_startIndex; } | 122 unsigned startIndex() const { return m_startIndex; } |
119 unsigned numCharacters() const { return m_numCharacters; } | 123 unsigned numCharacters() const { return m_numCharacters; } |
120 unsigned numGlyphs() const { return m_numGlyphs; } | 124 unsigned numGlyphs() const { return m_numGlyphs; } |
121 Vector<HarfBuzzRunGlyphData, 256>& glyphData() { return m_glyphData; } | 125 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; } | 126 hb_direction_t direction() { return m_direction; } |
126 bool rtl() { return m_direction == HB_DIRECTION_RTL; } | 127 bool rtl() { return m_direction == HB_DIRECTION_RTL; } |
127 hb_script_t script() { return m_script; } | 128 hb_script_t script() { return m_script; } |
128 | 129 |
129 private: | 130 private: |
130 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numChar
acters, hb_direction_t, hb_script_t); | 131 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numChar
acters, hb_direction_t, hb_script_t); |
131 | 132 |
132 const SimpleFontData* m_fontData; | 133 const SimpleFontData* m_fontData; |
133 unsigned m_startIndex; | 134 unsigned m_startIndex; |
134 size_t m_numCharacters; | 135 size_t m_numCharacters; |
135 unsigned m_numGlyphs; | 136 unsigned m_numGlyphs; |
136 hb_direction_t m_direction; | 137 hb_direction_t m_direction; |
137 hb_script_t m_script; | 138 hb_script_t m_script; |
138 Vector<HarfBuzzRunGlyphData, 256> m_glyphData; | |
139 float m_width; | |
140 }; | 139 }; |
141 | 140 |
142 float nextExpansionPerOpportunity(); | 141 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); | 142 void setExpansion(float); |
146 | |
147 void setFontFeatures(); | 143 void setFontFeatures(); |
148 | 144 |
149 bool createHarfBuzzRuns(); | 145 bool createHarfBuzzRuns(); |
150 bool createHarfBuzzRunsForSingleCharacter(); | 146 bool createHarfBuzzRunsForSingleCharacter(); |
151 bool shapeHarfBuzzRuns(); | 147 bool shapeHarfBuzzRuns(ShapeResult*); |
152 bool fillGlyphBuffer(GlyphBuffer*); | 148 void shapeResult(ShapeResult*, unsigned, HarfBuzzRun*, hb_buffer_t*); |
153 float fillGlyphBufferFromHarfBuzzRun(GlyphBuffer*, HarfBuzzRun*, float initi
alAdvance); | 149 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); | 150 void addHarfBuzzRun(unsigned startCharacter, unsigned endCharacter, const Si
mpleFontData*, UScriptCode); |
158 | 151 |
159 OwnPtr<UChar[]> m_normalizedBuffer; | 152 OwnPtr<UChar[]> m_normalizedBuffer; |
160 unsigned m_normalizedBufferLength; | 153 unsigned m_normalizedBufferLength; |
161 | 154 |
162 float m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word br
eak. | 155 float m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word br
eak. |
163 float m_letterSpacing; // Pixels to be added after each glyph. | 156 float m_letterSpacing; // Pixels to be added after each glyph. |
164 unsigned m_expansionOpportunityCount; | 157 unsigned m_expansionOpportunityCount; |
165 | 158 |
166 Vector<hb_feature_t, 4> m_features; | 159 Vector<hb_feature_t, 4> m_features; |
167 Vector<OwnPtr<HarfBuzzRun>, 16> m_harfBuzzRuns; | 160 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 }; | 161 }; |
176 | 162 |
177 } // namespace blink | 163 } // namespace blink |
178 | 164 |
179 #endif // HarfBuzzShaper_h | 165 #endif // HarfBuzzShaper_h |
OLD | NEW |