tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / Font.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, 2010, 2011 Apple Inc. All rights reserved.
6  * Copyright (C) 2008 Holger Hans Peter Freyther
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #ifndef Font_h
26 #define Font_h
27
28 #include "FontDescription.h"
29 #include "FontFallbackList.h"
30 #include "SimpleFontData.h"
31 #include "TextDirection.h"
32 #include "TypesettingFeatures.h"
33 #include <wtf/HashMap.h>
34 #include <wtf/HashSet.h>
35 #include <wtf/MathExtras.h>
36 #include <wtf/unicode/CharacterNames.h>
37
38 #if PLATFORM(QT)
39 #if HAVE(QRAWFONT)
40 #include <QRawFont>
41 QT_BEGIN_NAMESPACE
42 class QTextLayout;
43 QT_END_NAMESPACE
44 #else
45 #include <QFont>
46 #endif
47 #endif
48
49 // "X11/X.h" defines Complex to 0 and conflicts
50 // with Complex value in CodePath enum.
51 #ifdef Complex
52 #undef Complex
53 #endif
54
55 namespace WebCore {
56
57 class FloatPoint;
58 class FloatRect;
59 class FontData;
60 class FontMetrics;
61 class FontPlatformData;
62 class FontSelector;
63 class GlyphBuffer;
64 class GlyphPageTreeNode;
65 class GraphicsContext;
66 class TextRun;
67
68 struct GlyphData;
69
70 struct GlyphOverflow {
71     GlyphOverflow()
72         : left(0)
73         , right(0)
74         , top(0)
75         , bottom(0)
76         , computeBounds(false)
77     {
78     }
79
80     int left;
81     int right;
82     int top;
83     int bottom;
84     bool computeBounds;
85 };
86
87
88 class Font {
89 public:
90     Font();
91     Font(const FontDescription&, short letterSpacing, short wordSpacing);
92     // This constructor is only used if the platform wants to start with a native font.
93     Font(const FontPlatformData&, bool isPrinting, FontSmoothingMode = AutoSmoothing);
94     ~Font();
95
96     Font(const Font&);
97     Font& operator=(const Font&);
98
99     bool operator==(const Font& other) const;
100     bool operator!=(const Font& other) const { return !(*this == other); }
101
102     const FontDescription& fontDescription() const { return m_fontDescription; }
103
104     int pixelSize() const { return fontDescription().computedPixelSize(); }
105     float size() const { return fontDescription().computedSize(); }
106
107     void update(PassRefPtr<FontSelector>) const;
108
109     enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
110     void drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1, CustomFontNotReadyAction = DoNotPaintIfFontNotReady) const;
111     void drawEmphasisMarks(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from = 0, int to = -1) const;
112
113     float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
114     float width(const TextRun&, int& charsConsumed, String& glyphName) const;
115
116     int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
117     FloatRect selectionRectForText(const TextRun&, const FloatPoint&, int h, int from = 0, int to = -1) const;
118
119     bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
120
121     short wordSpacing() const { return m_wordSpacing; }
122     short letterSpacing() const { return m_letterSpacing; }
123     void setWordSpacing(short s) { m_wordSpacing = s; }
124     void setLetterSpacing(short s) { m_letterSpacing = s; }
125     bool isFixedPitch() const;
126     bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
127     
128     FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); }
129
130     TypesettingFeatures typesettingFeatures() const
131     {
132         TextRenderingMode textRenderingMode = m_fontDescription.textRenderingMode();
133         TypesettingFeatures features = textRenderingMode == OptimizeLegibility || textRenderingMode == GeometricPrecision ? Kerning | Ligatures : 0;
134
135         switch (m_fontDescription.kerning()) {
136         case FontDescription::NoneKerning:
137             features &= ~Kerning;
138             break;
139         case FontDescription::NormalKerning:
140             features |= Kerning;
141             break;
142         case FontDescription::AutoKerning:
143             break;
144         }
145
146         switch (m_fontDescription.commonLigaturesState()) {
147         case FontDescription::DisabledLigaturesState:
148             features &= ~Ligatures;
149             break;
150         case FontDescription::EnabledLigaturesState:
151             features |= Ligatures;
152             break;
153         case FontDescription::NormalLigaturesState:
154             break;
155         }
156
157         return features;
158     }
159
160     FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
161     const FontFamily& family() const { return m_fontDescription.family(); }
162
163     FontItalic italic() const { return m_fontDescription.italic(); }
164     FontWeight weight() const { return m_fontDescription.weight(); }
165     FontWidthVariant widthVariant() const { return m_fontDescription.widthVariant(); }
166
167     bool isPlatformFont() const { return m_isPlatformFont; }
168
169     // Metrics that we query the FontFallbackList for.
170     const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
171     float spaceWidth() const { return primaryFont()->spaceWidth() + m_letterSpacing; }
172     float tabWidth(const SimpleFontData&, unsigned tabSize, float position) const;
173     float tabWidth(unsigned tabSize, float position) const { return tabWidth(*primaryFont(), tabSize, position); }
174
175     int emphasisMarkAscent(const AtomicString&) const;
176     int emphasisMarkDescent(const AtomicString&) const;
177     int emphasisMarkHeight(const AtomicString&) const;
178
179     const SimpleFontData* primaryFont() const;
180     const FontData* fontDataAt(unsigned) const;
181     GlyphData glyphDataForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
182 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
183     const SimpleFontData* fontDataForCombiningCharacterSequence(const UChar*, size_t length, FontDataVariant) const;
184 #endif
185     std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
186     bool primaryFontHasGlyphForCharacter(UChar32) const;
187
188     static bool isCJKIdeograph(UChar32);
189     static bool isCJKIdeographOrSymbol(UChar32);
190
191     static unsigned expansionOpportunityCount(const UChar*, size_t length, TextDirection, bool& isAfterExpansion);
192
193 #if PLATFORM(QT)
194 #if HAVE(QRAWFONT)
195     QRawFont rawFont() const;
196 #else
197     QFont font() const;
198 #endif
199     QFont syntheticFont() const;
200 #endif
201
202     static void setShouldUseSmoothing(bool);
203     static bool shouldUseSmoothing();
204
205     enum CodePath { Auto, Simple, Complex, SimpleWithGlyphOverflow };
206     CodePath codePath(const TextRun&) const;
207     static CodePath characterRangeCodePath(const UChar*, unsigned len);
208
209 private:
210     enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
211
212     // Returns the initial in-stream advance.
213     float getGlyphsAndAdvancesForSimpleText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
214     void drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
215     void drawEmphasisMarksForSimpleText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from, int to) const;
216     void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
217     void drawGlyphBuffer(GraphicsContext*, const TextRun&, const GlyphBuffer&, const FloatPoint&) const;
218     void drawEmphasisMarks(GraphicsContext*, const TextRun&, const GlyphBuffer&, const AtomicString&, const FloatPoint&) const;
219     float floatWidthForSimpleText(const TextRun&, GlyphBuffer*, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
220     int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
221     FloatRect selectionRectForSimpleText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
222
223     bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
224
225     static bool canReturnFallbackFontsForComplexText();
226     static bool canExpandAroundIdeographsInComplexText();
227
228     // Returns the initial in-stream advance.
229     float getGlyphsAndAdvancesForComplexText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
230     void drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
231     void drawEmphasisMarksForComplexText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from, int to) const;
232     float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
233     int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
234     FloatRect selectionRectForComplexText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
235
236     friend struct WidthIterator;
237     friend class SVGTextRunRenderingContext;
238
239 public:
240     // Useful for debugging the different font rendering code paths.
241     static void setCodePath(CodePath);
242     static CodePath codePath();
243     static CodePath s_codePath;
244
245     static const uint8_t s_roundingHackCharacterTable[256];
246     static bool isRoundingHackCharacter(UChar32 c)
247     {
248         return !(c & ~0xFF) && s_roundingHackCharacterTable[c];
249     }
250
251     FontSelector* fontSelector() const;
252     static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == noBreakSpace; }
253     static bool treatAsZeroWidthSpace(UChar c) { return treatAsZeroWidthSpaceInComplexScript(c) || c == 0x200c || c == 0x200d; }
254     static bool treatAsZeroWidthSpaceInComplexScript(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == softHyphen || c == zeroWidthSpace || (c >= 0x200e && c <= 0x200f) || (c >= 0x202a && c <= 0x202e) || c == zeroWidthNoBreakSpace || c == objectReplacementCharacter; }
255     static bool canReceiveTextEmphasis(UChar32 c);
256
257     static inline UChar normalizeSpaces(UChar character)
258     {
259         if (treatAsSpace(character))
260             return space;
261
262         if (treatAsZeroWidthSpace(character))
263             return zeroWidthSpace;
264
265         return character;
266     }
267
268     static String normalizeSpaces(const UChar*, unsigned length);
269
270     bool needsTranscoding() const { return m_needsTranscoding; }
271     FontFallbackList* fontList() const { return m_fontList.get(); }
272
273 private:
274     bool loadingCustomFonts() const
275     {
276         return m_fontList && m_fontList->loadingCustomFonts();
277     }
278 #if PLATFORM(QT) && HAVE(QRAWFONT)
279     void initFormatForTextLayout(QTextLayout*) const;
280 #endif
281
282     FontDescription m_fontDescription;
283     mutable RefPtr<FontFallbackList> m_fontList;
284     short m_letterSpacing;
285     short m_wordSpacing;
286     bool m_isPlatformFont;
287     bool m_needsTranscoding;
288 };
289
290 inline Font::~Font()
291 {
292 }
293
294 inline const SimpleFontData* Font::primaryFont() const
295 {
296     ASSERT(m_fontList);
297     return m_fontList->primarySimpleFontData(this);
298 }
299
300 inline const FontData* Font::fontDataAt(unsigned index) const
301 {
302     ASSERT(m_fontList);
303     return m_fontList->fontDataAt(this, index);
304 }
305
306 inline bool Font::isFixedPitch() const
307 {
308     ASSERT(m_fontList);
309     return m_fontList->isFixedPitch(this);
310 }
311
312 inline FontSelector* Font::fontSelector() const
313 {
314     return m_fontList ? m_fontList->fontSelector() : 0;
315 }
316
317 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
318 {
319     if (!tabSize)
320         return letterSpacing();
321     float tabWidth = tabSize * fontData.spaceWidth() + letterSpacing();
322     return tabWidth - fmodf(position, tabWidth);
323 }
324
325 }
326
327 #endif