OLD | NEW |
| (Empty) |
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/fonts/GlyphBuffer.h" | |
36 #include "platform/geometry/FloatPoint.h" | |
37 #include "platform/graphics/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 namespace WebCore { | |
45 | |
46 class Font; | |
47 class SimpleFontData; | |
48 | |
49 class HarfBuzzShaper FINAL { | |
50 public: | |
51 enum NormalizeMode { | |
52 DoNotNormalizeMirrorChars, | |
53 NormalizeMirrorChars | |
54 }; | |
55 | |
56 HarfBuzzShaper(const Font*, const TextRun&); | |
57 | |
58 void setDrawRange(int from, int to); | |
59 bool shape(GlyphBuffer* = 0); | |
60 FloatPoint adjustStartPoint(const FloatPoint&); | |
61 float totalWidth() { return m_totalWidth; } | |
62 int offsetForPosition(float targetX); | |
63 FloatRect selectionRect(const FloatPoint&, int height, int from, int to); | |
64 | |
65 private: | |
66 class HarfBuzzRun { | |
67 public: | |
68 HarfBuzzRun(const HarfBuzzRun&); | |
69 ~HarfBuzzRun(); | |
70 | |
71 static PassOwnPtr<HarfBuzzRun> create(const SimpleFontData* fontData, un
signed startIndex, unsigned numCharacters, TextDirection direction, hb_script_t
script) | |
72 { | |
73 return adoptPtr(new HarfBuzzRun(fontData, startIndex, numCharacters,
direction, script)); | |
74 } | |
75 | |
76 void applyShapeResult(hb_buffer_t*); | |
77 void copyShapeResultAndGlyphPositions(const HarfBuzzRun&); | |
78 void setGlyphAndPositions(unsigned index, uint16_t glyphId, float advanc
e, float offsetX, float offsetY); | |
79 void setWidth(float width) { m_width = width; } | |
80 | |
81 int characterIndexForXPosition(float targetX); | |
82 float xPositionForOffset(unsigned offset); | |
83 | |
84 const SimpleFontData* fontData() { return m_fontData; } | |
85 unsigned startIndex() const { return m_startIndex; } | |
86 unsigned numCharacters() const { return m_numCharacters; } | |
87 unsigned numGlyphs() const { return m_numGlyphs; } | |
88 uint16_t* glyphs() { return &m_glyphs[0]; } | |
89 float* advances() { return &m_advances[0]; } | |
90 FloatPoint* offsets() { return &m_offsets[0]; } | |
91 uint16_t* glyphToCharacterIndexes() { return &m_glyphToCharacterIndexes[
0]; } | |
92 float width() { return m_width; } | |
93 bool rtl() { return m_direction == RTL; } | |
94 hb_script_t script() { return m_script; } | |
95 | |
96 private: | |
97 HarfBuzzRun(const SimpleFontData*, unsigned startIndex, unsigned numChar
acters, TextDirection, hb_script_t); | |
98 | |
99 const SimpleFontData* m_fontData; | |
100 unsigned m_startIndex; | |
101 size_t m_numCharacters; | |
102 unsigned m_numGlyphs; | |
103 TextDirection m_direction; | |
104 hb_script_t m_script; | |
105 Vector<uint16_t, 256> m_glyphs; | |
106 Vector<float, 256> m_advances; | |
107 Vector<uint16_t, 256> m_glyphToCharacterIndexes; | |
108 Vector<FloatPoint, 256> m_offsets; | |
109 float m_width; | |
110 }; | |
111 | |
112 void setNormalizedBuffer(NormalizeMode = DoNotNormalizeMirrorChars); | |
113 | |
114 bool isWordEnd(unsigned); | |
115 int determineWordBreakSpacing(); | |
116 // setPadding sets a number of pixels to be distributed across the TextRun. | |
117 // WebKit uses this to justify text. | |
118 void setPadding(int); | |
119 | |
120 // In complex text word-spacing affects each line-break, space (U+0020) and
non-breaking space (U+00A0). | |
121 static bool isCodepointSpace(UChar c) { return c == ' ' || c == noBreakSpace
|| c == '\n'; } | |
122 | |
123 void setFontFeatures(); | |
124 | |
125 bool collectHarfBuzzRuns(); | |
126 bool shapeHarfBuzzRuns(bool shouldSetDirection); | |
127 bool fillGlyphBuffer(GlyphBuffer*); | |
128 void fillGlyphBufferFromHarfBuzzRun(GlyphBuffer*, HarfBuzzRun*, FloatPoint&
firstOffsetOfNextRun); | |
129 void setGlyphPositionsForHarfBuzzRun(HarfBuzzRun*, hb_buffer_t*); | |
130 | |
131 GlyphBufferAdvance createGlyphBufferAdvance(float, float); | |
132 | |
133 const Font* m_font; | |
134 OwnPtr<UChar[]> m_normalizedBuffer; | |
135 unsigned m_normalizedBufferLength; | |
136 const TextRun& m_run; | |
137 | |
138 int m_wordSpacingAdjustment; // Delta adjustment (pixels) for each word brea
k. | |
139 float m_padding; // Pixels to be distributed over the line at word breaks. | |
140 float m_padPerWordBreak; // Pixels to be added to each word break. | |
141 float m_padError; // m_padPerWordBreak might have a fractional component. Si
nce we only add a whole number of padding pixels at each word break we accumulat
e error. This is the number of pixels that we are behind so far. | |
142 int m_letterSpacing; // Pixels to be added after each glyph. | |
143 | |
144 Vector<hb_feature_t, 4> m_features; | |
145 Vector<OwnPtr<HarfBuzzRun>, 16> m_harfBuzzRuns; | |
146 | |
147 FloatPoint m_startOffset; | |
148 | |
149 int m_fromIndex; | |
150 int m_toIndex; | |
151 | |
152 float m_totalWidth; | |
153 | |
154 friend struct CachedShapingResults; | |
155 }; | |
156 | |
157 } // namespace WebCore | |
158 | |
159 #endif // HarfBuzzShaper_h | |
OLD | NEW |