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/fonts/TextBlob.h"
33 #include "platform/text/TextDirection.h"
34 #include "platform/text/TextPath.h"
35 #include "wtf/HashMap.h"
36 #include "wtf/HashSet.h"
37 #include "wtf/MathExtras.h"
38 #include "wtf/unicode/CharacterNames.h"
39
40 // "X11/X.h" defines Complex to 0 and conflicts
41 // with Complex value in CodePath enum.
42 #ifdef Complex
43 #undef Complex
44 #endif
45
46 class SkTextBlob;
47 struct SkPoint;
48
49 namespace blink {
50
51 class FloatPoint;
52 class FloatRect;
53 class FontData;
54 class FontMetrics;
55 class FontSelector;
56 class GlyphBuffer;
57 class GraphicsContext;
58 class TextRun;
59 struct TextRunPaintInfo;
60
61 struct GlyphData;
62
63 struct GlyphOverflow {
GlyphOverflowGlyphOverflow64 GlyphOverflow()
65 : left(0)
66 , right(0)
67 , top(0)
68 , bottom(0)
69 , computeBounds(false)
70 {
71 }
72
isZeroGlyphOverflow73 bool isZero() const
74 {
75 return !left && !right && !top && !bottom;
76 }
77
78 int left;
79 int right;
80 int top;
81 int bottom;
82 bool computeBounds;
83 };
84
85
86 class PLATFORM_EXPORT Font {
87 public:
88 Font();
89 Font(const FontDescription&);
90 ~Font();
91
92 Font(const Font&);
93 Font& operator=(const Font&);
94
95 bool operator==(const Font& other) const;
96 bool operator!=(const Font& other) const { return !(*this == other); }
97
fontDescription()98 const FontDescription& fontDescription() const { return m_fontDescription; }
99
100 void update(PassRefPtrWillBeRawPtr<FontSelector>) const;
101
102 enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
103 float drawText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&, CustomFontNotReadyAction = DoNotPaintIfFontNotReady) const;
104 void drawEmphasisMarks(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
105
106 float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
107 float width(const TextRun&, int& charsConsumed, Glyph& glyphId) const;
108
109 int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
110 FloatRect selectionRectForText(const TextRun&, const FloatPoint&, int h, int from = 0, int to = -1, bool accountForGlyphBounds = false) const;
111
112 bool isFixedPitch() const;
113
114 // Metrics that we query the FontFallbackList for.
fontMetrics()115 const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
spaceWidth()116 float spaceWidth() const { return primaryFont()->spaceWidth() + fontDescription().letterSpacing(); }
117 float tabWidth(const SimpleFontData&, unsigned tabSize, float position) const;
tabWidth(unsigned tabSize,float position)118 float tabWidth(unsigned tabSize, float position) const { return tabWidth(*primaryFont(), tabSize, position); }
119
120 int emphasisMarkAscent(const AtomicString&) const;
121 int emphasisMarkDescent(const AtomicString&) const;
122 int emphasisMarkHeight(const AtomicString&) const;
123
124 const SimpleFontData* primaryFont() const;
125 const FontData* fontDataAt(unsigned) const;
126 inline GlyphData glyphDataForCharacter(UChar32& c, bool mirror, bool spaceNormalize = false, FontDataVariant variant = AutoVariant) const
127 {
128 return glyphDataAndPageForCharacter(c, mirror, spaceNormalize, variant).first;
129 }
130 #if OS(MACOSX)
131 const SimpleFontData* fontDataForCombiningCharacterSequence(const UChar*, size_t length, FontDataVariant) const;
132 #endif
133 std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32&, bool mirror, bool normalizeSpace = false, FontDataVariant = AutoVariant) const;
134 bool primaryFontHasGlyphForCharacter(UChar32) const;
135
136 CodePath codePath(const TextRun&) const;
137
138 PassTextBlobPtr buildTextBlob(const TextRunPaintInfo&, const FloatPoint& textOrigin, bool couldUseLCDRenderedText, CustomFontNotReadyAction = DoNotPaintIfFontNotReady) const;
139 void drawTextBlob(GraphicsContext*, const SkTextBlob*, const SkPoint& origin) const;
140
141 private:
142 enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
143
144 // Returns the initial in-stream advance.
145 float getGlyphsAndAdvancesForSimpleText(const TextRunPaintInfo&, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
146 float drawSimpleText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&) const;
147 void drawEmphasisMarksForSimpleText(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
148 void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, unsigned from, unsigned numGlyphs, const FloatPoint&, const FloatRect& textRect) const;
149 float drawGlyphBuffer(GraphicsContext*, const TextRunPaintInfo&, const GlyphBuffer&, const FloatPoint&) const;
150 void drawEmphasisMarks(GraphicsContext*, const TextRunPaintInfo&, const GlyphBuffer&, const AtomicString&, const FloatPoint&) const;
151 float floatWidthForSimpleText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, IntRectExtent* glyphBounds = 0) const;
152 int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
153 FloatRect selectionRectForSimpleText(const TextRun&, const FloatPoint&, int h, int from, int to, bool accountForGlyphBounds) const;
154
155 bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
156
157 // Returns the initial in-stream advance.
158 float getGlyphsAndAdvancesForComplexText(const TextRunPaintInfo&, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
159 float drawComplexText(GraphicsContext*, const TextRunPaintInfo&, const FloatPoint&) const;
160 void drawEmphasisMarksForComplexText(GraphicsContext*, const TextRunPaintInfo&, const AtomicString& mark, const FloatPoint&) const;
161 float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts, IntRectExtent* glyphBounds) const;
162 int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
163 FloatRect selectionRectForComplexText(const TextRun&, const FloatPoint&, int h, int from, int to) const;
164
165 PassTextBlobPtr buildTextBlobForSimpleText(const TextRunPaintInfo&, const FloatPoint& textOrigin, bool couldUseLCDRenderedText) const;
166 PassTextBlobPtr buildTextBlob(const GlyphBuffer&, float initialAdvance, const FloatRect& bounds, float& advance, bool couldUseLCD) const;
167
168 friend struct WidthIterator;
169 friend class SVGTextRunRenderingContext;
170
171 public:
172 // Useful for debugging the different font rendering code paths.
173 static void setCodePath(CodePath);
174 static CodePath codePath();
175 static CodePath s_codePath;
176
177 FontSelector* fontSelector() const;
178
fontList()179 FontFallbackList* fontList() const { return m_fontFallbackList.get(); }
180
181 void willUseFontData(UChar32) const;
182
183 static FloatRect pixelSnappedSelectionRect(float fromX, float toX, float y, float height);
184 private:
loadingCustomFonts()185 bool loadingCustomFonts() const
186 {
187 return m_fontFallbackList && m_fontFallbackList->loadingCustomFonts();
188 }
189
shouldSkipDrawing()190 bool shouldSkipDrawing() const
191 {
192 return m_fontFallbackList && m_fontFallbackList->shouldSkipDrawing();
193 }
194
195 FontDescription m_fontDescription;
196 mutable RefPtr<FontFallbackList> m_fontFallbackList;
197 };
198
~Font()199 inline Font::~Font()
200 {
201 }
202
primaryFont()203 inline const SimpleFontData* Font::primaryFont() const
204 {
205 ASSERT(m_fontFallbackList);
206 return m_fontFallbackList->primarySimpleFontData(m_fontDescription);
207 }
208
fontDataAt(unsigned index)209 inline const FontData* Font::fontDataAt(unsigned index) const
210 {
211 ASSERT(m_fontFallbackList);
212 return m_fontFallbackList->fontDataAt(m_fontDescription, index);
213 }
214
isFixedPitch()215 inline bool Font::isFixedPitch() const
216 {
217 ASSERT(m_fontFallbackList);
218 return m_fontFallbackList->isFixedPitch(m_fontDescription);
219 }
220
fontSelector()221 inline FontSelector* Font::fontSelector() const
222 {
223 return m_fontFallbackList ? m_fontFallbackList->fontSelector() : 0;
224 }
225
tabWidth(const SimpleFontData & fontData,unsigned tabSize,float position)226 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
227 {
228 if (!tabSize)
229 return fontDescription().letterSpacing();
230 float tabWidth = tabSize * fontData.spaceWidth() + fontDescription().letterSpacing();
231 return tabWidth - fmodf(position, tabWidth);
232 }
233
234 } // namespace blink
235
236 #endif
237