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/text/CharacterNames.h" | 43 #include "wtf/text/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 int offsetForPosition(float targetX); |
| 62 unsigned numCharacters() const { return m_numCharacters; } |
| 63 |
| 64 static float fillGlyphBuffer(Vector<RefPtr<ShapeResult>>&, |
| 65 GlyphBuffer*, const TextRun&, unsigned from, unsigned to); |
| 66 static float fillGlyphBufferForTextEmphasis(Vector<RefPtr<ShapeResult>>&, |
| 67 GlyphBuffer*, const TextRun&, const GlyphData* emphasisData, |
| 68 unsigned from, unsigned to); |
| 69 static FloatRect selectionRect(Vector<RefPtr<ShapeResult>>&, |
| 70 TextDirection, float totalWidth, const FloatPoint&, int height, |
| 71 unsigned from, unsigned to); |
| 72 |
| 73 unsigned numberOfRunsForTesting() const; |
| 74 bool runInfoForTesting(unsigned runIndex, unsigned& startIndex, |
| 75 unsigned& numGlyphs, hb_script_t&); |
| 76 uint16_t glyphForTesting(unsigned runIndex, size_t glyphIndex); |
| 77 float advanceForTesting(unsigned runIndex, size_t glyphIndex); |
| 78 |
| 79 private: |
| 80 struct RunInfo; |
| 81 |
| 82 template<TextDirection> |
| 83 float fillGlyphBufferForRun(GlyphBuffer*, const RunInfo*, |
| 84 float initialAdvance, unsigned from, unsigned to, unsigned runOffset); |
| 85 |
| 86 float fillGlyphBufferForTextEmphasisRun(GlyphBuffer*, const RunInfo*, |
| 87 const TextRun&, const GlyphData*, float initialAdvance, |
| 88 unsigned from, unsigned to, unsigned runOffset); |
| 89 |
| 90 float m_width; |
| 91 FloatRect m_glyphBoundingBox; |
| 92 Vector<RunInfo*> m_runs; |
| 93 |
| 94 unsigned m_numCharacters; |
| 95 unsigned m_numGlyphs : 31; |
| 96 |
| 97 // Overall direction for the TextRun, dictates which order each individual |
| 98 // sub run (represented by RunInfo structs in the m_runs vector) can have a |
| 99 // different text direction. |
| 100 unsigned m_direction : 1; |
| 101 |
| 102 friend class HarfBuzzShaper; |
| 103 }; |
52 | 104 |
53 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { | 105 class PLATFORM_EXPORT HarfBuzzShaper final : public Shaper { |
54 public: | 106 public: |
55 HarfBuzzShaper(const Font*, const TextRun&, const GlyphData* emphasisData =
nullptr, | 107 HarfBuzzShaper(const Font*, const TextRun&, |
56 HashSet<const SimpleFontData*>* fallbackFonts = nullptr, FloatRect* = nu
llptr); | 108 HashSet<const SimpleFontData*>* fallbackFonts = nullptr); |
57 | 109 PassRefPtr<ShapeResult> shapeResult(); |
58 void setDrawRange(int from, int to); | 110 ~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 | 111 |
91 private: | 112 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 { | 113 class PLATFORM_EXPORT HarfBuzzRun { |
100 public: | 114 public: |
101 HarfBuzzRun(const HarfBuzzRun&); | 115 HarfBuzzRun(const HarfBuzzRun&); |
102 ~HarfBuzzRun(); | 116 ~HarfBuzzRun(); |
103 | 117 |
104 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) | 118 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, hb_direction_t direction, hb_script_t
script) |
105 { | 119 { |
106 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); | 120 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); |
107 } | 121 } |
108 | 122 |
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; } | 123 const SimpleFontData* fontData() { return m_fontData; } |
118 unsigned startIndex() const { return m_startIndex; } | 124 unsigned startIndex() const { return m_startIndex; } |
119 unsigned numCharacters() const { return m_numCharacters; } | 125 unsigned numCharacters() const { return m_numCharacters; } |
120 unsigned numGlyphs() const { return m_numGlyphs; } | |
121 Vector<HarfBuzzRunGlyphData, 256>& glyphData() { return m_glyphData; } | |
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 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 |