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 "SkOncePtr.h" 11 #include "SkStream.h" 12 #include "SkTypes.h" 13 14 class SkFontStyle; 15 class SkTypeface; 16 17 class SkEmptyFontStyleSet : public SkFontStyleSet { 18 public: 19 int count() override { return 0; } 20 void getStyle(int, SkFontStyle*, SkString*) override { 21 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set"); 22 } 23 SkTypeface* createTypeface(int index) override { 24 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set"); 25 return nullptr; 26 } 27 SkTypeface* matchStyle(const SkFontStyle&) override { 28 return nullptr; 29 } 30 }; 31 32 SkFontStyleSet* SkFontStyleSet::CreateEmpty() { return new SkEmptyFontStyleSet; } 33 34 /////////////////////////////////////////////////////////////////////////////// 35 36 class SkEmptyFontMgr : public SkFontMgr { 37 protected: 38 int onCountFamilies() const override { 39 return 0; 40 } 41 void onGetFamilyName(int index, SkString* familyName) const override { 42 SkDEBUGFAIL("onGetFamilyName called with bad index"); 43 } 44 SkFontStyleSet* onCreateStyleSet(int index) const override { 45 SkDEBUGFAIL("onCreateStyleSet called with bad index"); 46 return nullptr; 47 } 48 SkFontStyleSet* onMatchFamily(const char[]) const override { 49 return SkFontStyleSet::CreateEmpty(); 50 } 51 52 virtual SkTypeface* onMatchFamilyStyle(const char[], 53 const SkFontStyle&) const override { 54 return nullptr; 55 } 56 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 } 63 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*, 64 const SkFontStyle&) const override { 65 return nullptr; 66 } 67 SkTypeface* onCreateFromData(SkData*, int) const override { 68 return nullptr; 69 } 70 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override { 71 delete stream; 72 return nullptr; 73 } 74 SkTypeface* onCreateFromFile(const char[], int) const override { 75 return nullptr; 76 } 77 SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const override { 78 return nullptr; 79 } 80 }; 81 82 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) { 83 if (nullptr == fsset) { 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 { 122 if (nullptr == data) { 123 return nullptr; 124 } 125 return this->onCreateFromData(data, ttcIndex); 126 } 127 128 SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const { 129 if (nullptr == stream) { 130 return nullptr; 131 } 132 return this->onCreateFromStream(stream, ttcIndex); 133 } 134 135 SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, const FontParameters& params) const { 136 if (nullptr == stream) { 137 return nullptr; 138 } 139 return this->onCreateFromStream(stream, params); 140 } 141 142 SkTypeface* SkFontMgr::createFromFontData(SkFontData* data) const { 143 if (nullptr == data) { 144 return nullptr; 145 } 146 return this->onCreateFromFontData(data); 147 } 148 149 // This implementation is temporary until it can be made pure virtual. 150 SkTypeface* SkFontMgr::onCreateFromStream(SkStreamAsset* stream, const FontParameters& p) const { 151 return this->createFromStream(stream, p.getCollectionIndex()); 152 } 153 154 // This implementation is temporary until it can be made pure virtual. 155 SkTypeface* SkFontMgr::onCreateFromFontData(SkFontData* data) const { 156 SkTypeface* ret = this->createFromStream(data->detachStream(), data->getIndex()); 157 delete data; 158 return ret; 159 } 160 161 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const { 162 if (nullptr == path) { 163 return nullptr; 164 } 165 return this->onCreateFromFile(path, ttcIndex); 166 } 167 168 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[], 169 unsigned styleBits) const { 170 return this->onLegacyCreateTypeface(familyName, styleBits); 171 } 172 173 SK_DECLARE_STATIC_ONCE_PTR(SkFontMgr, singleton); 174 SkFontMgr* SkFontMgr::RefDefault() { 175 return SkRef(singleton.get([]{ 176 SkFontMgr* fm = SkFontMgr::Factory(); 177 return fm ? fm : new SkEmptyFontMgr; 178 })); 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 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 }; 216 217 Score maxScore = { 0, 0 }; 218 for (int i = 0; i < count; ++i) { 219 SkFontStyle current; 220 this->getStyle(i, ¤t, nullptr); 221 Score currentScore = { 0, i }; 222 223 // CSS stretch. (This is the width.) 224 // This has the highest priority. 225 if (pattern.width() <= SkFontStyle::kNormal_Width) { 226 if (current.width() <= pattern.width()) { 227 currentScore.score += 10 - pattern.width() + current.width(); 228 } else { 229 currentScore.score += 10 - current.width(); 230 } 231 } else { 232 if (current.width() > pattern.width()) { 233 currentScore.score += 10 + pattern.width() - current.width(); 234 } else { 235 currentScore.score += current.width(); 236 } 237 } 238 currentScore.score *= 1002; 239 240 // CSS style (italic/oblique) 241 // Being italic trumps all valid weights which are not italic. 242 // Note that newer specs differentiate between italic and oblique. 243 if (pattern.isItalic() == current.isItalic()) { 244 currentScore.score += 1001; 245 } 246 247 // Synthetics (weight/style) [no stretch synthetic?] 248 249 // The 'closer' to the target weight, the higher the score. 250 // 1000 is the 'heaviest' recognized weight 251 if (pattern.weight() == current.weight()) { 252 currentScore.score += 1000; 253 } else if (pattern.weight() <= 500) { 254 if (400 <= pattern.weight() && pattern.weight() < 450) { 255 if (450 <= current.weight() && current.weight() <= 500) { 256 // Artificially boost the 500 weight. 257 // TODO: determine correct number to use. 258 currentScore.score += 500; 259 } 260 } 261 if (current.weight() <= pattern.weight()) { 262 currentScore.score += 1000 - pattern.weight() + current.weight(); 263 } else { 264 currentScore.score += 1000 - current.weight(); 265 } 266 } else if (pattern.weight() > 500) { 267 if (current.weight() > pattern.weight()) { 268 currentScore.score += 1000 + pattern.weight() - current.weight(); 269 } else { 270 currentScore.score += current.weight(); 271 } 272 } 273 274 if (currentScore.score > maxScore.score) { 275 maxScore = currentScore; 276 } 277 } 278 279 return this->createTypeface(maxScore.index); 280 } 281