1/*
2 * Copyright (C) 2006, 2007, 2008, 2010, 2013 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 USE(WINGDI)
27#include "wince/FontPlatformData.h"
28#elif PLATFORM(EFL) || PLATFORM(GTK)
29#include "freetype/FontPlatformData.h"
30#else
31
32#ifndef FontPlatformData_h
33#define FontPlatformData_h
34
35#include "FontOrientation.h"
36#include "FontWidthVariant.h"
37
38#if PLATFORM(WIN)
39#include "SharedGDIObject.h"
40#endif
41
42#if USE(CAIRO)
43#include <wtf/HashFunctions.h>
44#include <cairo.h>
45#endif
46
47#if OS(DARWIN)
48OBJC_CLASS NSFont;
49
50#if PLATFORM(IOS)
51#import <CoreGraphics/CoreGraphics.h>
52#endif
53
54typedef struct CGFont* CGFontRef;
55typedef const struct __CTFont* CTFontRef;
56#endif
57
58#include <wtf/Forward.h>
59#include <wtf/PassRefPtr.h>
60#include <wtf/RefCounted.h>
61#include <wtf/RetainPtr.h>
62#include <wtf/text/StringImpl.h>
63
64#if PLATFORM(WIN)
65#include <wtf/win/GDIObject.h>
66typedef struct HFONT__* HFONT;
67#endif
68
69#if USE(CG)
70typedef struct CGFont* CGFontRef;
71#if OS(DARWIN)
72typedef const struct __CTFont* CTFontRef;
73typedef UInt32 FMFont;
74typedef FMFont ATSUFontID;
75typedef UInt32 ATSFontRef;
76#endif
77#endif
78
79namespace WebCore {
80
81class FontDescription;
82class SharedBuffer;
83
84#if OS(DARWIN) && USE(APPKIT)
85inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
86#endif
87
88class FontPlatformData {
89    WTF_MAKE_FAST_ALLOCATED;
90public:
91    FontPlatformData(WTF::HashTableDeletedValueType);
92    FontPlatformData();
93    FontPlatformData(const FontPlatformData&);
94    FontPlatformData(const FontDescription&, const AtomicString& family);
95    FontPlatformData(float size, bool syntheticBold, bool syntheticOblique, FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
96
97#if OS(DARWIN)
98#if USE(APPKIT)
99    FontPlatformData(NSFont*, float size, bool isPrinterFont = false, bool syntheticBold = false, bool syntheticOblique = false,
100                     FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
101#else
102    FontPlatformData(CTFontRef, float size, bool isPrinterFont = false, bool syntheticBold = false, bool syntheticOblique = false,
103                     FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
104#endif
105
106#if USE(CG)
107    FontPlatformData(CGFontRef, float size, bool syntheticBold, bool syntheticOblique, FontOrientation, FontWidthVariant);
108#endif
109#endif
110#if PLATFORM(WIN)
111    FontPlatformData(GDIObject<HFONT>, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
112#if USE(CG)
113    FontPlatformData(GDIObject<HFONT>, CGFontRef, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
114#elif USE(CAIRO)
115    FontPlatformData(GDIObject<HFONT>, cairo_font_face_t*, float size, bool bold, bool italic);
116#endif
117#endif
118
119#if PLATFORM(IOS)
120    FontPlatformData(CTFontRef, float size, bool syntheticBold = false, bool syntheticOblique = false, FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
121#endif
122
123    ~FontPlatformData();
124
125#if PLATFORM(WIN)
126    HFONT hfont() const { return m_font ? m_font->get() : 0; }
127    bool useGDI() const { return m_useGDI; }
128#elif PLATFORM(IOS)
129    CTFontRef font() const { return m_font; }
130    void setFont(CTFontRef);
131#elif OS(DARWIN)
132    NSFont* font() const { return m_font; }
133    void setFont(NSFont*);
134#endif
135
136#if USE(CG)
137#if OS(DARWIN)
138    CGFontRef cgFont() const { return m_cgFont.get(); }
139    CTFontRef ctFont() const;
140
141#if !PLATFORM(IOS)
142    bool roundsGlyphAdvances() const;
143#else
144    bool roundsGlyphAdvances() const { return false; }
145#endif // !PLATFORM(IOS)
146
147    bool allowsLigatures() const;
148#else
149    CGFontRef cgFont() const { return m_cgFont.get(); }
150#endif
151#endif
152
153    bool isFixedPitch() const;
154    float size() const { return m_size; }
155    void setSize(float size) { m_size = size; }
156    bool syntheticBold() const { return m_syntheticBold; }
157    bool syntheticOblique() const { return m_syntheticOblique; }
158    bool isColorBitmapFont() const { return m_isColorBitmapFont; }
159    bool isCompositeFontReference() const { return m_isCompositeFontReference; }
160#if OS(DARWIN)
161    bool isPrinterFont() const { return m_isPrinterFont; }
162#endif
163    FontOrientation orientation() const { return m_orientation; }
164    FontWidthVariant widthVariant() const { return m_widthVariant; }
165
166    void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
167
168#if USE(CAIRO)
169    cairo_scaled_font_t* scaledFont() const { return m_scaledFont; }
170#endif
171
172    unsigned hash() const
173    {
174#if PLATFORM(WIN) && !USE(CAIRO)
175        return m_font ? m_font->hash() : 0;
176#elif OS(DARWIN)
177#if !PLATFORM(IOS)
178#if USE(CG)
179        ASSERT(m_font || !m_cgFont);
180#endif
181        uintptr_t hashCodes[3] = { (uintptr_t)m_font, m_widthVariant, static_cast<uintptr_t>(m_isPrinterFont << 3 | m_orientation << 2 | m_syntheticBold << 1 | m_syntheticOblique) };
182#else
183        ASSERT(m_font || !m_cgFont || m_isEmoji);
184        uintptr_t hashCodes[3] = { static_cast<uintptr_t>(CFHash(m_font)), m_widthVariant, static_cast<uintptr_t>(m_isEmoji << 4 | m_isPrinterFont << 3 | m_orientation << 2 | m_syntheticBold << 1 | m_syntheticOblique) };
185#endif // !PLATFORM(IOS)
186        return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
187#elif USE(CAIRO)
188        return PtrHash<cairo_scaled_font_t*>::hash(m_scaledFont);
189#endif
190    }
191
192    const FontPlatformData& operator=(const FontPlatformData&);
193
194    bool operator==(const FontPlatformData& other) const
195    {
196        return platformIsEqual(other)
197            && m_size == other.m_size
198            && m_syntheticBold == other.m_syntheticBold
199            && m_syntheticOblique == other.m_syntheticOblique
200            && m_isColorBitmapFont == other.m_isColorBitmapFont
201            && m_isCompositeFontReference == other.m_isCompositeFontReference
202#if OS(DARWIN)
203            && m_isPrinterFont == other.m_isPrinterFont
204#endif
205            && m_orientation == other.m_orientation
206            && m_widthVariant == other.m_widthVariant;
207    }
208
209    bool isHashTableDeletedValue() const
210    {
211#if PLATFORM(WIN) && !USE(CAIRO)
212        return m_font.isHashTableDeletedValue();
213#elif OS(DARWIN)
214        return m_font == hashTableDeletedFontValue();
215#elif USE(CAIRO)
216        return m_scaledFont == hashTableDeletedFontValue();
217#endif
218    }
219
220#if (OS(DARWIN) && USE(CG)) || (PLATFORM(WIN) && (USE(CG) || USE(CAIRO)))
221    PassRefPtr<SharedBuffer> openTypeTable(uint32_t table) const;
222#endif
223
224#ifndef NDEBUG
225    String description() const;
226#endif
227
228private:
229    bool platformIsEqual(const FontPlatformData&) const;
230    void platformDataInit(const FontPlatformData&);
231    const FontPlatformData& platformDataAssign(const FontPlatformData&);
232#if PLATFORM(IOS)
233    static CTFontRef hashTableDeletedFontValue() { return reinterpret_cast<CTFontRef>(-1); }
234#elif OS(DARWIN)
235    // Load various data about the font specified by |nsFont| with the size fontSize into the following output paramters:
236    void loadFont(NSFont*, float fontSize, NSFont*& outNSFont, CGFontRef&);
237    static NSFont* hashTableDeletedFontValue() { return reinterpret_cast<NSFont *>(-1); }
238#elif PLATFORM(WIN)
239    void platformDataInit(HFONT, float size, HDC, WCHAR* faceName);
240#endif
241
242#if USE(CAIRO)
243    static cairo_scaled_font_t* hashTableDeletedFontValue() { return reinterpret_cast<cairo_scaled_font_t*>(-1); }
244#endif
245
246public:
247    bool m_syntheticBold;
248    bool m_syntheticOblique;
249    FontOrientation m_orientation;
250#if PLATFORM(IOS)
251    bool m_isEmoji;
252#endif
253    float m_size;
254    FontWidthVariant m_widthVariant;
255
256private:
257#if PLATFORM(IOS)
258    CTFontRef m_font;
259#elif OS(DARWIN)
260    NSFont* m_font;
261#elif PLATFORM(WIN)
262    RefPtr<SharedGDIObject<HFONT>> m_font;
263#endif
264
265#if USE(CG)
266#if PLATFORM(WIN)
267    RetainPtr<CGFontRef> m_cgFont;
268#else
269    RetainPtr<CGFontRef> m_cgFont;
270    mutable RetainPtr<CTFontRef> m_CTFont;
271#endif
272#endif
273
274#if USE(CAIRO)
275    cairo_scaled_font_t* m_scaledFont;
276#endif
277
278    bool m_isColorBitmapFont;
279    bool m_isCompositeFontReference;
280#if OS(DARWIN)
281    bool m_isPrinterFont;
282#endif
283
284#if PLATFORM(WIN)
285    bool m_useGDI;
286#endif
287};
288
289} // namespace WebCore
290
291#endif // FontPlatformData_h
292
293#endif
294