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