Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / text / TextRun.h
1 /*
2  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3  *           (C) 2000 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2003, 2006, 2007, 2011 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef TextRun_h
25 #define TextRun_h
26
27 #include "platform/PlatformExport.h"
28 #include "platform/geometry/FloatRect.h"
29 #include "platform/text/TextDirection.h"
30 #include "platform/text/TextPath.h"
31 #include "wtf/RefCounted.h"
32 #include "wtf/text/WTFString.h"
33
34 namespace WebCore {
35
36 class FloatPoint;
37 class Font;
38 class GraphicsContext;
39 class GlyphBuffer;
40 class SimpleFontData;
41 struct GlyphData;
42 struct WidthIterator;
43
44 class PLATFORM_EXPORT TextRun {
45     WTF_MAKE_FAST_ALLOCATED;
46 public:
47     enum ExpansionBehaviorFlags {
48         ForbidTrailingExpansion = 0 << 0,
49         AllowTrailingExpansion = 1 << 0,
50         ForbidLeadingExpansion = 0 << 1,
51         AllowLeadingExpansion = 1 << 1,
52     };
53
54     typedef unsigned ExpansionBehavior;
55
56     enum RoundingHackFlags {
57         NoRounding = 0,
58         RunRounding = 1 << 0,
59         WordRounding = 1 << 1,
60     };
61
62     typedef unsigned RoundingHacks;
63
64     TextRun(const LChar* c, unsigned len, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
65         : m_charactersLength(len)
66         , m_len(len)
67         , m_xpos(xpos)
68         , m_horizontalGlyphStretch(1)
69         , m_expansion(expansion)
70         , m_expansionBehavior(expansionBehavior)
71         , m_is8Bit(true)
72         , m_allowTabs(false)
73         , m_direction(direction)
74         , m_directionalOverride(directionalOverride)
75         , m_characterScanForCodePath(characterScanForCodePath)
76         , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
77         , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
78         , m_disableSpacing(false)
79         , m_tabSize(0)
80     {
81         m_data.characters8 = c;
82     }
83
84     TextRun(const UChar* c, unsigned len, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
85         : m_charactersLength(len)
86         , m_len(len)
87         , m_xpos(xpos)
88         , m_horizontalGlyphStretch(1)
89         , m_expansion(expansion)
90         , m_expansionBehavior(expansionBehavior)
91         , m_is8Bit(false)
92         , m_allowTabs(false)
93         , m_direction(direction)
94         , m_directionalOverride(directionalOverride)
95         , m_characterScanForCodePath(characterScanForCodePath)
96         , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
97         , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
98         , m_disableSpacing(false)
99         , m_tabSize(0)
100     {
101         m_data.characters16 = c;
102     }
103
104     TextRun(const String& string, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
105         : m_charactersLength(string.length())
106         , m_len(string.length())
107         , m_xpos(xpos)
108         , m_horizontalGlyphStretch(1)
109         , m_expansion(expansion)
110         , m_expansionBehavior(expansionBehavior)
111         , m_allowTabs(false)
112         , m_direction(direction)
113         , m_directionalOverride(directionalOverride)
114         , m_characterScanForCodePath(characterScanForCodePath)
115         , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
116         , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
117         , m_disableSpacing(false)
118         , m_tabSize(0)
119     {
120         if (!m_charactersLength) {
121             m_is8Bit = true;
122             m_data.characters8 = 0;
123         } else if (string.is8Bit()) {
124             m_data.characters8 = string.characters8();
125             m_is8Bit = true;
126         } else {
127             m_data.characters16 = string.characters16();
128             m_is8Bit = false;
129         }
130     }
131
132     TextRun(const StringView& string, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
133         : m_charactersLength(string.length())
134         , m_len(string.length())
135         , m_xpos(xpos)
136         , m_horizontalGlyphStretch(1)
137         , m_expansion(expansion)
138         , m_expansionBehavior(expansionBehavior)
139         , m_allowTabs(false)
140         , m_direction(direction)
141         , m_directionalOverride(directionalOverride)
142         , m_characterScanForCodePath(characterScanForCodePath)
143         , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
144         , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
145         , m_disableSpacing(false)
146         , m_tabSize(0)
147     {
148         if (!m_charactersLength) {
149             m_is8Bit = true;
150             m_data.characters8 = 0;
151         } else if (string.is8Bit()) {
152             m_data.characters8 = string.characters8();
153             m_is8Bit = true;
154         } else {
155             m_data.characters16 = string.characters16();
156             m_is8Bit = false;
157         }
158     }
159
160     TextRun subRun(unsigned startOffset, unsigned length) const
161     {
162         ASSERT(startOffset < m_len);
163
164         TextRun result = *this;
165
166         if (is8Bit()) {
167             result.setText(data8(startOffset), length);
168             return result;
169         }
170         result.setText(data16(startOffset), length);
171         return result;
172     }
173
174     UChar operator[](unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); return is8Bit() ? m_data.characters8[i] :m_data.characters16[i]; }
175     const LChar* data8(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); ASSERT(is8Bit()); return &m_data.characters8[i]; }
176     const UChar* data16(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); ASSERT(!is8Bit()); return &m_data.characters16[i]; }
177
178     const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
179     const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
180
181     bool is8Bit() const { return m_is8Bit; }
182     int length() const { return m_len; }
183     int charactersLength() const { return m_charactersLength; }
184
185     void setText(const LChar* c, unsigned len) { m_data.characters8 = c; m_len = len; m_is8Bit = true;}
186     void setText(const UChar* c, unsigned len) { m_data.characters16 = c; m_len = len; m_is8Bit = false;}
187     void setText(const String&);
188     void setCharactersLength(unsigned charactersLength) { m_charactersLength = charactersLength; }
189
190     float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
191     void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
192
193     bool allowTabs() const { return m_allowTabs; }
194     unsigned tabSize() const { return m_tabSize; }
195     void setTabSize(bool, unsigned);
196
197     float xPos() const { return m_xpos; }
198     void setXPos(float xPos) { m_xpos = xPos; }
199     float expansion() const { return m_expansion; }
200     bool allowsLeadingExpansion() const { return m_expansionBehavior & AllowLeadingExpansion; }
201     bool allowsTrailingExpansion() const { return m_expansionBehavior & AllowTrailingExpansion; }
202     TextDirection direction() const { return static_cast<TextDirection>(m_direction); }
203     bool rtl() const { return m_direction == RTL; }
204     bool ltr() const { return m_direction == LTR; }
205     bool directionalOverride() const { return m_directionalOverride; }
206     bool characterScanForCodePath() const { return m_characterScanForCodePath; }
207     bool applyRunRounding() const { return m_applyRunRounding; }
208     bool applyWordRounding() const { return m_applyWordRounding; }
209     bool spacingDisabled() const { return m_disableSpacing; }
210
211     void disableSpacing() { m_disableSpacing = true; }
212     void disableRoundingHacks() { m_applyRunRounding = m_applyWordRounding = false; }
213     void setDirection(TextDirection direction) { m_direction = direction; }
214     void setDirectionalOverride(bool override) { m_directionalOverride = override; }
215     void setCharacterScanForCodePath(bool scan) { m_characterScanForCodePath = scan; }
216
217     class RenderingContext : public RefCounted<RenderingContext> {
218     public:
219         virtual ~RenderingContext() { }
220
221         virtual GlyphData glyphDataForCharacter(const Font&, const TextRun&, WidthIterator&, UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength) = 0;
222         virtual void drawSVGGlyphs(GraphicsContext*, const TextRun&, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const = 0;
223         virtual float floatWidthUsingSVGFont(const Font&, const TextRun&, int& charsConsumed, String& glyphName) const = 0;
224     };
225
226     RenderingContext* renderingContext() const { return m_renderingContext.get(); }
227     void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
228
229     static void setAllowsRoundingHacks(bool);
230     static bool allowsRoundingHacks();
231
232 private:
233     static bool s_allowsRoundingHacks;
234
235     union {
236         const LChar* characters8;
237         const UChar* characters16;
238     } m_data;
239     unsigned m_charactersLength; // Marks the end of the characters buffer. Default equals to m_len.
240     unsigned m_len;
241
242     // m_xpos is the x position relative to the left start of the text line, not relative to the left
243     // start of the containing block. In the case of right alignment or center alignment, left start of
244     // the text line is not the same as left start of the containing block.
245     float m_xpos;
246     float m_horizontalGlyphStretch;
247
248     float m_expansion;
249     ExpansionBehavior m_expansionBehavior : 2;
250     unsigned m_is8Bit : 1;
251     unsigned m_allowTabs : 1;
252     unsigned m_direction : 1;
253     unsigned m_directionalOverride : 1; // Was this direction set by an override character.
254     unsigned m_characterScanForCodePath : 1;
255     unsigned m_applyRunRounding : 1;
256     unsigned m_applyWordRounding : 1;
257     unsigned m_disableSpacing : 1;
258     unsigned m_tabSize;
259     RefPtr<RenderingContext> m_renderingContext;
260 };
261
262 inline void TextRun::setTabSize(bool allow, unsigned size)
263 {
264     m_allowTabs = allow;
265     m_tabSize = size;
266 }
267
268 // Container for parameters needed to paint TextRun.
269 struct TextRunPaintInfo {
270     explicit TextRunPaintInfo(const TextRun& r)
271         : run(r)
272         , from(0)
273         , to(r.length())
274     {
275     }
276
277     const TextRun& run;
278     int from;
279     int to;
280     FloatRect bounds;
281 };
282
283 }
284 #endif