blob: ed5a3e5ce189891f7600dd53c62b1a8a0a836d3c [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@google.com1a64a542013-03-12 15:41:18 +00008#include "SkFontLCDConfig.h"
agl@chromium.org309485b2009-07-21 17:41:32 +00009
reed@google.com1a64a542013-03-12 15:41:18 +000010static SkFontLCDConfig::LCDOrientation gLCDOrientation = SkFontLCDConfig::kHorizontal_LCDOrientation;
11static SkFontLCDConfig::LCDOrder gLCDOrder = SkFontLCDConfig::kRGB_LCDOrder;
agl@chromium.org309485b2009-07-21 17:41:32 +000012
reed@google.com1a64a542013-03-12 15:41:18 +000013SkFontLCDConfig::LCDOrientation SkFontLCDConfig::GetSubpixelOrientation() {
agl@chromium.org309485b2009-07-21 17:41:32 +000014 return gLCDOrientation;
15}
16
reed@google.com1a64a542013-03-12 15:41:18 +000017void SkFontLCDConfig::SetSubpixelOrientation(LCDOrientation orientation) {
agl@chromium.org309485b2009-07-21 17:41:32 +000018 gLCDOrientation = orientation;
19}
20
reed@google.com1a64a542013-03-12 15:41:18 +000021SkFontLCDConfig::LCDOrder SkFontLCDConfig::GetSubpixelOrder() {
agl@chromium.org309485b2009-07-21 17:41:32 +000022 return gLCDOrder;
23}
24
reed@google.com1a64a542013-03-12 15:41:18 +000025void SkFontLCDConfig::SetSubpixelOrder(LCDOrder order) {
agl@chromium.org309485b2009-07-21 17:41:32 +000026 gLCDOrder = order;
27}
reed@google.com1a64a542013-03-12 15:41:18 +000028
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}
reed@google.com83787c52013-03-26 17:19:15 +000049
50///////////////////////////////////////////////////////////////////////////////
51///////////////////////////////////////////////////////////////////////////////
52
reed@google.com964988f2013-03-29 14:57:22 +000053#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
reed@google.com83787c52013-03-26 17:19:15 +000069#include "SkFontMgr.h"
70
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000071SK_DEFINE_INST_COUNT(SkFontStyleSet)
72
reed@google.com964988f2013-03-29 14:57:22 +000073class SkEmptyFontStyleSet : public SkFontStyleSet {
74public:
75 virtual int count() SK_OVERRIDE { return 0; }
76 virtual void getStyle(int, SkFontStyle*, SkString*) SK_OVERRIDE {
mtklein@google.com330313a2013-08-22 15:37:26 +000077 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
reed@google.com964988f2013-03-29 14:57:22 +000078 }
79 virtual SkTypeface* createTypeface(int index) SK_OVERRIDE {
mtklein@google.com330313a2013-08-22 15:37:26 +000080 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
reed@google.com964988f2013-03-29 14:57:22 +000081 return NULL;
82 }
83 virtual SkTypeface* matchStyle(const SkFontStyle&) SK_OVERRIDE {
84 return NULL;
85 }
86};
87
88SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
89 return SkNEW(SkEmptyFontStyleSet);
90}
91
92///////////////////////////////////////////////////////////////////////////////
93
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000094SK_DEFINE_INST_COUNT(SkFontMgr)
95
reed@google.comdb235092013-03-27 20:12:59 +000096class SkEmptyFontMgr : public SkFontMgr {
97protected:
98 virtual int onCountFamilies() SK_OVERRIDE {
99 return 0;
100 }
101 virtual void onGetFamilyName(int index, SkString* familyName) SK_OVERRIDE {
mtklein@google.com330313a2013-08-22 15:37:26 +0000102 SkDEBUGFAIL("onGetFamilyName called with bad index");
reed@google.comdb235092013-03-27 20:12:59 +0000103 }
104 virtual SkFontStyleSet* onCreateStyleSet(int index) SK_OVERRIDE {
mtklein@google.com330313a2013-08-22 15:37:26 +0000105 SkDEBUGFAIL("onCreateStyleSet called with bad index");
reed@google.comdb235092013-03-27 20:12:59 +0000106 return NULL;
107 }
reed@google.com964988f2013-03-29 14:57:22 +0000108 virtual SkFontStyleSet* onMatchFamily(const char[]) SK_OVERRIDE {
109 return SkFontStyleSet::CreateEmpty();
110 }
111
reed@google.comdb235092013-03-27 20:12:59 +0000112 virtual SkTypeface* onMatchFamilyStyle(const char[],
113 const SkFontStyle&) SK_OVERRIDE {
114 return NULL;
115 }
116 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
117 const SkFontStyle&) SK_OVERRIDE {
118 return NULL;
119 }
120 virtual SkTypeface* onCreateFromData(SkData*, int) SK_OVERRIDE {
121 return NULL;
122 }
123 virtual SkTypeface* onCreateFromStream(SkStream*, int) SK_OVERRIDE {
124 return NULL;
125 }
126 virtual SkTypeface* onCreateFromFile(const char[], int) SK_OVERRIDE {
127 return NULL;
128 }
bungeman@google.com418ee432013-08-21 16:28:26 +0000129 virtual SkTypeface* onLegacyCreateTypeface(const char [], unsigned) SK_OVERRIDE {
130 return NULL;
131 }
reed@google.comdb235092013-03-27 20:12:59 +0000132};
133
reed@google.comc1ccda32013-04-19 14:28:54 +0000134static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
135 if (NULL == fsset) {
136 fsset = SkFontStyleSet::CreateEmpty();
137 }
138 return fsset;
139}
140
reed@google.com83787c52013-03-26 17:19:15 +0000141int SkFontMgr::countFamilies() {
142 return this->onCountFamilies();
143}
144
145void SkFontMgr::getFamilyName(int index, SkString* familyName) {
146 this->onGetFamilyName(index, familyName);
147}
148
149SkFontStyleSet* SkFontMgr::createStyleSet(int index) {
reed@google.comc1ccda32013-04-19 14:28:54 +0000150 return emptyOnNull(this->onCreateStyleSet(index));
reed@google.com83787c52013-03-26 17:19:15 +0000151}
152
reed@google.com964988f2013-03-29 14:57:22 +0000153SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) {
reed@google.comc1ccda32013-04-19 14:28:54 +0000154 return emptyOnNull(this->onMatchFamily(familyName));
reed@google.com964988f2013-03-29 14:57:22 +0000155}
156
reed@google.com83787c52013-03-26 17:19:15 +0000157SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
158 const SkFontStyle& fs) {
159 return this->onMatchFamilyStyle(familyName, fs);
160}
161
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +0000162SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
reed@google.com83787c52013-03-26 17:19:15 +0000163 const SkFontStyle& fs) {
reed@google.com919e9c32013-03-26 19:29:55 +0000164 return this->onMatchFaceStyle(face, fs);
reed@google.com83787c52013-03-26 17:19:15 +0000165}
166
167SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) {
reed@google.comb6bd24d2013-07-31 18:49:33 +0000168 if (NULL == data) {
169 return NULL;
170 }
reed@google.com83787c52013-03-26 17:19:15 +0000171 return this->onCreateFromData(data, ttcIndex);
172}
173
174SkTypeface* SkFontMgr::createFromStream(SkStream* stream, int ttcIndex) {
reed@google.comb6bd24d2013-07-31 18:49:33 +0000175 if (NULL == stream) {
176 return NULL;
177 }
reed@google.com83787c52013-03-26 17:19:15 +0000178 return this->onCreateFromStream(stream, ttcIndex);
179}
180
181SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) {
reed@google.comb6bd24d2013-07-31 18:49:33 +0000182 if (NULL == path) {
183 return NULL;
184 }
reed@google.com83787c52013-03-26 17:19:15 +0000185 return this->onCreateFromFile(path, ttcIndex);
186}
187
reed@google.com30ddd612013-07-30 17:47:39 +0000188SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
189 unsigned styleBits) {
190 return this->onLegacyCreateTypeface(familyName, styleBits);
191}
192
reed@google.com83787c52013-03-26 17:19:15 +0000193SkFontMgr* SkFontMgr::RefDefault() {
194 static SkFontMgr* gFM;
195 if (NULL == gFM) {
196 gFM = SkFontMgr::Factory();
reed@google.comdb235092013-03-27 20:12:59 +0000197 // we never want to return NULL
198 if (NULL == gFM) {
199 gFM = SkNEW(SkEmptyFontMgr);
200 }
reed@google.com83787c52013-03-26 17:19:15 +0000201 }
202 return SkRef(gFM);
203}
reed@google.com30ddd612013-07-30 17:47:39 +0000204
205//////////////////////////////////////////////////////////////////////////
206
207#ifdef SK_FONTHOST_USES_FONTMGR
208
209#if 0
210static SkFontStyle TypefaceStyleBitsToFontStyle(SkTypeface::Style styleBits) {
211 SkFontStyle::Weight weight = (styleBits & SkTypeface::kBold) ?
212 SkFontStyle::kBold_Weight :
213 SkFontStyle::kNormal_Weight;
214 SkFontStyle::Width width = SkFontStyle::kNormal_Width;
215 SkFontStyle::Slant slant = (styleBits & SkTypeface::kItalic) ?
216 SkFontStyle::kUpright_Slant :
217 SkFontStyle::kItalic_Slant;
218 return SkFontStyle(weight, width, slant);
219}
220#endif
221
222SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
223 const char familyName[],
224 SkTypeface::Style style) {
225 if (familyFace) {
226 return familyFace->refMatchingStyle(style);
227 } else {
228 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
229 return fm->legacyCreateTypeface(familyName, style);
230 }
231}
232
233SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
234 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
235 return fm->createFromFile(path);
236}
237
238SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
239 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
240 return fm->createFromStream(stream);
241}
242
243#endif