1 /*
2 * Copyright 2015 Google Inc.
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 "include/core/SkFontMgr.h"
9 #include "include/core/SkStream.h"
10 #include "include/core/SkTypes.h"
11 #include "include/private/base/SkOnce.h"
12 #include "src/core/SkFontDescriptor.h"
13
14 class SkFontStyle;
15 class SkTypeface;
16
17 class SkEmptyFontStyleSet : public SkFontStyleSet {
18 public:
count()19 int count() override { return 0; }
getStyle(int,SkFontStyle *,SkString *)20 void getStyle(int, SkFontStyle*, SkString*) override {
21 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
22 }
createTypeface(int index)23 SkTypeface* createTypeface(int index) override {
24 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
25 return nullptr;
26 }
matchStyle(const SkFontStyle &)27 SkTypeface* matchStyle(const SkFontStyle&) override {
28 return nullptr;
29 }
30 };
31
CreateEmpty()32 SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; }
33
34 ///////////////////////////////////////////////////////////////////////////////
35
36 class SkEmptyFontMgr : public SkFontMgr {
37 protected:
onCountFamilies() const38 int onCountFamilies() const override {
39 return 0;
40 }
onGetFamilyName(int index,SkString * familyName) const41 void onGetFamilyName(int index, SkString* familyName) const override {
42 SkDEBUGFAIL("onGetFamilyName called with bad index");
43 }
onCreateStyleSet(int index) const44 SkFontStyleSet* onCreateStyleSet(int index) const override {
45 SkDEBUGFAIL("onCreateStyleSet called with bad index");
46 return nullptr;
47 }
onMatchFamily(const char[]) const48 SkFontStyleSet* onMatchFamily(const char[]) const override {
49 return SkFontStyleSet::CreateEmpty();
50 }
51
onMatchFamilyStyle(const char[],const SkFontStyle &) const52 SkTypeface* onMatchFamilyStyle(const char[], const SkFontStyle&) const override {
53 return nullptr;
54 }
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const55 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
56 const SkFontStyle& style,
57 const char* bcp47[],
58 int bcp47Count,
59 SkUnichar character) const override {
60 return nullptr;
61 }
62
onMakeFromData(sk_sp<SkData>,int) const63 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int) const override {
64 return nullptr;
65 }
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int) const66 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int) const override {
67 return nullptr;
68 }
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &) const69 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
70 const SkFontArguments&) const override {
71 return nullptr;
72 }
onMakeFromFile(const char[],int) const73 sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
74 return nullptr;
75 }
onLegacyMakeTypeface(const char[],SkFontStyle) const76 sk_sp<SkTypeface> onLegacyMakeTypeface(const char [], SkFontStyle) const override {
77 return nullptr;
78 }
79 };
80
emptyOnNull(SkFontStyleSet * fsset)81 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
82 if (nullptr == fsset) {
83 fsset = SkFontStyleSet::CreateEmpty();
84 }
85 return fsset;
86 }
87
countFamilies() const88 int SkFontMgr::countFamilies() const {
89 return this->onCountFamilies();
90 }
91
getFamilyName(int index,SkString * familyName) const92 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
93 this->onGetFamilyName(index, familyName);
94 }
95
createStyleSet(int index) const96 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
97 return emptyOnNull(this->onCreateStyleSet(index));
98 }
99
matchFamily(const char familyName[]) const100 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
101 return emptyOnNull(this->onMatchFamily(familyName));
102 }
103
matchFamilyStyle(const char familyName[],const SkFontStyle & fs) const104 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
105 const SkFontStyle& fs) const {
106 return this->onMatchFamilyStyle(familyName, fs);
107 }
108
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const109 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
110 const char* bcp47[], int bcp47Count,
111 SkUnichar character) const {
112 return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
113 }
114
makeFromData(sk_sp<SkData> data,int ttcIndex) const115 sk_sp<SkTypeface> SkFontMgr::makeFromData(sk_sp<SkData> data, int ttcIndex) const {
116 if (nullptr == data) {
117 return nullptr;
118 }
119 return this->onMakeFromData(std::move(data), ttcIndex);
120 }
121
makeFromStream(std::unique_ptr<SkStreamAsset> stream,int ttcIndex) const122 sk_sp<SkTypeface> SkFontMgr::makeFromStream(std::unique_ptr<SkStreamAsset> stream,
123 int ttcIndex) const {
124 if (nullptr == stream) {
125 return nullptr;
126 }
127 return this->onMakeFromStreamIndex(std::move(stream), ttcIndex);
128 }
129
makeFromStream(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args) const130 sk_sp<SkTypeface> SkFontMgr::makeFromStream(std::unique_ptr<SkStreamAsset> stream,
131 const SkFontArguments& args) const {
132 if (nullptr == stream) {
133 return nullptr;
134 }
135 return this->onMakeFromStreamArgs(std::move(stream), args);
136 }
137
makeFromFile(const char path[],int ttcIndex) const138 sk_sp<SkTypeface> SkFontMgr::makeFromFile(const char path[], int ttcIndex) const {
139 if (nullptr == path) {
140 return nullptr;
141 }
142 return this->onMakeFromFile(path, ttcIndex);
143 }
144
legacyMakeTypeface(const char familyName[],SkFontStyle style) const145 sk_sp<SkTypeface> SkFontMgr::legacyMakeTypeface(const char familyName[], SkFontStyle style) const {
146 return this->onLegacyMakeTypeface(familyName, style);
147 }
148
RefEmpty()149 sk_sp<SkFontMgr> SkFontMgr::RefEmpty() {
150 static SkEmptyFontMgr singleton;
151 return sk_ref_sp(&singleton);
152 }
153
154 // A global function pointer that's not declared, but can be overriden at startup by test tools.
155 sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)() = nullptr;
156
RefDefault()157 sk_sp<SkFontMgr> SkFontMgr::RefDefault() {
158 static SkOnce once;
159 static sk_sp<SkFontMgr> singleton;
160
161 once([]{
162 sk_sp<SkFontMgr> fm = gSkFontMgr_DefaultFactory ? gSkFontMgr_DefaultFactory()
163 : SkFontMgr::Factory();
164 singleton = fm ? std::move(fm) : RefEmpty();
165 });
166 return singleton;
167 }
168
169 /**
170 * Width has the greatest priority.
171 * If the value of pattern.width is 5 (normal) or less,
172 * narrower width values are checked first, then wider values.
173 * If the value of pattern.width is greater than 5 (normal),
174 * wider values are checked first, followed by narrower values.
175 *
176 * Italic/Oblique has the next highest priority.
177 * If italic requested and there is some italic font, use it.
178 * If oblique requested and there is some oblique font, use it.
179 * If italic requested and there is some oblique font, use it.
180 * If oblique requested and there is some italic font, use it.
181 *
182 * Exact match.
183 * If pattern.weight < 400, weights below pattern.weight are checked
184 * in descending order followed by weights above pattern.weight
185 * in ascending order until a match is found.
186 * If pattern.weight > 500, weights above pattern.weight are checked
187 * in ascending order followed by weights below pattern.weight
188 * in descending order until a match is found.
189 * If pattern.weight is 400, 500 is checked first
190 * and then the rule for pattern.weight < 400 is used.
191 * If pattern.weight is 500, 400 is checked first
192 * and then the rule for pattern.weight < 400 is used.
193 */
matchStyleCSS3(const SkFontStyle & pattern)194 SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) {
195 int count = this->count();
196 if (0 == count) {
197 return nullptr;
198 }
199
200 struct Score {
201 int score;
202 int index;
203 Score& operator +=(int rhs) { this->score += rhs; return *this; }
204 Score& operator <<=(int rhs) { this->score <<= rhs; return *this; }
205 bool operator <(const Score& that) { return this->score < that.score; }
206 };
207
208 Score maxScore = { 0, 0 };
209 for (int i = 0; i < count; ++i) {
210 SkFontStyle current;
211 this->getStyle(i, ¤t, nullptr);
212 Score currentScore = { 0, i };
213
214 // CSS stretch / SkFontStyle::Width
215 // Takes priority over everything else.
216 if (pattern.width() <= SkFontStyle::kNormal_Width) {
217 if (current.width() <= pattern.width()) {
218 currentScore += 10 - pattern.width() + current.width();
219 } else {
220 currentScore += 10 - current.width();
221 }
222 } else {
223 if (current.width() > pattern.width()) {
224 currentScore += 10 + pattern.width() - current.width();
225 } else {
226 currentScore += current.width();
227 }
228 }
229 currentScore <<= 8;
230
231 // CSS style (normal, italic, oblique) / SkFontStyle::Slant (upright, italic, oblique)
232 // Takes priority over all valid weights.
233 static_assert(SkFontStyle::kUpright_Slant == 0 &&
234 SkFontStyle::kItalic_Slant == 1 &&
235 SkFontStyle::kOblique_Slant == 2,
236 "SkFontStyle::Slant values not as required.");
237 SkASSERT(0 <= pattern.slant() && pattern.slant() <= 2 &&
238 0 <= current.slant() && current.slant() <= 2);
239 static const int score[3][3] = {
240 /* Upright Italic Oblique [current]*/
241 /* Upright */ { 3 , 1 , 2 },
242 /* Italic */ { 1 , 3 , 2 },
243 /* Oblique */ { 1 , 2 , 3 },
244 /* [pattern] */
245 };
246 currentScore += score[pattern.slant()][current.slant()];
247 currentScore <<= 8;
248
249 // Synthetics (weight, style) [no stretch synthetic?]
250
251 // CSS weight / SkFontStyle::Weight
252 // The 'closer' to the target weight, the higher the score.
253 // 1000 is the 'heaviest' recognized weight
254 if (pattern.weight() == current.weight()) {
255 currentScore += 1000;
256 // less than 400 prefer lighter weights
257 } else if (pattern.weight() < 400) {
258 if (current.weight() <= pattern.weight()) {
259 currentScore += 1000 - pattern.weight() + current.weight();
260 } else {
261 currentScore += 1000 - current.weight();
262 }
263 // between 400 and 500 prefer heavier up to 500, then lighter weights
264 } else if (pattern.weight() <= 500) {
265 if (current.weight() >= pattern.weight() && current.weight() <= 500) {
266 currentScore += 1000 + pattern.weight() - current.weight();
267 } else if (current.weight() <= pattern.weight()) {
268 currentScore += 500 + current.weight();
269 } else {
270 currentScore += 1000 - current.weight();
271 }
272 // greater than 500 prefer heavier weights
273 } else if (pattern.weight() > 500) {
274 if (current.weight() > pattern.weight()) {
275 currentScore += 1000 + pattern.weight() - current.weight();
276 } else {
277 currentScore += current.weight();
278 }
279 }
280
281 if (maxScore < currentScore) {
282 maxScore = currentScore;
283 }
284 }
285
286 return this->createTypeface(maxScore.index);
287 }
288