Home | History | Annotate | Download | only in chromium
      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 FontPlatformDataLinux_h
     32 #define FontPlatformDataLinux_h
     33 
     34 #include "StringImpl.h"
     35 #include <wtf/RefPtr.h>
     36 #include <SkPaint.h>
     37 
     38 class SkTypeface;
     39 typedef uint32_t SkFontID;
     40 
     41 struct HB_FaceRec_;
     42 
     43 namespace WebCore {
     44 
     45 class FontDescription;
     46 class String;
     47 
     48 // -----------------------------------------------------------------------------
     49 // FontPlatformData is the handle which WebKit has on a specific face. A face
     50 // is the tuple of (font, size, ...etc). Here we are just wrapping a Skia
     51 // SkTypeface pointer and dealing with the reference counting etc.
     52 // -----------------------------------------------------------------------------
     53 class FontPlatformData {
     54 public:
     55     // Used for deleted values in the font cache's hash tables. The hash table
     56     // will create us with this structure, and it will compare other values
     57     // to this "Deleted" one. It expects the Deleted one to be differentiable
     58     // from the NULL one (created with the empty constructor), so we can't just
     59     // set everything to NULL.
     60     FontPlatformData(WTF::HashTableDeletedValueType)
     61         : m_typeface(hashTableDeletedFontValue())
     62         , m_textSize(0)
     63         , m_fakeBold(false)
     64         , m_fakeItalic(false)
     65         { }
     66 
     67     FontPlatformData()
     68         : m_typeface(0)
     69         , m_textSize(0)
     70         , m_fakeBold(false)
     71         , m_fakeItalic(false)
     72         { }
     73 
     74     FontPlatformData(float textSize, bool fakeBold, bool fakeItalic)
     75         : m_typeface(0)
     76         , m_textSize(textSize)
     77         , m_fakeBold(fakeBold)
     78         , m_fakeItalic(fakeItalic)
     79         { }
     80 
     81     FontPlatformData(const FontPlatformData&);
     82     FontPlatformData(SkTypeface*, float textSize, bool fakeBold, bool fakeItalic);
     83     FontPlatformData(const FontPlatformData& src, float textSize);
     84     ~FontPlatformData();
     85 
     86     // -------------------------------------------------------------------------
     87     // Return true iff this font is monospaced (i.e. every glyph has an equal x
     88     // advance)
     89     // -------------------------------------------------------------------------
     90     bool isFixedPitch() const;
     91 
     92     // -------------------------------------------------------------------------
     93     // Setup a Skia painting context to use this font.
     94     // -------------------------------------------------------------------------
     95     void setupPaint(SkPaint*) const;
     96 
     97     // -------------------------------------------------------------------------
     98     // Return Skia's unique id for this font. This encodes both the style and
     99     // the font's file name so refers to a single face.
    100     // -------------------------------------------------------------------------
    101     SkFontID uniqueID() const;
    102 
    103     unsigned hash() const;
    104     float size() const { return m_textSize; }
    105 
    106     bool operator==(const FontPlatformData&) const;
    107     FontPlatformData& operator=(const FontPlatformData&);
    108     bool isHashTableDeletedValue() const { return m_typeface == hashTableDeletedFontValue(); }
    109 
    110 #ifndef NDEBUG
    111     String description() const;
    112 #endif
    113 
    114     HB_FaceRec_* harfbuzzFace() const;
    115 
    116     // -------------------------------------------------------------------------
    117     // Global font preferences...
    118 
    119     static void setHinting(SkPaint::Hinting);
    120     static void setAntiAlias(bool on);
    121     static void setSubpixelGlyphs(bool on);
    122 
    123 private:
    124     class RefCountedHarfbuzzFace : public RefCounted<RefCountedHarfbuzzFace> {
    125     public:
    126         static PassRefPtr<RefCountedHarfbuzzFace> create(HB_FaceRec_* harfbuzzFace)
    127         {
    128             return adoptRef(new RefCountedHarfbuzzFace(harfbuzzFace));
    129         }
    130 
    131         ~RefCountedHarfbuzzFace();
    132 
    133         HB_FaceRec_* face() const { return m_harfbuzzFace; }
    134 
    135     private:
    136         RefCountedHarfbuzzFace(HB_FaceRec_* harfbuzzFace) : m_harfbuzzFace(harfbuzzFace)
    137         {
    138         }
    139 
    140         HB_FaceRec_* m_harfbuzzFace;
    141     };
    142 
    143     // FIXME: Could SkAutoUnref be used here?
    144     SkTypeface* m_typeface;
    145     float m_textSize;
    146     bool m_fakeBold;
    147     bool m_fakeItalic;
    148     mutable RefPtr<RefCountedHarfbuzzFace> m_harfbuzzFace;
    149 
    150     SkTypeface* hashTableDeletedFontValue() const { return reinterpret_cast<SkTypeface*>(-1); }
    151 };
    152 
    153 }  // namespace WebCore
    154 
    155 #endif  // ifdef FontPlatformData_h
    156