1 /*
2 * This file is part of the internal font implementation.
3 *
4 * Copyright (C) 2006, 2008, 2010 Apple Inc. All rights reserved.
5 * Copyright (C) 2007-2008 Torch Mobile, Inc.
6 *
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.
11 *
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.
16 *
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.
21 *
22 */
23
24 #ifndef SimpleFontData_h
25 #define SimpleFontData_h
26
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"
41
42 #if OS(MACOSX)
43 #include "wtf/RetainPtr.h"
44 #endif
45
46 namespace WebCore {
47
48 class CSSFontFaceSource;
49 class FontDescription;
50 class SharedBuffer;
51 struct WidthIterator;
52
53 enum FontDataVariant { AutoVariant, NormalVariant, SmallCapsVariant, EmphasisMarkVariant, BrokenIdeographVariant };
54 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
55
56 class PLATFORM_EXPORT SimpleFontData : public FontData {
57 public:
58 // Used to create platform fonts.
59 static PassRefPtr<SimpleFontData> create(const FontPlatformData& platformData, PassRefPtr<CustomFontData> customData = nullptr, bool isTextOrientationFallback = false)
60 {
61 return adoptRef(new SimpleFontData(platformData, customData, isTextOrientationFallback));
62 }
63
64 // Used to create SVG Fonts.
create(PassRefPtr<CustomFontData> customData,float fontSize,bool syntheticBold,bool syntheticItalic)65 static PassRefPtr<SimpleFontData> create(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic)
66 {
67 return adoptRef(new SimpleFontData(customData, fontSize, syntheticBold, syntheticItalic));
68 }
69
70 virtual ~SimpleFontData();
71
systemFallback()72 static const SimpleFontData* systemFallback() { return reinterpret_cast<const SimpleFontData*>(-1); }
73
platformData()74 const FontPlatformData& platformData() const { return m_platformData; }
75 #if ENABLE(OPENTYPE_VERTICAL)
verticalData()76 const OpenTypeVerticalData* verticalData() const { return m_verticalData.get(); }
77 #endif
78
79 PassRefPtr<SimpleFontData> smallCapsFontData(const FontDescription&) const;
80 PassRefPtr<SimpleFontData> emphasisMarkFontData(const FontDescription&) const;
81 PassRefPtr<SimpleFontData> brokenIdeographFontData() const;
82
variantFontData(const FontDescription & description,FontDataVariant variant)83 PassRefPtr<SimpleFontData> variantFontData(const FontDescription& description, FontDataVariant variant) const
84 {
85 switch (variant) {
86 case SmallCapsVariant:
87 return smallCapsFontData(description);
88 case EmphasisMarkVariant:
89 return emphasisMarkFontData(description);
90 case BrokenIdeographVariant:
91 return brokenIdeographFontData();
92 case AutoVariant:
93 case NormalVariant:
94 break;
95 }
96 ASSERT_NOT_REACHED();
97 return const_cast<SimpleFontData*>(this);
98 }
99
100 PassRefPtr<SimpleFontData> verticalRightOrientationFontData() const;
101 PassRefPtr<SimpleFontData> uprightOrientationFontData() const;
102
hasVerticalGlyphs()103 bool hasVerticalGlyphs() const { return m_hasVerticalGlyphs; }
isTextOrientationFallback()104 bool isTextOrientationFallback() const { return m_isTextOrientationFallback; }
105
fontMetrics()106 FontMetrics& fontMetrics() { return m_fontMetrics; }
fontMetrics()107 const FontMetrics& fontMetrics() const { return m_fontMetrics; }
sizePerUnit()108 float sizePerUnit() const { return platformData().size() / (fontMetrics().unitsPerEm() ? fontMetrics().unitsPerEm() : 1); }
109
maxCharWidth()110 float maxCharWidth() const { return m_maxCharWidth; }
setMaxCharWidth(float maxCharWidth)111 void setMaxCharWidth(float maxCharWidth) { m_maxCharWidth = maxCharWidth; }
112
avgCharWidth()113 float avgCharWidth() const { return m_avgCharWidth; }
setAvgCharWidth(float avgCharWidth)114 void setAvgCharWidth(float avgCharWidth) { m_avgCharWidth = avgCharWidth; }
115
116 FloatRect boundsForGlyph(Glyph) const;
117 float widthForGlyph(Glyph glyph) const;
118 FloatRect platformBoundsForGlyph(Glyph) const;
119 float platformWidthForGlyph(Glyph) const;
120
spaceWidth()121 float spaceWidth() const { return m_spaceWidth; }
adjustedSpaceWidth()122 float adjustedSpaceWidth() const { return m_adjustedSpaceWidth; }
setSpaceWidth(float spaceWidth)123 void setSpaceWidth(float spaceWidth) { m_spaceWidth = spaceWidth; }
124
125 #if OS(MACOSX)
syntheticBoldOffset()126 float syntheticBoldOffset() const { return m_syntheticBoldOffset; }
127 #endif
128
spaceGlyph()129 Glyph spaceGlyph() const { return m_spaceGlyph; }
setSpaceGlyph(Glyph spaceGlyph)130 void setSpaceGlyph(Glyph spaceGlyph) { m_spaceGlyph = spaceGlyph; }
zeroWidthSpaceGlyph()131 Glyph zeroWidthSpaceGlyph() const { return m_zeroWidthSpaceGlyph; }
setZeroWidthSpaceGlyph(Glyph spaceGlyph)132 void setZeroWidthSpaceGlyph(Glyph spaceGlyph) { m_zeroWidthSpaceGlyph = spaceGlyph; }
isZeroWidthSpaceGlyph(Glyph glyph)133 bool isZeroWidthSpaceGlyph(Glyph glyph) const { return glyph == m_zeroWidthSpaceGlyph && glyph; }
zeroGlyph()134 Glyph zeroGlyph() const { return m_zeroGlyph; }
setZeroGlyph(Glyph zeroGlyph)135 void setZeroGlyph(Glyph zeroGlyph) { m_zeroGlyph = zeroGlyph; }
136
137 virtual const SimpleFontData* fontDataForCharacter(UChar32) const OVERRIDE;
138
139 Glyph glyphForCharacter(UChar32) const;
140
141 void determinePitch();
pitch()142 Pitch pitch() const { return m_treatAsFixedPitch ? FixedPitch : VariablePitch; }
143
isSVGFont()144 bool isSVGFont() const { return m_customFontData && m_customFontData->isSVGFont(); }
isCustomFont()145 virtual bool isCustomFont() const OVERRIDE { return m_customFontData; }
isLoading()146 virtual bool isLoading() const OVERRIDE { return m_customFontData ? m_customFontData->isLoading() : false; }
isLoadingFallback()147 virtual bool isLoadingFallback() const OVERRIDE { return m_customFontData ? m_customFontData->isLoadingFallback() : false; }
148 virtual bool isSegmented() const OVERRIDE;
shouldSkipDrawing()149 virtual bool shouldSkipDrawing() const OVERRIDE { return m_customFontData && m_customFontData->shouldSkipDrawing(); }
150
missingGlyphData()151 const GlyphData& missingGlyphData() const { return m_missingGlyphData; }
setMissingGlyphData(const GlyphData & glyphData)152 void setMissingGlyphData(const GlyphData& glyphData) { m_missingGlyphData = glyphData; }
153
154 #ifndef NDEBUG
155 virtual String description() const OVERRIDE;
156 #endif
157
158 #if OS(MACOSX)
159 const SimpleFontData* getCompositeFontReferenceFontData(NSFont *key) const;
getNSFont()160 NSFont* getNSFont() const { return m_platformData.font(); }
161 #endif
162
163 #if OS(MACOSX)
164 CFDictionaryRef getCFStringAttributes(TypesettingFeatures, FontOrientation) const;
165 #endif
166
167 bool canRenderCombiningCharacterSequence(const UChar*, size_t) const;
168
customFontData()169 PassRefPtr<CustomFontData> customFontData() const { return m_customFontData; }
170
171 // Implemented by the platform.
172 virtual bool fillGlyphPage(GlyphPage* pageToFill, unsigned offset, unsigned length, UChar* buffer, unsigned bufferLength) const;
173
174 protected:
175 SimpleFontData(const FontPlatformData&, PassRefPtr<CustomFontData> customData, bool isTextOrientationFallback = false);
176
177 SimpleFontData(PassRefPtr<CustomFontData> customData, float fontSize, bool syntheticBold, bool syntheticItalic);
178
179 private:
180 void platformInit();
181 void platformGlyphInit();
182 void platformCharWidthInit();
183 void platformDestroy();
184
185 void initCharWidths();
186
187 PassRefPtr<SimpleFontData> createScaledFontData(const FontDescription&, float scaleFactor) const;
188 PassRefPtr<SimpleFontData> platformCreateScaledFontData(const FontDescription&, float scaleFactor) const;
189
190 FontMetrics m_fontMetrics;
191 float m_maxCharWidth;
192 float m_avgCharWidth;
193
194 FontPlatformData m_platformData;
195
196 mutable OwnPtr<GlyphMetricsMap<FloatRect> > m_glyphToBoundsMap;
197 mutable GlyphMetricsMap<float> m_glyphToWidthMap;
198
199 bool m_treatAsFixedPitch;
200
201 bool m_isTextOrientationFallback;
202 bool m_isBrokenIdeographFallback;
203 #if ENABLE(OPENTYPE_VERTICAL)
204 RefPtr<OpenTypeVerticalData> m_verticalData;
205 #endif
206 bool m_hasVerticalGlyphs;
207
208 Glyph m_spaceGlyph;
209 float m_spaceWidth;
210 Glyph m_zeroGlyph;
211 float m_adjustedSpaceWidth;
212
213 Glyph m_zeroWidthSpaceGlyph;
214
215 GlyphData m_missingGlyphData;
216
217 struct DerivedFontData {
218 static PassOwnPtr<DerivedFontData> create(bool forCustomFont);
219 ~DerivedFontData();
220
221 bool forCustomFont;
222 RefPtr<SimpleFontData> smallCaps;
223 RefPtr<SimpleFontData> emphasisMark;
224 RefPtr<SimpleFontData> brokenIdeograph;
225 RefPtr<SimpleFontData> verticalRightOrientation;
226 RefPtr<SimpleFontData> uprightOrientation;
227 #if OS(MACOSX)
228 mutable RetainPtr<CFMutableDictionaryRef> compositeFontReferences;
229 #endif
230
231 private:
DerivedFontDataDerivedFontData232 DerivedFontData(bool custom)
233 : forCustomFont(custom)
234 {
235 }
236 };
237
238 mutable OwnPtr<DerivedFontData> m_derivedFontData;
239
240 RefPtr<CustomFontData> m_customFontData;
241
242 #if OS(MACOSX)
243 float m_syntheticBoldOffset;
244
245 mutable HashMap<unsigned, RetainPtr<CFDictionaryRef> > m_CFStringAttributes;
246 #endif
247
248 mutable OwnPtr<HashMap<String, bool> > m_combiningCharacterSequenceSupport;
249 };
250
boundsForGlyph(Glyph glyph)251 ALWAYS_INLINE FloatRect SimpleFontData::boundsForGlyph(Glyph glyph) const
252 {
253 if (isZeroWidthSpaceGlyph(glyph))
254 return FloatRect();
255
256 FloatRect bounds;
257 if (m_glyphToBoundsMap) {
258 bounds = m_glyphToBoundsMap->metricsForGlyph(glyph);
259 if (bounds.width() != cGlyphSizeUnknown)
260 return bounds;
261 }
262
263 bounds = platformBoundsForGlyph(glyph);
264 if (!m_glyphToBoundsMap)
265 m_glyphToBoundsMap = adoptPtr(new GlyphMetricsMap<FloatRect>);
266 m_glyphToBoundsMap->setMetricsForGlyph(glyph, bounds);
267 return bounds;
268 }
269
widthForGlyph(Glyph glyph)270 ALWAYS_INLINE float SimpleFontData::widthForGlyph(Glyph glyph) const
271 {
272 if (isZeroWidthSpaceGlyph(glyph))
273 return 0;
274
275 float width = m_glyphToWidthMap.metricsForGlyph(glyph);
276 if (width != cGlyphSizeUnknown)
277 return width;
278
279 if (isSVGFont())
280 width = m_customFontData->widthForSVGGlyph(glyph, m_platformData.size());
281 #if ENABLE(OPENTYPE_VERTICAL)
282 else if (m_verticalData)
283 #if OS(MACOSX)
284 width = m_verticalData->advanceHeight(this, glyph) + m_syntheticBoldOffset;
285 #else
286 width = m_verticalData->advanceHeight(this, glyph);
287 #endif
288 #endif
289 else
290 width = platformWidthForGlyph(glyph);
291
292 m_glyphToWidthMap.setMetricsForGlyph(glyph, width);
293 return width;
294 }
295
296 } // namespace WebCore
297 #endif // SimpleFontData_h
298