SkFontHost.cpp revision ef284a84f503adfd08ee52b5aee142c548698ea4
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
10static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
11static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
12
13SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
14    return gLCDOrientation;
15}
16
17void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
18    gLCDOrientation = orientation;
19}
20
21SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
22    return gLCDOrder;
23}
24
25void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) {
26    gLCDOrder = order;
27}
28
29///////////////////////////////////////////////////////////////////////////////
30// Legacy wrappers : remove from SkFontHost when webkit switches to new API
31
32#include "SkFontHost.h"
33
34SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
35    return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
36}
37
38void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
39    SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
40}
41
42SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
43    return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
44}
45
46void SkFontHost::SetSubpixelOrder(LCDOrder order) {
47    SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
48}
49
50///////////////////////////////////////////////////////////////////////////////
51///////////////////////////////////////////////////////////////////////////////
52
53#include "SkFontStyle.h"
54
55SkFontStyle::SkFontStyle() {
56    fUnion.fU32 = 0;
57    fUnion.fR.fWeight = kNormal_Weight;
58    fUnion.fR.fWidth = kNormal_Width;
59    fUnion.fR.fSlant = kUpright_Slant;
60}
61
62SkFontStyle::SkFontStyle(int weight, int width, Slant slant) {
63    fUnion.fU32 = 0;
64    fUnion.fR.fWeight = SkPin32(weight, kThin_Weight, kBlack_Weight);
65    fUnion.fR.fWidth = SkPin32(width, kUltraCondensed_Width, kUltaExpanded_Width);
66    fUnion.fR.fSlant = SkPin32(slant, kUpright_Slant, kItalic_Slant);
67}
68
69#include "SkFontMgr.h"
70
71
72SK_DEFINE_INST_COUNT(SkFontStyleSet)
73
74class SkEmptyFontStyleSet : public SkFontStyleSet {
75public:
76    virtual int count() SK_OVERRIDE { return 0; }
77    virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
78        SkASSERT(!"SkFontStyleSet::getStyle called on empty set");
79    }
80    virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
81        SkASSERT(!"SkFontStyleSet::createTypeface called on empty set");
82        return NULL;
83    }
84    virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
85        return NULL;
86    }
87};
88
89SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
90    return SkNEW(SkEmptyFontStyleSet);
91}
92
93///////////////////////////////////////////////////////////////////////////////
94
95SK_DEFINE_INST_COUNT(SkFontMgr)
96
97class SkEmptyFontMgr : public SkFontMgr {
98protected:
99    virtual int onCountFamilies() SK_OVERRIDE {
100        return 0;
101    }
102    virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
103        SkASSERT(!"onGetFamilyName called with bad index");
104    }
105    virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE {
106        SkASSERT(!"onCreateStyleSet called with bad index");
107        return NULL;
108    }
109    virtual SkFontStyleSet* onMatchFamily(const char[]) SK_OVERRIDE {
110        return SkFontStyleSet::CreateEmpty();
111    }
112
113    virtual SkTypeface* onMatchFamilyStyle(const char[],
114                                           const SkFontStyle&) SK_OVERRIDE {
115        return NULL;
116    }
117    virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
118                                         const SkFontStyle&) SK_OVERRIDE {
119        return NULL;
120    }
121    virtual SkTypeface* onCreateFromData(SkData*, int) SK_OVERRIDE {
122        return NULL;
123    }
124    virtual SkTypeface* onCreateFromStream(SkStream*, int) SK_OVERRIDE {
125        return NULL;
126    }
127    virtual SkTypeface* onCreateFromFile(const char[], int) SK_OVERRIDE {
128        return NULL;
129    }
130};
131
132static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
133    if (NULL == fsset) {
134        fsset = SkFontStyleSet::CreateEmpty();
135    }
136    return fsset;
137}
138
139int SkFontMgr::countFamilies() {
140    return this->onCountFamilies();
141}
142
143void SkFontMgr::getFamilyName(int index, SkString* familyName) {
144    this->onGetFamilyName(index, familyName);
145}
146
147SkFontStyleSet* SkFontMgr::createStyleSet(int index) {
148    return emptyOnNull(this->onCreateStyleSet(index));
149}
150
151SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) {
152    return emptyOnNull(this->onMatchFamily(familyName));
153}
154
155SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
156                                        const SkFontStyle& fs) {
157    return this->onMatchFamilyStyle(familyName, fs);
158}
159
160SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
161                                      const SkFontStyle& fs) {
162    return this->onMatchFaceStyle(face, fs);
163}
164
165SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) {
166    return this->onCreateFromData(data, ttcIndex);
167}
168
169SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) {
170    return this->onCreateFromStream(stream, ttcIndex);
171}
172
173SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) {
174    return this->onCreateFromFile(path, ttcIndex);
175}
176
177SkFontMgr* SkFontMgr::RefDefault() {
178    static SkFontMgr* gFM;
179    if (NULL == gFM) {
180        gFM = SkFontMgr::Factory();
181        // we never want to return NULL
182        if (NULL == gFM) {
183            gFM = SkNEW(SkEmptyFontMgr);
184        }
185    }
186    return SkRef(gFM);
187}
188