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, bool allowTabs = false, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, RoundingHacks roundingHacks = RunRounding | WordRounding)
63 , m_charactersLength(len)
66 , m_expansion(expansion)
67 , m_expansionBehavior(expansionBehavior)
69 , m_horizontalGlyphStretch(1)
71 , m_allowTabs(allowTabs)
72 , m_direction(direction)
73 , m_directionalOverride(directionalOverride)
74 , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
75 , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
76 , m_disableSpacing(false)
80 TextRun(const String& s, bool allowTabs = false, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, RoundingHacks roundingHacks = RunRounding | WordRounding)
81 : m_characters(s.characters())
82 , m_charactersLength(s.length())
85 , m_expansion(expansion)
86 , m_expansionBehavior(expansionBehavior)
88 , m_horizontalGlyphStretch(1)
90 , m_allowTabs(allowTabs)
91 , m_direction(direction)
92 , m_directionalOverride(directionalOverride)
93 , m_applyRunRounding((roundingHacks & RunRounding) && s_allowsRoundingHacks)
94 , m_applyWordRounding((roundingHacks & WordRounding) && s_allowsRoundingHacks)
95 , m_disableSpacing(false)
99 UChar operator[](int i) const { ASSERT(i >= 0 && i < m_len); return m_characters[i]; }
100 const UChar* data(int i) const { ASSERT(i >= 0 && i < m_len); return &m_characters[i]; }
102 const UChar* characters() const { return m_characters; }
103 int length() const { return m_len; }
104 int charactersLength() const { return m_charactersLength; }
106 void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
107 void setCharactersLength(int charactersLength) { m_charactersLength = charactersLength; }
110 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
111 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
114 bool allowTabs() const { return m_allowTabs; }
115 void setAllowTabs(bool allowTabs) { m_allowTabs = allowTabs; }
116 float xPos() const { return m_xpos; }
117 void setXPos(float xPos) { m_xpos = xPos; }
118 float expansion() const { return m_expansion; }
119 bool allowsLeadingExpansion() const { return m_expansionBehavior & AllowLeadingExpansion; }
120 bool allowsTrailingExpansion() const { return m_expansionBehavior & AllowTrailingExpansion; }
121 TextDirection direction() const { return m_direction; }
122 bool rtl() const { return m_direction == RTL; }
123 bool ltr() const { return m_direction == LTR; }
124 bool directionalOverride() const { return m_directionalOverride; }
125 bool applyRunRounding() const { return m_applyRunRounding; }
126 bool applyWordRounding() const { return m_applyWordRounding; }
127 bool spacingDisabled() const { return m_disableSpacing; }
129 void disableSpacing() { m_disableSpacing = true; }
130 void disableRoundingHacks() { m_applyRunRounding = m_applyWordRounding = false; }
131 void setDirection(TextDirection direction) { m_direction = direction; }
132 void setDirectionalOverride(bool override) { m_directionalOverride = override; }
134 class RenderingContext : public RefCounted<RenderingContext> {
136 virtual ~RenderingContext() { }
138 #if ENABLE(SVG_FONTS)
139 virtual GlyphData glyphDataForCharacter(const Font&, const TextRun&, WidthIterator&, UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength) = 0;
140 virtual void drawSVGGlyphs(GraphicsContext*, const TextRun&, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const = 0;
141 virtual float floatWidthUsingSVGFont(const Font&, const TextRun&, int& charsConsumed, String& glyphName) const = 0;
145 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
146 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
148 static void setAllowsRoundingHacks(bool);
149 static bool allowsRoundingHacks();
152 static bool s_allowsRoundingHacks;
154 const UChar* m_characters;
155 int m_charactersLength; // Marks the end of the m_characters buffer. Default equals to m_len.
158 // m_xpos is the x position relative to the left start of the text line, not relative to the left
159 // start of the containing block. In the case of right alignment or center alignment, left start of
160 // the text line is not the same as left start of the containing block.
163 ExpansionBehavior m_expansionBehavior;
165 float m_horizontalGlyphStretch;
168 TextDirection m_direction;
169 bool m_directionalOverride; // Was this direction set by an override character.
170 bool m_applyRunRounding;
171 bool m_applyWordRounding;
172 bool m_disableSpacing;
173 RefPtr<RenderingContext> m_renderingContext;