Update To 11.40.268.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/fonts/Glyph.h"
29 #include "platform/geometry/FloatRect.h"
30 #include "platform/heap/Heap.h"
31 #include "platform/text/TextDirection.h"
32 #include "platform/text/TextPath.h"
33 #include "wtf/RefCounted.h"
34 #include "wtf/text/WTFString.h"
35
36 class SkTextBlob;
37
38 namespace blink {
39
40 class PLATFORM_EXPORT TextRun {
41     WTF_MAKE_FAST_ALLOCATED;
42 public:
43     enum ExpansionBehaviorFlags {
44         ForbidTrailingExpansion = 0 << 0,
45         AllowTrailingExpansion = 1 << 0,
46         ForbidLeadingExpansion = 0 << 1,
47         AllowLeadingExpansion = 1 << 1,
48     };
49
50     typedef unsigned ExpansionBehavior;
51
52     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)
53         : m_charactersLength(len)
54         , m_len(len)
55         , m_xpos(xpos)
56         , m_horizontalGlyphStretch(1)
57         , m_expansion(expansion)
58         , m_expansionBehavior(expansionBehavior)
59         , m_is8Bit(true)
60         , m_allowTabs(false)
61         , m_direction(direction)
62         , m_directionalOverride(directionalOverride)
63         , m_characterScanForCodePath(characterScanForCodePath)
64         , m_useComplexCodePath(false)
65         , m_disableSpacing(false)
66         , m_tabSize(0)
67         , m_normalizeSpace(false)
68     {
69         m_data.characters8 = c;
70     }
71
72     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)
73         : m_charactersLength(len)
74         , m_len(len)
75         , m_xpos(xpos)
76         , m_horizontalGlyphStretch(1)
77         , m_expansion(expansion)
78         , m_expansionBehavior(expansionBehavior)
79         , m_is8Bit(false)
80         , m_allowTabs(false)
81         , m_direction(direction)
82         , m_directionalOverride(directionalOverride)
83         , m_characterScanForCodePath(characterScanForCodePath)
84         , m_useComplexCodePath(false)
85         , m_disableSpacing(false)
86         , m_tabSize(0)
87         , m_normalizeSpace(false)
88     {
89         m_data.characters16 = c;
90     }
91
92     TextRun(const String& string, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, bool normalizeSpace = false)
93         : m_charactersLength(string.length())
94         , m_len(string.length())
95         , m_xpos(xpos)
96         , m_horizontalGlyphStretch(1)
97         , m_expansion(expansion)
98         , m_expansionBehavior(expansionBehavior)
99         , m_allowTabs(false)
100         , m_direction(direction)
101         , m_directionalOverride(directionalOverride)
102         , m_characterScanForCodePath(characterScanForCodePath)
103         , m_useComplexCodePath(false)
104         , m_disableSpacing(false)
105         , m_tabSize(0)
106         , m_normalizeSpace(normalizeSpace)
107     {
108         if (!m_charactersLength) {
109             m_is8Bit = true;
110             m_data.characters8 = 0;
111         } else if (string.is8Bit()) {
112             m_data.characters8 = string.characters8();
113             m_is8Bit = true;
114         } else {
115             m_data.characters16 = string.characters16();
116             m_is8Bit = false;
117         }
118     }
119
120     TextRun(const StringView& string, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, bool normalizeSpace = false)
121         : m_charactersLength(string.length())
122         , m_len(string.length())
123         , m_xpos(xpos)
124         , m_horizontalGlyphStretch(1)
125         , m_expansion(expansion)
126         , m_expansionBehavior(expansionBehavior)
127         , m_allowTabs(false)
128         , m_direction(direction)
129         , m_directionalOverride(directionalOverride)
130         , m_characterScanForCodePath(characterScanForCodePath)
131         , m_useComplexCodePath(false)
132         , m_disableSpacing(false)
133         , m_tabSize(0)
134         , m_normalizeSpace(normalizeSpace)
135     {
136         if (!m_charactersLength) {
137             m_is8Bit = true;
138             m_data.characters8 = 0;
139         } else if (string.is8Bit()) {
140             m_data.characters8 = string.characters8();
141             m_is8Bit = true;
142         } else {
143             m_data.characters16 = string.characters16();
144             m_is8Bit = false;
145         }
146     }
147
148     TextRun subRun(unsigned startOffset, unsigned length) const
149     {
150         ASSERT(startOffset < m_len);
151
152         TextRun result = *this;
153
154         if (is8Bit()) {
155             result.setText(data8(startOffset), length);
156             return result;
157         }
158         result.setText(data16(startOffset), length);
159         return result;
160     }
161
162     UChar operator[](unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); return is8Bit() ? m_data.characters8[i] :m_data.characters16[i]; }
163     const LChar* data8(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); ASSERT(is8Bit()); return &m_data.characters8[i]; }
164     const UChar* data16(unsigned i) const { ASSERT_WITH_SECURITY_IMPLICATION(i < m_len); ASSERT(!is8Bit()); return &m_data.characters16[i]; }
165
166     const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
167     const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
168
169     bool is8Bit() const { return m_is8Bit; }
170     int length() const { return m_len; }
171     int charactersLength() const { return m_charactersLength; }
172
173     bool normalizeSpace() const { return m_normalizeSpace; }
174     void setNormalizeSpace(bool normalizeSpace) { m_normalizeSpace = normalizeSpace; }
175
176     void setText(const LChar* c, unsigned len) { m_data.characters8 = c; m_len = len; m_is8Bit = true;}
177     void setText(const UChar* c, unsigned len) { m_data.characters16 = c; m_len = len; m_is8Bit = false;}
178     void setText(const String&);
179     void setCharactersLength(unsigned charactersLength) { m_charactersLength = charactersLength; }
180
181     float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
182     void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
183
184     bool allowTabs() const { return m_allowTabs; }
185     unsigned tabSize() const { return m_tabSize; }
186     void setTabSize(bool, unsigned);
187
188     float xPos() const { return m_xpos; }
189     void setXPos(float xPos) { m_xpos = xPos; }
190     float expansion() const { return m_expansion; }
191     bool allowsLeadingExpansion() const { return m_expansionBehavior & AllowLeadingExpansion; }
192     bool allowsTrailingExpansion() const { return m_expansionBehavior & AllowTrailingExpansion; }
193     TextDirection direction() const { return static_cast<TextDirection>(m_direction); }
194     bool rtl() const { return m_direction == RTL; }
195     bool ltr() const { return m_direction == LTR; }
196     bool directionalOverride() const { return m_directionalOverride; }
197     bool characterScanForCodePath() const { return m_characterScanForCodePath; }
198     bool useComplexCodePath() const { return m_useComplexCodePath; }
199     bool spacingDisabled() const { return m_disableSpacing; }
200
201     void disableSpacing() { m_disableSpacing = true; }
202     void setDirection(TextDirection direction) { m_direction = direction; }
203     void setDirectionalOverride(bool override) { m_directionalOverride = override; }
204     void setCharacterScanForCodePath(bool scan) { m_characterScanForCodePath = scan; }
205     void setUseComplexCodePath(bool useComplex) { m_useComplexCodePath = useComplex; }
206
207     class RenderingContext : public RefCounted<RenderingContext> {
208     public:
209         virtual ~RenderingContext() { }
210     };
211
212     RenderingContext* renderingContext() const { return m_renderingContext.get(); }
213     void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
214
215 private:
216     union {
217         const LChar* characters8;
218         const UChar* characters16;
219     } m_data;
220     unsigned m_charactersLength; // Marks the end of the characters buffer. Default equals to m_len.
221     unsigned m_len;
222
223     // m_xpos is the x position relative to the left start of the text line, not relative to the left
224     // start of the containing block. In the case of right alignment or center alignment, left start of
225     // the text line is not the same as left start of the containing block.
226     float m_xpos;
227     float m_horizontalGlyphStretch;
228
229     float m_expansion;
230     ExpansionBehavior m_expansionBehavior : 2;
231     unsigned m_is8Bit : 1;
232     unsigned m_allowTabs : 1;
233     unsigned m_direction : 1;
234     unsigned m_directionalOverride : 1; // Was this direction set by an override character.
235     unsigned m_characterScanForCodePath : 1;
236     unsigned m_useComplexCodePath : 1;
237     unsigned m_disableSpacing : 1;
238     unsigned m_tabSize;
239     bool m_normalizeSpace;
240     RefPtr<RenderingContext> m_renderingContext;
241 };
242
243 inline void TextRun::setTabSize(bool allow, unsigned size)
244 {
245     m_allowTabs = allow;
246     m_tabSize = size;
247 }
248
249 // Container for parameters needed to paint TextRun.
250 struct TextRunPaintInfo {
251     STACK_ALLOCATED();
252 public:
253     explicit TextRunPaintInfo(const TextRun& r)
254         : run(r)
255         , from(0)
256         , to(r.length())
257         , cachedTextBlob(nullptr)
258     {
259     }
260
261     const TextRun& run;
262     int from;
263     int to;
264     FloatRect bounds;
265     RefPtr<const SkTextBlob>* cachedTextBlob;
266 };
267
268 }
269 #endif