initial import
[vuplus_webkit] / Source / WebCore / platform / graphics / FontPlatformData.h
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc.
3  * Copyright (C) 2006 Michael Emmel mike.emmel@gmail.com
4  * Copyright (C) 2007 Holger Hans Peter Freyther
5  * Copyright (C) 2007 Pioneer Research Center USA, Inc.
6  * Copyright (C) 2010, 2011 Brent Fulgham <bfulgham@webkit.org>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public License
19  * along with this library; see the file COPYING.LIB.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  *
23  */
24
25 // FIXME: This is temporary until all ports switch to using this file.
26 #if PLATFORM(CHROMIUM) && !OS(DARWIN)
27 #include "chromium/FontPlatformData.h"
28 #elif PLATFORM(QT)
29 #include "qt/FontPlatformData.h"
30 #elif PLATFORM(WIN) && OS(WINCE)
31 #include "wince/FontPlatformData.h"
32 #elif PLATFORM(WX)
33 #include "wx/FontPlatformData.h"
34 #elif (PLATFORM(EFL) || PLATFORM(GTK)) && USE(FREETYPE)
35 #include "freetype/FontPlatformData.h"
36 #elif (PLATFORM(EFL) || PLATFORM(GTK)) && USE(PANGO)
37 #include "pango/FontPlatformData.h"
38 #else
39
40 #ifndef FontPlatformData_h
41 #define FontPlatformData_h
42
43 #include "FontOrientation.h"
44 #include "FontWidthVariant.h"
45 #include "GlyphBuffer.h"
46 #include "TextOrientation.h"
47
48 #if PLATFORM(WIN)
49 #include "RefCountedGDIHandle.h"
50 #endif
51
52 #if USE(CAIRO)
53 #include "HashFunctions.h"
54 #include <cairo.h>
55 #endif
56
57 #if OS(DARWIN)
58 #ifdef __OBJC__
59 @class NSFont;
60 #else
61 class NSFont;
62 #endif
63
64 typedef struct CGFont* CGFontRef;
65 typedef const struct __CTFont* CTFontRef;
66
67 #include <CoreFoundation/CFBase.h>
68 #include <objc/objc-auto.h>
69 #endif
70
71 #include <wtf/Forward.h>
72 #include <wtf/PassRefPtr.h>
73 #include <wtf/RefCounted.h>
74 #include <wtf/RetainPtr.h>
75 #include <wtf/text/StringImpl.h>
76
77 #if PLATFORM(CHROMIUM) && OS(DARWIN)
78 #include "CrossProcessFontLoading.h"  
79 #endif
80
81 #if PLATFORM(WIN)
82 typedef struct HFONT__* HFONT;
83 #endif
84
85 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
86 typedef struct CGFont* CGFontRef;
87 #if OS(DARWIN)
88 typedef const struct __CTFont* CTFontRef;
89 typedef UInt32 FMFont;
90 typedef FMFont ATSUFontID;
91 typedef UInt32 ATSFontRef;
92 #endif
93 #endif
94
95 namespace WebCore {
96
97 class FontDescription;
98
99 #if OS(DARWIN)
100 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
101 #endif
102
103 class FontPlatformData {
104 public:
105     FontPlatformData(WTF::HashTableDeletedValueType)
106         : m_syntheticBold(false)
107         , m_syntheticOblique(false)
108         , m_orientation(Horizontal)
109         , m_textOrientation(TextOrientationVerticalRight)
110         , m_size(0)
111         , m_widthVariant(RegularWidth)
112 #if PLATFORM(WIN)
113         , m_font(WTF::HashTableDeletedValue)
114 #elif OS(DARWIN)
115         , m_font(hashTableDeletedFontValue())
116 #endif
117 #if USE(CG) && PLATFORM(WIN)
118         , m_cgFont(0)
119 #elif USE(CAIRO)
120         , m_scaledFont(hashTableDeletedFontValue())
121 #endif
122         , m_isColorBitmapFont(false)
123 #if PLATFORM(WIN)
124         , m_useGDI(false)
125 #endif
126         {
127         }
128
129     FontPlatformData()
130         : m_syntheticBold(false)
131         , m_syntheticOblique(false)
132         , m_orientation(Horizontal)
133         , m_textOrientation(TextOrientationVerticalRight)
134         , m_size(0)
135         , m_widthVariant(RegularWidth)
136 #if OS(DARWIN)
137         , m_font(0)
138 #endif
139 #if USE(CG) && PLATFORM(WIN)
140         , m_cgFont(0)
141 #elif USE(CAIRO)
142         , m_scaledFont(0)
143 #endif
144         , m_isColorBitmapFont(false)
145 #if PLATFORM(WIN)
146         , m_useGDI(false)
147 #endif
148     {
149     }
150
151     FontPlatformData(const FontPlatformData&);
152     FontPlatformData(const FontDescription&, const AtomicString& family);
153     FontPlatformData(float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation = Horizontal,
154                      TextOrientation textOrientation = TextOrientationVerticalRight, FontWidthVariant widthVariant = RegularWidth)
155         : m_syntheticBold(syntheticBold)
156         , m_syntheticOblique(syntheticOblique)
157         , m_orientation(orientation)
158         , m_textOrientation(textOrientation)
159         , m_size(size)
160         , m_widthVariant(widthVariant)
161 #if OS(DARWIN)
162         , m_font(0)
163 #endif
164 #if USE(CG) && PLATFORM(WIN)
165         , m_cgFont(0)
166 #elif USE(CAIRO)
167         , m_scaledFont(0)
168 #endif
169         , m_isColorBitmapFont(false)
170 #if PLATFORM(WIN)
171         , m_useGDI(false)
172 #endif
173     {
174     }
175
176 #if OS(DARWIN)
177     FontPlatformData(NSFont*, float size, bool syntheticBold = false, bool syntheticOblique = false, FontOrientation = Horizontal,
178                      TextOrientation = TextOrientationVerticalRight, FontWidthVariant = RegularWidth);
179 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
180     FontPlatformData(CGFontRef cgFont, float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation,
181                      TextOrientation textOrientation, FontWidthVariant widthVariant)
182         : m_syntheticBold(syntheticBold)
183         , m_syntheticOblique(syntheticOblique)
184         , m_orientation(orientation)
185         , m_textOrientation(textOrientation)
186         , m_size(size)
187         , m_widthVariant(widthVariant)
188         , m_font(0)
189         , m_cgFont(cgFont)
190         , m_isColorBitmapFont(false)
191     {
192     }
193 #endif
194 #endif
195 #if PLATFORM(WIN)
196     FontPlatformData(HFONT, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
197 #if USE(CG)
198     FontPlatformData(HFONT, CGFontRef, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
199 #endif
200 #endif
201 #if USE(CAIRO)
202     FontPlatformData(cairo_font_face_t*, float size, bool bold, bool italic);
203 #endif
204
205     ~FontPlatformData();
206
207 #if PLATFORM(WIN)
208     HFONT hfont() const { return m_font ? m_font->handle() : 0; }
209     bool useGDI() const { return m_useGDI; }
210 #elif OS(DARWIN)
211     NSFont* font() const { return m_font; }
212     void setFont(NSFont*);
213 #endif
214
215 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
216 #if OS(DARWIN)
217     CGFontRef cgFont() const { return m_cgFont.get(); }
218     CTFontRef ctFont() const;
219
220     bool roundsGlyphAdvances() const;
221     bool allowsLigatures() const;
222 #else
223     CGFontRef cgFont() const { return m_cgFont.get(); }
224 #endif
225 #endif
226
227     bool isFixedPitch() const;
228     float size() const { return m_size; }
229     void setSize(float size) { m_size = size; }
230     bool syntheticBold() const { return m_syntheticBold; }
231     bool syntheticOblique() const { return m_syntheticOblique; }
232     bool isColorBitmapFont() const { return m_isColorBitmapFont; }
233     FontOrientation orientation() const { return m_orientation; }
234     TextOrientation textOrientation() const { return m_textOrientation; }
235     FontWidthVariant widthVariant() const { return m_widthVariant; }
236
237     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
238
239 #if USE(CAIRO)
240     cairo_scaled_font_t* scaledFont() const { return m_scaledFont; }
241 #endif
242
243     unsigned hash() const
244     {
245 #if PLATFORM(WIN) && !USE(CAIRO)
246         return m_font ? m_font->hash() : 0;
247 #elif OS(DARWIN)
248 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
249         ASSERT(m_font || !m_cgFont);
250 #endif
251         uintptr_t hashCodes[3] = { (uintptr_t)m_font, m_widthVariant, m_textOrientation << 3 | m_orientation << 2 | m_syntheticBold << 1 | m_syntheticOblique };
252         return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
253 #elif USE(CAIRO)
254         return PtrHash<cairo_scaled_font_t*>::hash(m_scaledFont);
255 #endif
256     }
257
258     const FontPlatformData& operator=(const FontPlatformData&);
259
260     bool operator==(const FontPlatformData& other) const
261     {
262         return platformIsEqual(other)
263             && m_size == other.m_size
264             && m_syntheticBold == other.m_syntheticBold
265             && m_syntheticOblique == other.m_syntheticOblique
266             && m_isColorBitmapFont == other.m_isColorBitmapFont
267             && m_orientation == other.m_orientation
268             && m_textOrientation == other.m_textOrientation
269             && m_widthVariant == other.m_widthVariant;
270     }
271
272     bool isHashTableDeletedValue() const
273     {
274 #if PLATFORM(WIN) && !USE(CAIRO)
275         return m_font.isHashTableDeletedValue();
276 #elif OS(DARWIN)
277         return m_font == hashTableDeletedFontValue();
278 #elif USE(CAIRO)
279         return m_scaledFont == hashTableDeletedFontValue();
280 #endif
281     }
282
283
284 #ifndef NDEBUG
285     String description() const;
286 #endif
287
288 private:
289     bool platformIsEqual(const FontPlatformData&) const;
290     void platformDataInit(const FontPlatformData&);
291     const FontPlatformData& platformDataAssign(const FontPlatformData&);
292 #if OS(DARWIN)
293     // Load various data about the font specified by |nsFont| with the size fontSize into the following output paramters:
294     // Note: Callers should always take into account that for the Chromium port, |outNSFont| isn't necessarily the same
295     // font as |nsFont|. This because the sandbox may block loading of the original font.
296     // * outNSFont - The font that was actually loaded, for the Chromium port this may be different than nsFont.
297     // The caller is responsible for calling CFRelease() on this parameter when done with it.
298     // * cgFont - CGFontRef representing the input font at the specified point size.
299     void loadFont(NSFont*, float fontSize, NSFont*& outNSFont, CGFontRef&);
300     static NSFont* hashTableDeletedFontValue() { return reinterpret_cast<NSFont *>(-1); }
301 #elif PLATFORM(WIN)
302     void platformDataInit(HFONT, float size, HDC, WCHAR* faceName);
303 #endif
304
305 #if USE(CAIRO)
306     static cairo_scaled_font_t* hashTableDeletedFontValue() { return reinterpret_cast<cairo_scaled_font_t*>(-1); }
307 #endif
308
309 public:
310     bool m_syntheticBold;
311     bool m_syntheticOblique;
312     FontOrientation m_orientation;
313     TextOrientation m_textOrientation;
314     float m_size;
315     FontWidthVariant m_widthVariant;
316
317 private:
318 #if OS(DARWIN)
319     NSFont* m_font;
320 #elif PLATFORM(WIN)
321     RefPtr<RefCountedGDIHandle<HFONT> > m_font;
322 #endif
323
324 #if USE(CG) || USE(SKIA_ON_MAC_CHROMIUM)
325 #if PLATFORM(WIN)
326     RetainPtr<CGFontRef> m_cgFont;
327 #else
328     RetainPtr<CGFontRef> m_cgFont;
329     mutable RetainPtr<CTFontRef> m_CTFont;
330 #endif
331 #endif
332
333 #if USE(CAIRO)
334     cairo_scaled_font_t* m_scaledFont;
335 #endif
336
337 #if PLATFORM(CHROMIUM) && OS(DARWIN)
338     RefPtr<MemoryActivatedFont> m_inMemoryFont;
339 #endif
340
341     bool m_isColorBitmapFont;
342
343 #if PLATFORM(WIN)
344     bool m_useGDI;
345 #endif
346 };
347
348 } // namespace WebCore
349
350 #endif // FontPlatformData_h
351
352 #endif