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