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 "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"
39 // "X11/X.h" defines Complex to 0 and conflicts
40 // with Complex value in CodePath enum.
51 class FontPlatformData;
54 class GraphicsContext;
56 struct TextRunPaintInfo;
60 struct GlyphOverflow {
66 , computeBounds(false)
78 class PLATFORM_EXPORT Font {
81 Font(const FontDescription&);
85 Font& operator=(const Font&);
87 bool operator==(const Font& other) const;
88 bool operator!=(const Font& other) const { return !(*this == other); }
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; }
95 void update(PassRefPtr<FontSelector>) const;
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;
101 float width(const TextRun&, HashSet<const SimpleFontData*>* fallbackFonts = 0, GlyphOverflow* = 0) const;
102 float width(const TextRun&, int& charsConsumed, String& glyphName) const;
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;
107 bool isFixedPitch() const;
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); }
115 int emphasisMarkAscent(const AtomicString&) const;
116 int emphasisMarkDescent(const AtomicString&) const;
117 int emphasisMarkHeight(const AtomicString&) const;
119 const SimpleFontData* primaryFont() const;
120 const FontData* fontDataAt(unsigned) const;
121 inline GlyphData glyphDataForCharacter(UChar32 c, bool mirror, FontDataVariant variant = AutoVariant) const
123 return glyphDataAndPageForCharacter(c, mirror, variant).first;
126 const SimpleFontData* fontDataForCombiningCharacterSequence(const UChar*, size_t length, FontDataVariant) const;
128 std::pair<GlyphData, GlyphPage*> glyphDataAndPageForCharacter(UChar32, bool mirror, FontDataVariant = AutoVariant) const;
129 bool primaryFontHasGlyphForCharacter(UChar32) const;
131 CodePath codePath(const TextRun&) const;
134 enum ForTextEmphasisOrNot { NotForTextEmphasis, ForTextEmphasis };
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;
147 bool getEmphasisMarkGlyphData(const AtomicString&, GlyphData&) const;
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;
157 friend struct WidthIterator;
158 friend class SVGTextRunRenderingContext;
161 // Useful for debugging the different font rendering code paths.
162 static void setCodePath(CodePath);
163 static CodePath codePath();
164 static CodePath s_codePath;
166 FontSelector* fontSelector() const;
168 FontFallbackList* fontList() const { return m_fontFallbackList.get(); }
170 void willUseFontData() const;
173 bool loadingCustomFonts() const
175 return m_fontFallbackList && m_fontFallbackList->loadingCustomFonts();
178 FontDescription m_fontDescription;
179 mutable RefPtr<FontFallbackList> m_fontFallbackList;
186 inline const SimpleFontData* Font::primaryFont() const
188 ASSERT(m_fontFallbackList);
189 return m_fontFallbackList->primarySimpleFontData(m_fontDescription);
192 inline const FontData* Font::fontDataAt(unsigned index) const
194 ASSERT(m_fontFallbackList);
195 return m_fontFallbackList->fontDataAt(m_fontDescription, index);
198 inline bool Font::isFixedPitch() const
200 ASSERT(m_fontFallbackList);
201 return m_fontFallbackList->isFixedPitch(m_fontDescription);
204 inline FontSelector* Font::fontSelector() const
206 return m_fontFallbackList ? m_fontFallbackList->fontSelector() : 0;
209 inline float Font::tabWidth(const SimpleFontData& fontData, unsigned tabSize, float position) const
212 return fontDescription().letterSpacing();
213 float tabWidth = tabSize * fontData.spaceWidth() + fontDescription().letterSpacing();
214 return tabWidth - fmodf(position, tabWidth);