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 "DashArray.h"
29 #include "FontDescription.h"
30 #include "FontGlyphs.h"
32 #include "SimpleFontData.h"
33 #include "TextDirection.h"
34 #include "TypesettingFeatures.h"
35 #include <wtf/HashMap.h>
36 #include <wtf/HashSet.h>
37 #include <wtf/unicode/CharacterNames.h>
39 // "X11/X.h" defines Complex to 0 and conflicts
40 // with Complex value in CodePath enum.
51 class FontPlatformData;
54 class GraphicsContext;
62 struct GlyphOverflow {
68 , computeBounds(false)
74 return !left && !right && !top && !bottom;
77 inline void extendTo(const GlyphOverflow& other)
79 left = std::max(left, other.left);
80 right = std::max(right, other.right);
81 top = std::max(top, other.top);
82 bottom = std::max(bottom, other.bottom);
85 bool operator!=(const GlyphOverflow& other)
87 return left != other.left || right != other.right || top != other.top || bottom != other.bottom;
97 class GlyphToPathTranslator {
99 enum class GlyphUnderlineType {SkipDescenders, SkipGlyph, DrawOverGlyph};
100 virtual bool containsMorePaths() = 0;
101 virtual Path path() = 0;
102 virtual std::pair<float, float> extents() = 0;
103 virtual GlyphUnderlineType underlineType() = 0;
104 virtual void advance() = 0;
105 virtual ~GlyphToPathTranslator() { }
107 GlyphToPathTranslator::GlyphUnderlineType computeUnderlineType(const TextRun&, const GlyphBuffer&, unsigned index);
112 Font(const FontDescription&, float letterSpacing, float wordSpacing);
113 // This constructor is only used if the platform wants to start with a native font.
114 Font(const FontPlatformData&, bool isPrinting, FontSmoothingMode = AutoSmoothing);
116 // FIXME: We should make this constructor platform-independent.
118 Font(const FontPlatformData&, PassRefPtr<FontSelector>);
123 Font& operator=(const Font&);
125 bool operator==(const Font& other) const;
126 bool operator!=(const Font& other) const { return !(*this == other); }
128 const FontDescription& fontDescription() const { return m_fontDescription; }
130 int pixelSize() const { return fontDescription().computedPixelSize(); }
131 float size() const { return fontDescription().computedSize(); }
133 void update(PassRefPtr<FontSelector>) const;
135 enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
136 float drawText(GraphicsContext*, const TextRun&, const FloatPoint&, unsigned from = 0, int to = -1, CustomFontNotReadyAction = DoNotPaintIfFontNotReady) const;
137 void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, unsigned from, unsigned numGlyphs, const FloatPoint&) const;
138 void drawEmphasisMarks(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, unsigned from = 0, int to = -1) const;
140 DashArray dashesForIntersectionsWithRect(const TextRun&, const FloatPoint& textOrigin, const FloatRect& lineExtents) const;
142 float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
143 float width(const TextRun&, int& charsConsumed, String& glyphName) const;
145 PassOwnPtr<TextLayout> createLayout(RenderText*, float xPos, bool collapseWhiteSpace) const;
146 static void deleteLayout(TextLayout*);
147 static float width(TextLayout&, unsigned from, unsigned len, HashSet<const SimpleFontData*>* fallbackFonts = 0);
149 int offsetForPosition(const TextRun&, float position, bool includePartialGlyphs) const;
150 void adjustSelectionRectForText(const TextRun&, LayoutRect& selectionRect, unsigned from = 0, int to = -1) const;
152 bool isSmallCaps() const { return m_fontDescription.smallCaps(); }
154 float wordSpacing() const { return m_wordSpacing; }
155 float letterSpacing() const { return m_letterSpacing; }
156 void setWordSpacing(float s) { m_wordSpacing = s; }
157 void setLetterSpacing(float s) { m_letterSpacing = s; }
158 bool isFixedPitch() const;
159 bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); }
161 FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); }
163 TypesettingFeatures typesettingFeatures() const { return static_cast<TypesettingFeatures>(m_typesettingFeatures); }
165 const AtomicString& firstFamily() const { return m_fontDescription.firstFamily(); }
166 unsigned familyCount() const { return m_fontDescription.familyCount(); }
167 const AtomicString& familyAt(unsigned i) const { return m_fontDescription.familyAt(i); }
169 FontItalic italic() const { return m_fontDescription.italic(); }
170 FontWeight weight() const { return m_fontDescription.weight(); }
171 FontWidthVariant widthVariant() const { return m_fontDescription.widthVariant(); }
173 bool isPlatformFont() const { return m_glyphs->isForPlatformFont(); }
175 const FontMetrics& fontMetrics() const { return primaryFont()->fontMetrics(); }
176 float spaceWidth() const { return primaryFont()->spaceWidth() + m_letterSpacing; }
177 float tabWidth(const SimpleFontData&, unsigned tabSize, float position) const;
178 float tabWidth(unsigned tabSize, float position) const { return tabWidth(*primaryFont(), tabSize, position); }
179 bool hasValidAverageCharWidth() const;
180 bool fastAverageCharWidthIfAvailable(float &width) const; // returns true on success
182 int emphasisMarkAscent(const AtomicString&) const;
183 int emphasisMarkDescent(const AtomicString&) const;
184 int emphasisMarkHeight(const AtomicString&) const;
186 const SimpleFontData* primaryFont() const;
187 const FontData* fontDataAt(unsigned) const;
188 GlyphData glyphDataForCharacter(UChar32 c, bool mirror, FontDataVariant variant = AutoVariant) const
190 return glyphDataAndPageForCharacter(c, mirror, variant).first;
193 const SimpleFontData* fontDataForCombiningCharacterSequence(const UChar*, size_t length, FontDataVariant) const;
195 std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32 c, bool mirror, FontDataVariant variant) const
197 return m_glyphs->glyphDataAndPageForCharacter(m_fontDescription, c, mirror, variant);
199 bool primaryFontHasGlyphForCharacter(UChar32) const;
201 static bool isCJKIdeograph(UChar32);
202 static bool isCJKIdeographOrSymbol(UChar32);
204 static unsigned expansionOpportunityCount(const LChar*, size_t length, TextDirection, bool& isAfterExpansion);
205 static unsigned expansionOpportunityCount(const UChar*, size_t length, TextDirection, bool& isAfterExpansion);
207 static void setShouldUseSmoothing(bool);
208 static bool shouldUseSmoothing();
210 enum CodePath { Auto, Simple, Complex, SimpleWithGlyphOverflow };
211 CodePath codePath(const TextRun&) const;
212 static CodePath characterRangeCodePath(const LChar*, unsigned) { return Simple; }
213 static CodePath characterRangeCodePath(const UChar*, unsigned len);
215 bool primaryFontDataIsSystemFont() const;
218 enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
220 // Returns the initial in-stream advance.
221 float getGlyphsAndAdvancesForSimpleText(const TextRun&, unsigned from, unsigned to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
222 float drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, unsigned from, unsigned to) const;
223 void drawEmphasisMarksForSimpleText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, unsigned from, unsigned to) const;
224 void drawGlyphBuffer(GraphicsContext*, const TextRun&, const GlyphBuffer&, FloatPoint&) const;
225 void drawEmphasisMarks(GraphicsContext*, const TextRun&, const GlyphBuffer&, const AtomicString&, const FloatPoint&) const;
226 float floatWidthForSimpleText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
227 int offsetForPositionForSimpleText(const TextRun&, float position, bool includePartialGlyphs) const;
228 void adjustSelectionRectForSimpleText(const TextRun&, LayoutRect& selectionRect, unsigned from, unsigned to) const;
230 bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
232 static bool canReturnFallbackFontsForComplexText();
233 static bool canExpandAroundIdeographsInComplexText();
235 // Returns the initial in-stream advance.
236 float getGlyphsAndAdvancesForComplexText(const TextRun&, unsigned from, unsigned to, GlyphBuffer&, ForTextEmphasisOrNot = NotForTextEmphasis) const;
237 float drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, unsigned from, unsigned to) const;
238 void drawEmphasisMarksForComplexText(GraphicsContext*, const TextRun&, const AtomicString& mark, const FloatPoint&, unsigned from, unsigned to) const;
239 float floatWidthForComplexText(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
240 int offsetForPositionForComplexText(const TextRun&, float position, bool includePartialGlyphs) const;
241 void adjustSelectionRectForComplexText(const TextRun&, LayoutRect& selectionRect, unsigned from, unsigned to) const;
243 friend struct WidthIterator;
244 friend class SVGTextRunRenderingContext;
247 #if ENABLE(IOS_TEXT_AUTOSIZING)
248 bool equalForTextAutoSizing(const Font& other) const
250 return m_fontDescription.equalForTextAutoSizing(other.m_fontDescription)
251 && m_letterSpacing == other.m_letterSpacing
252 && m_wordSpacing == other.m_wordSpacing;
256 // Useful for debugging the different font rendering code paths.
257 static void setCodePath(CodePath);
258 static CodePath codePath();
259 static CodePath s_codePath;
261 static void setDefaultTypesettingFeatures(TypesettingFeatures);
262 static TypesettingFeatures defaultTypesettingFeatures();
264 static const uint8_t s_roundingHackCharacterTable[256];
265 static bool isRoundingHackCharacter(UChar32 c)
267 return !(c & ~0xFF) && s_roundingHackCharacterTable[c];
270 FontSelector* fontSelector() const;
271 static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == noBreakSpace; }
272 static bool treatAsZeroWidthSpace(UChar c) { return treatAsZeroWidthSpaceInComplexScript(c) || c == 0x200c || c == 0x200d; }
273 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; }
274 static bool canReceiveTextEmphasis(UChar32 c);
276 static inline UChar normalizeSpaces(UChar character)
278 if (treatAsSpace(character))
281 if (treatAsZeroWidthSpace(character))
282 return zeroWidthSpace;
287 static String normalizeSpaces(const LChar*, unsigned length);
288 static String normalizeSpaces(const UChar*, unsigned length);
290 bool useBackslashAsYenSymbol() const { return m_useBackslashAsYenSymbol; }
291 FontGlyphs* glyphs() const { return m_glyphs.get(); }
294 bool loadingCustomFonts() const
296 return m_glyphs && m_glyphs->loadingCustomFonts();
299 TypesettingFeatures computeTypesettingFeatures() const
301 TextRenderingMode textRenderingMode = m_fontDescription.textRenderingMode();
302 TypesettingFeatures features = s_defaultTypesettingFeatures;
304 switch (textRenderingMode) {
305 case AutoTextRendering:
308 features &= ~(Kerning | Ligatures);
310 case GeometricPrecision:
311 case OptimizeLegibility:
312 features |= Kerning | Ligatures;
316 switch (m_fontDescription.kerning()) {
317 case FontDescription::NoneKerning:
318 features &= ~Kerning;
320 case FontDescription::NormalKerning:
323 case FontDescription::AutoKerning:
327 switch (m_fontDescription.commonLigaturesState()) {
328 case FontDescription::DisabledLigaturesState:
329 features &= ~Ligatures;
331 case FontDescription::EnabledLigaturesState:
332 features |= Ligatures;
334 case FontDescription::NormalLigaturesState:
341 static TypesettingFeatures s_defaultTypesettingFeatures;
343 FontDescription m_fontDescription;
344 mutable RefPtr<FontGlyphs> m_glyphs;
345 float m_letterSpacing;
347 mutable bool m_useBackslashAsYenSymbol;
348 mutable unsigned m_typesettingFeatures : 2; // (TypesettingFeatures) Caches values computed from m_fontDescription.
351 void invalidateFontGlyphsCache();
352 void pruneUnreferencedEntriesFromFontGlyphsCache();
353 void clearWidthCaches();
359 inline const SimpleFontData* Font::primaryFont() const
362 return m_glyphs->primarySimpleFontData(m_fontDescription);
365 inline const FontData* Font::fontDataAt(unsigned index) const
368 return m_glyphs->realizeFontDataAt(m_fontDescription, index);
371 inline bool Font::isFixedPitch() const
374 return m_glyphs->isFixedPitch(m_fontDescription);
377 inline FontSelector* Font::fontSelector() const
379 return m_glyphs ? m_glyphs->fontSelector() : 0;
382 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
385 return letterSpacing();
386 float tabWidth = tabSize * fontData.spaceWidth() + letterSpacing();
387 return tabWidth - fmodf(position, tabWidth);
394 template <> void deleteOwnedPtr<WebCore::TextLayout>(WebCore::TextLayout*);