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