2 * Copyright 2009 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkFontLCDConfig.h"
11 static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
12 static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
14 SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
15 return gLCDOrientation;
18 void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
19 gLCDOrientation = orientation;
22 SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
26 void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) {
30 ///////////////////////////////////////////////////////////////////////////////
31 // Legacy wrappers : remove from SkFontHost when webkit switches to new API
33 #include "SkFontHost.h"
35 SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
36 return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
39 void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
40 SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
43 SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
44 return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
47 void SkFontHost::SetSubpixelOrder(LCDOrder order) {
48 SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
51 ///////////////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////////
54 #include "SkFontStyle.h"
56 SkFontStyle::SkFontStyle() {
58 fUnion.fR.fWeight = kNormal_Weight;
59 fUnion.fR.fWidth = kNormal_Width;
60 fUnion.fR.fSlant = kUpright_Slant;
63 SkFontStyle::SkFontStyle(int weight, int width, Slant slant) {
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);
70 #include "SkFontMgr.h"
72 class SkEmptyFontStyleSet : public SkFontStyleSet {
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");
78 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
79 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
82 virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
87 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
88 return SkNEW(SkEmptyFontStyleSet);
91 ///////////////////////////////////////////////////////////////////////////////
93 class SkEmptyFontMgr : public SkFontMgr {
95 virtual int onCountFamilies() const SK_OVERRIDE {
98 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
99 SkDEBUGFAIL("onGetFamilyName called with bad index");
101 virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
102 SkDEBUGFAIL("onCreateStyleSet called with bad index");
105 virtual SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE {
106 return SkFontStyleSet::CreateEmpty();
109 virtual SkTypeface* onMatchFamilyStyle(const char[],
110 const SkFontStyle&) const SK_OVERRIDE {
113 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
114 const SkFontStyle&) const SK_OVERRIDE {
117 virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
120 virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
123 virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
126 virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
131 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
133 fsset = SkFontStyleSet::CreateEmpty();
138 int SkFontMgr::countFamilies() const {
139 return this->onCountFamilies();
142 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
143 this->onGetFamilyName(index, familyName);
146 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
147 return emptyOnNull(this->onCreateStyleSet(index));
150 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
151 return emptyOnNull(this->onMatchFamily(familyName));
154 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
155 const SkFontStyle& fs) const {
156 return this->onMatchFamilyStyle(familyName, fs);
159 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
160 const SkFontStyle& fs) const {
161 return this->onMatchFaceStyle(face, fs);
164 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
168 return this->onCreateFromData(data, ttcIndex);
171 SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
172 if (NULL == stream) {
175 return this->onCreateFromStream(stream, ttcIndex);
178 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
182 return this->onCreateFromFile(path, ttcIndex);
185 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
186 unsigned styleBits) const {
187 return this->onLegacyCreateTypeface(familyName, styleBits);
190 void set_up_default(SkFontMgr** singleton) {
191 *singleton = SkFontMgr::Factory();
192 // we never want to return NULL
193 if (NULL == *singleton) {
194 *singleton = SkNEW(SkEmptyFontMgr);
198 SkFontMgr* SkFontMgr::RefDefault() {
199 static SkFontMgr* gFM = NULL;
200 SK_DECLARE_STATIC_ONCE(once);
201 SkOnce(&once, set_up_default, &gFM);
205 //////////////////////////////////////////////////////////////////////////
207 #ifndef SK_FONTHOST_DOES_NOT_USE_FONTMGR
210 static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) {
211 SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ?
212 SkFontStyle::kBold_Weight :
213 SkFontStyle::kNormal_Weight;
214 SkFontStyle::Width width = SkFontStyle::kNormal_Width;
215 SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ?
216 SkFontStyle::kUpright_Slant :
217 SkFontStyle::kItalic_Slant;
218 return SkFontStyle(weight, width, slant);
222 SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
223 const char familyName[],
224 SkTypeface::Style style) {
225 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
227 bool bold = style & SkTypeface::kBold;
228 bool italic = style & SkTypeface::kItalic;
229 SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight
230 : SkFontStyle::kNormal_Weight,
231 SkFontStyle::kNormal_Width,
232 italic ? SkFontStyle::kItalic_Slant
233 : SkFontStyle::kUpright_Slant);
234 return fm->matchFaceStyle(familyFace, newStyle);
236 return fm->legacyCreateTypeface(familyName, style);
240 SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
241 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
242 return fm->createFromFile(path);
245 SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
246 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
247 return fm->createFromStream(stream);