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