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 "TextDirection.h"
28 #include <wtf/RefCounted.h>
29 #include <wtf/text/WTFString.h>
36 class GraphicsContext;
44 enum ExpansionBehaviorFlags {
45 ForbidTrailingExpansion = 0 << 0,
46 AllowTrailingExpansion = 1 << 0,
47 ForbidLeadingExpansion = 0 << 1,
48 AllowLeadingExpansion = 1 << 1,
51 typedef unsigned ExpansionBehavior;
53 enum RoundingHackFlags {
56 WordRounding = 1 << 1,
59 typedef unsigned RoundingHacks;
61 TextRun(const UChar* c, int len, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
63 , m_charactersLength(len)
67 , m_horizontalGlyphStretch(1)
69 , m_expansion(expansion)
70 , m_expansionBehavior(expansionBehavior)
72 , m_direction(direction)
73 , m_directionalOverride(directionalOverride)
74 , m_characterScanForCodePath(characterScanForCodePath)
75 , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
76 , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
77 , m_disableSpacing(false)
82 TextRun(const String& s, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
83 : m_characters(s.characters())
84 , m_charactersLength(s.length())
88 , m_horizontalGlyphStretch(1)
90 , m_expansion(expansion)
91 , 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)
103 UChar operator[](int i) const { ASSERT(i >= 0 && i < m_len); return m_characters[i]; }
104 const UChar* data(int i) const { ASSERT(i >= 0 && i < m_len); return &m_characters[i]; }
106 const UChar* characters() const { return m_characters; }
107 int length() const { return m_len; }
108 int charactersLength() const { return m_charactersLength; }
110 void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
111 void setCharactersLength(int charactersLength) { m_charactersLength = charactersLength; }
114 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
115 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
118 bool allowTabs() const { return m_allowTabs; }
119 unsigned tabSize() const { return m_tabSize; }
120 void setTabSize(bool, unsigned);
122 float xPos() const { return m_xpos; }
123 void setXPos(float xPos) { m_xpos = xPos; }
124 float expansion() const { return m_expansion; }
125 bool allowsLeadingExpansion() const { return m_expansionBehavior & AllowLeadingExpansion; }
126 bool allowsTrailingExpansion() const { return m_expansionBehavior & AllowTrailingExpansion; }
127 TextDirection direction() const { return static_cast<TextDirection>(m_direction); }
128 bool rtl() const { return m_direction == RTL; }
129 bool ltr() const { return m_direction == LTR; }
130 bool directionalOverride() const { return m_directionalOverride; }
131 bool characterScanForCodePath() const { return m_characterScanForCodePath; }
132 bool applyRunRounding() const { return m_applyRunRounding; }
133 bool applyWordRounding() const { return m_applyWordRounding; }
134 bool spacingDisabled() const { return m_disableSpacing; }
136 void disableSpacing() { m_disableSpacing = true; }
137 void disableRoundingHacks() { m_applyRunRounding = m_applyWordRounding = false; }
138 void setDirection(TextDirection direction) { m_direction = direction; }
139 void setDirectionalOverride(bool override) { m_directionalOverride = override; }
140 void setCharacterScanForCodePath(bool scan) { m_characterScanForCodePath = scan; }
142 class RenderingContext : public RefCounted<RenderingContext> {
144 virtual ~RenderingContext() { }
146 #if ENABLE(SVG_FONTS)
147 virtual GlyphData glyphDataForCharacter(const Font&, const TextRun&, WidthIterator&, UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength) = 0;
148 virtual void drawSVGGlyphs(GraphicsContext*, const TextRun&, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const = 0;
149 virtual float floatWidthUsingSVGFont(const Font&, const TextRun&, int& charsConsumed, String& glyphName) const = 0;
153 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
154 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
156 static void setAllowsRoundingHacks(bool);
157 static bool allowsRoundingHacks();
160 static bool s_allowsRoundingHacks;
162 const UChar* m_characters;
163 int m_charactersLength; // Marks the end of the m_characters buffer. Default equals to m_len.
166 // m_xpos is the x position relative to the left start of the text line, not relative to the left
167 // start of the containing block. In the case of right alignment or center alignment, left start of
168 // the text line is not the same as left start of the containing block.
171 float m_horizontalGlyphStretch;
175 ExpansionBehavior m_expansionBehavior : 2;
176 unsigned m_allowTabs : 1;
177 unsigned m_direction : 1;
178 unsigned m_directionalOverride : 1; // Was this direction set by an override character.
179 unsigned m_characterScanForCodePath : 1;
180 unsigned m_applyRunRounding : 1;
181 unsigned m_applyWordRounding : 1;
182 unsigned m_disableSpacing : 1;
184 RefPtr<RenderingContext> m_renderingContext;
187 inline void TextRun::setTabSize(bool allow, unsigned size)