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