tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / 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, float xpos = 0, float expansion = 0, ExpansionBehavior expansionBehavior = AllowTrailingExpansion | ForbidLeadingExpansion, TextDirection direction = LTR, bool directionalOverride = false, bool characterScanForCodePath = true, RoundingHacks roundingHacks = RunRounding | WordRounding)
62         : m_characters(c)
63         , m_charactersLength(len)
64         , m_len(len)
65         , m_xpos(xpos)
66 #if ENABLE(SVG)
67         , m_horizontalGlyphStretch(1)
68 #endif
69         , m_expansion(expansion)
70         , m_expansionBehavior(expansionBehavior)
71         , m_allowTabs(false)
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)
78         , m_tabSize(0)
79     {
80     }
81
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())
85         , m_len(s.length())
86         , m_xpos(xpos)
87 #if ENABLE(SVG)
88         , m_horizontalGlyphStretch(1)
89 #endif
90         , m_expansion(expansion)
91         , m_expansionBehavior(expansionBehavior)
92         , m_allowTabs(false)
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)
99         , m_tabSize(0)
100     {
101     }
102
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]; }
105
106     const UChar* characters() const { return m_characters; }
107     int length() const { return m_len; }
108     int charactersLength() const { return m_charactersLength; }
109
110     void setText(const UChar* c, int len) { m_characters = c; m_len = len; }
111     void setCharactersLength(int charactersLength) { m_charactersLength = charactersLength; }
112
113 #if ENABLE(SVG)
114     float horizontalGlyphStretch() const { return m_horizontalGlyphStretch; }
115     void setHorizontalGlyphStretch(float scale) { m_horizontalGlyphStretch = scale; }
116 #endif
117
118     bool allowTabs() const { return m_allowTabs; }
119     unsigned tabSize() const { return m_tabSize; }
120     void setTabSize(bool, unsigned);
121
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; }
135
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; }
141
142     class RenderingContext : public RefCounted<RenderingContext> {
143     public:
144         virtual ~RenderingContext() { }
145
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;
150 #endif
151     };
152
153     RenderingContext* renderingContext() const { return m_renderingContext.get(); }
154     void setRenderingContext(PassRefPtr<RenderingContext> context) { m_renderingContext = context; }
155
156     static void setAllowsRoundingHacks(bool);
157     static bool allowsRoundingHacks();
158
159 private:
160     static bool s_allowsRoundingHacks;
161
162     const UChar* m_characters;
163     int m_charactersLength; // Marks the end of the m_characters buffer. Default equals to m_len.
164     int m_len;
165
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.
169     float m_xpos;  
170 #if ENABLE(SVG)
171     float m_horizontalGlyphStretch;
172 #endif
173
174     float m_expansion;
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;
183     unsigned m_tabSize;
184     RefPtr<RenderingContext> m_renderingContext;
185 };
186
187 inline void TextRun::setTabSize(bool allow, unsigned size)
188 {
189     m_allowTabs = allow;
190     m_tabSize = size;
191 }
192
193 }
194
195 #endif