gfx/skia/trunk/src/core/SkFontHost.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/core/SkFontHost.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,250 @@
     1.4 +/*
     1.5 + * Copyright 2009 The Android Open Source Project
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkFontLCDConfig.h"
    1.12 +#include "SkOnce.h"
    1.13 +
    1.14 +static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
    1.15 +static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
    1.16 +
    1.17 +SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
    1.18 +    return gLCDOrientation;
    1.19 +}
    1.20 +
    1.21 +void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
    1.22 +    gLCDOrientation = orientation;
    1.23 +}
    1.24 +
    1.25 +SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
    1.26 +    return gLCDOrder;
    1.27 +}
    1.28 +
    1.29 +void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) {
    1.30 +    gLCDOrder = order;
    1.31 +}
    1.32 +
    1.33 +///////////////////////////////////////////////////////////////////////////////
    1.34 +// Legacy wrappers : remove from SkFontHost when webkit switches to new API
    1.35 +
    1.36 +#include "SkFontHost.h"
    1.37 +
    1.38 +SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
    1.39 +    return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
    1.40 +}
    1.41 +
    1.42 +void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
    1.43 +    SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
    1.44 +}
    1.45 +
    1.46 +SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
    1.47 +    return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
    1.48 +}
    1.49 +
    1.50 +void SkFontHost::SetSubpixelOrder(LCDOrder order) {
    1.51 +    SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
    1.52 +}
    1.53 +
    1.54 +///////////////////////////////////////////////////////////////////////////////
    1.55 +///////////////////////////////////////////////////////////////////////////////
    1.56 +
    1.57 +#include "SkFontStyle.h"
    1.58 +
    1.59 +SkFontStyle::SkFontStyle() {
    1.60 +    fUnion.fU32 = 0;
    1.61 +    fUnion.fR.fWeight = kNormal_Weight;
    1.62 +    fUnion.fR.fWidth = kNormal_Width;
    1.63 +    fUnion.fR.fSlant = kUpright_Slant;
    1.64 +}
    1.65 +
    1.66 +SkFontStyle::SkFontStyle(int weight, int width, Slant slant) {
    1.67 +    fUnion.fU32 = 0;
    1.68 +    fUnion.fR.fWeight = SkPin32(weight, kThin_Weight, kBlack_Weight);
    1.69 +    fUnion.fR.fWidth = SkPin32(width, kUltraCondensed_Width, kUltaExpanded_Width);
    1.70 +    fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant);
    1.71 +}
    1.72 +
    1.73 +#include "SkFontMgr.h"
    1.74 +
    1.75 +class SkEmptyFontStyleSet : public SkFontStyleSet {
    1.76 +public:
    1.77 +    virtual int count() SK_OVERRIDE { return 0; }
    1.78 +    virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
    1.79 +        SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
    1.80 +    }
    1.81 +    virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
    1.82 +        SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
    1.83 +        return NULL;
    1.84 +    }
    1.85 +    virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
    1.86 +        return NULL;
    1.87 +    }
    1.88 +};
    1.89 +
    1.90 +SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
    1.91 +    return SkNEW(SkEmptyFontStyleSet);
    1.92 +}
    1.93 +
    1.94 +///////////////////////////////////////////////////////////////////////////////
    1.95 +
    1.96 +class SkEmptyFontMgr : public SkFontMgr {
    1.97 +protected:
    1.98 +    virtual int onCountFamilies() const SK_OVERRIDE {
    1.99 +        return 0;
   1.100 +    }
   1.101 +    virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
   1.102 +        SkDEBUGFAIL("onGetFamilyName called with bad index");
   1.103 +    }
   1.104 +    virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
   1.105 +        SkDEBUGFAIL("onCreateStyleSet called with bad index");
   1.106 +        return NULL;
   1.107 +    }
   1.108 +    virtual SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE {
   1.109 +        return SkFontStyleSet::CreateEmpty();
   1.110 +    }
   1.111 +
   1.112 +    virtual SkTypeface* onMatchFamilyStyle(const char[],
   1.113 +                                           const SkFontStyle&) const SK_OVERRIDE {
   1.114 +        return NULL;
   1.115 +    }
   1.116 +    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
   1.117 +                                         const SkFontStyle&) const SK_OVERRIDE {
   1.118 +        return NULL;
   1.119 +    }
   1.120 +    virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
   1.121 +        return NULL;
   1.122 +    }
   1.123 +    virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
   1.124 +        return NULL;
   1.125 +    }
   1.126 +    virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
   1.127 +        return NULL;
   1.128 +    }
   1.129 +    virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
   1.130 +        return NULL;
   1.131 +    }
   1.132 +};
   1.133 +
   1.134 +static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
   1.135 +    if (NULL == fsset) {
   1.136 +        fsset = SkFontStyleSet::CreateEmpty();
   1.137 +    }
   1.138 +    return fsset;
   1.139 +}
   1.140 +
   1.141 +int SkFontMgr::countFamilies() const {
   1.142 +    return this->onCountFamilies();
   1.143 +}
   1.144 +
   1.145 +void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
   1.146 +    this->onGetFamilyName(index, familyName);
   1.147 +}
   1.148 +
   1.149 +SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
   1.150 +    return emptyOnNull(this->onCreateStyleSet(index));
   1.151 +}
   1.152 +
   1.153 +SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
   1.154 +    return emptyOnNull(this->onMatchFamily(familyName));
   1.155 +}
   1.156 +
   1.157 +SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
   1.158 +                                        const SkFontStyle& fs) const {
   1.159 +    return this->onMatchFamilyStyle(familyName, fs);
   1.160 +}
   1.161 +
   1.162 +SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
   1.163 +                                      const SkFontStyle& fs) const {
   1.164 +    return this->onMatchFaceStyle(face, fs);
   1.165 +}
   1.166 +
   1.167 +SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
   1.168 +    if (NULL == data) {
   1.169 +        return NULL;
   1.170 +    }
   1.171 +    return this->onCreateFromData(data, ttcIndex);
   1.172 +}
   1.173 +
   1.174 +SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
   1.175 +    if (NULL == stream) {
   1.176 +        return NULL;
   1.177 +    }
   1.178 +    return this->onCreateFromStream(stream, ttcIndex);
   1.179 +}
   1.180 +
   1.181 +SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
   1.182 +    if (NULL == path) {
   1.183 +        return NULL;
   1.184 +    }
   1.185 +    return this->onCreateFromFile(path, ttcIndex);
   1.186 +}
   1.187 +
   1.188 +SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
   1.189 +                                            unsigned styleBits) const {
   1.190 +    return this->onLegacyCreateTypeface(familyName, styleBits);
   1.191 +}
   1.192 +
   1.193 +void set_up_default(SkFontMgr** singleton) {
   1.194 +  *singleton = SkFontMgr::Factory();
   1.195 +  // we never want to return NULL
   1.196 +  if (NULL == *singleton) {
   1.197 +      *singleton = SkNEW(SkEmptyFontMgr);
   1.198 +  }
   1.199 +}
   1.200 +
   1.201 +SkFontMgr* SkFontMgr::RefDefault() {
   1.202 +    static SkFontMgr* gFM = NULL;
   1.203 +    SK_DECLARE_STATIC_ONCE(once);
   1.204 +    SkOnce(&once, set_up_default, &gFM);
   1.205 +    return SkRef(gFM);
   1.206 +}
   1.207 +
   1.208 +//////////////////////////////////////////////////////////////////////////
   1.209 +
   1.210 +#ifndef SK_FONTHOST_DOES_NOT_USE_FONTMGR
   1.211 +
   1.212 +#if 0
   1.213 +static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) {
   1.214 +    SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ?
   1.215 +                                     SkFontStyle::kBold_Weight :
   1.216 +                                     SkFontStyle::kNormal_Weight;
   1.217 +    SkFontStyle::Width width = SkFontStyle::kNormal_Width;
   1.218 +    SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ?
   1.219 +                                     SkFontStyle::kUpright_Slant :
   1.220 +                                     SkFontStyle::kItalic_Slant;
   1.221 +    return SkFontStyle(weight, width, slant);
   1.222 +}
   1.223 +#endif
   1.224 +
   1.225 +SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
   1.226 +                                       const char familyName[],
   1.227 +                                       SkTypeface::Style style) {
   1.228 +    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
   1.229 +    if (familyFace) {
   1.230 +        bool bold = style & SkTypeface::kBold;
   1.231 +        bool italic = style & SkTypeface::kItalic;
   1.232 +        SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight
   1.233 +                                                : SkFontStyle::kNormal_Weight,
   1.234 +                                           SkFontStyle::kNormal_Width,
   1.235 +                                           italic ? SkFontStyle::kItalic_Slant
   1.236 +                                                  : SkFontStyle::kUpright_Slant);
   1.237 +        return fm->matchFaceStyle(familyFace, newStyle);
   1.238 +    } else {
   1.239 +        return fm->legacyCreateTypeface(familyName, style);
   1.240 +    }
   1.241 +}
   1.242 +
   1.243 +SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
   1.244 +    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
   1.245 +    return fm->createFromFile(path);
   1.246 +}
   1.247 +
   1.248 +SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
   1.249 +    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
   1.250 +    return fm->createFromStream(stream);
   1.251 +}
   1.252 +
   1.253 +#endif

mercurial