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