Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright 2009 The Android Open Source Project
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkFontLCDConfig.h"
      9 #include "SkLazyPtr.h"
     10 
     11 static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
     12 static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
     13 
     14 SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
     15     return gLCDOrientation;
     16 }
     17 
     18 void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
     19     gLCDOrientation = orientation;
     20 }
     21 
     22 SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
     23     return gLCDOrder;
     24 }
     25 
     26 void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) {
     27     gLCDOrder = order;
     28 }
     29 
     30 ///////////////////////////////////////////////////////////////////////////////
     31 // Legacy wrappers : remove from SkFontHost when webkit switches to new API
     32 
     33 #include "SkFontHost.h"
     34 
     35 SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
     36     return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
     37 }
     38 
     39 void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
     40     SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
     41 }
     42 
     43 SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
     44     return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
     45 }
     46 
     47 void SkFontHost::SetSubpixelOrder(LCDOrder order) {
     48     SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
     49 }
     50 
     51 ///////////////////////////////////////////////////////////////////////////////
     52 ///////////////////////////////////////////////////////////////////////////////
     53 
     54 #include "SkFontStyle.h"
     55 
     56 SkFontStyle::SkFontStyle() {
     57     fUnion.fU32 = 0;
     58     fUnion.fR.fWeight = kNormal_Weight;
     59     fUnion.fR.fWidth = kNormal_Width;
     60     fUnion.fR.fSlant = kUpright_Slant;
     61 }
     62 
     63 SkFontStyle::SkFontStyle(int weight, int width, Slant slant) {
     64     fUnion.fU32 = 0;
     65     fUnion.fR.fWeight = SkPin32(weight, kThin_Weight, kBlack_Weight);
     66     fUnion.fR.fWidth = SkPin32(width, kUltraCondensed_Width, kUltaExpanded_Width);
     67     fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant);
     68 }
     69 
     70 #include "SkFontMgr.h"
     71 
     72 class SkEmptyFontStyleSet : public SkFontStyleSet {
     73 public:
     74     virtual int count() SK_OVERRIDE { return 0; }
     75     virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
     76         SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
     77     }
     78     virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
     79         SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
     80         return NULL;
     81     }
     82     virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
     83         return NULL;
     84     }
     85 };
     86 
     87 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
     88     return SkNEW(SkEmptyFontStyleSet);
     89 }
     90 
     91 ///////////////////////////////////////////////////////////////////////////////
     92 
     93 class SkEmptyFontMgr : public SkFontMgr {
     94 protected:
     95     virtual int onCountFamilies() const SK_OVERRIDE {
     96         return 0;
     97     }
     98     virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
     99         SkDEBUGFAIL("onGetFamilyName called with bad index");
    100     }
    101     virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
    102         SkDEBUGFAIL("onCreateStyleSet called with bad index");
    103         return NULL;
    104     }
    105     virtual SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE {
    106         return SkFontStyleSet::CreateEmpty();
    107     }
    108 
    109     virtual SkTypeface* onMatchFamilyStyle(const char[],
    110                                            const SkFontStyle&) const SK_OVERRIDE {
    111         return NULL;
    112     }
    113     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
    114                                                     const SkFontStyle& style,
    115                                                     const char bpc47[],
    116                                                     uint32_t character) const SK_OVERRIDE {
    117         return NULL;
    118     }
    119     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
    120                                          const SkFontStyle&) const SK_OVERRIDE {
    121         return NULL;
    122     }
    123     virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
    124         return NULL;
    125     }
    126     virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
    127         return NULL;
    128     }
    129     virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
    130         return NULL;
    131     }
    132     virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
    133         return NULL;
    134     }
    135 };
    136 
    137 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
    138     if (NULL == fsset) {
    139         fsset = SkFontStyleSet::CreateEmpty();
    140     }
    141     return fsset;
    142 }
    143 
    144 int SkFontMgr::countFamilies() const {
    145     return this->onCountFamilies();
    146 }
    147 
    148 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
    149     this->onGetFamilyName(index, familyName);
    150 }
    151 
    152 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
    153     return emptyOnNull(this->onCreateStyleSet(index));
    154 }
    155 
    156 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
    157     return emptyOnNull(this->onMatchFamily(familyName));
    158 }
    159 
    160 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
    161                                         const SkFontStyle& fs) const {
    162     return this->onMatchFamilyStyle(familyName, fs);
    163 }
    164 
    165 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
    166                                                  const char bpc47[], uint32_t character) const {
    167     return this->onMatchFamilyStyleCharacter(familyName, style, bpc47, character);
    168 }
    169 
    170 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
    171                                       const SkFontStyle& fs) const {
    172     return this->onMatchFaceStyle(face, fs);
    173 }
    174 
    175 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
    176     if (NULL == data) {
    177         return NULL;
    178     }
    179     return this->onCreateFromData(data, ttcIndex);
    180 }
    181 
    182 SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
    183     if (NULL == stream) {
    184         return NULL;
    185     }
    186     return this->onCreateFromStream(stream, ttcIndex);
    187 }
    188 
    189 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
    190     if (NULL == path) {
    191         return NULL;
    192     }
    193     return this->onCreateFromFile(path, ttcIndex);
    194 }
    195 
    196 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
    197                                             unsigned styleBits) const {
    198     return this->onLegacyCreateTypeface(familyName, styleBits);
    199 }
    200 
    201 SkFontMgr* SkFontMgr::CreateDefault() {
    202     SkFontMgr* fm = SkFontMgr::Factory();
    203     return fm ? fm : SkNEW(SkEmptyFontMgr);
    204 }
    205 
    206 SkFontMgr* SkFontMgr::RefDefault() {
    207     SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, CreateDefault);
    208     return SkRef(singleton.get());
    209 }
    210 
    211 //////////////////////////////////////////////////////////////////////////
    212 
    213 #ifndef SK_FONTHOST_DOES_NOT_USE_FONTMGR
    214 
    215 #if 0
    216 static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) {
    217     SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ?
    218                                      SkFontStyle::kBold_Weight :
    219                                      SkFontStyle::kNormal_Weight;
    220     SkFontStyle::Width width = SkFontStyle::kNormal_Width;
    221     SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ?
    222                                      SkFontStyle::kUpright_Slant :
    223                                      SkFontStyle::kItalic_Slant;
    224     return SkFontStyle(weight, width, slant);
    225 }
    226 #endif
    227 
    228 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
    229                                        const char familyName[],
    230                                        SkTypeface::Style style) {
    231     SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
    232     if (familyFace) {
    233         bool bold = style & SkTypeface::kBold;
    234         bool italic = style & SkTypeface::kItalic;
    235         SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight
    236                                                 : SkFontStyle::kNormal_Weight,
    237                                            SkFontStyle::kNormal_Width,
    238                                            italic ? SkFontStyle::kItalic_Slant
    239                                                   : SkFontStyle::kUpright_Slant);
    240         return fm->matchFaceStyle(familyFace, newStyle);
    241     } else {
    242         return fm->legacyCreateTypeface(familyName, style);
    243     }
    244 }
    245 
    246 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
    247     SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
    248     return fm->createFromFile(path);
    249 }
    250 
    251 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
    252     SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
    253     return fm->createFromStream(stream);
    254 }
    255 
    256 #endif
    257