• 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 "SkFontDescriptor.h"
9 #include "SkFontMgr.h"
10 #include "SkOnce.h"
11 #include "SkStream.h"
12 #include "SkTypes.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     virtual SkTypeface* onMatchFamilyStyle(const char[],
53                                            const SkFontStyle&) const override {
54         return nullptr;
55     }
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const56     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
57                                                     const SkFontStyle& style,
58                                                     const char* bcp47[],
59                                                     int bcp47Count,
60                                                     SkUnichar character) const override {
61         return nullptr;
62     }
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &) const63     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
64                                          const SkFontStyle&) const override {
65         return nullptr;
66     }
onCreateFromData(SkData *,int) const67     SkTypeface* onCreateFromData(SkData*, int) const override {
68         return nullptr;
69     }
onCreateFromStream(SkStreamAsset * stream,int) const70     SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override {
71         delete stream;
72         return nullptr;
73     }
onCreateFromFile(const char[],int) const74     SkTypeface* onCreateFromFile(const char[], int) const override {
75         return nullptr;
76     }
onLegacyCreateTypeface(const char[],SkFontStyle) const77     SkTypeface* onLegacyCreateTypeface(const char [], SkFontStyle) const override {
78         return nullptr;
79     }
80 };
81 
emptyOnNull(SkFontStyleSet * fsset)82 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
83     if (nullptr == fsset) {
84         fsset = SkFontStyleSet::CreateEmpty();
85     }
86     return fsset;
87 }
88 
countFamilies() const89 int SkFontMgr::countFamilies() const {
90     return this->onCountFamilies();
91 }
92 
getFamilyName(int index,SkString * familyName) const93 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
94     this->onGetFamilyName(index, familyName);
95 }
96 
createStyleSet(int index) const97 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
98     return emptyOnNull(this->onCreateStyleSet(index));
99 }
100 
matchFamily(const char familyName[]) const101 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
102     return emptyOnNull(this->onMatchFamily(familyName));
103 }
104 
matchFamilyStyle(const char familyName[],const SkFontStyle & fs) const105 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
106                                         const SkFontStyle& fs) const {
107     return this->onMatchFamilyStyle(familyName, fs);
108 }
109 
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const110 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
111                                                  const char* bcp47[], int bcp47Count,
112                                                  SkUnichar character) const {
113     return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
114 }
115 
matchFaceStyle(const SkTypeface * face,const SkFontStyle & fs) const116 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
117                                       const SkFontStyle& fs) const {
118     return this->onMatchFaceStyle(face, fs);
119 }
120 
createFromData(SkData * data,int ttcIndex) const121 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
122     if (nullptr == data) {
123         return nullptr;
124     }
125     return this->onCreateFromData(data, ttcIndex);
126 }
127 
createFromStream(SkStreamAsset * stream,int ttcIndex) const128 SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const {
129     if (nullptr == stream) {
130         return nullptr;
131     }
132     return this->onCreateFromStream(stream, ttcIndex);
133 }
134 
createFromStream(SkStreamAsset * stream,const SkFontArguments & args) const135 SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const SkFontArguments& args) const {
136     if (nullptr == stream) {
137         return nullptr;
138     }
139     return this->onCreateFromStream(stream, args);
140 }
141 
createFromFontData(std::unique_ptr<SkFontData> data) const142 SkTypeface* SkFontMgr::createFromFontData(std::unique_ptr<SkFontData> data) const {
143     if (nullptr == data) {
144         return nullptr;
145     }
146     return this->onCreateFromFontData(std::move(data));
147 }
148 
149 // This implementation is temporary until it can be made pure virtual.
onCreateFromStream(SkStreamAsset * stream,const SkFontArguments & args) const150 SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const SkFontArguments& args) const{
151     return this->createFromStream(stream, args.getCollectionIndex());
152 }
153 
154 // This implementation is temporary until it can be made pure virtual.
onCreateFromFontData(std::unique_ptr<SkFontData> data) const155 SkTypeface* SkFontMgr::onCreateFromFontData(std::unique_ptr<SkFontData> data) const {
156     return this->createFromStream(data->detachStream().release(), data->getIndex());
157 }
158 
createFromFile(const char path[],int ttcIndex) const159 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
160     if (nullptr == path) {
161         return nullptr;
162     }
163     return this->onCreateFromFile(path, ttcIndex);
164 }
165 
legacyCreateTypeface(const char familyName[],SkFontStyle style) const166 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], SkFontStyle style) const {
167     return this->onLegacyCreateTypeface(familyName, style);
168 }
169 
RefDefault()170 sk_sp<SkFontMgr> SkFontMgr::RefDefault() {
171     static SkOnce once;
172     static sk_sp<SkFontMgr> singleton;
173 
174     once([]{
175         sk_sp<SkFontMgr> fm = 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         } else if (pattern.weight() <= 500) {
269             if (400 <= pattern.weight() && pattern.weight() < 450) {
270                 if (450 <= current.weight() && current.weight() <= 500) {
271                     // Artificially boost the 500 weight.
272                     // TODO: determine correct number to use.
273                     currentScore += 500;
274                 }
275             }
276             if (current.weight() <= pattern.weight()) {
277                 currentScore += 1000 - pattern.weight() + current.weight();
278             } else {
279                 currentScore += 1000 - current.weight();
280             }
281         } else if (pattern.weight() > 500) {
282             if (current.weight() > pattern.weight()) {
283                 currentScore += 1000 + pattern.weight() - current.weight();
284             } else {
285                 currentScore += current.weight();
286             }
287         }
288 
289         if (maxScore < currentScore) {
290             maxScore = currentScore;
291         }
292     }
293 
294     return this->createTypeface(maxScore.index);
295 }
296