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
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.
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.
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.
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>
49 // "X11/X.h" defines Complex to 0 and conflicts
50 // with Complex value in CodePath enum.
61 class FontPlatformData;
64 class GlyphPageTreeNode;
65 class GraphicsContext;
70 struct GlyphOverflow {
76 , computeBounds(false)
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);
97 Font& operator=(const Font&);
99 bool operator==(const Font& other) const;
100 bool operator!=(const Font& other) const { return !(*this == other); }
102 const FontDescription& fontDescription() const { return m_fontDescription; }
104 int pixelSize() const { return fontDescription().computedPixelSize(); }
105 float size() const { return fontDescription().computedSize(); }
107 void update(PassRefPtr<FontSelector>) const;
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;
113 float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
114 float width(const TextRun&, int& charsConsumed, String& glyphName) const;
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;
119 bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
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(); }
128 FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); }
130 TypesettingFeatures typesettingFeatures() const
132 TextRenderingMode textRenderingMode = m_fontDescription.textRenderingMode();
133 TypesettingFeatures features = textRenderingMode == OptimizeLegibility || textRenderingMode == GeometricPrecision ? Kerning | Ligatures : 0;
135 switch (m_fontDescription.kerning()) {
136 case FontDescription::NoneKerning:
137 features &= ~Kerning;
139 case FontDescription::NormalKerning:
142 case FontDescription::AutoKerning:
146 switch (m_fontDescription.commonLigaturesState()) {
147 case FontDescription::DisabledLigaturesState:
148 features &= ~Ligatures;
150 case FontDescription::EnabledLigaturesState:
151 features |= Ligatures;
153 case FontDescription::NormalLigaturesState:
160 FontFamily& firstFamily() { return m_fontDescription.firstFamily(); }
161 const FontFamily& family() const { return m_fontDescription.family(); }
163 FontItalic italic() const { return m_fontDescription.italic(); }
164 FontWeight weight() const { return m_fontDescription.weight(); }
165 FontWidthVariant widthVariant() const { return m_fontDescription.widthVariant(); }
167 bool isPlatformFont() const { return m_isPlatformFont; }
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); }
175 int emphasisMarkAscent(const AtomicString&) const;
176 int emphasisMarkDescent(const AtomicString&) const;
177 int emphasisMarkHeight(const AtomicString&) const;
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;
185 std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
186 bool primaryFontHasGlyphForCharacter(UChar32) const;
188 static bool isCJKIdeograph(UChar32);
189 static bool isCJKIdeographOrSymbol(UChar32);
191 static unsigned expansionOpportunityCount(const UChar*, size_t length, TextDirection, bool& isAfterExpansion);
195 QRawFont rawFont() const;
199 QFont syntheticFont() const;
202 static void setShouldUseSmoothing(bool);
203 static bool shouldUseSmoothing();
205 enum CodePath { Auto, Simple, Complex, SimpleWithGlyphOverflow };
206 CodePath codePath(const TextRun&) const;
207 static CodePath characterRangeCodePath(const UChar*, unsigned len);
210 enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
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;
223 bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
225 static bool canReturnFallbackFontsForComplexText();
226 static bool canExpandAroundIdeographsInComplexText();
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;
236 friend struct WidthIterator;
237 friend class SVGTextRunRenderingContext;
240 // Useful for debugging the different font rendering code paths.
241 static void setCodePath(CodePath);
242 static CodePath codePath();
243 static CodePath s_codePath;
245 static const uint8_t s_roundingHackCharacterTable[256];
246 static bool isRoundingHackCharacter(UChar32 c)
248 return !(c & ~0xFF) && s_roundingHackCharacterTable[c];
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);
257 static inline UChar normalizeSpaces(UChar character)
259 if (treatAsSpace(character))
262 if (treatAsZeroWidthSpace(character))
263 return zeroWidthSpace;
268 static String normalizeSpaces(const UChar*, unsigned length);
270 bool needsTranscoding() const { return m_needsTranscoding; }
271 FontFallbackList* fontList() const { return m_fontList.get(); }
274 bool loadingCustomFonts() const
276 return m_fontList && m_fontList->loadingCustomFonts();
278 #if PLATFORM(QT) && HAVE(QRAWFONT)
279 void initFormatForTextLayout(QTextLayout*) const;
282 FontDescription m_fontDescription;
283 mutable RefPtr<FontFallbackList> m_fontList;
284 short m_letterSpacing;
286 bool m_isPlatformFont;
287 bool m_needsTranscoding;
294 inline const SimpleFontData* Font::primaryFont() const
297 return m_fontList->primarySimpleFontData(this);
300 inline const FontData* Font::fontDataAt(unsigned index) const
303 return m_fontList->fontDataAt(this, index);
306 inline bool Font::isFixedPitch() const
309 return m_fontList->isFixedPitch(this);
312 inline FontSelector* Font::fontSelector() const
314 return m_fontList ? m_fontList->fontSelector() : 0;
317 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
320 return letterSpacing();
321 float tabWidth = tabSize * fontData.spaceWidth() + letterSpacing();
322 return tabWidth - fmodf(position, tabWidth);