bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 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 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 8 | #include "SkFontDescriptor.h" |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 9 | #include "SkFontMgr.h" |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 10 | #include "SkOnce.h" |
scroggo | a1193e4 | 2015-01-21 12:09:53 -0800 | [diff] [blame] | 11 | #include "SkStream.h" |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 12 | #include "SkTypes.h" |
| 13 | |
| 14 | class SkFontStyle; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 15 | class SkTypeface; |
| 16 | |
| 17 | class SkEmptyFontStyleSet : public SkFontStyleSet { |
| 18 | public: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 19 | int count() override { return 0; } |
| 20 | void getStyle(int, SkFontStyle*, SkString*) override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 21 | SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set"); |
| 22 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 23 | SkTypeface* createTypeface(int index) override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 24 | SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 25 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 26 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkTypeface* matchStyle(const SkFontStyle&) override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 28 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 29 | } |
| 30 | }; |
| 31 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 32 | SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; } |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 33 | |
| 34 | /////////////////////////////////////////////////////////////////////////////// |
| 35 | |
| 36 | class SkEmptyFontMgr : public SkFontMgr { |
| 37 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | int onCountFamilies() const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 39 | return 0; |
| 40 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | void onGetFamilyName(int index, SkString* familyName) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 42 | SkDEBUGFAIL("onGetFamilyName called with bad index"); |
| 43 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkFontStyleSet* onCreateStyleSet(int index) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 45 | SkDEBUGFAIL("onCreateStyleSet called with bad index"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 46 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 47 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | SkFontStyleSet* onMatchFamily(const char[]) const override { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 49 | return SkFontStyleSet::CreateEmpty(); |
| 50 | } |
| 51 | |
| 52 | virtual SkTypeface* onMatchFamilyStyle(const char[], |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | const SkFontStyle&) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 54 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 55 | } |
| 56 | virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
| 57 | const SkFontStyle& style, |
| 58 | const char* bcp47[], |
| 59 | int bcp47Count, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 60 | SkUnichar character) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 62 | } |
| 63 | virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | const SkFontStyle&) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 65 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 66 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | SkTypeface* onCreateFromData(SkData*, int) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 68 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 69 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 70 | SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 71 | delete stream; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 72 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 73 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 74 | SkTypeface* onCreateFromFile(const char[], int) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 76 | } |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 77 | SkTypeface* onLegacyCreateTypeface(const char [], SkFontStyle) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 79 | } |
| 80 | }; |
| 81 | |
| 82 | static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 83 | if (nullptr == fsset) { |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 84 | fsset = SkFontStyleSet::CreateEmpty(); |
| 85 | } |
| 86 | return fsset; |
| 87 | } |
| 88 | |
| 89 | int SkFontMgr::countFamilies() const { |
| 90 | return this->onCountFamilies(); |
| 91 | } |
| 92 | |
| 93 | void SkFontMgr::getFamilyName(int index, SkString* familyName) const { |
| 94 | this->onGetFamilyName(index, familyName); |
| 95 | } |
| 96 | |
| 97 | SkFontStyleSet* SkFontMgr::createStyleSet(int index) const { |
| 98 | return emptyOnNull(this->onCreateStyleSet(index)); |
| 99 | } |
| 100 | |
| 101 | SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const { |
| 102 | return emptyOnNull(this->onMatchFamily(familyName)); |
| 103 | } |
| 104 | |
| 105 | SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[], |
| 106 | const SkFontStyle& fs) const { |
| 107 | return this->onMatchFamilyStyle(familyName, fs); |
| 108 | } |
| 109 | |
| 110 | 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 | |
| 116 | SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face, |
| 117 | const SkFontStyle& fs) const { |
| 118 | return this->onMatchFaceStyle(face, fs); |
| 119 | } |
| 120 | |
| 121 | SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 122 | if (nullptr == data) { |
| 123 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 124 | } |
| 125 | return this->onCreateFromData(data, ttcIndex); |
| 126 | } |
| 127 | |
bungeman | 5f213d9 | 2015-01-27 05:39:10 -0800 | [diff] [blame] | 128 | SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 129 | if (nullptr == stream) { |
| 130 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 131 | } |
| 132 | return this->onCreateFromStream(stream, ttcIndex); |
| 133 | } |
| 134 | |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 135 | SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const SkFontArguments& args) const { |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 136 | if (nullptr == stream) { |
| 137 | return nullptr; |
| 138 | } |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 139 | return this->onCreateFromStream(stream, args); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 140 | } |
| 141 | |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 142 | SkTypeface* SkFontMgr::createFromFontData(std::unique_ptr<SkFontData> data) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 143 | if (nullptr == data) { |
| 144 | return nullptr; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 145 | } |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 146 | return this->onCreateFromFontData(std::move(data)); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | // This implementation is temporary until it can be made pure virtual. |
Ben Wagner | fc49734 | 2017-02-24 11:15:26 -0500 | [diff] [blame] | 150 | SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const SkFontArguments& args) const{ |
| 151 | return this->createFromStream(stream, args.getCollectionIndex()); |
bungeman | f6c7107 | 2016-01-21 14:17:47 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | // This implementation is temporary until it can be made pure virtual. |
bungeman | f93d711 | 2016-09-16 06:24:20 -0700 | [diff] [blame] | 155 | SkTypeface* SkFontMgr::onCreateFromFontData(std::unique_ptr<SkFontData> data) const { |
| 156 | return this->createFromStream(data->detachStream().release(), data->getIndex()); |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 157 | } |
| 158 | |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 159 | SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | if (nullptr == path) { |
| 161 | return nullptr; |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 162 | } |
| 163 | return this->onCreateFromFile(path, ttcIndex); |
| 164 | } |
| 165 | |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 166 | SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], SkFontStyle style) const { |
bungeman | 11a77c6 | 2016-04-12 13:45:06 -0700 | [diff] [blame] | 167 | return this->onLegacyCreateTypeface(familyName, style); |
bungeman | e1a828c | 2015-01-20 11:15:37 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Ben Wagner | 3546ff1 | 2017-01-03 13:32:36 -0500 | [diff] [blame] | 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 | } |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 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 | 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; |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 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; } |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
| 220 | Score maxScore = { 0, 0 }; |
| 221 | for (int i = 0; i < count; ++i) { |
| 222 | SkFontStyle current; |
| 223 | this->getStyle(i, ¤t, nullptr); |
| 224 | Score currentScore = { 0, i }; |
| 225 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 226 | // CSS stretch / SkFontStyle::Width |
| 227 | // Takes priority over everything else. |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 228 | if (pattern.width() <= SkFontStyle::kNormal_Width) { |
| 229 | if (current.width() <= pattern.width()) { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 230 | currentScore += 10 - pattern.width() + current.width(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 231 | } else { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 232 | currentScore += 10 - current.width(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 233 | } |
| 234 | } else { |
| 235 | if (current.width() > pattern.width()) { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 236 | currentScore += 10 + pattern.width() - current.width(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 237 | } else { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 238 | currentScore += current.width(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 241 | currentScore <<= 8; |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 242 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 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; |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 260 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 261 | // Synthetics (weight, style) [no stretch synthetic?] |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 262 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 263 | // CSS weight / SkFontStyle::Weight |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 264 | // The 'closer' to the target weight, the higher the score. |
| 265 | // 1000 is the 'heaviest' recognized weight |
| 266 | if (pattern.weight() == current.weight()) { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 267 | currentScore += 1000; |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 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. |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 273 | currentScore += 500; |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | if (current.weight() <= pattern.weight()) { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 277 | currentScore += 1000 - pattern.weight() + current.weight(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 278 | } else { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 279 | currentScore += 1000 - current.weight(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 280 | } |
| 281 | } else if (pattern.weight() > 500) { |
| 282 | if (current.weight() > pattern.weight()) { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 283 | currentScore += 1000 + pattern.weight() - current.weight(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 284 | } else { |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 285 | currentScore += current.weight(); |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
bungeman | b4bb7d8 | 2016-04-27 10:21:04 -0700 | [diff] [blame] | 289 | if (maxScore < currentScore) { |
bungeman | 147ea2f | 2015-11-12 09:50:08 -0800 | [diff] [blame] | 290 | maxScore = currentScore; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return this->createTypeface(maxScore.index); |
| 295 | } |