2 * This file is part of the internal font implementation.
4 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved.
5 * Copyright (C) 2007-2008 Torch Mobile, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef SimpleFontData_h
25 #define SimpleFontData_h
27 #include "FontBaseline.h"
29 #include "FontMetrics.h"
30 #include "FontPlatformData.h"
31 #include "FloatRect.h"
32 #include "GlyphMetricsMap.h"
33 #include "GlyphPageTreeNode.h"
34 #include "TypesettingFeatures.h"
35 #include <wtf/OwnPtr.h>
36 #include <wtf/PassOwnPtr.h>
37 #include <wtf/text/StringHash.h>
39 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
40 #include <wtf/RetainPtr.h>
43 #if (PLATFORM(WIN) && !OS(WINCE)) \
44 || (OS(WINDOWS) && PLATFORM(WX))
62 class FontDescription;
66 enum FontDataVariant { AutoVariant, NormalVariant, SmallCapsVariant, EmphasisMarkVariant, BrokenIdeographVariant };
67 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
69 class SimpleFontData : public FontData {
71 class AdditionalFontData {
72 WTF_MAKE_FAST_ALLOCATED;
74 virtual ~AdditionalFontData() { }
76 virtual void initializeFontData(SimpleFontData*, float fontSize) = 0;
77 virtual float widthForSVGGlyph(Glyph, float fontSize) const = 0;
78 virtual bool fillSVGGlyphPage(GlyphPage*, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength, const SimpleFontData*) const = 0;
79 virtual bool applySVGGlyphSelection(WidthIterator&, GlyphData&, bool mirror, int currentCharacter, unsigned& advanceLength) const = 0;
82 // Used to create platform fonts.
83 SimpleFontData(const FontPlatformData&, bool isCustomFont = false, bool isLoading = false, bool isTextOrientationFallback = false);
85 // Used to create SVG Fonts.
86 SimpleFontData(PassOwnPtr<AdditionalFontData>, float fontSize, bool syntheticBold, bool syntheticItalic);
88 virtual ~SimpleFontData();
90 const FontPlatformData& platformData() const { return m_platformData; }
92 SimpleFontData* smallCapsFontData(const FontDescription&) const;
93 SimpleFontData* emphasisMarkFontData(const FontDescription&) const;
94 SimpleFontData* brokenIdeographFontData() const;
96 SimpleFontData* variantFontData(const FontDescription& description, FontDataVariant variant) const
99 case SmallCapsVariant:
100 return smallCapsFontData(description);
101 case EmphasisMarkVariant:
102 return emphasisMarkFontData(description);
103 case BrokenIdeographVariant:
104 return brokenIdeographFontData();
109 ASSERT_NOT_REACHED();
110 return const_cast<SimpleFontData*>(this);
113 SimpleFontData* verticalRightOrientationFontData() const;
114 SimpleFontData* uprightOrientationFontData() const;
116 bool hasVerticalGlyphs() const { return m_hasVerticalGlyphs; }
117 bool isTextOrientationFallback() const { return m_isTextOrientationFallback; }
119 FontMetrics& fontMetrics() { return m_fontMetrics; }
120 const FontMetrics& fontMetrics() const { return m_fontMetrics; }
121 float sizePerUnit() const { return platformData().size() / (fontMetrics().unitsPerEm() ? fontMetrics().unitsPerEm() : 1); }
123 float maxCharWidth() const { return m_maxCharWidth; }
124 void setMaxCharWidth(float maxCharWidth) { m_maxCharWidth = maxCharWidth; }
126 float avgCharWidth() const { return m_avgCharWidth; }
127 void setAvgCharWidth(float avgCharWidth) { m_avgCharWidth = avgCharWidth; }
129 FloatRect boundsForGlyph(Glyph) const;
130 float widthForGlyph(Glyph glyph) const;
131 FloatRect platformBoundsForGlyph(Glyph) const;
132 float platformWidthForGlyph(Glyph) const;
134 float spaceWidth() const { return m_spaceWidth; }
135 float adjustedSpaceWidth() const { return m_adjustedSpaceWidth; }
136 void setSpaceWidth(float spaceWidth) { m_spaceWidth = spaceWidth; }
138 #if USE(CG) || USE(CAIRO) || PLATFORM(WX) || USE(SKIA_ON_MAC_CHROMIUM)
139 float syntheticBoldOffset() const { return m_syntheticBoldOffset; }
142 Glyph spaceGlyph() const { return m_spaceGlyph; }
143 void setSpaceGlyph(Glyph spaceGlyph) { m_spaceGlyph = spaceGlyph; }
144 void setZeroWidthSpaceGlyph(Glyph spaceGlyph) { m_zeroWidthSpaceGlyph = spaceGlyph; }
145 bool isZeroWidthSpaceGlyph(Glyph glyph) const { return glyph == m_zeroWidthSpaceGlyph && glyph; }
147 virtual const SimpleFontData* fontDataForCharacter(UChar32) const;
148 virtual bool containsCharacters(const UChar*, int length) const;
150 void determinePitch();
151 Pitch pitch() const { return m_treatAsFixedPitch ? FixedPitch : VariablePitch; }
153 AdditionalFontData* fontData() const { return m_fontData.get(); }
154 bool isSVGFont() const { return m_fontData; }
156 virtual bool isCustomFont() const { return m_isCustomFont; }
157 virtual bool isLoading() const { return m_isLoading; }
158 virtual bool isSegmented() const;
160 const GlyphData& missingGlyphData() const { return m_missingGlyphData; }
161 void setMissingGlyphData(const GlyphData& glyphData) { m_missingGlyphData = glyphData; }
164 virtual String description() const;
167 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
168 const SimpleFontData* getCompositeFontReferenceFontData(NSFont *key) const;
169 NSFont* getNSFont() const { return m_platformData.font(); }
170 #elif (PLATFORM(WX) && OS(DARWIN))
171 const SimpleFontData* getCompositeFontReferenceFontData(NSFont *key) const;
172 NSFont* getNSFont() const { return m_platformData.nsFont(); }
175 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
176 CFDictionaryRef getCFStringAttributes(TypesettingFeatures, FontOrientation) const;
177 bool canRenderCombiningCharacterSequence(const UChar*, size_t) const;
182 QFont getQtFont() const { return m_platformData.font(); }
184 QRawFont getQtRawFont() const { return m_platformData.rawFont(); }
185 #endif // !HAVE(QRAWFONT)
188 #if PLATFORM(WIN) || (OS(WINDOWS) && PLATFORM(WX))
189 bool isSystemFont() const { return m_isSystemFont; }
190 #if !OS(WINCE) // disable unused members to save space
191 SCRIPT_FONTPROPERTIES* scriptFontProperties() const;
192 SCRIPT_CACHE* scriptCache() const { return &m_scriptCache; }
194 static void setShouldApplyMacAscentHack(bool);
195 static bool shouldApplyMacAscentHack();
196 static float ascentConsideringMacAscentHack(const WCHAR*, float ascent, float descent);
200 wxFont* getWxFont() const { return m_platformData.font(); }
203 #if ENABLE(TIZEN_EMOJI_FONT)
204 HashMap<unsigned short, int> m_emojiColor;
209 void platformGlyphInit();
210 void platformCharWidthInit();
211 void platformDestroy();
213 void initCharWidths();
217 PassOwnPtr<SimpleFontData> createScaledFontData(const FontDescription&, float scaleFactor) const;
219 #if (PLATFORM(WIN) && !OS(WINCE)) \
220 || (OS(WINDOWS) && PLATFORM(WX))
222 void platformCommonDestroy();
223 FloatRect boundsForGDIGlyph(Glyph glyph) const;
224 float widthForGDIGlyph(Glyph glyph) const;
227 FontMetrics m_fontMetrics;
228 float m_maxCharWidth;
229 float m_avgCharWidth;
231 FontPlatformData m_platformData;
232 OwnPtr<AdditionalFontData> m_fontData;
234 mutable OwnPtr<GlyphMetricsMap<FloatRect> > m_glyphToBoundsMap;
235 mutable GlyphMetricsMap<float> m_glyphToWidthMap;
237 bool m_treatAsFixedPitch;
238 bool m_isCustomFont; // Whether or not we are custom font loaded via @font-face
239 bool m_isLoading; // Whether or not this custom font is still in the act of loading.
241 bool m_isTextOrientationFallback;
242 bool m_isBrokenIdeographFallback;
243 bool m_hasVerticalGlyphs;
247 float m_adjustedSpaceWidth;
249 Glyph m_zeroWidthSpaceGlyph;
251 GlyphData m_missingGlyphData;
253 struct DerivedFontData {
254 static PassOwnPtr<DerivedFontData> create(bool forCustomFont);
258 OwnPtr<SimpleFontData> smallCaps;
259 OwnPtr<SimpleFontData> emphasisMark;
260 OwnPtr<SimpleFontData> brokenIdeograph;
261 OwnPtr<SimpleFontData> verticalRightOrientation;
262 OwnPtr<SimpleFontData> uprightOrientation;
263 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
264 mutable RetainPtr<CFMutableDictionaryRef> compositeFontReferences;
268 DerivedFontData(bool custom)
269 : forCustomFont(custom)
274 mutable OwnPtr<DerivedFontData> m_derivedFontData;
276 #if USE(CG) || USE(CAIRO) || PLATFORM(WX) || USE(SKIA_ON_MAC_CHROMIUM)
277 float m_syntheticBoldOffset;
280 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
281 mutable HashMap<unsigned, RetainPtr<CFDictionaryRef> > m_CFStringAttributes;
282 mutable OwnPtr<HashMap<String, bool> > m_combiningCharacterSequenceSupport;
285 #if PLATFORM(WIN) || (OS(WINDOWS) && PLATFORM(WX))
287 #if !OS(WINCE) // disable unused members to save space
288 mutable SCRIPT_CACHE m_scriptCache;
289 mutable SCRIPT_FONTPROPERTIES* m_scriptFontProperties;
294 #if !(PLATFORM(QT) && !HAVE(QRAWFONT))
295 ALWAYS_INLINE FloatRect SimpleFontData::boundsForGlyph(Glyph glyph) const
297 if (isZeroWidthSpaceGlyph(glyph))
301 if (m_glyphToBoundsMap) {
302 bounds = m_glyphToBoundsMap->metricsForGlyph(glyph);
303 if (bounds.width() != cGlyphSizeUnknown)
307 bounds = platformBoundsForGlyph(glyph);
308 if (!m_glyphToBoundsMap)
309 m_glyphToBoundsMap = adoptPtr(new GlyphMetricsMap<FloatRect>);
310 m_glyphToBoundsMap->setMetricsForGlyph(glyph, bounds);
314 ALWAYS_INLINE float SimpleFontData::widthForGlyph(Glyph glyph) const
316 if (isZeroWidthSpaceGlyph(glyph))
319 float width = m_glyphToWidthMap.metricsForGlyph(glyph);
320 if (width != cGlyphSizeUnknown)
324 width = m_fontData->widthForSVGGlyph(glyph, m_platformData.size());
326 width = platformWidthForGlyph(glyph);
328 m_glyphToWidthMap.setMetricsForGlyph(glyph, width);
331 #endif // HAVE(QRAWFONT)
333 } // namespace WebCore
335 #endif // SimpleFontData_h