michael@0: /* michael@0: * Copyright 2009 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkFontLCDConfig.h" michael@0: #include "SkOnce.h" michael@0: michael@0: static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation; michael@0: static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder; michael@0: michael@0: SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() { michael@0: return gLCDOrientation; michael@0: } michael@0: michael@0: void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) { michael@0: gLCDOrientation = orientation; michael@0: } michael@0: michael@0: SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() { michael@0: return gLCDOrder; michael@0: } michael@0: michael@0: void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) { michael@0: gLCDOrder = order; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: // Legacy wrappers : remove from SkFontHost when webkit switches to new API michael@0: michael@0: #include "SkFontHost.h" michael@0: michael@0: SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() { michael@0: return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation(); michael@0: } michael@0: michael@0: void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) { michael@0: SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation); michael@0: } michael@0: michael@0: SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() { michael@0: return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder(); michael@0: } michael@0: michael@0: void SkFontHost::SetSubpixelOrder(LCDOrder order) { michael@0: SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkFontStyle.h" michael@0: michael@0: SkFontStyle::SkFontStyle() { michael@0: fUnion.fU32 = 0; michael@0: fUnion.fR.fWeight = kNormal_Weight; michael@0: fUnion.fR.fWidth = kNormal_Width; michael@0: fUnion.fR.fSlant = kUpright_Slant; michael@0: } michael@0: michael@0: SkFontStyle::SkFontStyle(int weight, int width, Slant slant) { michael@0: fUnion.fU32 = 0; michael@0: fUnion.fR.fWeight = SkPin32(weight, kThin_Weight, kBlack_Weight); michael@0: fUnion.fR.fWidth = SkPin32(width, kUltraCondensed_Width, kUltaExpanded_Width); michael@0: fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant); michael@0: } michael@0: michael@0: #include "SkFontMgr.h" michael@0: michael@0: class SkEmptyFontStyleSet : public SkFontStyleSet { michael@0: public: michael@0: virtual int count() SK_OVERRIDE { return 0; } michael@0: virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE { michael@0: SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set"); michael@0: } michael@0: virtual SkTypeface* createTypeface(int index) SK_OVERRIDE { michael@0: SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set"); michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: }; michael@0: michael@0: SkFontStyleSet* SkFontStyleSet::CreateEmpty() { michael@0: return SkNEW(SkEmptyFontStyleSet); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: class SkEmptyFontMgr : public SkFontMgr { michael@0: protected: michael@0: virtual int onCountFamilies() const SK_OVERRIDE { michael@0: return 0; michael@0: } michael@0: virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { michael@0: SkDEBUGFAIL("onGetFamilyName called with bad index"); michael@0: } michael@0: virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE { michael@0: SkDEBUGFAIL("onCreateStyleSet called with bad index"); michael@0: return NULL; michael@0: } michael@0: virtual SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE { michael@0: return SkFontStyleSet::CreateEmpty(); michael@0: } michael@0: michael@0: virtual SkTypeface* onMatchFamilyStyle(const char[], michael@0: const SkFontStyle&) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, michael@0: const SkFontStyle&) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE { michael@0: return NULL; michael@0: } michael@0: }; michael@0: michael@0: static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) { michael@0: if (NULL == fsset) { michael@0: fsset = SkFontStyleSet::CreateEmpty(); michael@0: } michael@0: return fsset; michael@0: } michael@0: michael@0: int SkFontMgr::countFamilies() const { michael@0: return this->onCountFamilies(); michael@0: } michael@0: michael@0: void SkFontMgr::getFamilyName(int index, SkString* familyName) const { michael@0: this->onGetFamilyName(index, familyName); michael@0: } michael@0: michael@0: SkFontStyleSet* SkFontMgr::createStyleSet(int index) const { michael@0: return emptyOnNull(this->onCreateStyleSet(index)); michael@0: } michael@0: michael@0: SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const { michael@0: return emptyOnNull(this->onMatchFamily(familyName)); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[], michael@0: const SkFontStyle& fs) const { michael@0: return this->onMatchFamilyStyle(familyName, fs); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face, michael@0: const SkFontStyle& fs) const { michael@0: return this->onMatchFaceStyle(face, fs); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const { michael@0: if (NULL == data) { michael@0: return NULL; michael@0: } michael@0: return this->onCreateFromData(data, ttcIndex); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const { michael@0: if (NULL == stream) { michael@0: return NULL; michael@0: } michael@0: return this->onCreateFromStream(stream, ttcIndex); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const { michael@0: if (NULL == path) { michael@0: return NULL; michael@0: } michael@0: return this->onCreateFromFile(path, ttcIndex); michael@0: } michael@0: michael@0: SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], michael@0: unsigned styleBits) const { michael@0: return this->onLegacyCreateTypeface(familyName, styleBits); michael@0: } michael@0: michael@0: void set_up_default(SkFontMgr** singleton) { michael@0: *singleton = SkFontMgr::Factory(); michael@0: // we never want to return NULL michael@0: if (NULL == *singleton) { michael@0: *singleton = SkNEW(SkEmptyFontMgr); michael@0: } michael@0: } michael@0: michael@0: SkFontMgr* SkFontMgr::RefDefault() { michael@0: static SkFontMgr* gFM = NULL; michael@0: SK_DECLARE_STATIC_ONCE(once); michael@0: SkOnce(&once, set_up_default, &gFM); michael@0: return SkRef(gFM); michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #ifndef SK_FONTHOST_DOES_NOT_USE_FONTMGR michael@0: michael@0: #if 0 michael@0: static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) { michael@0: SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ? michael@0: SkFontStyle::kBold_Weight : michael@0: SkFontStyle::kNormal_Weight; michael@0: SkFontStyle::Width width = SkFontStyle::kNormal_Width; michael@0: SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ? michael@0: SkFontStyle::kUpright_Slant : michael@0: SkFontStyle::kItalic_Slant; michael@0: return SkFontStyle(weight, width, slant); michael@0: } michael@0: #endif michael@0: michael@0: SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, michael@0: const char familyName[], michael@0: SkTypeface::Style style) { michael@0: SkAutoTUnref fm(SkFontMgr::RefDefault()); michael@0: if (familyFace) { michael@0: bool bold = style & SkTypeface::kBold; michael@0: bool italic = style & SkTypeface::kItalic; michael@0: SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight michael@0: : SkFontStyle::kNormal_Weight, michael@0: SkFontStyle::kNormal_Width, michael@0: italic ? SkFontStyle::kItalic_Slant michael@0: : SkFontStyle::kUpright_Slant); michael@0: return fm->matchFaceStyle(familyFace, newStyle); michael@0: } else { michael@0: return fm->legacyCreateTypeface(familyName, style); michael@0: } michael@0: } michael@0: michael@0: SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { michael@0: SkAutoTUnref fm(SkFontMgr::RefDefault()); michael@0: return fm->createFromFile(path); michael@0: } michael@0: michael@0: SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { michael@0: SkAutoTUnref fm(SkFontMgr::RefDefault()); michael@0: return fm->createFromStream(stream); michael@0: } michael@0: michael@0: #endif