Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc.
      3  * Copyright (C) 2006 Michael Emmel mike.emmel (at) 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 (at) 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 #elif PLATFORM(ANDROID)
     39 #include "android/FontPlatformData.h"
     40 #else
     41 
     42 #ifndef FontPlatformData_h
     43 #define FontPlatformData_h
     44 
     45 #include "FontOrientation.h"
     46 #include "FontWidthVariant.h"
     47 #include "GlyphBuffer.h"
     48 #include "TextOrientation.h"
     49 
     50 #if PLATFORM(WIN)
     51 #include "RefCountedGDIHandle.h"
     52 #endif
     53 
     54 #if USE(CAIRO)
     55 #include "HashFunctions.h"
     56 #include <cairo.h>
     57 #endif
     58 
     59 #if OS(DARWIN)
     60 #ifdef __OBJC__
     61 @class NSFont;
     62 #else
     63 class NSFont;
     64 #endif
     65 
     66 typedef struct CGFont* CGFontRef;
     67 #ifndef BUILDING_ON_TIGER
     68 typedef const struct __CTFont* CTFontRef;
     69 #endif
     70 
     71 #include <CoreFoundation/CFBase.h>
     72 #include <objc/objc-auto.h>
     73 #endif
     74 
     75 #include <wtf/Forward.h>
     76 #include <wtf/PassRefPtr.h>
     77 #include <wtf/RefCounted.h>
     78 #include <wtf/RetainPtr.h>
     79 #include <wtf/text/StringImpl.h>
     80 
     81 #if PLATFORM(CHROMIUM) && OS(DARWIN)
     82 #include "CrossProcessFontLoading.h"
     83 #endif
     84 
     85 #if PLATFORM(WIN)
     86 typedef struct HFONT__* HFONT;
     87 #endif
     88 
     89 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
     90 typedef struct CGFont* CGFontRef;
     91 #if OS(DARWIN)
     92 #ifndef BUILDING_ON_TIGER
     93 typedef const struct __CTFont* CTFontRef;
     94 typedef UInt32 ATSUFontID;
     95 typedef UInt32 ATSFontRef;
     96 #endif
     97 #endif
     98 #endif
     99 
    100 namespace WebCore {
    101 
    102 class FontDescription;
    103 
    104 #if OS(DARWIN) && !defined(BUILDING_ON_TIGER)
    105 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
    106 #endif
    107 
    108 class FontPlatformData {
    109 public:
    110     FontPlatformData(WTF::HashTableDeletedValueType)
    111         : m_syntheticBold(false)
    112         , m_syntheticOblique(false)
    113         , m_orientation(Horizontal)
    114         , m_textOrientation(TextOrientationVerticalRight)
    115         , m_size(0)
    116         , m_widthVariant(RegularWidth)
    117 #if PLATFORM(WIN)
    118         , m_font(WTF::HashTableDeletedValue)
    119 #elif OS(DARWIN)
    120         , m_font(hashTableDeletedFontValue())
    121 #endif
    122 #if USE(CG) && (defined(BUILDING_ON_TIGER) || PLATFORM(WIN))
    123         , m_cgFont(0)
    124 #elif USE(CAIRO)
    125         , m_scaledFont(hashTableDeletedFontValue())
    126 #endif
    127         , m_isColorBitmapFont(false)
    128 #if PLATFORM(WIN)
    129         , m_useGDI(false)
    130 #endif
    131         {
    132         }
    133 
    134     FontPlatformData()
    135         : m_syntheticBold(false)
    136         , m_syntheticOblique(false)
    137         , m_orientation(Horizontal)
    138         , m_textOrientation(TextOrientationVerticalRight)
    139         , m_size(0)
    140         , m_widthVariant(RegularWidth)
    141 #if OS(DARWIN)
    142         , m_font(0)
    143 #endif
    144 #if USE(CG) && (defined(BUILDING_ON_TIGER) || PLATFORM(WIN))
    145         , m_cgFont(0)
    146 #elif USE(CAIRO)
    147         , m_scaledFont(0)
    148 #endif
    149         , m_isColorBitmapFont(false)
    150 #if PLATFORM(WIN)
    151         , m_useGDI(false)
    152 #endif
    153     {
    154     }
    155 
    156     FontPlatformData(const FontPlatformData&);
    157     FontPlatformData(const FontDescription&, const AtomicString& family);
    158     FontPlatformData(float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation = Horizontal,
    159                      TextOrientation textOrientation = TextOrientationVerticalRight, FontWidthVariant widthVariant = RegularWidth)
    160         : m_syntheticBold(syntheticBold)
    161         , m_syntheticOblique(syntheticOblique)
    162         , m_orientation(orientation)
    163         , m_textOrientation(textOrientation)
    164         , m_size(size)
    165         , m_widthVariant(widthVariant)
    166 #if OS(DARWIN)
    167         , m_font(0)
    168 #endif
    169 #if USE(CG) && (defined(BUILDING_ON_TIGER) || PLATFORM(WIN))
    170         , m_cgFont(0)
    171 #elif USE(CAIRO)
    172         , m_scaledFont(0)
    173 #endif
    174         , m_isColorBitmapFont(false)
    175 #if PLATFORM(WIN)
    176         , m_useGDI(false)
    177 #endif
    178     {
    179     }
    180 
    181 #if OS(DARWIN)
    182     FontPlatformData(NSFont*, float size, bool syntheticBold = false, bool syntheticOblique = false, FontOrientation = Horizontal,
    183                      TextOrientation = TextOrientationVerticalRight, FontWidthVariant = RegularWidth);
    184 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
    185     FontPlatformData(CGFontRef cgFont, float size, bool syntheticBold, bool syntheticOblique, FontOrientation orientation,
    186                      TextOrientation textOrientation, FontWidthVariant widthVariant)
    187         : m_syntheticBold(syntheticBold)
    188         , m_syntheticOblique(syntheticOblique)
    189         , m_orientation(orientation)
    190         , m_textOrientation(textOrientation)
    191         , m_size(size)
    192         , m_widthVariant(widthVariant)
    193         , m_font(0)
    194         , m_cgFont(cgFont)
    195         , m_isColorBitmapFont(false)
    196     {
    197     }
    198 #endif
    199 #endif
    200 #if PLATFORM(WIN)
    201     FontPlatformData(HFONT, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
    202 #if USE(CG)
    203     FontPlatformData(HFONT, CGFontRef, float size, bool syntheticBold, bool syntheticOblique, bool useGDI);
    204 #endif
    205 #endif
    206 #if USE(CAIRO)
    207     FontPlatformData(cairo_font_face_t*, float size, bool bold, bool italic);
    208 #endif
    209 
    210     ~FontPlatformData();
    211 
    212 #if PLATFORM(WIN)
    213     HFONT hfont() const { return m_font ? m_font->handle() : 0; }
    214     bool useGDI() const { return m_useGDI; }
    215 #elif OS(DARWIN)
    216     NSFont* font() const { return m_font; }
    217     void setFont(NSFont*);
    218 #endif
    219 
    220 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
    221 #if OS(DARWIN)
    222 #ifndef BUILDING_ON_TIGER
    223     CGFontRef cgFont() const { return m_cgFont.get(); }
    224 #else
    225     CGFontRef cgFont() const { return m_cgFont; }
    226 #endif
    227     CTFontRef ctFont() const;
    228 
    229     bool roundsGlyphAdvances() const;
    230     bool allowsLigatures() const;
    231 #else
    232     CGFontRef cgFont() const { return m_cgFont.get(); }
    233 #endif
    234 #endif
    235 
    236     bool isFixedPitch() const;
    237     float size() const { return m_size; }
    238     void setSize(float size) { m_size = size; }
    239     bool syntheticBold() const { return m_syntheticBold; }
    240     bool syntheticOblique() const { return m_syntheticOblique; }
    241     bool isColorBitmapFont() const { return m_isColorBitmapFont; }
    242     FontOrientation orientation() const { return m_orientation; }
    243     TextOrientation textOrientation() const { return m_textOrientation; }
    244     FontWidthVariant widthVariant() const { return m_widthVariant; }
    245 
    246     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
    247 
    248 #if USE(CAIRO)
    249     cairo_scaled_font_t* scaledFont() const { return m_scaledFont; }
    250 #endif
    251 
    252     unsigned hash() const
    253     {
    254 #if PLATFORM(WIN) && !USE(CAIRO)
    255         return m_font ? m_font->hash() : 0;
    256 #elif OS(DARWIN)
    257 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
    258         ASSERT(m_font || !m_cgFont);
    259 #endif
    260         uintptr_t hashCodes[3] = { (uintptr_t)m_font, m_widthVariant, m_textOrientation << 3 | m_orientation << 2 | m_syntheticBold << 1 | m_syntheticOblique };
    261         return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
    262 #elif USE(CAIRO)
    263         return PtrHash<cairo_scaled_font_t*>::hash(m_scaledFont);
    264 #endif
    265     }
    266 
    267     const FontPlatformData& operator=(const FontPlatformData&);
    268 
    269     bool operator==(const FontPlatformData& other) const
    270     {
    271         return platformIsEqual(other)
    272             && m_size == other.m_size
    273             && m_syntheticBold == other.m_syntheticBold
    274             && m_syntheticOblique == other.m_syntheticOblique
    275             && m_isColorBitmapFont == other.m_isColorBitmapFont
    276             && m_orientation == other.m_orientation
    277             && m_textOrientation == other.m_textOrientation
    278             && m_widthVariant == other.m_widthVariant;
    279     }
    280 
    281     bool isHashTableDeletedValue() const
    282     {
    283 #if PLATFORM(WIN) && !USE(CAIRO)
    284         return m_font.isHashTableDeletedValue();
    285 #elif OS(DARWIN)
    286         return m_font == hashTableDeletedFontValue();
    287 #elif USE(CAIRO)
    288         return m_scaledFont == hashTableDeletedFontValue();
    289 #endif
    290     }
    291 
    292 
    293 #ifndef NDEBUG
    294     String description() const;
    295 #endif
    296 
    297 private:
    298     bool platformIsEqual(const FontPlatformData&) const;
    299     void platformDataInit(const FontPlatformData&);
    300     const FontPlatformData& platformDataAssign(const FontPlatformData&);
    301 #if OS(DARWIN)
    302     // Load various data about the font specified by |nsFont| with the size fontSize into the following output paramters:
    303     // Note: Callers should always take into account that for the Chromium port, |outNSFont| isn't necessarily the same
    304     // font as |nsFont|. This because the sandbox may block loading of the original font.
    305     // * outNSFont - The font that was actually loaded, for the Chromium port this may be different than nsFont.
    306     // The caller is responsible for calling CFRelease() on this parameter when done with it.
    307     // * cgFont - CGFontRef representing the input font at the specified point size.
    308     void loadFont(NSFont*, float fontSize, NSFont*& outNSFont, CGFontRef&);
    309     static NSFont* hashTableDeletedFontValue() { return reinterpret_cast<NSFont *>(-1); }
    310 #elif PLATFORM(WIN)
    311     void platformDataInit(HFONT, float size, HDC, WCHAR* faceName);
    312 #endif
    313 
    314 #if USE(CAIRO)
    315     static cairo_scaled_font_t* hashTableDeletedFontValue() { return reinterpret_cast<cairo_scaled_font_t*>(-1); }
    316 #endif
    317 
    318 public:
    319     bool m_syntheticBold;
    320     bool m_syntheticOblique;
    321     FontOrientation m_orientation;
    322     TextOrientation m_textOrientation;
    323     float m_size;
    324     FontWidthVariant m_widthVariant;
    325 
    326 private:
    327 #if OS(DARWIN)
    328     NSFont* m_font;
    329 #elif PLATFORM(WIN)
    330     RefPtr<RefCountedGDIHandle<HFONT> > m_font;
    331 #endif
    332 
    333 #if USE(CG) || USE(SKIA_ON_MAC_CHROME)
    334 #if PLATFORM(WIN)
    335     RetainPtr<CGFontRef> m_cgFont;
    336 #else
    337 #ifndef BUILDING_ON_TIGER
    338     RetainPtr<CGFontRef> m_cgFont;
    339 #else
    340     CGFontRef m_cgFont; // It is not necessary to refcount this, since either an NSFont owns it or some CachedFont has it referenced.
    341 #endif
    342     mutable RetainPtr<CTFontRef> m_CTFont;
    343 #endif
    344 #endif
    345 
    346 #if USE(CAIRO)
    347     cairo_scaled_font_t* m_scaledFont;
    348 #endif
    349 
    350 #if PLATFORM(CHROMIUM) && OS(DARWIN)
    351     RefPtr<MemoryActivatedFont> m_inMemoryFont;
    352 #endif
    353 
    354     bool m_isColorBitmapFont;
    355 
    356 #if PLATFORM(WIN)
    357     bool m_useGDI;
    358 #endif
    359 };
    360 
    361 } // namespace WebCore
    362 
    363 #endif // FontPlatformData_h
    364 
    365 #endif
    366