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.
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.
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.
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.
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"
39 class GraphicsContext;
45 class PLATFORM_EXPORT TextRun {
46 WTF_MAKE_FAST_ALLOCATED;
48 enum ExpansionBehaviorFlags {
49 ForbidTrailingExpansion = 0 << 0,
50 AllowTrailingExpansion = 1 << 0,
51 ForbidLeadingExpansion = 0 << 1,
52 AllowLeadingExpansion = 1 << 1,
55 typedef unsigned ExpansionBehavior;
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)
61 , m_horizontalGlyphStretch(1)
62 , m_expansion(expansion)
63 , m_expansionBehavior(expansionBehavior)
66 , m_direction(direction)
67 , m_directionalOverride(directionalOverride)
68 , m_characterScanForCodePath(characterScanForCodePath)
69 , m_disableSpacing(false)
72 m_data.characters8 = c;
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)
79 , m_horizontalGlyphStretch(1)
80 , m_expansion(expansion)
81 , m_expansionBehavior(expansionBehavior)
84 , m_direction(direction)
85 , m_directionalOverride(directionalOverride)
86 , m_characterScanForCodePath(characterScanForCodePath)
87 , m_disableSpacing(false)
90 m_data.characters16 = c;
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())
97 , m_horizontalGlyphStretch(1)
98 , m_expansion(expansion)
99 , m_expansionBehavior(expansionBehavior)
101 , m_direction(direction)
102 , m_directionalOverride(directionalOverride)
103 , m_characterScanForCodePath(characterScanForCodePath)
104 , m_disableSpacing(false)
107 if (!m_charactersLength) {
109 m_data.characters8 = 0;
110 } else if (string.is8Bit()) {
111 m_data.characters8 = string.characters8();
114 m_data.characters16 = string.characters16();
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())
123 , m_horizontalGlyphStretch(1)
124 , m_expansion(expansion)
125 , m_expansionBehavior(expansionBehavior)
127 , m_direction(direction)
128 , m_directionalOverride(directionalOverride)
129 , m_characterScanForCodePath(characterScanForCodePath)
130 , m_disableSpacing(false)
133 if (!m_charactersLength) {
135 m_data.characters8 = 0;
136 } else if (string.is8Bit()) {
137 m_data.characters8 = string.characters8();
140 m_data.characters16 = string.characters16();
145 TextRun subRun(unsigned startOffset, unsigned length) const
147 ASSERT(startOffset < m_len);
149 TextRun result = *this;
152 result.setText(data8(startOffset), length);
155 result.setText(data16(startOffset), length);
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]; }
163 const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
164 const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
166 bool is8Bit() const { return m_is8Bit; }
167 int length() const { return m_len; }
168 int charactersLength() const { return m_charactersLength; }
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; }
175 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
176 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
178 bool allowTabs() const { return m_allowTabs; }
179 unsigned tabSize() const { return m_tabSize; }
180 void setTabSize(bool, unsigned);
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; }
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; }
199 class RenderingContext : public RefCounted<RenderingContext> {
201 virtual ~RenderingContext() { }
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;
208 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
209 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
213 const LChar* characters8;
214 const UChar* characters16;
216 unsigned m_charactersLength; // Marks the end of the characters buffer. Default equals to m_len.
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.
223 float m_horizontalGlyphStretch;
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;
234 RefPtr<RenderingContext> m_renderingContext;
237 inline void TextRun::setTabSize(bool allow, unsigned size)
243 // Container for parameters needed to paint TextRun.
244 struct TextRunPaintInfo {
245 explicit TextRunPaintInfo(const TextRun& r)