Home | History | Annotate | Download | only in fonts
      1 /*
      2  * Copyright (c) 2006, 2007, 2008, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef FontPlatformData_h
     32 #define FontPlatformData_h
     33 
     34 #include "SkPaint.h"
     35 #include "platform/PlatformExport.h"
     36 #include "platform/SharedBuffer.h"
     37 #include "platform/fonts/FontDescription.h"
     38 #include "platform/fonts/FontOrientation.h"
     39 #include "platform/fonts/FontRenderStyle.h"
     40 #include "platform/fonts/opentype/OpenTypeVerticalData.h"
     41 #include "wtf/Forward.h"
     42 #include "wtf/HashTableDeletedValueType.h"
     43 #include "wtf/RefPtr.h"
     44 #include "wtf/text/CString.h"
     45 #include "wtf/text/StringImpl.h"
     46 
     47 #if OS(MACOSX)
     48 OBJC_CLASS NSFont;
     49 
     50 typedef struct CGFont* CGFontRef;
     51 typedef const struct __CTFont* CTFontRef;
     52 
     53 #include "platform/fonts/mac/MemoryActivatedFont.h"
     54 #include <CoreFoundation/CFBase.h>
     55 #include <objc/objc-auto.h>
     56 
     57 inline CTFontRef toCTFontRef(NSFont *nsFont) { return reinterpret_cast<CTFontRef>(nsFont); }
     58 #endif // OS(MACOSX)
     59 
     60 class SkTypeface;
     61 typedef uint32_t SkFontID;
     62 
     63 namespace blink {
     64 
     65 class GraphicsContext;
     66 class HarfBuzzFace;
     67 
     68 class PLATFORM_EXPORT FontPlatformData {
     69 public:
     70     // Used for deleted values in the font cache's hash tables. The hash table
     71     // will create us with this structure, and it will compare other values
     72     // to this "Deleted" one. It expects the Deleted one to be differentiable
     73     // from the 0 one (created with the empty constructor), so we can't just
     74     // set everything to 0.
     75     FontPlatformData(WTF::HashTableDeletedValueType);
     76     FontPlatformData();
     77     FontPlatformData(const FontPlatformData&);
     78     FontPlatformData(float size, bool syntheticBold, bool syntheticItalic, FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
     79 #if OS(MACOSX)
     80     FontPlatformData(NSFont*, float size, bool syntheticBold = false, bool syntheticItalic = false,
     81                      FontOrientation = Horizontal, FontWidthVariant = RegularWidth);
     82     FontPlatformData(CGFontRef, float size, bool syntheticBold, bool syntheticOblique, FontOrientation, FontWidthVariant);
     83 #else
     84     FontPlatformData(PassRefPtr<SkTypeface>, const char* name, float textSize, bool syntheticBold, bool syntheticItalic, FontOrientation = Horizontal, bool subpixelTextPosition = defaultUseSubpixelPositioning());
     85     FontPlatformData(const FontPlatformData& src, float textSize);
     86 #endif
     87     ~FontPlatformData();
     88 
     89 #if OS(MACOSX)
     90     NSFont* font() const { return m_font; }
     91     void setFont(NSFont*);
     92 
     93     CGFontRef cgFont() const { return m_cgFont.get(); }
     94     CTFontRef ctFont() const;
     95 
     96     bool roundsGlyphAdvances() const;
     97     bool allowsLigatures() const;
     98 
     99     bool isColorBitmapFont() const { return m_isColorBitmapFont; }
    100     bool isCompositeFontReference() const { return m_isCompositeFontReference; }
    101 
    102     FontWidthVariant widthVariant() const { return m_widthVariant; }
    103 #endif
    104 
    105     String fontFamilyName() const;
    106     float size() const { return m_textSize; }
    107     bool isFixedPitch() const;
    108     bool syntheticBold() const { return m_syntheticBold; }
    109     bool syntheticItalic() const { return m_syntheticItalic; }
    110 
    111     SkTypeface* typeface() const;
    112     HarfBuzzFace* harfBuzzFace() const;
    113     SkFontID uniqueID() const;
    114     unsigned hash() const;
    115 
    116     FontOrientation orientation() const { return m_orientation; }
    117     void setOrientation(FontOrientation orientation) { m_orientation = orientation; }
    118     void setSyntheticBold(bool syntheticBold) { m_syntheticBold = syntheticBold; }
    119     void setSyntheticItalic(bool syntheticItalic) { m_syntheticItalic = syntheticItalic; }
    120     bool operator==(const FontPlatformData&) const;
    121     const FontPlatformData& operator=(const FontPlatformData&);
    122 
    123     bool isHashTableDeletedValue() const { return m_isHashTableDeletedValue; }
    124 #if OS(WIN)
    125     void setMinSizeForAntiAlias(unsigned size) { m_minSizeForAntiAlias = size; }
    126     unsigned minSizeForAntiAlias() const { return m_minSizeForAntiAlias; }
    127     void setMinSizeForSubpixel(float size) { m_minSizeForSubpixel = size; }
    128     float minSizeForSubpixel() const { return m_minSizeForSubpixel; }
    129     void setHinting(SkPaint::Hinting style)
    130     {
    131         m_style.useAutoHint = 0;
    132         m_style.hintStyle = style;
    133     }
    134 #endif
    135     bool fontContainsCharacter(UChar32 character);
    136 
    137 #if ENABLE(OPENTYPE_VERTICAL)
    138     PassRefPtr<OpenTypeVerticalData> verticalData() const;
    139     PassRefPtr<SharedBuffer> openTypeTable(uint32_t table) const;
    140 #endif
    141 
    142 #if !OS(MACOSX)
    143     // The returned styles are all actual styles without FontRenderStyle::NoPreference.
    144     const FontRenderStyle& fontRenderStyle() const { return m_style; }
    145     void setupPaint(SkPaint*, GraphicsContext* = 0) const;
    146 #endif
    147 
    148 #if OS(WIN)
    149     int paintTextFlags() const { return m_paintTextFlags; }
    150 #else
    151     static void setHinting(SkPaint::Hinting);
    152     static void setAutoHint(bool);
    153     static void setUseBitmaps(bool);
    154     static void setAntiAlias(bool);
    155     static void setSubpixelRendering(bool);
    156 #endif
    157 
    158 private:
    159 #if !OS(MACOSX)
    160     bool static defaultUseSubpixelPositioning();
    161     void querySystemForRenderStyle(bool useSkiaSubpixelPositioning);
    162 #else
    163     // Load various data about the font specified by |nsFont| with the size fontSize into the following output paramters:
    164     // Note: Callers should always take into account that for the Chromium port, |outNSFont| isn't necessarily the same
    165     // font as |nsFont|. This because the sandbox may block loading of the original font.
    166     // * outNSFont - The font that was actually loaded, for the Chromium port this may be different than nsFont.
    167     // The caller is responsible for calling CFRelease() on this parameter when done with it.
    168     // * cgFont - CGFontRef representing the input font at the specified point size.
    169     void loadFont(NSFont*, float fontSize, NSFont*& outNSFont, CGFontRef&);
    170     void platformDataInit(const FontPlatformData&);
    171     const FontPlatformData& platformDataAssign(const FontPlatformData&);
    172     bool isAATFont(CTFontRef) const;
    173 #endif
    174 
    175     mutable RefPtr<SkTypeface> m_typeface;
    176 #if !OS(WIN)
    177     CString m_family;
    178 #endif
    179 
    180 public:
    181     float m_textSize;
    182     bool m_syntheticBold;
    183     bool m_syntheticItalic;
    184     FontOrientation m_orientation;
    185 #if OS(MACOSX)
    186     bool m_isColorBitmapFont;
    187     bool m_isCompositeFontReference;
    188 #endif
    189     FontWidthVariant m_widthVariant;
    190 private:
    191 #if OS(MACOSX)
    192     NSFont* m_font;
    193     RetainPtr<CGFontRef> m_cgFont;
    194     mutable RetainPtr<CTFontRef> m_CTFont;
    195     RefPtr<MemoryActivatedFont> m_inMemoryFont;
    196 #else
    197     FontRenderStyle m_style;
    198 #endif
    199 
    200     mutable RefPtr<HarfBuzzFace> m_harfBuzzFace;
    201     bool m_isHashTableDeletedValue;
    202 #if OS(WIN)
    203     int m_paintTextFlags;
    204     bool m_useSubpixelPositioning;
    205     unsigned m_minSizeForAntiAlias;
    206     float m_minSizeForSubpixel;
    207 #endif
    208 };
    209 
    210 } // namespace blink
    211 
    212 #endif // ifdef FontPlatformData_h
    213