]>
Commit | Line | Data |
---|---|---|
a90939db JF |
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 Apple Computer, Inc. | |
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 "TextRun.h" | |
29 | #include "FontDescription.h" | |
30 | #include "SimpleFontData.h" | |
31 | #include <wtf/HashMap.h> | |
32 | #include <wtf/MathExtras.h> | |
33 | ||
34 | #if PLATFORM(QT) | |
35 | #include <QFont> | |
36 | #endif | |
37 | ||
38 | namespace WebCore { | |
39 | ||
40 | class FloatPoint; | |
41 | class FloatRect; | |
42 | class FontData; | |
43 | class FontFallbackList; | |
44 | class FontPlatformData; | |
45 | class FontSelector; | |
46 | class GlyphBuffer; | |
47 | class GlyphPageTreeNode; | |
48 | class GraphicsContext; | |
49 | class IntPoint; | |
50 | class SVGFontElement; | |
51 | ||
52 | struct GlyphData; | |
53 | ||
54 | const unsigned defaultUnitsPerEm = 1000; | |
55 | ||
56 | class Font { | |
57 | public: | |
58 | Font(); | |
59 | Font(const FontDescription&, float letterSpacing, float wordSpacing); | |
60 | // This constructor is only used if the platform wants to start with a native font. | |
61 | Font(const FontPlatformData&, bool isPrinting); | |
62 | ~Font(); | |
63 | ||
64 | Font(const Font&); | |
65 | Font& operator=(const Font&); | |
66 | ||
67 | bool operator==(const Font& other) const; | |
68 | bool operator!=(const Font& other) const { | |
69 | return !(*this == other); | |
70 | } | |
71 | ||
72 | const FontDescription& fontDescription() const { return m_fontDescription; } | |
73 | ||
74 | int pixelSize() const { return fontDescription().computedPixelSize(); } | |
75 | float size() const { return fontDescription().computedSize(); } | |
76 | ||
77 | void update(PassRefPtr<FontSelector>) const; | |
78 | ||
79 | float drawText(GraphicsContext*, const TextRun&, const FloatPoint&, int from = 0, int to = -1) const; | |
80 | ||
81 | int width(const TextRun& run) const { return lroundf(floatWidth(run)); } | |
82 | float floatWidth(const TextRun&) const; | |
83 | float floatWidth(const TextRun& run, int extraCharsAvailable, int& charsConsumed, String& glyphName) const; | |
84 | ||
85 | int offsetForPosition(const TextRun&, int position, bool includePartialGlyphs) const; | |
86 | FloatRect selectionRectForText(const TextRun&, const IntPoint&, int h, int from = 0, int to = -1) const; | |
87 | ||
88 | bool isSmallCaps() const { return m_fontDescription.smallCaps(); } | |
89 | ||
90 | float wordSpacing() const { return m_wordSpacing; } | |
91 | float letterSpacing() const { return m_letterSpacing; } | |
92 | void setWordSpacing(float s) { m_wordSpacing = s; } | |
93 | void setLetterSpacing(float s) { m_letterSpacing = s; } | |
94 | bool isFixedPitch() const; | |
95 | bool isPrinterFont() const { return m_fontDescription.usePrinterFont(); } | |
96 | ||
97 | FontRenderingMode renderingMode() const { return m_fontDescription.renderingMode(); } | |
98 | ||
99 | FontFamily& firstFamily() { return m_fontDescription.firstFamily(); } | |
100 | const FontFamily& family() const { return m_fontDescription.family(); } | |
101 | ||
102 | bool italic() const { return m_fontDescription.italic(); } | |
103 | FontWeight weight() const { return m_fontDescription.weight(); } | |
104 | ||
105 | bool isPlatformFont() const { return m_isPlatformFont; } | |
106 | ||
107 | // Metrics that we query the FontFallbackList for. | |
108 | int ascent() const { return primaryFont()->ascent(); } | |
109 | int descent() const { return primaryFont()->descent(); } | |
110 | int height() const { return ascent() + descent(); } | |
111 | int lineSpacing() const { return primaryFont()->lineSpacing(); } | |
112 | int lineGap() const { return primaryFont()->lineGap(); } | |
113 | float xHeight() const { return primaryFont()->xHeight(); } | |
114 | unsigned unitsPerEm() const { return primaryFont()->unitsPerEm(); } | |
115 | int spaceWidth() const { return (int)ceilf(primaryFont()->m_adjustedSpaceWidth + m_letterSpacing); } | |
116 | int tabWidth() const { return 8 * spaceWidth(); } | |
117 | ||
118 | const SimpleFontData* primaryFont() const { | |
119 | if (!m_cachedPrimaryFont) | |
120 | cachePrimaryFont(); | |
121 | return m_cachedPrimaryFont; | |
122 | } | |
123 | ||
124 | const FontData* fontDataAt(unsigned) const; | |
125 | const GlyphData& glyphDataForCharacter(UChar32, bool mirror, bool forceSmallCaps = false) const; | |
126 | // Used for complex text, and does not utilize the glyph map cache. | |
127 | const FontData* fontDataForCharacters(const UChar*, int length) const; | |
128 | ||
129 | #if PLATFORM(QT) | |
130 | QFont font() const; | |
131 | #endif | |
132 | ||
133 | private: | |
134 | #if ENABLE(SVG_FONTS) | |
135 | void drawTextUsingSVGFont(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; | |
136 | float floatWidthUsingSVGFont(const TextRun&) const; | |
137 | float floatWidthUsingSVGFont(const TextRun&, int extraCharsAvailable, int& charsConsumed, String& glyphName) const; | |
138 | FloatRect selectionRectForTextUsingSVGFont(const TextRun&, const IntPoint&, int h, int from, int to) const; | |
139 | int offsetForPositionForTextUsingSVGFont(const TextRun&, int position, bool includePartialGlyphs) const; | |
140 | #endif | |
141 | ||
142 | #if USE(FONT_FAST_PATH) | |
143 | bool canUseGlyphCache(const TextRun&) const; | |
144 | float drawSimpleText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; | |
145 | void drawGlyphs(GraphicsContext*, const SimpleFontData*, const GlyphBuffer&, int from, int to, const FloatPoint&, bool setColor = true) const; | |
146 | void drawGlyphBuffer(GraphicsContext*, const GlyphBuffer&, const TextRun&, FloatPoint&) const; | |
147 | float floatWidthForSimpleText(const TextRun&, GlyphBuffer*) const; | |
148 | int offsetForPositionForSimpleText(const TextRun&, int position, bool includePartialGlyphs) const; | |
149 | FloatRect selectionRectForSimpleText(const TextRun&, const IntPoint&, int h, int from, int to) const; | |
150 | #endif | |
151 | ||
152 | float drawComplexText(GraphicsContext*, const TextRun&, const FloatPoint&, int from, int to) const; | |
153 | float floatWidthForComplexText(const TextRun&) const; | |
154 | int offsetForPositionForComplexText(const TextRun&, int position, bool includePartialGlyphs) const; | |
155 | FloatRect selectionRectForComplexText(const TextRun&, const IntPoint&, int h, int from, int to) const; | |
156 | void cachePrimaryFont() const; | |
157 | ||
158 | friend struct WidthIterator; | |
159 | ||
160 | public: | |
161 | bool equalForTextAutoSizing (const Font &other) const { | |
162 | return (m_fontDescription.equalForTextAutoSizing(other.m_fontDescription) && | |
163 | m_letterSpacing == other.m_letterSpacing && | |
164 | m_wordSpacing == other.m_wordSpacing); | |
165 | } | |
166 | ||
167 | // Useful for debugging the different font rendering code paths. | |
168 | #if USE(FONT_FAST_PATH) | |
169 | enum CodePath { Auto, Simple, Complex }; | |
170 | static void setCodePath(CodePath); | |
171 | static CodePath codePath(); | |
172 | static CodePath s_codePath; | |
173 | ||
174 | static const uint8_t gRoundingHackCharacterTable[256]; | |
175 | static bool isRoundingHackCharacter(UChar32 c) | |
176 | { | |
177 | return (((c & ~0xFF) == 0 && gRoundingHackCharacterTable[c]) || c == 0x200e || c == 0x200f); | |
178 | } | |
179 | #endif | |
180 | ||
181 | FontSelector* fontSelector() const; | |
182 | static bool treatAsSpace(UChar c) { return c == ' ' || c == '\t' || c == '\n' || c == 0x00A0; } | |
183 | static bool treatAsZeroWidthSpace(UChar c) { return c < 0x20 || (c >= 0x7F && c < 0xA0) || c == 0x200e || c == 0x200f || (c >= 0x202a && c <= 0x202e) || c == 0xFFFC; } | |
184 | ||
185 | #if ENABLE(SVG_FONTS) | |
186 | bool isSVGFont() const; | |
187 | SVGFontElement* svgFont() const; | |
188 | #endif | |
189 | ||
190 | private: | |
191 | FontDescription m_fontDescription; | |
192 | mutable RefPtr<FontFallbackList> m_fontList; | |
193 | mutable HashMap<int, GlyphPageTreeNode*> m_pages; | |
194 | mutable GlyphPageTreeNode* m_pageZero; | |
195 | mutable const SimpleFontData* m_cachedPrimaryFont; | |
196 | float m_letterSpacing; | |
197 | float m_wordSpacing; | |
198 | bool m_isPlatformFont; | |
199 | }; | |
200 | ||
201 | } | |
202 | ||
203 | #endif |