Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / fonts / 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 "platform/PlatformExport.h"
29 #include "platform/fonts/FontDescription.h"
30 #include "platform/fonts/FontFallbackList.h"
31 #include "platform/fonts/SimpleFontData.h"
32 #include "platform/text/TextDirection.h"
33 #include "platform/text/TextPath.h"
34 #include "wtf/HashMap.h"
35 #include "wtf/HashSet.h"
36 #include "wtf/MathExtras.h"
37 #include "wtf/unicode/CharacterNames.h"
38
39 // "X11/X.h" defines Complex to 0 and conflicts
40 // with Complex value in CodePath enum.
41 #ifdef Complex
42 #undef Complex
43 #endif
44
45 namespace WebCore {
46
47 class FloatPoint;
48 class FloatRect;
49 class FontData;
50 class FontMetrics;
51 class FontPlatformData;
52 class FontSelector;
53 class GlyphBuffer;
54 class GraphicsContext;
55 class TextRun;
56 struct TextRunPaintInfo;
57
58 struct GlyphData;
59
60 struct GlyphOverflow {
61     GlyphOverflow()
62         : left(0)
63         , right(0)
64         , top(0)
65         , bottom(0)
66         , computeBounds(false)
67     {
68     }
69
70     int left;
71     int right;
72     int top;
73     int bottom;
74     bool computeBounds;
75 };
76
77
78 class PLATFORM_EXPORT Font {
79 public:
80     Font();
81     Font(const FontDescription&);
82     ~Font();
83
84     Font(const Font&);
85     Font& operator=(const Font&);
86
87     bool operator==(const Font& other) const;
88     bool operator!=(const Font& other) const { return !(*this == other); }
89
90     const FontDescription& fontDescription() const { return m_fontDescription; }
91     // FIXME: This is currently used by RenderStyle::setWordSpacing and RenderStyle::setLetterSpacing.
92     // They are being removed. Do NOT add new uses of this function. Use FontBuilder instead.
93     FontDescription& mutableFontDescription() { return m_fontDescription; }
94
95     void update(PassRefPtr<FontSelector>) const;
96
97     enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
98     void drawText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&, CustomFontNotReadyAction = DoNotPaintIfFontNotReady) const;
99     void drawEmphasisMarks(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
100
101     float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
102     float width(const TextRun&, int& charsConsumed, String& glyphName) const;
103
104     int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
105     FloatRect selectionRectForText(const TextRun&, const FloatPoint&, int h, int from = 0, int to = -1) const;
106
107     bool isFixedPitch() const;
108
109     // Metrics that we query the FontFallbackList for.
110     const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
111     float spaceWidth() const { return primaryFont()->spaceWidth() + fontDescription().letterSpacing(); }
112     float tabWidth(const SimpleFontData&, unsigned tabSize, float position) const;
113     float tabWidth(unsigned tabSize, float position) const { return tabWidth(*primaryFont(), tabSize, position); }
114
115     int emphasisMarkAscent(const AtomicString&) const;
116     int emphasisMarkDescent(const AtomicString&) const;
117     int emphasisMarkHeight(const AtomicString&) const;
118
119     const SimpleFontData* primaryFont() const;
120     const FontData* fontDataAt(unsigned) const;
121     inline GlyphData glyphDataForCharacter(UChar32 c, bool mirror, FontDataVariant variant = AutoVariant) const
122     {
123         return glyphDataAndPageForCharacter(c, mirror, variant).first;
124     }
125 #if OS(MACOSX)
126     const SimpleFontData* fontDataForCombiningCharacterSequence(const UChar*, size_t length, FontDataVariant) const;
127 #endif
128     std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
129     bool primaryFontHasGlyphForCharacter(UChar32) const;
130
131     CodePath codePath(const TextRun&) const;
132
133 private:
134     enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
135
136     // Returns the initial in-stream advance.
137     float getGlyphsAndAdvancesForSimpleText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
138     void drawSimpleText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&) const;
139     void drawEmphasisMarksForSimpleText(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
140     void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, unsigned from, unsigned numGlyphs, const FloatPoint&, const FloatRect& textRect) const;
141     void drawGlyphBuffer(GraphicsContext*, const TextRunPaintInfo&, const GlyphBuffer&, const FloatPoint&) const;
142     void drawEmphasisMarks(GraphicsContext*, const TextRunPaintInfo&, const GlyphBuffer&, const AtomicString&, const FloatPoint&) const;
143     float floatWidthForSimpleText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
144     int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
145     FloatRect selectionRectForSimpleText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
146
147     bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
148
149     // Returns the initial in-stream advance.
150     float getGlyphsAndAdvancesForComplexText(const TextRun&, int from, int to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
151     void drawComplexText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&) const;
152     void drawEmphasisMarksForComplexText(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
153     float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
154     int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
155     FloatRect selectionRectForComplexText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
156
157     friend struct WidthIterator;
158     friend class SVGTextRunRenderingContext;
159
160 public:
161     // Useful for debugging the different font rendering code paths.
162     static void setCodePath(CodePath);
163     static CodePath codePath();
164     static CodePath s_codePath;
165
166     FontSelector* fontSelector() const;
167
168     FontFallbackList* fontList() const { return m_fontFallbackList.get(); }
169
170     void willUseFontData() const;
171
172 private:
173     bool loadingCustomFonts() const
174     {
175         return m_fontFallbackList && m_fontFallbackList->loadingCustomFonts();
176     }
177
178     FontDescription m_fontDescription;
179     mutable RefPtr<FontFallbackList> m_fontFallbackList;
180 };
181
182 inline Font::~Font()
183 {
184 }
185
186 inline const SimpleFontData* Font::primaryFont() const
187 {
188     ASSERT(m_fontFallbackList);
189     return m_fontFallbackList->primarySimpleFontData(m_fontDescription);
190 }
191
192 inline const FontData* Font::fontDataAt(unsigned index) const
193 {
194     ASSERT(m_fontFallbackList);
195     return m_fontFallbackList->fontDataAt(m_fontDescription, index);
196 }
197
198 inline bool Font::isFixedPitch() const
199 {
200     ASSERT(m_fontFallbackList);
201     return m_fontFallbackList->isFixedPitch(m_fontDescription);
202 }
203
204 inline FontSelector* Font::fontSelector() const
205 {
206     return m_fontFallbackList ? m_fontFallbackList->fontSelector() : 0;
207 }
208
209 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
210 {
211     if (!tabSize)
212         return fontDescription().letterSpacing();
213     float tabWidth = tabSize * fontData.spaceWidth() + fontDescription().letterSpacing();
214     return tabWidth - fmodf(position, tabWidth);
215 }
216
217 }
218
219 #endif