• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
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 
149 // A global function pointer that's not declared, but can be overriden at startup by test tools.
150 sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)() = nullptr;
151 
RefDefault()152 sk_sp<SkFontMgr> SkFontMgr::RefDefault() {
153     static SkOnce once;
154     static sk_sp<SkFontMgr> singleton;
155 
156     once([]{
157         sk_sp<SkFontMgr> fm = gSkFontMgr_DefaultFactory ? gSkFontMgr_DefaultFactory()
158                                                         : SkFontMgr::Factory();
159         singleton = fm ? std::move(fm) : sk_make_sp<SkEmptyFontMgr>();
160     });
161     return singleton;
162 }
163 
164 /**
165 * Width has the greatest priority.
166 * If the value of pattern.width is 5 (normal) or less,
167 *    narrower width values are checked first, then wider values.
168 * If the value of pattern.width is greater than 5 (normal),
169 *    wider values are checked first, followed by narrower values.
170 *
171 * Italic/Oblique has the next highest priority.
172 * If italic requested and there is some italic font, use it.
173 * If oblique requested and there is some oblique font, use it.
174 * If italic requested and there is some oblique font, use it.
175 * If oblique requested and there is some italic font, use it.
176 *
177 * Exact match.
178 * If pattern.weight < 400, weights below pattern.weight are checked
179 *   in descending order followed by weights above pattern.weight
180 *   in ascending order until a match is found.
181 * If pattern.weight > 500, weights above pattern.weight are checked
182 *   in ascending order followed by weights below pattern.weight
183 *   in descending order until a match is found.
184 * If pattern.weight is 400, 500 is checked first
185 *   and then the rule for pattern.weight < 400 is used.
186 * If pattern.weight is 500, 400 is checked first
187 *   and then the rule for pattern.weight < 400 is used.
188 */
matchStyleCSS3(const SkFontStyle & pattern)189 SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) {
190     int count = this->count();
191     if (0 == count) {
192         return nullptr;
193     }
194 
195     struct Score {
196         int score;
197         int index;
198         Score& operator +=(int rhs) { this->score += rhs; return *this; }
199         Score& operator <<=(int rhs) { this->score <<= rhs; return *this; }
200         bool operator <(const Score& that) { return this->score < that.score; }
201     };
202 
203     Score maxScore = { 0, 0 };
204     for (int i = 0; i < count; ++i) {
205         SkFontStyle current;
206         this->getStyle(i, &current, nullptr);
207         Score currentScore = { 0, i };
208 
209         // CSS stretch / SkFontStyle::Width
210         // Takes priority over everything else.
211         if (pattern.width() <= SkFontStyle::kNormal_Width) {
212             if (current.width() <= pattern.width()) {
213                 currentScore += 10 - pattern.width() + current.width();
214             } else {
215                 currentScore += 10 - current.width();
216             }
217         } else {
218             if (current.width() > pattern.width()) {
219                 currentScore += 10 + pattern.width() - current.width();
220             } else {
221                 currentScore += current.width();
222             }
223         }
224         currentScore <<= 8;
225 
226         // CSS style (normal, italic, oblique) / SkFontStyle::Slant (upright, italic, oblique)
227         // Takes priority over all valid weights.
228         static_assert(SkFontStyle::kUpright_Slant == 0 &&
229                       SkFontStyle::kItalic_Slant  == 1 &&
230                       SkFontStyle::kOblique_Slant == 2,
231                       "SkFontStyle::Slant values not as required.");
232         SkASSERT(0 <= pattern.slant() && pattern.slant() <= 2 &&
233                  0 <= current.slant() && current.slant() <= 2);
234         static const int score[3][3] = {
235             /*               Upright Italic Oblique  [current]*/
236             /*   Upright */ {   3   ,  1   ,   2   },
237             /*   Italic  */ {   1   ,  3   ,   2   },
238             /*   Oblique */ {   1   ,  2   ,   3   },
239             /* [pattern] */
240         };
241         currentScore += score[pattern.slant()][current.slant()];
242         currentScore <<= 8;
243 
244         // Synthetics (weight, style) [no stretch synthetic?]
245 
246         // CSS weight / SkFontStyle::Weight
247         // The 'closer' to the target weight, the higher the score.
248         // 1000 is the 'heaviest' recognized weight
249         if (pattern.weight() == current.weight()) {
250             currentScore += 1000;
251         // less than 400 prefer lighter weights
252         } else if (pattern.weight() < 400) {
253             if (current.weight() <= pattern.weight()) {
254                 currentScore += 1000 - pattern.weight() + current.weight();
255             } else {
256                 currentScore += 1000 - current.weight();
257             }
258         // between 400 and 500 prefer heavier up to 500, then lighter weights
259         } else if (pattern.weight() <= 500) {
260             if (current.weight() >= pattern.weight() && current.weight() <= 500) {
261                 currentScore += 1000 + pattern.weight() - current.weight();
262             } else if (current.weight() <= pattern.weight()) {
263                 currentScore += 500 + current.weight();
264             } else {
265                 currentScore += 1000 - current.weight();
266             }
267         // greater than 500 prefer heavier weights
268         } else if (pattern.weight() > 500) {
269             if (current.weight() > pattern.weight()) {
270                 currentScore += 1000 + pattern.weight() - current.weight();
271             } else {
272                 currentScore += current.weight();
273             }
274         }
275 
276         if (maxScore < currentScore) {
277             maxScore = currentScore;
278         }
279     }
280 
281     return this->createTypeface(maxScore.index);
282 }
283