SkFontHost.cpp revision 72c9faab45124e08c85f70ca38536914862d947c
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
11static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
12static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
13
14SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
15    return gLCDOrientation;
16}
17
18void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
19    gLCDOrientation = orientation;
20}
21
22SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
23    return gLCDOrder;
24}
25
26void 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
35SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
36    return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
37}
38
39void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
40    SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
41}
42
43SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
44    return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
45}
46
47void SkFontHost::SetSubpixelOrder(LCDOrder order) {
48    SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
49}
50
51///////////////////////////////////////////////////////////////////////////////
52///////////////////////////////////////////////////////////////////////////////
53
54#include "SkFontStyle.h"
55
56SkFontStyle::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
63SkFontStyle::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
70SkFontStyle::SkFontStyle(unsigned oldStyle) {
71    fUnion.fU32 = 0;
72    fUnion.fR.fWeight = (oldStyle & SkTypeface::kBold) ? SkFontStyle::kBold_Weight
73                                                       : SkFontStyle::kNormal_Weight;
74    fUnion.fR.fWidth = SkFontStyle::kNormal_Width;
75    fUnion.fR.fSlant = (oldStyle & SkTypeface::kItalic) ? SkFontStyle::kItalic_Slant
76                                                        : SkFontStyle::kUpright_Slant;
77}
78
79#include "SkFontMgr.h"
80
81class SkEmptyFontStyleSet : public SkFontStyleSet {
82public:
83    int count() SK_OVERRIDE { return 0; }
84    void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
85        SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
86    }
87    SkTypeface* createTypeface(int index) SK_OVERRIDE {
88        SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
89        return NULL;
90    }
91    SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
92        return NULL;
93    }
94};
95
96SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
97    return SkNEW(SkEmptyFontStyleSet);
98}
99
100///////////////////////////////////////////////////////////////////////////////
101
102class SkEmptyFontMgr : public SkFontMgr {
103protected:
104    int onCountFamilies() const SK_OVERRIDE {
105        return 0;
106    }
107    void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
108        SkDEBUGFAIL("onGetFamilyName called with bad index");
109    }
110    SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
111        SkDEBUGFAIL("onCreateStyleSet called with bad index");
112        return NULL;
113    }
114    SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE {
115        return SkFontStyleSet::CreateEmpty();
116    }
117
118    virtual SkTypeface* onMatchFamilyStyle(const char[],
119                                           const SkFontStyle&) const SK_OVERRIDE {
120        return NULL;
121    }
122    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
123                                                    const SkFontStyle& style,
124                                                    const char* bcp47[],
125                                                    int bcp47Count,
126                                                    SkUnichar character) const SK_OVERRIDE {
127        return NULL;
128    }
129    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
130                                         const SkFontStyle&) const SK_OVERRIDE {
131        return NULL;
132    }
133    SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
134        return NULL;
135    }
136    SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
137        return NULL;
138    }
139    SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
140        return NULL;
141    }
142    SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
143        return NULL;
144    }
145};
146
147static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
148    if (NULL == fsset) {
149        fsset = SkFontStyleSet::CreateEmpty();
150    }
151    return fsset;
152}
153
154int SkFontMgr::countFamilies() const {
155    return this->onCountFamilies();
156}
157
158void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
159    this->onGetFamilyName(index, familyName);
160}
161
162SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
163    return emptyOnNull(this->onCreateStyleSet(index));
164}
165
166SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
167    return emptyOnNull(this->onMatchFamily(familyName));
168}
169
170SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
171                                        const SkFontStyle& fs) const {
172    return this->onMatchFamilyStyle(familyName, fs);
173}
174
175SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
176                                                 const char* bcp47[], int bcp47Count,
177                                                 SkUnichar character) const {
178    return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
179}
180
181SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
182                                      const SkFontStyle& fs) const {
183    return this->onMatchFaceStyle(face, fs);
184}
185
186SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
187    if (NULL == data) {
188        return NULL;
189    }
190    return this->onCreateFromData(data, ttcIndex);
191}
192
193SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
194    if (NULL == stream) {
195        return NULL;
196    }
197    return this->onCreateFromStream(stream, ttcIndex);
198}
199
200SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
201    if (NULL == path) {
202        return NULL;
203    }
204    return this->onCreateFromFile(path, ttcIndex);
205}
206
207SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
208                                            unsigned styleBits) const {
209    return this->onLegacyCreateTypeface(familyName, styleBits);
210}
211
212// As a template argument this must have external linkage.
213SkFontMgr* sk_fontmgr_create_default() {
214    SkFontMgr* fm = SkFontMgr::Factory();
215    return fm ? fm : SkNEW(SkEmptyFontMgr);
216}
217
218SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default);
219
220SkFontMgr* SkFontMgr::RefDefault() {
221    return SkRef(singleton.get());
222}
223