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/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"
38 class GraphicsContext;
44 class PLATFORM_EXPORT TextRun {
45 WTF_MAKE_FAST_ALLOCATED;
47 enum ExpansionBehaviorFlags {
48 ForbidTrailingExpansion = 0 << 0,
49 AllowTrailingExpansion = 1 << 0,
50 ForbidLeadingExpansion = 0 << 1,
51 AllowLeadingExpansion = 1 << 1,
54 typedef unsigned ExpansionBehavior;
56 enum RoundingHackFlags {
59 WordRounding = 1 << 1,
62 typedef unsigned RoundingHacks;
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)
68 , m_horizontalGlyphStretch(1)
69 , m_expansion(expansion)
70 , m_expansionBehavior(expansionBehavior)
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)
81 m_data.characters8 = c;
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)
88 , m_horizontalGlyphStretch(1)
89 , m_expansion(expansion)
90 , m_expansionBehavior(expansionBehavior)
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)
101 m_data.characters16 = c;
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())
108 , m_horizontalGlyphStretch(1)
109 , m_expansion(expansion)
110 , m_expansionBehavior(expansionBehavior)
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)
120 if (!m_charactersLength) {
122 m_data.characters8 = 0;
123 } else if (string.is8Bit()) {
124 m_data.characters8 = string.characters8();
127 m_data.characters16 = string.characters16();
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())
136 , m_horizontalGlyphStretch(1)
137 , m_expansion(expansion)
138 , m_expansionBehavior(expansionBehavior)
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)
148 if (!m_charactersLength) {
150 m_data.characters8 = 0;
151 } else if (string.is8Bit()) {
152 m_data.characters8 = string.characters8();
155 m_data.characters16 = string.characters16();
160 TextRun subRun(unsigned startOffset, unsigned length) const
162 ASSERT(startOffset < m_len);
164 TextRun result = *this;
167 result.setText(data8(startOffset), length);
170 result.setText(data16(startOffset), length);
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]; }
178 const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
179 const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
181 bool is8Bit() const { return m_is8Bit; }
182 int length() const { return m_len; }
183 int charactersLength() const { return m_charactersLength; }
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; }
190 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
191 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
193 bool allowTabs() const { return m_allowTabs; }
194 unsigned tabSize() const { return m_tabSize; }
195 void setTabSize(bool, unsigned);
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; }
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; }
217 class RenderingContext : public RefCounted<RenderingContext> {
219 virtual ~RenderingContext() { }
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;
226 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
227 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
229 static void setAllowsRoundingHacks(bool);
230 static bool allowsRoundingHacks();
233 static bool s_allowsRoundingHacks;
236 const LChar* characters8;
237 const UChar* characters16;
239 unsigned m_charactersLength; // Marks the end of the characters buffer. Default equals to m_len.
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.
246 float m_horizontalGlyphStretch;
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;
259 RefPtr<RenderingContext> m_renderingContext;
262 inline void TextRun::setTabSize(bool allow, unsigned size)
268 // Container for parameters needed to paint TextRun.
269 struct TextRunPaintInfo {
270 explicit TextRunPaintInfo(const TextRun& r)