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/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"
40 class PLATFORM_EXPORT TextRun {
41 WTF_MAKE_FAST_ALLOCATED;
43 enum ExpansionBehaviorFlags {
44 ForbidTrailingExpansion = 0 << 0,
45 AllowTrailingExpansion = 1 << 0,
46 ForbidLeadingExpansion = 0 << 1,
47 AllowLeadingExpansion = 1 << 1,
50 typedef unsigned ExpansionBehavior;
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)
56 , m_horizontalGlyphStretch(1)
57 , m_expansion(expansion)
58 , m_expansionBehavior(expansionBehavior)
61 , m_direction(direction)
62 , m_directionalOverride(directionalOverride)
63 , m_characterScanForCodePath(characterScanForCodePath)
64 , m_useComplexCodePath(false)
65 , m_disableSpacing(false)
67 , m_normalizeSpace(false)
69 m_data.characters8 = c;
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)
76 , m_horizontalGlyphStretch(1)
77 , m_expansion(expansion)
78 , m_expansionBehavior(expansionBehavior)
81 , m_direction(direction)
82 , m_directionalOverride(directionalOverride)
83 , m_characterScanForCodePath(characterScanForCodePath)
84 , m_useComplexCodePath(false)
85 , m_disableSpacing(false)
87 , m_normalizeSpace(false)
89 m_data.characters16 = c;
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())
96 , m_horizontalGlyphStretch(1)
97 , m_expansion(expansion)
98 , m_expansionBehavior(expansionBehavior)
100 , m_direction(direction)
101 , m_directionalOverride(directionalOverride)
102 , m_characterScanForCodePath(characterScanForCodePath)
103 , m_useComplexCodePath(false)
104 , m_disableSpacing(false)
106 , m_normalizeSpace(normalizeSpace)
108 if (!m_charactersLength) {
110 m_data.characters8 = 0;
111 } else if (string.is8Bit()) {
112 m_data.characters8 = string.characters8();
115 m_data.characters16 = string.characters16();
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())
124 , m_horizontalGlyphStretch(1)
125 , m_expansion(expansion)
126 , m_expansionBehavior(expansionBehavior)
128 , m_direction(direction)
129 , m_directionalOverride(directionalOverride)
130 , m_characterScanForCodePath(characterScanForCodePath)
131 , m_useComplexCodePath(false)
132 , m_disableSpacing(false)
134 , m_normalizeSpace(normalizeSpace)
136 if (!m_charactersLength) {
138 m_data.characters8 = 0;
139 } else if (string.is8Bit()) {
140 m_data.characters8 = string.characters8();
143 m_data.characters16 = string.characters16();
148 TextRun subRun(unsigned startOffset, unsigned length) const
150 ASSERT(startOffset < m_len);
152 TextRun result = *this;
155 result.setText(data8(startOffset), length);
158 result.setText(data16(startOffset), length);
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]; }
166 const LChar* characters8() const { ASSERT(is8Bit()); return m_data.characters8; }
167 const UChar* characters16() const { ASSERT(!is8Bit()); return m_data.characters16; }
169 bool is8Bit() const { return m_is8Bit; }
170 int length() const { return m_len; }
171 int charactersLength() const { return m_charactersLength; }
173 bool normalizeSpace() const { return m_normalizeSpace; }
174 void setNormalizeSpace(bool normalizeSpace) { m_normalizeSpace = normalizeSpace; }
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; }
181 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
182 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
184 bool allowTabs() const { return m_allowTabs; }
185 unsigned tabSize() const { return m_tabSize; }
186 void setTabSize(bool, unsigned);
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; }
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; }
207 class RenderingContext : public RefCounted<RenderingContext> {
209 virtual ~RenderingContext() { }
212 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
213 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
217 const LChar* characters8;
218 const UChar* characters16;
220 unsigned m_charactersLength; // Marks the end of the characters buffer. Default equals to m_len.
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.
227 float m_horizontalGlyphStretch;
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;
239 bool m_normalizeSpace;
240 RefPtr<RenderingContext> m_renderingContext;
243 inline void TextRun::setTabSize(bool allow, unsigned size)
249 // Container for parameters needed to paint TextRun.
250 struct TextRunPaintInfo {
253 explicit TextRunPaintInfo(const TextRun& r)
257 , cachedTextBlob(nullptr)
265 RefPtr<const SkTextBlob>* cachedTextBlob;