initial import
[vuplus_webkit] / 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 #include <QFont>
40 #endif
41
42 namespace WebCore {
43
44 class FloatPoint;
45 class FloatRect;
46 class FontData;
47 class FontMetrics;
48 class FontPlatformData;
49 class FontSelector;
50 class GlyphBuffer;
51 class GlyphPageTreeNode;
52 class GraphicsContext;
53 class TextRun;
54
55 struct GlyphData;
56
57 struct GlyphOverflow {
58     GlyphOverflow()
59         : left(0)
60         , right(0)
61         , top(0)
62         , bottom(0)
63         , computeBounds(false)
64     {
65     }
66
67     int left;
68     int right;
69     int top;
70     int bottom;
71     bool computeBounds;
72 };
73
74
75 class Font {
76 public:
77     Font();
78     Font(const FontDescription&, short letterSpacing, short wordSpacing);
79     // This constructor is only used if the platform wants to start with a native font.
80     Font(const FontPlatformData&, bool isPrinting, FontSmoothingMode = AutoSmoothing);
81     ~Font();
82
83     Font(const Font&);
84     Font& operator=(const Font&);
85
86     bool operator==(const Font& other) const;
87     bool operator!=(const Font& other) const { return !(*this == other); }
88
89     const FontDescription& fontDescription() const { return m_fontDescription; }
90
91     int pixelSize() const { return fontDescription().computedPixelSize(); }
92     float size() const { return fontDescription().computedSize(); }
93
94     void update(PassRefPtr<FontSelector>) const;
95
96     void drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1) const;
97     void drawEmphasisMarks(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from = 0, int to = -1) const;
98
99     float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
100     float width(const TextRun&, int& charsConsumed, String& glyphName) const;
101
102     int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
103     FloatRect selectionRectForText(const TextRun&, const FloatPoint&, int h, int from = 0, int to = -1) const;
104
105     bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
106
107     short wordSpacing() const { return m_wordSpacing; }
108     short letterSpacing() const { return m_letterSpacing; }
109     void setWordSpacing(short s) { m_wordSpacing = s; }
110     void setLetterSpacing(short s) { m_letterSpacing = s; }
111     bool isFixedPitch() const;
112     bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
113     
114     FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); }
115
116     TypesettingFeatures typesettingFeatures() const
117     {
118         TextRenderingMode textRenderingMode = m_fontDescription.textRenderingMode();
119         return textRenderingMode == OptimizeLegibility || textRenderingMode == GeometricPrecision ? Kerning | Ligatures : 0;
120     }
121
122     FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
123     const FontFamily& family() const { return m_fontDescription.family(); }
124
125     FontItalic italic() const { return m_fontDescription.italic(); }
126     FontWeight weight() const { return m_fontDescription.weight(); }
127     FontWidthVariant widthVariant() const { return m_fontDescription.widthVariant(); }
128
129     bool isPlatformFont() const { return m_isPlatformFont; }
130
131     // Metrics that we query the FontFallbackList for.
132     const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
133     float spaceWidth() const { return primaryFont()->spaceWidth() + m_letterSpacing; }
134     float tabWidth(const SimpleFontData& fontData) const { return 8 * fontData.spaceWidth() + letterSpacing(); }
135     int emphasisMarkAscent(const AtomicString&) const;
136     int emphasisMarkDescent(const AtomicString&) const;
137     int emphasisMarkHeight(const AtomicString&) const;
138
139     const SimpleFontData* primaryFont() const;
140     const FontData* fontDataAt(unsigned) const;
141     GlyphData glyphDataForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
142     std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
143     bool primaryFontHasGlyphForCharacter(UChar32) const;
144
145     static bool isCJKIdeograph(UChar32);
146     static bool isCJKIdeographOrSymbol(UChar32);
147
148     static unsigned expansionOpportunityCount(const UChar*, size_t length, TextDirection, bool& isAfterExpansion);
149
150 #if PLATFORM(QT)
151     QFont font() const;
152 #endif
153
154     static void setShouldUseSmoothing(bool);
155     static bool shouldUseSmoothing();
156
157     enum CodePath { Auto, Simple, Complex, SimpleWithGlyphOverflow };
158
159 private:
160     enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
161
162     // Returns the initial in-stream advance.
163     float getGlyphsAndAdvancesForSimpleText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
164     void drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
165     void drawEmphasisMarksForSimpleText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from, int to) const;
166     void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&) const;
167     void drawGlyphBuffer(GraphicsContext*, const TextRun&, const GlyphBuffer&, const FloatPoint&) const;
168     void drawEmphasisMarks(GraphicsContext*, const TextRun&, const GlyphBuffer&, const AtomicString&, const FloatPoint&) const;
169     float floatWidthForSimpleText(const TextRun&, GlyphBuffer*, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
170     int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
171     FloatRect selectionRectForSimpleText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
172
173     bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
174
175     static bool canReturnFallbackFontsForComplexText();
176     static bool canExpandAroundIdeographsInComplexText();
177
178     CodePath codePath(const TextRun&) const;
179
180     // Returns the initial in-stream advance.
181     float getGlyphsAndAdvancesForComplexText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
182     void drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const;
183     void drawEmphasisMarksForComplexText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, int from, int to) const;
184     float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
185     int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
186     FloatRect selectionRectForComplexText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
187
188     friend struct WidthIterator;
189     friend class SVGTextRunRenderingContext;
190
191 public:
192     // Useful for debugging the different font rendering code paths.
193     static void setCodePath(CodePath);
194     static CodePath codePath();
195     static CodePath s_codePath;
196
197     static const uint8_t s_roundingHackCharacterTable[256];
198     static bool isRoundingHackCharacter(UChar32 c)
199     {
200         return !(c & ~0xFF) && s_roundingHackCharacterTable[c];
201     }
202
203     FontSelector* fontSelector() const;
204     static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == noBreakSpace; }
205     static bool treatAsZeroWidthSpace(UChar c) { return treatAsZeroWidthSpaceInComplexScript(c) || c == 0x200c || c == 0x200d; }
206     static bool treatAsZeroWidthSpaceInComplexScript(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == softHyphen || (c >= 0x200e && c <= 0x200f) || (c >= 0x202a && c <= 0x202e) || c == zeroWidthNoBreakSpace || c == objectReplacementCharacter; }
207     static bool canReceiveTextEmphasis(UChar32 c);
208
209     static inline UChar normalizeSpaces(UChar character)
210     {
211         if (treatAsSpace(character))
212             return space;
213
214         if (treatAsZeroWidthSpace(character))
215             return zeroWidthSpace;
216
217         return character;
218     }
219
220     static String normalizeSpaces(const UChar*, unsigned length);
221
222     bool needsTranscoding() const { return m_needsTranscoding; }
223     FontFallbackList* fontList() const { return m_fontList.get(); }
224
225 private:
226     bool loadingCustomFonts() const
227     {
228         return m_fontList && m_fontList->loadingCustomFonts();
229     }
230
231     FontDescription m_fontDescription;
232     mutable RefPtr<FontFallbackList> m_fontList;
233     short m_letterSpacing;
234     short m_wordSpacing;
235     bool m_isPlatformFont;
236     bool m_needsTranscoding;
237 };
238
239 inline Font::~Font()
240 {
241 }
242
243 inline const SimpleFontData* Font::primaryFont() const
244 {
245     ASSERT(m_fontList);
246     return m_fontList->primarySimpleFontData(this);
247 }
248
249 inline const FontData* Font::fontDataAt(unsigned index) const
250 {
251     ASSERT(m_fontList);
252     return m_fontList->fontDataAt(this, index);
253 }
254
255 inline bool Font::isFixedPitch() const
256 {
257     ASSERT(m_fontList);
258     return m_fontList->isFixedPitch(this);
259 }
260
261 inline FontSelector* Font::fontSelector() const
262 {
263     return m_fontList ? m_fontList->fontSelector() : 0;
264 }
265
266 }
267
268 #endif