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 #include "config.h"
     32 #include "FontPlatformData.h"
     33 
     34 #include "HarfbuzzSkia.h"
     35 #include "NotImplemented.h"
     36 #include "PlatformBridge.h"
     37 #include "PlatformString.h"
     38 
     39 #include "SkAdvancedTypefaceMetrics.h"
     40 #include "SkPaint.h"
     41 #include "SkTypeface.h"
     42 
     43 #include <wtf/text/StringImpl.h>
     44 
     45 namespace WebCore {
     46 
     47 static SkPaint::Hinting skiaHinting = SkPaint::kNormal_Hinting;
     48 static bool isSkiaAntiAlias = true;
     49 static bool isSkiaSubpixelGlyphs = false;
     50 
     51 void FontPlatformData::setHinting(SkPaint::Hinting hinting)
     52 {
     53     skiaHinting = hinting;
     54 }
     55 
     56 void FontPlatformData::setAntiAlias(bool isAntiAlias)
     57 {
     58     isSkiaAntiAlias = isAntiAlias;
     59 }
     60 
     61 void FontPlatformData::setSubpixelGlyphs(bool isSubpixelGlyphs)
     62 {
     63     isSkiaSubpixelGlyphs = isSubpixelGlyphs;
     64 }
     65 
     66 FontPlatformData::RefCountedHarfbuzzFace::~RefCountedHarfbuzzFace()
     67 {
     68     HB_FreeFace(m_harfbuzzFace);
     69 }
     70 
     71 FontPlatformData::FontPlatformData(const FontPlatformData& src)
     72     : m_typeface(src.m_typeface)
     73     , m_family(src.m_family)
     74     , m_textSize(src.m_textSize)
     75     , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
     76     , m_fakeBold(src.m_fakeBold)
     77     , m_fakeItalic(src.m_fakeItalic)
     78     , m_orientation(src.m_orientation)
     79     , m_textOrientation(src.m_textOrientation)
     80     , m_style(src.m_style)
     81     , m_harfbuzzFace(src.m_harfbuzzFace)
     82 {
     83     SkSafeRef(m_typeface);
     84 }
     85 
     86 FontPlatformData::FontPlatformData(SkTypeface* tf, const char* family, float textSize, bool fakeBold, bool fakeItalic, FontOrientation orientation, TextOrientation textOrientation)
     87     : m_typeface(tf)
     88     , m_family(family)
     89     , m_textSize(textSize)
     90     , m_emSizeInFontUnits(0)
     91     , m_fakeBold(fakeBold)
     92     , m_fakeItalic(fakeItalic)
     93     , m_orientation(orientation)
     94     , m_textOrientation(textOrientation)
     95 {
     96     SkSafeRef(m_typeface);
     97     querySystemForRenderStyle();
     98 }
     99 
    100 FontPlatformData::FontPlatformData(const FontPlatformData& src, float textSize)
    101     : m_typeface(src.m_typeface)
    102     , m_family(src.m_family)
    103     , m_textSize(textSize)
    104     , m_emSizeInFontUnits(src.m_emSizeInFontUnits)
    105     , m_fakeBold(src.m_fakeBold)
    106     , m_fakeItalic(src.m_fakeItalic)
    107     , m_orientation(src.m_orientation)
    108     , m_textOrientation(src.m_textOrientation)
    109     , m_harfbuzzFace(src.m_harfbuzzFace)
    110 {
    111     SkSafeRef(m_typeface);
    112     querySystemForRenderStyle();
    113 }
    114 
    115 FontPlatformData::~FontPlatformData()
    116 {
    117     SkSafeUnref(m_typeface);
    118 }
    119 
    120 int FontPlatformData::emSizeInFontUnits() const
    121 {
    122     if (m_emSizeInFontUnits)
    123         return m_emSizeInFontUnits;
    124 
    125     SkAdvancedTypefaceMetrics* metrics = m_typeface->getAdvancedTypefaceMetrics(SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo);
    126     m_emSizeInFontUnits = metrics->fEmSize;
    127     metrics->unref();
    128     return m_emSizeInFontUnits;
    129 }
    130 
    131 FontPlatformData& FontPlatformData::operator=(const FontPlatformData& src)
    132 {
    133     SkRefCnt_SafeAssign(m_typeface, src.m_typeface);
    134 
    135     m_family = src.m_family;
    136     m_textSize = src.m_textSize;
    137     m_fakeBold = src.m_fakeBold;
    138     m_fakeItalic = src.m_fakeItalic;
    139     m_harfbuzzFace = src.m_harfbuzzFace;
    140     m_orientation = src.m_orientation;
    141     m_textOrientation = src.m_textOrientation;
    142     m_style = src.m_style;
    143     m_emSizeInFontUnits = src.m_emSizeInFontUnits;
    144 
    145     return *this;
    146 }
    147 
    148 #ifndef NDEBUG
    149 String FontPlatformData::description() const
    150 {
    151     return String();
    152 }
    153 #endif
    154 
    155 void FontPlatformData::setupPaint(SkPaint* paint) const
    156 {
    157     const float ts = m_textSize >= 0 ? m_textSize : 12;
    158 
    159     paint->setAntiAlias(m_style.useAntiAlias == FontRenderStyle::NoPreference ? isSkiaAntiAlias : m_style.useAntiAlias);
    160     switch (m_style.useHinting) {
    161     case FontRenderStyle::NoPreference:
    162         paint->setHinting(skiaHinting);
    163         break;
    164     case 0:
    165         paint->setHinting(SkPaint::kNo_Hinting);
    166         break;
    167     default:
    168         paint->setHinting(static_cast<SkPaint::Hinting>(m_style.hintStyle));
    169         break;
    170     }
    171 
    172     paint->setEmbeddedBitmapText(m_style.useBitmaps);
    173     paint->setTextSize(SkFloatToScalar(ts));
    174     paint->setTypeface(m_typeface);
    175     paint->setFakeBoldText(m_fakeBold);
    176     paint->setTextSkewX(m_fakeItalic ? -SK_Scalar1 / 4 : 0);
    177     paint->setAutohinted(m_style.useAutoHint);
    178 
    179     if (m_style.useAntiAlias == 1 || (m_style.useAntiAlias == FontRenderStyle::NoPreference && isSkiaAntiAlias))
    180         paint->setLCDRenderText(m_style.useSubpixel == FontRenderStyle::NoPreference ? isSkiaSubpixelGlyphs : m_style.useSubpixel);
    181 }
    182 
    183 SkFontID FontPlatformData::uniqueID() const
    184 {
    185     return m_typeface->uniqueID();
    186 }
    187 
    188 bool FontPlatformData::operator==(const FontPlatformData& a) const
    189 {
    190     // If either of the typeface pointers are invalid (either NULL or the
    191     // special deleted value) then we test for pointer equality. Otherwise, we
    192     // call SkTypeface::Equal on the valid pointers.
    193     bool typefacesEqual;
    194     if (m_typeface == hashTableDeletedFontValue()
    195         || a.m_typeface == hashTableDeletedFontValue()
    196         || !m_typeface
    197         || !a.m_typeface)
    198         typefacesEqual = m_typeface == a.m_typeface;
    199     else
    200         typefacesEqual = SkTypeface::Equal(m_typeface, a.m_typeface);
    201 
    202     return typefacesEqual
    203         && m_textSize == a.m_textSize
    204         && m_fakeBold == a.m_fakeBold
    205         && m_fakeItalic == a.m_fakeItalic
    206         && m_orientation == a.m_orientation
    207         && m_textOrientation == a.m_textOrientation
    208         && m_style == a.m_style;
    209 }
    210 
    211 unsigned FontPlatformData::hash() const
    212 {
    213     unsigned h = SkTypeface::UniqueID(m_typeface);
    214     h ^= 0x01010101 * ((static_cast<int>(m_textOrientation) << 3) | (static_cast<int>(m_orientation) << 2) | (static_cast<int>(m_fakeBold) << 1) | static_cast<int>(m_fakeItalic));
    215 
    216     // This memcpy is to avoid a reinterpret_cast that breaks strict-aliasing
    217     // rules. Memcpy is generally optimized enough so that performance doesn't
    218     // matter here.
    219     uint32_t textSizeBytes;
    220     memcpy(&textSizeBytes, &m_textSize, sizeof(uint32_t));
    221     h ^= textSizeBytes;
    222 
    223     return h;
    224 }
    225 
    226 bool FontPlatformData::isFixedPitch() const
    227 {
    228     notImplemented();
    229     return false;
    230 }
    231 
    232 HB_FaceRec_* FontPlatformData::harfbuzzFace() const
    233 {
    234     if (!m_harfbuzzFace)
    235         m_harfbuzzFace = RefCountedHarfbuzzFace::create(HB_NewFace(const_cast<FontPlatformData*>(this), harfbuzzSkiaGetTable));
    236 
    237     return m_harfbuzzFace->face();
    238 }
    239 
    240 void FontPlatformData::querySystemForRenderStyle()
    241 {
    242     if (!m_family.length()) {
    243         // We don't have a family for this. Probably because it's a webfont. We
    244         // set all the values to 'no preference' and take the defaults passed
    245         // in from XSETTINGS.
    246         m_style.useBitmaps = FontRenderStyle::NoPreference;
    247         m_style.useAutoHint = FontRenderStyle::NoPreference;
    248         m_style.useHinting = FontRenderStyle::NoPreference;
    249         m_style.useAntiAlias = FontRenderStyle::NoPreference;
    250         m_style.useSubpixel = FontRenderStyle::NoPreference;
    251         return;
    252     }
    253 
    254     PlatformBridge::getRenderStyleForStrike(m_family.data(), (((int)m_textSize) << 2) | (m_typeface->style() & 3), &m_style);
    255 }
    256 
    257 }  // namespace WebCore
    258