blob: 8ce0b39682f23fdfc659d1c1374dc740cf1aa0cf [file] [log] [blame]
bungemane1a828c2015-01-20 11:15:37 -08001/*
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
bungeman41868fe2015-05-20 09:21:04 -07008#include "SkFontDescriptor.h"
bungemane1a828c2015-01-20 11:15:37 -08009#include "SkFontMgr.h"
mtkleinffa4a922016-05-05 16:05:56 -070010#include "SkOnce.h"
scroggoa1193e42015-01-21 12:09:53 -080011#include "SkStream.h"
bungemane1a828c2015-01-20 11:15:37 -080012#include "SkTypes.h"
13
14class SkFontStyle;
bungemane1a828c2015-01-20 11:15:37 -080015class SkTypeface;
16
17class SkEmptyFontStyleSet : public SkFontStyleSet {
18public:
mtklein36352bf2015-03-25 18:17:31 -070019 int count() override { return 0; }
20 void getStyle(int, SkFontStyle*, SkString*) override {
bungemane1a828c2015-01-20 11:15:37 -080021 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
22 }
mtklein36352bf2015-03-25 18:17:31 -070023 SkTypeface* createTypeface(int index) override {
bungemane1a828c2015-01-20 11:15:37 -080024 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
halcanary96fcdcc2015-08-27 07:41:13 -070025 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080026 }
mtklein36352bf2015-03-25 18:17:31 -070027 SkTypeface* matchStyle(const SkFontStyle&) override {
halcanary96fcdcc2015-08-27 07:41:13 -070028 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080029 }
30};
31
halcanary385fe4d2015-08-26 13:07:48 -070032SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; }
bungemane1a828c2015-01-20 11:15:37 -080033
34///////////////////////////////////////////////////////////////////////////////
35
36class SkEmptyFontMgr : public SkFontMgr {
37protected:
mtklein36352bf2015-03-25 18:17:31 -070038 int onCountFamilies() const override {
bungemane1a828c2015-01-20 11:15:37 -080039 return 0;
40 }
mtklein36352bf2015-03-25 18:17:31 -070041 void onGetFamilyName(int index, SkString* familyName) const override {
bungemane1a828c2015-01-20 11:15:37 -080042 SkDEBUGFAIL("onGetFamilyName called with bad index");
43 }
mtklein36352bf2015-03-25 18:17:31 -070044 SkFontStyleSet* onCreateStyleSet(int index) const override {
bungemane1a828c2015-01-20 11:15:37 -080045 SkDEBUGFAIL("onCreateStyleSet called with bad index");
halcanary96fcdcc2015-08-27 07:41:13 -070046 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080047 }
mtklein36352bf2015-03-25 18:17:31 -070048 SkFontStyleSet* onMatchFamily(const char[]) const override {
bungemane1a828c2015-01-20 11:15:37 -080049 return SkFontStyleSet::CreateEmpty();
50 }
51
52 virtual SkTypeface* onMatchFamilyStyle(const char[],
mtklein36352bf2015-03-25 18:17:31 -070053 const SkFontStyle&) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070054 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080055 }
56 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
57 const SkFontStyle& style,
58 const char* bcp47[],
59 int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -070060 SkUnichar character) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070061 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080062 }
63 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
mtklein36352bf2015-03-25 18:17:31 -070064 const SkFontStyle&) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070065 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080066 }
mtklein36352bf2015-03-25 18:17:31 -070067 SkTypeface* onCreateFromData(SkData*, int) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070068 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080069 }
mtklein36352bf2015-03-25 18:17:31 -070070 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override {
halcanary385fe4d2015-08-26 13:07:48 -070071 delete stream;
halcanary96fcdcc2015-08-27 07:41:13 -070072 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080073 }
mtklein36352bf2015-03-25 18:17:31 -070074 SkTypeface* onCreateFromFile(const char[], int) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070075 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080076 }
bungeman11a77c62016-04-12 13:45:06 -070077 SkTypeface* onLegacyCreateTypeface(const char [], SkFontStyle) const override {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -080079 }
80};
81
82static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
halcanary96fcdcc2015-08-27 07:41:13 -070083 if (nullptr == fsset) {
bungemane1a828c2015-01-20 11:15:37 -080084 fsset = SkFontStyleSet::CreateEmpty();
85 }
86 return fsset;
87}
88
89int SkFontMgr::countFamilies() const {
90 return this->onCountFamilies();
91}
92
93void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
94 this->onGetFamilyName(index, familyName);
95}
96
97SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
98 return emptyOnNull(this->onCreateStyleSet(index));
99}
100
101SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
102 return emptyOnNull(this->onMatchFamily(familyName));
103}
104
105SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
106 const SkFontStyle& fs) const {
107 return this->onMatchFamilyStyle(familyName, fs);
108}
109
110SkTypeface* 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
116SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
117 const SkFontStyle& fs) const {
118 return this->onMatchFaceStyle(face, fs);
119}
120
121SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700122 if (nullptr == data) {
123 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -0800124 }
125 return this->onCreateFromData(data, ttcIndex);
126}
127
bungeman5f213d92015-01-27 05:39:10 -0800128SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700129 if (nullptr == stream) {
130 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -0800131 }
132 return this->onCreateFromStream(stream, ttcIndex);
133}
134
Ben Wagnerfc497342017-02-24 11:15:26 -0500135SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const SkFontArguments& args) const {
bungemanf6c71072016-01-21 14:17:47 -0800136 if (nullptr == stream) {
137 return nullptr;
138 }
Ben Wagnerfc497342017-02-24 11:15:26 -0500139 return this->onCreateFromStream(stream, args);
bungemanf6c71072016-01-21 14:17:47 -0800140}
141
bungemanf93d7112016-09-16 06:24:20 -0700142SkTypeface* SkFontMgr::createFromFontData(std::unique_ptr<SkFontData> data) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700143 if (nullptr == data) {
144 return nullptr;
bungeman41868fe2015-05-20 09:21:04 -0700145 }
bungemanf93d7112016-09-16 06:24:20 -0700146 return this->onCreateFromFontData(std::move(data));
bungeman41868fe2015-05-20 09:21:04 -0700147}
148
149// This implementation is temporary until it can be made pure virtual.
Ben Wagnerfc497342017-02-24 11:15:26 -0500150SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const SkFontArguments& args) const{
151 return this->createFromStream(stream, args.getCollectionIndex());
bungemanf6c71072016-01-21 14:17:47 -0800152}
153
154// This implementation is temporary until it can be made pure virtual.
bungemanf93d7112016-09-16 06:24:20 -0700155SkTypeface* SkFontMgr::onCreateFromFontData(std::unique_ptr<SkFontData> data) const {
156 return this->createFromStream(data->detachStream().release(), data->getIndex());
bungeman41868fe2015-05-20 09:21:04 -0700157}
158
bungemane1a828c2015-01-20 11:15:37 -0800159SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
halcanary96fcdcc2015-08-27 07:41:13 -0700160 if (nullptr == path) {
161 return nullptr;
bungemane1a828c2015-01-20 11:15:37 -0800162 }
163 return this->onCreateFromFile(path, ttcIndex);
164}
165
bungeman11a77c62016-04-12 13:45:06 -0700166SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], SkFontStyle style) const {
bungeman11a77c62016-04-12 13:45:06 -0700167 return this->onLegacyCreateTypeface(familyName, style);
bungemane1a828c2015-01-20 11:15:37 -0800168}
169
Ben Wagner3546ff12017-01-03 13:32:36 -0500170sk_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}
bungeman147ea2f2015-11-12 09:50:08 -0800180
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*/
206SkTypeface* 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;
bungemanb4bb7d82016-04-27 10:21:04 -0700215 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; }
bungeman147ea2f2015-11-12 09:50:08 -0800218 };
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
bungemanb4bb7d82016-04-27 10:21:04 -0700226 // CSS stretch / SkFontStyle::Width
227 // Takes priority over everything else.
bungeman147ea2f2015-11-12 09:50:08 -0800228 if (pattern.width() <= SkFontStyle::kNormal_Width) {
229 if (current.width() <= pattern.width()) {
bungemanb4bb7d82016-04-27 10:21:04 -0700230 currentScore += 10 - pattern.width() + current.width();
bungeman147ea2f2015-11-12 09:50:08 -0800231 } else {
bungemanb4bb7d82016-04-27 10:21:04 -0700232 currentScore += 10 - current.width();
bungeman147ea2f2015-11-12 09:50:08 -0800233 }
234 } else {
235 if (current.width() > pattern.width()) {
bungemanb4bb7d82016-04-27 10:21:04 -0700236 currentScore += 10 + pattern.width() - current.width();
bungeman147ea2f2015-11-12 09:50:08 -0800237 } else {
bungemanb4bb7d82016-04-27 10:21:04 -0700238 currentScore += current.width();
bungeman147ea2f2015-11-12 09:50:08 -0800239 }
240 }
bungemanb4bb7d82016-04-27 10:21:04 -0700241 currentScore <<= 8;
bungeman147ea2f2015-11-12 09:50:08 -0800242
bungemanb4bb7d82016-04-27 10:21:04 -0700243 // 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;
bungeman147ea2f2015-11-12 09:50:08 -0800260
bungemanb4bb7d82016-04-27 10:21:04 -0700261 // Synthetics (weight, style) [no stretch synthetic?]
bungeman147ea2f2015-11-12 09:50:08 -0800262
bungemanb4bb7d82016-04-27 10:21:04 -0700263 // CSS weight / SkFontStyle::Weight
bungeman147ea2f2015-11-12 09:50:08 -0800264 // The 'closer' to the target weight, the higher the score.
265 // 1000 is the 'heaviest' recognized weight
266 if (pattern.weight() == current.weight()) {
bungemanb4bb7d82016-04-27 10:21:04 -0700267 currentScore += 1000;
bungeman147ea2f2015-11-12 09:50:08 -0800268 } 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.
bungemanb4bb7d82016-04-27 10:21:04 -0700273 currentScore += 500;
bungeman147ea2f2015-11-12 09:50:08 -0800274 }
275 }
276 if (current.weight() <= pattern.weight()) {
bungemanb4bb7d82016-04-27 10:21:04 -0700277 currentScore += 1000 - pattern.weight() + current.weight();
bungeman147ea2f2015-11-12 09:50:08 -0800278 } else {
bungemanb4bb7d82016-04-27 10:21:04 -0700279 currentScore += 1000 - current.weight();
bungeman147ea2f2015-11-12 09:50:08 -0800280 }
281 } else if (pattern.weight() > 500) {
282 if (current.weight() > pattern.weight()) {
bungemanb4bb7d82016-04-27 10:21:04 -0700283 currentScore += 1000 + pattern.weight() - current.weight();
bungeman147ea2f2015-11-12 09:50:08 -0800284 } else {
bungemanb4bb7d82016-04-27 10:21:04 -0700285 currentScore += current.weight();
bungeman147ea2f2015-11-12 09:50:08 -0800286 }
287 }
288
bungemanb4bb7d82016-04-27 10:21:04 -0700289 if (maxScore < currentScore) {
bungeman147ea2f2015-11-12 09:50:08 -0800290 maxScore = currentScore;
291 }
292 }
293
294 return this->createTypeface(maxScore.index);
295}