tagging audio streams and changing audio sink to pulseaudio
[profile/ivi/webkit-efl.git] / Source / WebCore / platform / graphics / 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 "FontBaseline.h"
28 #include "FontData.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>
38
39 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
40 #include <wtf/RetainPtr.h>
41 #endif
42
43 #if (PLATFORM(WIN) && !OS(WINCE)) \
44     || (OS(WINDOWS) && PLATFORM(WX))
45 #include <usp10.h>
46 #endif
47
48 #if USE(CAIRO)
49 #include <cairo.h>
50 #endif
51
52 #if PLATFORM(QT)
53 #if !HAVE(QRAWFONT)
54 #include <QFont>
55 #else
56 #include <QRawFont>
57 #endif
58 #endif
59
60 namespace WebCore {
61
62 class FontDescription;
63 class SharedBuffer;
64 struct WidthIterator;
65
66 enum FontDataVariant { AutoVariant, NormalVariant, SmallCapsVariant, EmphasisMarkVariant, BrokenIdeographVariant };
67 enum Pitch { UnknownPitch, FixedPitch, VariablePitch };
68
69 class SimpleFontData : public FontData {
70 public:
71     class AdditionalFontData {
72         WTF_MAKE_FAST_ALLOCATED;
73     public:
74         virtual ~AdditionalFontData() { }
75
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;
80     };
81
82     // Used to create platform fonts.
83     SimpleFontData(const FontPlatformData&, bool isCustomFont = false, bool isLoading = false, bool isTextOrientationFallback = false);
84
85     // Used to create SVG Fonts.
86     SimpleFontData(PassOwnPtr<AdditionalFontData>, float fontSize, bool syntheticBold, bool syntheticItalic);
87
88     virtual ~SimpleFontData();
89
90     const FontPlatformData& platformData() const { return m_platformData; }
91
92     SimpleFontData* smallCapsFontData(const FontDescription&) const;
93     SimpleFontData* emphasisMarkFontData(const FontDescription&) const;
94     SimpleFontData* brokenIdeographFontData() const;
95
96     SimpleFontData* variantFontData(const FontDescription& description, FontDataVariant variant) const
97     {
98         switch (variant) {
99         case SmallCapsVariant:
100             return smallCapsFontData(description);
101         case EmphasisMarkVariant:
102             return emphasisMarkFontData(description);
103         case BrokenIdeographVariant:
104             return brokenIdeographFontData();
105         case AutoVariant:
106         case NormalVariant:
107             break;
108         }
109         ASSERT_NOT_REACHED();
110         return const_cast<SimpleFontData*>(this);
111     }
112
113     SimpleFontData* verticalRightOrientationFontData() const;
114     SimpleFontData* uprightOrientationFontData() const;
115
116     bool hasVerticalGlyphs() const { return m_hasVerticalGlyphs; }
117     bool isTextOrientationFallback() const { return m_isTextOrientationFallback; }
118
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); }
122     
123     float maxCharWidth() const { return m_maxCharWidth; }
124     void setMaxCharWidth(float maxCharWidth) { m_maxCharWidth = maxCharWidth; }
125
126     float avgCharWidth() const { return m_avgCharWidth; }
127     void setAvgCharWidth(float avgCharWidth) { m_avgCharWidth = avgCharWidth; }
128
129     FloatRect boundsForGlyph(Glyph) const;
130     float widthForGlyph(Glyph glyph) const;
131     FloatRect platformBoundsForGlyph(Glyph) const;
132     float platformWidthForGlyph(Glyph) const;
133
134     float spaceWidth() const { return m_spaceWidth; }
135     float adjustedSpaceWidth() const { return m_adjustedSpaceWidth; }
136     void setSpaceWidth(float spaceWidth) { m_spaceWidth = spaceWidth; }
137
138 #if USE(CG) || USE(CAIRO) || PLATFORM(WX) || USE(SKIA_ON_MAC_CHROMIUM)
139     float syntheticBoldOffset() const { return m_syntheticBoldOffset; }
140 #endif
141
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; }
146
147     virtual const SimpleFontData* fontDataForCharacter(UChar32) const;
148     virtual bool containsCharacters(const UChar*, int length) const;
149
150     void determinePitch();
151     Pitch pitch() const { return m_treatAsFixedPitch ? FixedPitch : VariablePitch; }
152
153     AdditionalFontData* fontData() const { return m_fontData.get(); }
154     bool isSVGFont() const { return m_fontData; }
155
156     virtual bool isCustomFont() const { return m_isCustomFont; }
157     virtual bool isLoading() const { return m_isLoading; }
158     virtual bool isSegmented() const;
159
160     const GlyphData& missingGlyphData() const { return m_missingGlyphData; }
161     void setMissingGlyphData(const GlyphData& glyphData) { m_missingGlyphData = glyphData; }
162
163 #ifndef NDEBUG
164     virtual String description() const;
165 #endif
166
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(); }
173 #endif
174
175 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
176     CFDictionaryRef getCFStringAttributes(TypesettingFeatures, FontOrientation) const;
177     bool canRenderCombiningCharacterSequence(const UChar*, size_t) const;
178 #endif
179
180 #if PLATFORM(QT)
181 #if !HAVE(QRAWFONT)
182     QFont getQtFont() const { return m_platformData.font(); }
183 #else
184     QRawFont getQtRawFont() const { return m_platformData.rawFont(); }
185 #endif // !HAVE(QRAWFONT)
186 #endif
187
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; }
193 #endif
194     static void setShouldApplyMacAscentHack(bool);
195     static bool shouldApplyMacAscentHack();
196     static float ascentConsideringMacAscentHack(const WCHAR*, float ascent, float descent);
197 #endif
198
199 #if PLATFORM(WX)
200     wxFont* getWxFont() const { return m_platformData.font(); }
201 #endif
202
203 #if ENABLE(TIZEN_EMOJI_FONT)
204     HashMap<unsigned short, int> m_emojiColor;
205 #endif
206
207 private:
208     void platformInit();
209     void platformGlyphInit();
210     void platformCharWidthInit();
211     void platformDestroy();
212     
213     void initCharWidths();
214
215     void commonInit();
216
217     PassOwnPtr<SimpleFontData> createScaledFontData(const FontDescription&, float scaleFactor) const;
218
219 #if (PLATFORM(WIN) && !OS(WINCE)) \
220     || (OS(WINDOWS) && PLATFORM(WX))
221     void initGDIFont();
222     void platformCommonDestroy();
223     FloatRect boundsForGDIGlyph(Glyph glyph) const;
224     float widthForGDIGlyph(Glyph glyph) const;
225 #endif
226
227     FontMetrics m_fontMetrics;
228     float m_maxCharWidth;
229     float m_avgCharWidth;
230     
231     FontPlatformData m_platformData;
232     OwnPtr<AdditionalFontData> m_fontData;
233
234     mutable OwnPtr<GlyphMetricsMap<FloatRect> > m_glyphToBoundsMap;
235     mutable GlyphMetricsMap<float> m_glyphToWidthMap;
236
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.
240     
241     bool m_isTextOrientationFallback;
242     bool m_isBrokenIdeographFallback;
243     bool m_hasVerticalGlyphs;
244     
245     Glyph m_spaceGlyph;
246     float m_spaceWidth;
247     float m_adjustedSpaceWidth;
248
249     Glyph m_zeroWidthSpaceGlyph;
250
251     GlyphData m_missingGlyphData;
252
253     struct DerivedFontData {
254         static PassOwnPtr<DerivedFontData> create(bool forCustomFont);
255         ~DerivedFontData();
256
257         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;
265 #endif
266         
267     private:
268         DerivedFontData(bool custom)
269             : forCustomFont(custom)
270         {
271         }
272     };
273
274     mutable OwnPtr<DerivedFontData> m_derivedFontData;
275
276 #if USE(CG) || USE(CAIRO) || PLATFORM(WX) || USE(SKIA_ON_MAC_CHROMIUM)
277     float m_syntheticBoldOffset;
278 #endif
279
280 #if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
281     mutable HashMap<unsigned, RetainPtr<CFDictionaryRef> > m_CFStringAttributes;
282     mutable OwnPtr<HashMap<String, bool> > m_combiningCharacterSequenceSupport;
283 #endif
284
285 #if PLATFORM(WIN) || (OS(WINDOWS) && PLATFORM(WX))
286     bool m_isSystemFont;
287 #if !OS(WINCE) // disable unused members to save space
288     mutable SCRIPT_CACHE m_scriptCache;
289     mutable SCRIPT_FONTPROPERTIES* m_scriptFontProperties;
290 #endif
291 #endif
292 };
293
294 #if !(PLATFORM(QT) && !HAVE(QRAWFONT))
295 ALWAYS_INLINE FloatRect SimpleFontData::boundsForGlyph(Glyph glyph) const
296 {
297     if (isZeroWidthSpaceGlyph(glyph))
298         return FloatRect();
299
300     FloatRect bounds;
301     if (m_glyphToBoundsMap) {
302         bounds = m_glyphToBoundsMap->metricsForGlyph(glyph);
303         if (bounds.width() != cGlyphSizeUnknown)
304             return bounds;
305     }
306
307     bounds = platformBoundsForGlyph(glyph);
308     if (!m_glyphToBoundsMap)
309         m_glyphToBoundsMap = adoptPtr(new GlyphMetricsMap<FloatRect>);
310     m_glyphToBoundsMap->setMetricsForGlyph(glyph, bounds);
311     return bounds;
312 }
313
314 ALWAYS_INLINE float SimpleFontData::widthForGlyph(Glyph glyph) const
315 {
316     if (isZeroWidthSpaceGlyph(glyph))
317         return 0;
318
319     float width = m_glyphToWidthMap.metricsForGlyph(glyph);
320     if (width != cGlyphSizeUnknown)
321         return width;
322
323     if (m_fontData)
324         width = m_fontData->widthForSVGGlyph(glyph, m_platformData.size());
325     else
326         width = platformWidthForGlyph(glyph);
327
328     m_glyphToWidthMap.setMetricsForGlyph(glyph, width);
329     return width;
330 }
331 #endif // HAVE(QRAWFONT)
332
333 } // namespace WebCore
334
335 #endif // SimpleFontData_h