initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / TextRun.h
1 /*
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.
6  *
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.
11  *
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.
16  *
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.
21  *
22  */
23
24 #ifndef TextRun_h
25 #define TextRun_h
26
27 #include "TextDirection.h"
28 #include <wtf/RefCounted.h>
29 #include <wtf/text/WTFString.h>
30
31 namespace WebCore {
32
33 class FloatPoint;
34 class FloatRect;
35 class Font;
36 class GraphicsContext;
37 class GlyphBuffer;
38 class SimpleFontData;
39 struct GlyphData;
40 struct WidthIterator;
41
42 class TextRun {
43 public:
44     enum ExpansionBehaviorFlags {
45         ForbidTrailingExpansion = 0 << 0,
46         AllowTrailingExpansion = 1 << 0,
47         ForbidLeadingExpansion = 0 << 1,
48         AllowLeadingExpansion = 1 << 1,
49     };
50
51     typedef unsigned ExpansionBehavior;
52
53     enum RoundingHackFlags {
54         NoRounding = 0,
55         RunRounding = 1 << 0,
56         WordRounding = 1 << 1,
57     };
58
59     typedef unsigned RoundingHacks;
60
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)
62         : m_characters(c)
63         , m_charactersLength(len)
64         , m_len(len)
65         , m_xpos(xpos)
66         , m_expansion(expansion)
67         , m_expansionBehavior(expansionBehavior)
68 #if ENABLE(SVG)
69         , m_horizontalGlyphStretch(1)
70 #endif
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)
77     {
78     }
79
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())
83         , m_len(s.length())
84         , m_xpos(xpos)
85         , m_expansion(expansion)
86         , m_expansionBehavior(expansionBehavior)
87 #if ENABLE(SVG)
88         , m_horizontalGlyphStretch(1)
89 #endif
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)
96     {
97     }
98
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]; }
101
102     const UChar* characters() const { return m_characters; }
103     int length() const { return m_len; }
104     int charactersLength() const { return m_charactersLength; }
105
106     void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
107     void setCharactersLength(int charactersLength) { m_charactersLength = charactersLength; }
108
109 #if ENABLE(SVG)
110     float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
111     void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
112 #endif
113
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; }
128
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; }
133
134     class RenderingContext : public RefCounted<RenderingContext> {
135     public:
136         virtual ~RenderingContext() { }
137
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;
142 #endif
143     };
144
145     RenderingContext* renderingContext() const { return m_renderingContext.get(); }
146     void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
147
148     static void setAllowsRoundingHacks(bool);
149     static bool allowsRoundingHacks();
150
151 private:
152     static bool s_allowsRoundingHacks;
153
154     const UChar* m_characters;
155     int m_charactersLength; // Marks the end of the m_characters buffer. Default equals to m_len.
156     int m_len;
157
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.
161     float m_xpos;  
162     float m_expansion;
163     ExpansionBehavior m_expansionBehavior;
164 #if ENABLE(SVG)
165     float m_horizontalGlyphStretch;
166 #endif
167     bool m_allowTabs;
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;
174 };
175
176 }
177
178 #endif