Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkFontHost.cpp
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
11 static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
12 static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
13
14 SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
15     return gLCDOrientation;
16 }
17
18 void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
19     gLCDOrientation = orientation;
20 }
21
22 SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
23     return gLCDOrder;
24 }
25
26 void 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
35 SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
36     return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
37 }
38
39 void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
40     SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
41 }
42
43 SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
44     return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
45 }
46
47 void SkFontHost::SetSubpixelOrder(LCDOrder order) {
48     SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////////
53
54 #include "SkFontStyle.h"
55
56 SkFontStyle::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
63 SkFontStyle::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
72 class SkEmptyFontStyleSet : public SkFontStyleSet {
73 public:
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
87 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
88     return SkNEW(SkEmptyFontStyleSet);
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 class SkEmptyFontMgr : public SkFontMgr {
94 protected:
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     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
114                                                     const SkFontStyle& style,
115                                                     const char bpc47[],
116                                                     uint32_t character) const SK_OVERRIDE {
117         return NULL;
118     }
119     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
120                                          const SkFontStyle&) const SK_OVERRIDE {
121         return NULL;
122     }
123     virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
124         return NULL;
125     }
126     virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
127         return NULL;
128     }
129     virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
130         return NULL;
131     }
132     virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
133         return NULL;
134     }
135 };
136
137 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
138     if (NULL == fsset) {
139         fsset = SkFontStyleSet::CreateEmpty();
140     }
141     return fsset;
142 }
143
144 int SkFontMgr::countFamilies() const {
145     return this->onCountFamilies();
146 }
147
148 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
149     this->onGetFamilyName(index, familyName);
150 }
151
152 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
153     return emptyOnNull(this->onCreateStyleSet(index));
154 }
155
156 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
157     return emptyOnNull(this->onMatchFamily(familyName));
158 }
159
160 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
161                                         const SkFontStyle& fs) const {
162     return this->onMatchFamilyStyle(familyName, fs);
163 }
164
165 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
166                                                  const char bpc47[], uint32_t character) const {
167     return this->onMatchFamilyStyleCharacter(familyName, style, bpc47, character);
168 }
169
170 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
171                                       const SkFontStyle& fs) const {
172     return this->onMatchFaceStyle(face, fs);
173 }
174
175 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
176     if (NULL == data) {
177         return NULL;
178     }
179     return this->onCreateFromData(data, ttcIndex);
180 }
181
182 SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
183     if (NULL == stream) {
184         return NULL;
185     }
186     return this->onCreateFromStream(stream, ttcIndex);
187 }
188
189 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
190     if (NULL == path) {
191         return NULL;
192     }
193     return this->onCreateFromFile(path, ttcIndex);
194 }
195
196 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
197                                             unsigned styleBits) const {
198     return this->onLegacyCreateTypeface(familyName, styleBits);
199 }
200
201 SkFontMgr* SkFontMgr::CreateDefault() {
202     SkFontMgr* fm = SkFontMgr::Factory();
203     return fm ? fm : SkNEW(SkEmptyFontMgr);
204 }
205
206 SkFontMgr* SkFontMgr::RefDefault() {
207     SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, CreateDefault);
208     return SkRef(singleton.get());
209 }