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 
14 class SkFontStyle;
15 class SkTypeface;
16 
17 class SkEmptyFontStyleSet : public SkFontStyleSet {
18 public:
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 
CreateEmpty()32 SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; }
33 
34 ///////////////////////////////////////////////////////////////////////////////
35 
36 class SkEmptyFontMgr : public SkFontMgr {
37 protected:
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 
63     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int) const override {
64         return nullptr;
65     }
66     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int) const override {
67         return nullptr;
68     }
69     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
70                                            const SkFontArguments&) const override {
71         return nullptr;
72     }
73     sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
74         return nullptr;
75     }
76     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 
149 #ifdef OHOS_SUPPORT
getSystemFonts()150 std::vector<sk_sp<SkTypeface>> SkFontMgr::getSystemFonts()
151 {
152     return this->onGetSystemFonts();
153 }
154 
onGetSystemFonts() const155 std::vector<sk_sp<SkTypeface>> SkFontMgr::onGetSystemFonts() const
156 {
157     return {};
158 }
159 #endif
160 
RefEmpty()161 sk_sp<SkFontMgr> SkFontMgr::RefEmpty() {
162     static SkEmptyFontMgr singleton;
163     return sk_ref_sp(&singleton);
164 }
165 
166 // A global function pointer that's not declared, but can be overriden at startup by test tools.
167 sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)() = nullptr;
168 
RefDefault()169 sk_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 */
matchStyleCSS3(const SkFontStyle& pattern)206 SkTypeface* 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