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 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)
55 , m_charactersLength(len)
58 , m_expansion(expansion)
59 , m_expansionBehavior(expansionBehavior)
61 , m_horizontalGlyphStretch(1)
63 , m_allowTabs(allowTabs)
64 , m_direction(direction)
65 , m_directionalOverride(directionalOverride)
66 , m_disableSpacing(false)
70 TextRun(const String& s, bool allowTabs = false, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false)
71 : m_characters(s.characters())
72 , m_charactersLength(s.length())
75 , m_expansion(expansion)
76 , m_expansionBehavior(expansionBehavior)
78 , m_horizontalGlyphStretch(1)
80 , m_allowTabs(allowTabs)
81 , m_direction(direction)
82 , m_directionalOverride(directionalOverride)
83 , m_disableSpacing(false)
87 UChar operator[](int i) const { ASSERT(i >= 0 && i < m_len); return m_characters[i]; }
88 const UChar* data(int i) const { ASSERT(i >= 0 && i < m_len); return &m_characters[i]; }
90 const UChar* characters() const { return m_characters; }
91 int length() const { return m_len; }
92 int charactersLength() const { return m_charactersLength; }
94 void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
95 void setCharactersLength(int charactersLength) { m_charactersLength = charactersLength; }
98 float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
99 void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
102 bool allowTabs() const { return m_allowTabs; }
103 void setAllowTabs(bool allowTabs) { m_allowTabs = allowTabs; }
104 float xPos() const { return m_xpos; }
105 void setXPos(float xPos) { m_xpos = xPos; }
106 float expansion() const { return m_expansion; }
107 bool allowsLeadingExpansion() const { return m_expansionBehavior & AllowLeadingExpansion; }
108 bool allowsTrailingExpansion() const { return m_expansionBehavior & AllowTrailingExpansion; }
109 TextDirection direction() const { return m_direction; }
110 bool rtl() const { return m_direction == RTL; }
111 bool ltr() const { return m_direction == LTR; }
112 bool directionalOverride() const { return m_directionalOverride; }
113 bool spacingDisabled() const { return m_disableSpacing; }
115 void disableSpacing() { m_disableSpacing = true; }
116 void setDirection(TextDirection direction) { m_direction = direction; }
117 void setDirectionalOverride(bool override) { m_directionalOverride = override; }
119 class RenderingContext : public RefCounted<RenderingContext> {
121 virtual ~RenderingContext() { }
123 #if ENABLE(SVG_FONTS)
124 virtual GlyphData glyphDataForCharacter(const Font&, const TextRun&, WidthIterator&, UChar32 character, bool mirror, int currentCharacter, unsigned& advanceLength) = 0;
125 virtual void drawSVGGlyphs(GraphicsContext*, const TextRun&, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const = 0;
126 virtual float floatWidthUsingSVGFont(const Font&, const TextRun&, int& charsConsumed, String& glyphName) const = 0;
130 RenderingContext* renderingContext() const { return m_renderingContext.get(); }
131 void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
134 const UChar* m_characters;
135 int m_charactersLength; // Marks the end of the m_characters buffer. Default equals to m_len.
138 // m_xpos is the x position relative to the left start of the text line, not relative to the left
139 // start of the containing block. In the case of right alignment or center alignment, left start of
140 // the text line is not the same as left start of the containing block.
143 ExpansionBehavior m_expansionBehavior;
145 float m_horizontalGlyphStretch;
148 TextDirection m_direction;
149 bool m_directionalOverride; // Was this direction set by an override character.
150 bool m_disableSpacing;
151 RefPtr<RenderingContext> m_renderingContext;