14cca5a6a58f37132a84a70c0303180c6ddff979
[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 SkFontStyle::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
81 class SkEmptyFontStyleSet : public SkFontStyleSet {
82 public:
83     virtual int count() SK_OVERRIDE { return 0; }
84     virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
85         SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
86     }
87     virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
88         SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
89         return NULL;
90     }
91     virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
92         return NULL;
93     }
94 };
95
96 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
97     return SkNEW(SkEmptyFontStyleSet);
98 }
99
100 ///////////////////////////////////////////////////////////////////////////////
101
102 class SkEmptyFontMgr : public SkFontMgr {
103 protected:
104     virtual int onCountFamilies() const SK_OVERRIDE {
105         return 0;
106     }
107     virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
108         SkDEBUGFAIL("onGetFamilyName called with bad index");
109     }
110     virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
111         SkDEBUGFAIL("onCreateStyleSet called with bad index");
112         return NULL;
113     }
114     virtual 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 #ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
123     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
124                                                     const SkFontStyle& style,
125                                                     const char* bcp47[],
126                                                     int bcp47Count,
127                                                     SkUnichar character) const SK_OVERRIDE {
128 #else
129     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
130                                                     const SkFontStyle& style,
131                                                     const char bcp47[],
132                                                     SkUnichar character) const SK_OVERRIDE {
133 #endif
134         return NULL;
135     }
136     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
137                                          const SkFontStyle&) const SK_OVERRIDE {
138         return NULL;
139     }
140     virtual SkTypeface* onCreateFromData(SkData*, int) const SK_OVERRIDE {
141         return NULL;
142     }
143     virtual SkTypeface* onCreateFromStream(SkStream*, int) const SK_OVERRIDE {
144         return NULL;
145     }
146     virtual SkTypeface* onCreateFromFile(const char[], int) const SK_OVERRIDE {
147         return NULL;
148     }
149     virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const SK_OVERRIDE {
150         return NULL;
151     }
152 };
153
154 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
155     if (NULL == fsset) {
156         fsset = SkFontStyleSet::CreateEmpty();
157     }
158     return fsset;
159 }
160
161 int SkFontMgr::countFamilies() const {
162     return this->onCountFamilies();
163 }
164
165 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
166     this->onGetFamilyName(index, familyName);
167 }
168
169 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
170     return emptyOnNull(this->onCreateStyleSet(index));
171 }
172
173 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
174     return emptyOnNull(this->onMatchFamily(familyName));
175 }
176
177 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
178                                         const SkFontStyle& fs) const {
179     return this->onMatchFamilyStyle(familyName, fs);
180 }
181
182 #ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
183 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
184                                                  const char* bcp47[], int bcp47Count,
185                                                  SkUnichar character) const {
186     return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
187 }
188 #else
189 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
190                                                  const char bcp47[], SkUnichar character) const {
191     return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, character);
192 }
193 #endif
194
195 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
196                                       const SkFontStyle& fs) const {
197     return this->onMatchFaceStyle(face, fs);
198 }
199
200 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
201     if (NULL == data) {
202         return NULL;
203     }
204     return this->onCreateFromData(data, ttcIndex);
205 }
206
207 SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
208     if (NULL == stream) {
209         return NULL;
210     }
211     return this->onCreateFromStream(stream, ttcIndex);
212 }
213
214 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
215     if (NULL == path) {
216         return NULL;
217     }
218     return this->onCreateFromFile(path, ttcIndex);
219 }
220
221 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
222                                             unsigned styleBits) const {
223     return this->onLegacyCreateTypeface(familyName, styleBits);
224 }
225
226 // As a template argument this must have external linkage.
227 SkFontMgr* sk_fontmgr_create_default() {
228     SkFontMgr* fm = SkFontMgr::Factory();
229     return fm ? fm : SkNEW(SkEmptyFontMgr);
230 }
231
232 SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default);
233
234 SkFontMgr* SkFontMgr::RefDefault() {
235     return SkRef(singleton.get());
236 }