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
GetSubpixelOrientation()14 SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
15 return gLCDOrientation;
16 }
17
SetSubpixelOrientation(LCDOrientation orientation)18 void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
19 gLCDOrientation = orientation;
20 }
21
GetSubpixelOrder()22 SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
23 return gLCDOrder;
24 }
25
SetSubpixelOrder(LCDOrder order)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
GetSubpixelOrientation()35 SkFontHost::LCDOrientation SkFontHost::GetSubpixelOrientation() {
36 return (SkFontHost::LCDOrientation)SkFontLCDConfig::GetSubpixelOrientation();
37 }
38
SetSubpixelOrientation(LCDOrientation orientation)39 void SkFontHost::SetSubpixelOrientation(LCDOrientation orientation) {
40 SkFontLCDConfig::SetSubpixelOrientation((SkFontLCDConfig::LCDOrientation)orientation);
41 }
42
GetSubpixelOrder()43 SkFontHost::LCDOrder SkFontHost::GetSubpixelOrder() {
44 return (SkFontHost::LCDOrder)SkFontLCDConfig::GetSubpixelOrder();
45 }
46
SetSubpixelOrder(LCDOrder order)47 void SkFontHost::SetSubpixelOrder(LCDOrder order) {
48 SkFontLCDConfig::SetSubpixelOrder((SkFontLCDConfig::LCDOrder)order);
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 ///////////////////////////////////////////////////////////////////////////////
53
54 #include "SkFontStyle.h"
55
SkFontStyle()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
SkFontStyle(int weight,int width,Slant slant)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:
count()74 virtual int count() SK_OVERRIDE { return 0; }
getStyle(int,SkFontStyle *,SkString *)75 virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
76 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
77 }
createTypeface(int index)78 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
79 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
80 return NULL;
81 }
matchStyle(const SkFontStyle &)82 virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
83 return NULL;
84 }
85 };
86
CreateEmpty()87 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
88 return SkNEW(SkEmptyFontStyleSet);
89 }
90
91 ///////////////////////////////////////////////////////////////////////////////
92
93 class SkEmptyFontMgr : public SkFontMgr {
94 protected:
onCountFamilies() const95 virtual int onCountFamilies() const SK_OVERRIDE {
96 return 0;
97 }
onGetFamilyName(int index,SkString * familyName) const98 virtual void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE {
99 SkDEBUGFAIL("onGetFamilyName called with bad index");
100 }
onCreateStyleSet(int index) const101 virtual SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE {
102 SkDEBUGFAIL("onCreateStyleSet called with bad index");
103 return NULL;
104 }
onMatchFamily(const char[]) const105 virtual SkFontStyleSet* onMatchFamily(const char[]) const SK_OVERRIDE {
106 return SkFontStyleSet::CreateEmpty();
107 }
108
onMatchFamilyStyle(const char[],const SkFontStyle &) const109 virtual SkTypeface* onMatchFamilyStyle(const char[],
110 const SkFontStyle&) const SK_OVERRIDE {
111 return NULL;
112 }
113 #ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const114 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
emptyOnNull(SkFontStyleSet * fsset)145 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
146 if (NULL == fsset) {
147 fsset = SkFontStyleSet::CreateEmpty();
148 }
149 return fsset;
150 }
151
countFamilies() const152 int SkFontMgr::countFamilies() const {
153 return this->onCountFamilies();
154 }
155
getFamilyName(int index,SkString * familyName) const156 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
157 this->onGetFamilyName(index, familyName);
158 }
159
createStyleSet(int index) const160 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
161 return emptyOnNull(this->onCreateStyleSet(index));
162 }
163
matchFamily(const char familyName[]) const164 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
165 return emptyOnNull(this->onMatchFamily(familyName));
166 }
167
matchFamilyStyle(const char familyName[],const SkFontStyle & fs) const168 SkTypeface* 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
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const174 SkTypeface* 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
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char bcp47[],SkUnichar character) const180 SkTypeface* 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
matchFaceStyle(const SkTypeface * face,const SkFontStyle & fs) const186 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
187 const SkFontStyle& fs) const {
188 return this->onMatchFaceStyle(face, fs);
189 }
190
createFromData(SkData * data,int ttcIndex) const191 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
192 if (NULL == data) {
193 return NULL;
194 }
195 return this->onCreateFromData(data, ttcIndex);
196 }
197
createFromStream(SkStream * stream,int ttcIndex) const198 SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) const {
199 if (NULL == stream) {
200 return NULL;
201 }
202 return this->onCreateFromStream(stream, ttcIndex);
203 }
204
createFromFile(const char path[],int ttcIndex) const205 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
206 if (NULL == path) {
207 return NULL;
208 }
209 return this->onCreateFromFile(path, ttcIndex);
210 }
211
legacyCreateTypeface(const char familyName[],unsigned styleBits) const212 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
213 unsigned styleBits) const {
214 return this->onLegacyCreateTypeface(familyName, styleBits);
215 }
216
CreateDefault()217 SkFontMgr* SkFontMgr::CreateDefault() {
218 SkFontMgr* fm = SkFontMgr::Factory();
219 return fm ? fm : SkNEW(SkEmptyFontMgr);
220 }
221
RefDefault()222 SkFontMgr* SkFontMgr::RefDefault() {
223 SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, CreateDefault);
224 return SkRef(singleton.get());
225 }
226