Line data Source code
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 0 : class SkEmptyFontStyleSet : public SkFontStyleSet {
18 : public:
19 0 : int count() override { return 0; }
20 0 : void getStyle(int, SkFontStyle*, SkString*) override {
21 0 : SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
22 0 : }
23 0 : SkTypeface* createTypeface(int index) override {
24 0 : SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
25 0 : return nullptr;
26 : }
27 0 : SkTypeface* matchStyle(const SkFontStyle&) override {
28 0 : return nullptr;
29 : }
30 : };
31 :
32 0 : SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; }
33 :
34 : ///////////////////////////////////////////////////////////////////////////////
35 :
36 0 : class SkEmptyFontMgr : public SkFontMgr {
37 : protected:
38 0 : int onCountFamilies() const override {
39 0 : return 0;
40 : }
41 0 : void onGetFamilyName(int index, SkString* familyName) const override {
42 0 : SkDEBUGFAIL("onGetFamilyName called with bad index");
43 0 : }
44 0 : SkFontStyleSet* onCreateStyleSet(int index) const override {
45 0 : SkDEBUGFAIL("onCreateStyleSet called with bad index");
46 0 : return nullptr;
47 : }
48 0 : SkFontStyleSet* onMatchFamily(const char[]) const override {
49 0 : return SkFontStyleSet::CreateEmpty();
50 : }
51 :
52 0 : virtual SkTypeface* onMatchFamilyStyle(const char[],
53 : const SkFontStyle&) const override {
54 0 : return nullptr;
55 : }
56 0 : virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
57 : const SkFontStyle& style,
58 : const char* bcp47[],
59 : int bcp47Count,
60 : SkUnichar character) const override {
61 0 : return nullptr;
62 : }
63 0 : virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
64 : const SkFontStyle&) const override {
65 0 : return nullptr;
66 : }
67 0 : SkTypeface* onCreateFromData(SkData*, int) const override {
68 0 : return nullptr;
69 : }
70 0 : SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override {
71 0 : delete stream;
72 0 : return nullptr;
73 : }
74 0 : SkTypeface* onCreateFromFile(const char[], int) const override {
75 0 : return nullptr;
76 : }
77 0 : SkTypeface* onLegacyCreateTypeface(const char [], SkFontStyle) const override {
78 0 : return nullptr;
79 : }
80 : };
81 :
82 0 : static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
83 0 : if (nullptr == fsset) {
84 0 : fsset = SkFontStyleSet::CreateEmpty();
85 : }
86 0 : return fsset;
87 : }
88 :
89 0 : int SkFontMgr::countFamilies() const {
90 0 : return this->onCountFamilies();
91 : }
92 :
93 0 : void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
94 0 : this->onGetFamilyName(index, familyName);
95 0 : }
96 :
97 0 : SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
98 0 : return emptyOnNull(this->onCreateStyleSet(index));
99 : }
100 :
101 0 : SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
102 0 : return emptyOnNull(this->onMatchFamily(familyName));
103 : }
104 :
105 0 : SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
106 : const SkFontStyle& fs) const {
107 0 : return this->onMatchFamilyStyle(familyName, fs);
108 : }
109 :
110 0 : SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
111 : const char* bcp47[], int bcp47Count,
112 : SkUnichar character) const {
113 0 : return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
114 : }
115 :
116 0 : SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
117 : const SkFontStyle& fs) const {
118 0 : return this->onMatchFaceStyle(face, fs);
119 : }
120 :
121 0 : SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
122 0 : if (nullptr == data) {
123 0 : return nullptr;
124 : }
125 0 : return this->onCreateFromData(data, ttcIndex);
126 : }
127 :
128 0 : SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const {
129 0 : if (nullptr == stream) {
130 0 : return nullptr;
131 : }
132 0 : return this->onCreateFromStream(stream, ttcIndex);
133 : }
134 :
135 0 : SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const SkFontArguments& args) const {
136 0 : if (nullptr == stream) {
137 0 : return nullptr;
138 : }
139 0 : return this->onCreateFromStream(stream, args);
140 : }
141 :
142 0 : SkTypeface* SkFontMgr::createFromFontData(std::unique_ptr<SkFontData> data) const {
143 0 : if (nullptr == data) {
144 0 : return nullptr;
145 : }
146 0 : return this->onCreateFromFontData(std::move(data));
147 : }
148 :
149 : // This implementation is temporary until it can be made pure virtual.
150 0 : SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const SkFontArguments& args) const{
151 0 : return this->createFromStream(stream, args.getCollectionIndex());
152 : }
153 :
154 : // This implementation is temporary until it can be made pure virtual.
155 0 : SkTypeface* SkFontMgr::onCreateFromFontData(std::unique_ptr<SkFontData> data) const {
156 0 : return this->createFromStream(data->detachStream().release(), data->getIndex());
157 : }
158 :
159 0 : SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
160 0 : if (nullptr == path) {
161 0 : return nullptr;
162 : }
163 0 : return this->onCreateFromFile(path, ttcIndex);
164 : }
165 :
166 0 : SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], SkFontStyle style) const {
167 0 : return this->onLegacyCreateTypeface(familyName, style);
168 : }
169 :
170 0 : sk_sp<SkFontMgr> SkFontMgr::RefDefault() {
171 : static SkOnce once;
172 0 : static sk_sp<SkFontMgr> singleton;
173 :
174 0 : once([]{
175 0 : sk_sp<SkFontMgr> fm = SkFontMgr::Factory();
176 0 : singleton = fm ? std::move(fm) : sk_make_sp<SkEmptyFontMgr>();
177 0 : });
178 0 : 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 : */
206 0 : SkTypeface* SkFontStyleSet::matchStyleCSS3(const SkFontStyle& pattern) {
207 0 : int count = this->count();
208 0 : if (0 == count) {
209 0 : return nullptr;
210 : }
211 :
212 : struct Score {
213 : int score;
214 : int index;
215 0 : Score& operator +=(int rhs) { this->score += rhs; return *this; }
216 0 : Score& operator <<=(int rhs) { this->score <<= rhs; return *this; }
217 0 : bool operator <(const Score& that) { return this->score < that.score; }
218 : };
219 :
220 0 : Score maxScore = { 0, 0 };
221 0 : for (int i = 0; i < count; ++i) {
222 0 : SkFontStyle current;
223 0 : this->getStyle(i, ¤t, nullptr);
224 0 : Score currentScore = { 0, i };
225 :
226 : // CSS stretch / SkFontStyle::Width
227 : // Takes priority over everything else.
228 0 : if (pattern.width() <= SkFontStyle::kNormal_Width) {
229 0 : if (current.width() <= pattern.width()) {
230 0 : currentScore += 10 - pattern.width() + current.width();
231 : } else {
232 0 : currentScore += 10 - current.width();
233 : }
234 : } else {
235 0 : if (current.width() > pattern.width()) {
236 0 : currentScore += 10 + pattern.width() - current.width();
237 : } else {
238 0 : currentScore += current.width();
239 : }
240 : }
241 0 : 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 0 : 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 0 : currentScore += score[pattern.slant()][current.slant()];
259 0 : 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 0 : if (pattern.weight() == current.weight()) {
267 0 : currentScore += 1000;
268 0 : } else if (pattern.weight() <= 500) {
269 0 : if (400 <= pattern.weight() && pattern.weight() < 450) {
270 0 : if (450 <= current.weight() && current.weight() <= 500) {
271 : // Artificially boost the 500 weight.
272 : // TODO: determine correct number to use.
273 0 : currentScore += 500;
274 : }
275 : }
276 0 : if (current.weight() <= pattern.weight()) {
277 0 : currentScore += 1000 - pattern.weight() + current.weight();
278 : } else {
279 0 : currentScore += 1000 - current.weight();
280 : }
281 0 : } else if (pattern.weight() > 500) {
282 0 : if (current.weight() > pattern.weight()) {
283 0 : currentScore += 1000 + pattern.weight() - current.weight();
284 : } else {
285 0 : currentScore += current.weight();
286 : }
287 : }
288 :
289 0 : if (maxScore < currentScore) {
290 0 : maxScore = currentScore;
291 : }
292 : }
293 :
294 0 : return this->createTypeface(maxScore.index);
295 : }
|