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 "platform/PlatformExport.h"
28 #include "platform/fonts/CustomFontData.h"
29 #include "platform/fonts/FontBaseline.h"
30 #include "platform/fonts/FontData.h"
31 #include "platform/fonts/FontMetrics.h"
32 #include "platform/fonts/FontPlatformData.h"
33 #include "platform/fonts/GlyphMetricsMap.h"
34 #include "platform/fonts/GlyphPageTreeNode.h"
35 #include "platform/fonts/TypesettingFeatures.h"
36 #include "platform/fonts/opentype/OpenTypeVerticalData.h"
37 #include "platform/geometry/FloatRect.h"
38 #include "wtf/OwnPtr.h"
39 #include "wtf/PassOwnPtr.h"
40 #include "wtf/text/StringHash.h"
43 #include "wtf/RetainPtr.h"
48 class FontDescription;
50 enum FontDataVariant { AutoVariant, NormalVariant, SmallCapsVariant, EmphasisMarkVariant, BrokenIdeographVariant };
51 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
53 class PLATFORM_EXPORT SimpleFontData : public FontData {
55 // Used to create platform fonts.
56 static PassRefPtr<SimpleFontData> create(const FontPlatformData& platformData, PassRefPtr<CustomFontData> customData = nullptr, bool isTextOrientationFallback = false)
58 return adoptRef(new SimpleFontData(platformData, customData, isTextOrientationFallback));
61 // Used to create SVG Fonts.
62 static PassRefPtr<SimpleFontData> create(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic)
64 return adoptRef(new SimpleFontData(customData, fontSize, syntheticBold, syntheticItalic));
67 virtual ~SimpleFontData();
69 static const SimpleFontData* systemFallback() { return reinterpret_cast<const SimpleFontData*>(-1); }
71 const FontPlatformData& platformData() const { return m_platformData; }
72 #if ENABLE(OPENTYPE_VERTICAL)
73 const OpenTypeVerticalData* verticalData() const { return m_verticalData.get(); }
76 PassRefPtr<SimpleFontData> smallCapsFontData(const FontDescription&) const;
77 PassRefPtr<SimpleFontData> emphasisMarkFontData(const FontDescription&) const;
78 PassRefPtr<SimpleFontData> brokenIdeographFontData() const;
80 PassRefPtr<SimpleFontData> variantFontData(const FontDescription& description, FontDataVariant variant) const
83 case SmallCapsVariant:
84 return smallCapsFontData(description);
85 case EmphasisMarkVariant:
86 return emphasisMarkFontData(description);
87 case BrokenIdeographVariant:
88 return brokenIdeographFontData();
94 return const_cast<SimpleFontData*>(this);
97 PassRefPtr<SimpleFontData> verticalRightOrientationFontData() const;
98 PassRefPtr<SimpleFontData> uprightOrientationFontData() const;
100 bool hasVerticalGlyphs() const { return m_hasVerticalGlyphs; }
101 bool isTextOrientationFallback() const { return m_isTextOrientationFallback; }
103 FontMetrics& fontMetrics() { return m_fontMetrics; }
104 const FontMetrics& fontMetrics() const { return m_fontMetrics; }
105 float sizePerUnit() const { return platformData().size() / (fontMetrics().unitsPerEm() ? fontMetrics().unitsPerEm() : 1); }
107 float maxCharWidth() const { return m_maxCharWidth; }
108 void setMaxCharWidth(float maxCharWidth) { m_maxCharWidth = maxCharWidth; }
110 float avgCharWidth() const { return m_avgCharWidth; }
111 void setAvgCharWidth(float avgCharWidth) { m_avgCharWidth = avgCharWidth; }
113 FloatRect boundsForGlyph(Glyph) const;
114 float widthForGlyph(Glyph glyph) const;
115 FloatRect platformBoundsForGlyph(Glyph) const;
116 float platformWidthForGlyph(Glyph) const;
118 float spaceWidth() const { return m_spaceWidth; }
119 void setSpaceWidth(float spaceWidth) { m_spaceWidth = spaceWidth; }
122 float syntheticBoldOffset() const { return m_syntheticBoldOffset; }
125 Glyph spaceGlyph() const { return m_spaceGlyph; }
126 void setSpaceGlyph(Glyph spaceGlyph) { m_spaceGlyph = spaceGlyph; }
127 Glyph zeroWidthSpaceGlyph() const { return m_zeroWidthSpaceGlyph; }
128 void setZeroWidthSpaceGlyph(Glyph spaceGlyph) { m_zeroWidthSpaceGlyph = spaceGlyph; }
129 bool isZeroWidthSpaceGlyph(Glyph glyph) const { return glyph == m_zeroWidthSpaceGlyph && glyph; }
130 Glyph zeroGlyph() const { return m_zeroGlyph; }
131 void setZeroGlyph(Glyph zeroGlyph) { m_zeroGlyph = zeroGlyph; }
133 virtual const SimpleFontData* fontDataForCharacter(UChar32) const OVERRIDE;
135 Glyph glyphForCharacter(UChar32) const;
137 void determinePitch();
138 Pitch pitch() const { return m_treatAsFixedPitch ? FixedPitch : VariablePitch; }
140 bool isSVGFont() const { return m_customFontData && m_customFontData->isSVGFont(); }
141 virtual bool isCustomFont() const OVERRIDE { return m_customFontData; }
142 virtual bool isLoading() const OVERRIDE { return m_customFontData ? m_customFontData->isLoading() : false; }
143 virtual bool isLoadingFallback() const OVERRIDE { return m_customFontData ? m_customFontData->isLoadingFallback() : false; }
144 virtual bool isSegmented() const OVERRIDE;
145 virtual bool shouldSkipDrawing() const OVERRIDE { return m_customFontData && m_customFontData->shouldSkipDrawing(); }
147 const GlyphData& missingGlyphData() const { return m_missingGlyphData; }
148 void setMissingGlyphData(const GlyphData& glyphData) { m_missingGlyphData = glyphData; }
151 const SimpleFontData* getCompositeFontReferenceFontData(NSFont *key) const;
152 NSFont* getNSFont() const { return m_platformData.font(); }
156 CFDictionaryRef getCFStringAttributes(TypesettingFeatures, FontOrientation) const;
159 bool canRenderCombiningCharacterSequence(const UChar*, size_t) const;
161 PassRefPtr<CustomFontData> customFontData() const { return m_customFontData; }
163 // Implemented by the platform.
164 virtual bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const;
167 SimpleFontData(const FontPlatformData&, PassRefPtr<CustomFontData> customData, bool isTextOrientationFallback = false);
169 SimpleFontData(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic);
173 void platformGlyphInit();
174 void platformCharWidthInit();
175 void platformDestroy();
177 void initCharWidths();
179 PassRefPtr<SimpleFontData> createScaledFontData(const FontDescription&, float scaleFactor) const;
180 PassRefPtr<SimpleFontData> platformCreateScaledFontData(const FontDescription&, float scaleFactor) const;
182 FontMetrics m_fontMetrics;
183 float m_maxCharWidth;
184 float m_avgCharWidth;
186 FontPlatformData m_platformData;
188 mutable OwnPtr<GlyphMetricsMap<FloatRect> > m_glyphToBoundsMap;
189 mutable GlyphMetricsMap<float> m_glyphToWidthMap;
191 bool m_treatAsFixedPitch;
193 bool m_isTextOrientationFallback;
194 bool m_isBrokenIdeographFallback;
195 #if ENABLE(OPENTYPE_VERTICAL)
196 RefPtr<OpenTypeVerticalData> m_verticalData;
198 bool m_hasVerticalGlyphs;
204 Glyph m_zeroWidthSpaceGlyph;
206 GlyphData m_missingGlyphData;
208 struct DerivedFontData {
209 static PassOwnPtr<DerivedFontData> create(bool forCustomFont);
213 RefPtr<SimpleFontData> smallCaps;
214 RefPtr<SimpleFontData> emphasisMark;
215 RefPtr<SimpleFontData> brokenIdeograph;
216 RefPtr<SimpleFontData> verticalRightOrientation;
217 RefPtr<SimpleFontData> uprightOrientation;
219 mutable RetainPtr<CFMutableDictionaryRef> compositeFontReferences;
223 DerivedFontData(bool custom)
224 : forCustomFont(custom)
229 mutable OwnPtr<DerivedFontData> m_derivedFontData;
231 RefPtr<CustomFontData> m_customFontData;
234 float m_syntheticBoldOffset;
236 mutable HashMap<unsigned, RetainPtr<CFDictionaryRef> > m_CFStringAttributes;
239 mutable OwnPtr<HashMap<String, bool> > m_combiningCharacterSequenceSupport;
242 ALWAYS_INLINE FloatRect SimpleFontData::boundsForGlyph(Glyph glyph) const
244 if (isZeroWidthSpaceGlyph(glyph))
248 if (m_glyphToBoundsMap) {
249 bounds = m_glyphToBoundsMap->metricsForGlyph(glyph);
250 if (bounds.width() != cGlyphSizeUnknown)
254 bounds = platformBoundsForGlyph(glyph);
255 if (!m_glyphToBoundsMap)
256 m_glyphToBoundsMap = adoptPtr(new GlyphMetricsMap<FloatRect>);
257 m_glyphToBoundsMap->setMetricsForGlyph(glyph, bounds);
261 ALWAYS_INLINE float SimpleFontData::widthForGlyph(Glyph glyph) const
263 if (isZeroWidthSpaceGlyph(glyph))
266 float width = m_glyphToWidthMap.metricsForGlyph(glyph);
267 if (width != cGlyphSizeUnknown)
271 width = m_customFontData->widthForSVGGlyph(glyph, m_platformData.size());
272 #if ENABLE(OPENTYPE_VERTICAL)
273 else if (m_verticalData)
275 width = m_verticalData->advanceHeight(this, glyph) + m_syntheticBoldOffset;
277 width = m_verticalData->advanceHeight(this, glyph);
281 width = platformWidthForGlyph(glyph);
283 m_glyphToWidthMap.setMetricsForGlyph(glyph, width);
287 DEFINE_FONT_DATA_TYPE_CASTS(SimpleFontData, false);
290 #endif // SimpleFontData_h