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
70#include "SkFontMgr.h"
71
72class SkEmptyFontStyleSet : public SkFontStyleSet {
73public:
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
87SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
88    return SkNEW(SkEmptyFontStyleSet);
89}
90
91///////////////////////////////////////////////////////////////////////////////
92
93class SkEmptyFontMgr : public SkFontMgr {
94protected:
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#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
114    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
115                                                    const SkFontStyle& style,
116                                                    const char* bcp47[],
117                                                    int bcp47Count,
118                                                    SkUnichar character) const SK_OVERRIDE {
119#else
120    virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
121                                                    const SkFontStyle& style,
122                                                    const char bcp47[],
123                                                    SkUnichar character) const SK_OVERRIDE {
124#endif
125        return NULL;
126    }
127    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
128                                         const SkFontStyle&) const SK_OVERRIDE {
129        return NULL;
130    }
131    virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
132        return NULL;
133    }
134    virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
135        return NULL;
136    }
137    virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
138        return NULL;
139    }
140    virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
141        return NULL;
142    }
143};
144
145static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
146    if (NULL == fsset) {
147        fsset = SkFontStyleSet::CreateEmpty();
148    }
149    return fsset;
150}
151
152int SkFontMgr::countFamilies() const {
153    return this->onCountFamilies();
154}
155
156void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
157    this->onGetFamilyName(index, familyName);
158}
159
160SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
161    return emptyOnNull(this->onCreateStyleSet(index));
162}
163
164SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
165    return emptyOnNull(this->onMatchFamily(familyName));
166}
167
168SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
169                                        const SkFontStyle& fs) const {
170    return this->onMatchFamilyStyle(familyName, fs);
171}
172
173#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
174SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
175                                                 const char* bcp47[], int bcp47Count,
176                                                 SkUnichar character) const {
177    return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
178}
179#else
180SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
181                                                 const char bcp47[], SkUnichar character) const {
182    return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, character);
183}
184#endif
185
186SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
187                                      const SkFontStyle& fs) const {
188    return this->onMatchFaceStyle(face, fs);
189}
190
191SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
192    if (NULL == data) {
193        return NULL;
194    }
195    return this->onCreateFromData(data, ttcIndex);
196}
197
198SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
199    if (NULL == stream) {
200        return NULL;
201    }
202    return this->onCreateFromStream(stream, ttcIndex);
203}
204
205SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
206    if (NULL == path) {
207        return NULL;
208    }
209    return this->onCreateFromFile(path, ttcIndex);
210}
211
212SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
213                                            unsigned styleBits) const {
214    return this->onLegacyCreateTypeface(familyName, styleBits);
215}
216
217SkFontMgr* SkFontMgr::CreateDefault() {
218    SkFontMgr* fm = SkFontMgr::Factory();
219    return fm ? fm : SkNEW(SkEmptyFontMgr);
220}
221
222SkFontMgr* SkFontMgr::RefDefault() {
223    SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, CreateDefault);
224    return SkRef(singleton.get());
225}
226