Home | History | Annotate | Download | only in mac
      1 /*
      2  * This file is part of the internal font implementation.
      3  * It should not be included by source files outside of it.
      4  *
      5  * Copyright (C) 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
      6  *
      7  * This library is free software; you can redistribute it and/or
      8  * modify it under the terms of the GNU Library General Public
      9  * License as published by the Free Software Foundation; either
     10  * version 2 of the License, or (at your option) any later version.
     11  *
     12  * This library is distributed in the hope that it will be useful,
     13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  * Library General Public License for more details.
     16  *
     17  * You should have received a copy of the GNU Library General Public License
     18  * along with this library; see the file COPYING.LIB.  If not, write to
     19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     20  * Boston, MA 02110-1301, USA.
     21  *
     22  */
     23 
     24 #ifndef FontPlatformData_h
     25 #define FontPlatformData_h
     26 
     27 #include "StringImpl.h"
     28 
     29 #ifdef __OBJC__
     30 @class NSFont;
     31 #else
     32 class NSFont;
     33 #endif
     34 
     35 typedef struct CGFont* CGFontRef;
     36 #ifndef BUILDING_ON_TIGER
     37 typedef const struct __CTFont* CTFontRef;
     38 #endif
     39 
     40 #include <CoreFoundation/CFBase.h>
     41 #include <objc/objc-auto.h>
     42 #include <wtf/RetainPtr.h>
     43 
     44 typedef UInt32 ATSUFontID;
     45 
     46 namespace WebCore {
     47 
     48 class String;
     49 
     50 #ifndef BUILDING_ON_TIGER
     51 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
     52 #endif
     53 
     54 struct FontPlatformData {
     55     FontPlatformData(float size, bool syntheticBold, bool syntheticOblique)
     56         : m_syntheticBold(syntheticBold)
     57         , m_syntheticOblique(syntheticOblique)
     58         , m_atsuFontID(0)
     59         , m_size(size)
     60         , m_font(0)
     61 #ifdef BUILDING_ON_TIGER
     62         , m_cgFont(0)
     63 #endif
     64     {
     65     }
     66 
     67     FontPlatformData(NSFont *nsFont, bool syntheticBold = false, bool syntheticOblique = false);
     68 
     69     FontPlatformData(CGFontRef cgFont, ATSUFontID fontID, float size, bool syntheticBold, bool syntheticOblique)
     70         : m_syntheticBold(syntheticBold)
     71         , m_syntheticOblique(syntheticOblique)
     72         , m_atsuFontID(fontID)
     73         , m_size(size)
     74         , m_font(0)
     75         , m_cgFont(cgFont)
     76     {
     77     }
     78 
     79     FontPlatformData(const FontPlatformData&);
     80 
     81     ~FontPlatformData();
     82 
     83     FontPlatformData(WTF::HashTableDeletedValueType) : m_font(hashTableDeletedFontValue()) { }
     84     bool isHashTableDeletedValue() const { return m_font == hashTableDeletedFontValue(); }
     85 
     86     float size() const { return m_size; }
     87     bool syntheticBold() const { return m_syntheticBold; }
     88     bool syntheticOblique() const { return m_syntheticOblique; }
     89 
     90     bool m_syntheticBold;
     91     bool m_syntheticOblique;
     92 
     93     ATSUFontID m_atsuFontID;
     94     float m_size;
     95 
     96     unsigned hash() const
     97     {
     98         ASSERT(m_font != 0 || m_cgFont == 0);
     99         uintptr_t hashCodes[2] = { (uintptr_t)m_font, m_syntheticBold << 1 | m_syntheticOblique };
    100         return StringImpl::computeHash(reinterpret_cast<UChar*>(hashCodes), sizeof(hashCodes) / sizeof(UChar));
    101     }
    102 
    103     const FontPlatformData& operator=(const FontPlatformData& f);
    104 
    105     bool operator==(const FontPlatformData& other) const
    106     {
    107         return m_font == other.m_font && m_syntheticBold == other.m_syntheticBold && m_syntheticOblique == other.m_syntheticOblique &&
    108                m_cgFont == other.m_cgFont && m_size == other.m_size && m_atsuFontID == other.m_atsuFontID;
    109     }
    110 
    111     NSFont *font() const { return m_font; }
    112     void setFont(NSFont *font);
    113 
    114     bool roundsGlyphAdvances() const;
    115     bool allowsLigatures() const;
    116 
    117 #ifndef BUILDING_ON_TIGER
    118     CGFontRef cgFont() const { return m_cgFont.get(); }
    119 #else
    120     CGFontRef cgFont() const { return m_cgFont; }
    121 #endif
    122 
    123 #ifndef NDEBUG
    124     String description() const;
    125 #endif
    126 
    127 private:
    128     static NSFont *hashTableDeletedFontValue() { return reinterpret_cast<NSFont *>(-1); }
    129 
    130     NSFont *m_font;
    131 #ifndef BUILDING_ON_TIGER
    132     RetainPtr<CGFontRef> m_cgFont;
    133 #else
    134     CGFontRef m_cgFont; // It is not necessary to refcount this, since either an NSFont owns it or some CachedFont has it referenced.
    135 #endif
    136 };
    137 
    138 }
    139 
    140 #endif
    141