Home | History | Annotate | Download | only in fonts
      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 mac switch to using FontPlatformDataHarfBuzz.h and we merge it with this file.
     26 #if !OS(MACOSX)
     27 #include "platform/fonts/harfbuzz/FontPlatformDataHarfBuzz.h"
     28 
     29 #else
     30 
     31 #ifndef FontPlatformData_h
     32 #define FontPlatformData_h
     33 
     34 #include "platform/PlatformExport.h"
     35 #include "platform/fonts/FontOrientation.h"
     36 #include "platform/fonts/FontWidthVariant.h"
     37 
     38 #if OS(MACOSX)
     39 OBJC_CLASS NSFont;
     40 
     41 typedef struct CGFont* CGFontRef;
     42 typedef const struct __CTFont* CTFontRef;
     43 
     44 #include <CoreFoundation/CFBase.h>
     45 #include <objc/objc-auto.h>
     46 #endif
     47 
     48 #include "wtf/Forward.h"
     49 #include "wtf/HashTableDeletedValueType.h"
     50 #include "wtf/PassRefPtr.h"
     51 #include "wtf/RefCounted.h"
     52 #include "wtf/RetainPtr.h"
     53 #include "wtf/text/StringImpl.h"
     54 
     55 #if OS(MACOSX)
     56 #include "platform/fonts/mac/MemoryActivatedFont.h"
     57 #include "third_party/skia/include/core/SkTypeface.h"
     58 #endif
     59 
     60 #if OS(MACOSX)
     61 typedef struct CGFont* CGFontRef;
     62 typedef const struct __CTFont* CTFontRef;
     63 typedef UInt32 FMFont;
     64 typedef FMFont ATSUFontID;
     65 typedef UInt32 ATSFontRef;
     66 #endif
     67 
     68 namespace WebCore {
     69 
     70 class FontDescription;
     71 class SharedBuffer;
     72 
     73 #if OS(MACOSX)
     74 class HarfBuzzFace;
     75 #endif
     76 
     77 #if OS(MACOSX)
     78 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
     79 #endif
     80 
     81 class PLATFORM_EXPORT FontPlatformData {
     82 public:
     83     FontPlatformData(WTF::HashTableDeletedValueType);
     84     FontPlatformData();
     85     FontPlatformData(const FontPlatformData&);
     86     FontPlatformData(const FontDescription&, const AtomicString& family);
     87     FontPlatformData(float size, bool syntheticBold, bool syntheticOblique, FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
     88 
     89 #if OS(MACOSX)
     90     FontPlatformData(NSFont*, float size, bool syntheticBold = false, bool syntheticOblique = false,
     91                      FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
     92     FontPlatformData(CGFontRef, float size, bool syntheticBold, bool syntheticOblique, FontOrientation, FontWidthVariant);
     93 #endif
     94 
     95     ~FontPlatformData();
     96 
     97 #if OS(MACOSX)
     98     NSFont* font() const { return m_font; }
     99     void setFont(NSFont*);
    100 #endif
    101 
    102 #if OS(MACOSX)
    103     CGFontRef cgFont() const { return m_cgFont.get(); }
    104     CTFontRef ctFont() const;
    105     SkTypeface* typeface() const;
    106 
    107     bool roundsGlyphAdvances() const;
    108     bool allowsLigatures() const;
    109 #endif
    110 
    111     String fontFamilyName() const;
    112     bool isFixedPitch() const;
    113     float size() const { return m_size; }
    114     void setSize(float size) { m_size = size; }
    115     bool syntheticBold() const { return m_syntheticBold; }
    116     bool syntheticOblique() const { return m_syntheticOblique; }
    117     bool isColorBitmapFont() const { return m_isColorBitmapFont; }
    118     bool isCompositeFontReference() const { return m_isCompositeFontReference; }
    119 
    120     FontOrientation orientation() const { return m_orientation; }
    121     FontWidthVariant widthVariant() const { return m_widthVariant; }
    122 
    123     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
    124 
    125 #if OS(MACOSX)
    126     HarfBuzzFace* harfBuzzFace();
    127 #endif
    128 
    129     unsigned hash() const
    130     {
    131 #if OS(MACOSX)
    132         ASSERT(m_font || !m_cgFont);
    133         uintptr_t hashCodes[3] = { (uintptr_t)m_font, m_widthVariant, static_cast<uintptr_t>(m_orientation << 2 | m_syntheticBold << 1 | m_syntheticOblique) };
    134         return StringHasher::hashMemory<sizeof(hashCodes)>(hashCodes);
    135 #endif
    136     }
    137 
    138     const FontPlatformData& operator=(const FontPlatformData&);
    139 
    140     bool operator==(const FontPlatformData& other) const
    141     {
    142         return platformIsEqual(other)
    143             && m_size == other.m_size
    144             && m_syntheticBold == other.m_syntheticBold
    145             && m_syntheticOblique == other.m_syntheticOblique
    146             && m_isColorBitmapFont == other.m_isColorBitmapFont
    147             && m_isCompositeFontReference == other.m_isCompositeFontReference
    148             && m_orientation == other.m_orientation
    149             && m_widthVariant == other.m_widthVariant;
    150     }
    151 
    152     bool isHashTableDeletedValue() const
    153     {
    154 #if OS(MACOSX)
    155         return m_font == hashTableDeletedFontValue();
    156 #endif
    157     }
    158 
    159 #ifndef NDEBUG
    160     String description() const;
    161 #endif
    162 
    163 private:
    164     bool platformIsEqual(const FontPlatformData&) const;
    165     void platformDataInit(const FontPlatformData&);
    166     const FontPlatformData& platformDataAssign(const FontPlatformData&);
    167 #if OS(MACOSX)
    168     // Load various data about the font specified by |nsFont| with the size fontSize into the following output paramters:
    169     // Note: Callers should always take into account that for the Chromium port, |outNSFont| isn't necessarily the same
    170     // font as |nsFont|. This because the sandbox may block loading of the original font.
    171     // * outNSFont - The font that was actually loaded, for the Chromium port this may be different than nsFont.
    172     // The caller is responsible for calling CFRelease() on this parameter when done with it.
    173     // * cgFont - CGFontRef representing the input font at the specified point size.
    174     void loadFont(NSFont*, float fontSize, NSFont*& outNSFont, CGFontRef&);
    175     static NSFont* hashTableDeletedFontValue() { return reinterpret_cast<NSFont *>(-1); }
    176 #endif
    177 
    178 public:
    179     bool m_syntheticBold;
    180     bool m_syntheticOblique;
    181     FontOrientation m_orientation;
    182     float m_size;
    183     FontWidthVariant m_widthVariant;
    184 
    185 private:
    186 #if OS(MACOSX)
    187     NSFont* m_font;
    188 #endif
    189 
    190 #if OS(MACOSX)
    191     RetainPtr<CGFontRef> m_cgFont;
    192     mutable RetainPtr<CTFontRef> m_CTFont;
    193 
    194     RefPtr<MemoryActivatedFont> m_inMemoryFont;
    195     RefPtr<HarfBuzzFace> m_harfBuzzFace;
    196     mutable RefPtr<SkTypeface> m_typeface;
    197 #endif
    198 
    199     bool m_isColorBitmapFont;
    200     bool m_isCompositeFontReference;
    201 };
    202 
    203 } // namespace WebCore
    204 
    205 #endif // FontPlatformData_h
    206 
    207 #endif
    208