OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFontConfigParser_android.h" | 8 #include "SkFontConfigParser_android.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkFontHost_FreeType_common.h" | 10 #include "SkFontHost_FreeType_common.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 public: | 29 public: |
30 SkTypeface_Android(int index, | 30 SkTypeface_Android(int index, |
31 const SkFontStyle& style, | 31 const SkFontStyle& style, |
32 bool isFixedPitch, | 32 bool isFixedPitch, |
33 const SkString& familyName) | 33 const SkString& familyName) |
34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) | 34 : INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch) |
35 , fIndex(index) | 35 , fIndex(index) |
36 , fFamilyName(familyName) { } | 36 , fFamilyName(familyName) { } |
37 | 37 |
38 protected: | 38 protected: |
39 void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { | 39 void onGetFamilyName(SkString* familyName) const override { |
40 *familyName = fFamilyName; | 40 *familyName = fFamilyName; |
41 } | 41 } |
42 | 42 |
43 int fIndex; | 43 int fIndex; |
44 SkString fFamilyName; | 44 SkString fFamilyName; |
45 | 45 |
46 private: | 46 private: |
47 typedef SkTypeface_FreeType INHERITED; | 47 typedef SkTypeface_FreeType INHERITED; |
48 }; | 48 }; |
49 | 49 |
50 class SkTypeface_AndroidSystem : public SkTypeface_Android { | 50 class SkTypeface_AndroidSystem : public SkTypeface_Android { |
51 public: | 51 public: |
52 SkTypeface_AndroidSystem(const SkString& pathName, | 52 SkTypeface_AndroidSystem(const SkString& pathName, |
53 int index, | 53 int index, |
54 const SkFontStyle& style, | 54 const SkFontStyle& style, |
55 bool isFixedPitch, | 55 bool isFixedPitch, |
56 const SkString& familyName, | 56 const SkString& familyName, |
57 const SkLanguage& lang, | 57 const SkLanguage& lang, |
58 FontVariant variantStyle) | 58 FontVariant variantStyle) |
59 : INHERITED(index, style, isFixedPitch, familyName) | 59 : INHERITED(index, style, isFixedPitch, familyName) |
60 , fPathName(pathName) | 60 , fPathName(pathName) |
61 , fLang(lang) | 61 , fLang(lang) |
62 , fVariantStyle(variantStyle) { } | 62 , fVariantStyle(variantStyle) { } |
63 | 63 |
64 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 64 virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
65 bool* serialize) const SK_OVERRIDE { | 65 bool* serialize) const override { |
66 SkASSERT(desc); | 66 SkASSERT(desc); |
67 SkASSERT(serialize); | 67 SkASSERT(serialize); |
68 desc->setFamilyName(fFamilyName.c_str()); | 68 desc->setFamilyName(fFamilyName.c_str()); |
69 desc->setFontFileName(fPathName.c_str()); | 69 desc->setFontFileName(fPathName.c_str()); |
70 desc->setFontIndex(fIndex); | 70 desc->setFontIndex(fIndex); |
71 *serialize = false; | 71 *serialize = false; |
72 } | 72 } |
73 SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 73 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
74 *ttcIndex = fIndex; | 74 *ttcIndex = fIndex; |
75 return SkStream::NewFromFile(fPathName.c_str()); | 75 return SkStream::NewFromFile(fPathName.c_str()); |
76 } | 76 } |
77 | 77 |
78 const SkString fPathName; | 78 const SkString fPathName; |
79 const SkLanguage fLang; | 79 const SkLanguage fLang; |
80 const FontVariant fVariantStyle; | 80 const FontVariant fVariantStyle; |
81 | 81 |
82 typedef SkTypeface_Android INHERITED; | 82 typedef SkTypeface_Android INHERITED; |
83 }; | 83 }; |
84 | 84 |
85 class SkTypeface_AndroidStream : public SkTypeface_Android { | 85 class SkTypeface_AndroidStream : public SkTypeface_Android { |
86 public: | 86 public: |
87 SkTypeface_AndroidStream(SkStreamAsset* stream, | 87 SkTypeface_AndroidStream(SkStreamAsset* stream, |
88 int index, | 88 int index, |
89 const SkFontStyle& style, | 89 const SkFontStyle& style, |
90 bool isFixedPitch, | 90 bool isFixedPitch, |
91 const SkString& familyName) | 91 const SkString& familyName) |
92 : INHERITED(index, style, isFixedPitch, familyName) | 92 : INHERITED(index, style, isFixedPitch, familyName) |
93 , fStream(stream) { } | 93 , fStream(stream) { } |
94 | 94 |
95 virtual void onGetFontDescriptor(SkFontDescriptor* desc, | 95 virtual void onGetFontDescriptor(SkFontDescriptor* desc, |
96 bool* serialize) const SK_OVERRIDE { | 96 bool* serialize) const override { |
97 SkASSERT(desc); | 97 SkASSERT(desc); |
98 SkASSERT(serialize); | 98 SkASSERT(serialize); |
99 desc->setFamilyName(fFamilyName.c_str()); | 99 desc->setFamilyName(fFamilyName.c_str()); |
100 desc->setFontFileName(NULL); | 100 desc->setFontFileName(NULL); |
101 *serialize = true; | 101 *serialize = true; |
102 } | 102 } |
103 | 103 |
104 SkStreamAsset* onOpenStream(int* ttcIndex) const SK_OVERRIDE { | 104 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
105 *ttcIndex = fIndex; | 105 *ttcIndex = fIndex; |
106 return fStream->duplicate(); | 106 return fStream->duplicate(); |
107 } | 107 } |
108 | 108 |
109 private: | 109 private: |
110 SkAutoTDelete<SkStreamAsset> fStream; | 110 SkAutoTDelete<SkStreamAsset> fStream; |
111 | 111 |
112 typedef SkTypeface_Android INHERITED; | 112 typedef SkTypeface_Android INHERITED; |
113 }; | 113 }; |
114 | 114 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 familyName = *cannonicalFamilyName; | 162 familyName = *cannonicalFamilyName; |
163 } | 163 } |
164 | 164 |
165 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, | 165 fStyles.push_back().reset(SkNEW_ARGS(SkTypeface_AndroidSystem, |
166 (pathName, ttcIndex, | 166 (pathName, ttcIndex, |
167 style, isFixedWidth, familyNam
e, | 167 style, isFixedWidth, familyNam
e, |
168 lang, variant))); | 168 lang, variant))); |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 int count() SK_OVERRIDE { | 172 int count() override { |
173 return fStyles.count(); | 173 return fStyles.count(); |
174 } | 174 } |
175 void getStyle(int index, SkFontStyle* style, SkString* name) SK_OVERRIDE { | 175 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
176 if (index < 0 || fStyles.count() <= index) { | 176 if (index < 0 || fStyles.count() <= index) { |
177 return; | 177 return; |
178 } | 178 } |
179 if (style) { | 179 if (style) { |
180 *style = this->style(index); | 180 *style = this->style(index); |
181 } | 181 } |
182 if (name) { | 182 if (name) { |
183 name->reset(); | 183 name->reset(); |
184 } | 184 } |
185 } | 185 } |
186 SkTypeface_AndroidSystem* createTypeface(int index) SK_OVERRIDE { | 186 SkTypeface_AndroidSystem* createTypeface(int index) override { |
187 if (index < 0 || fStyles.count() <= index) { | 187 if (index < 0 || fStyles.count() <= index) { |
188 return NULL; | 188 return NULL; |
189 } | 189 } |
190 return SkRef(fStyles[index].get()); | 190 return SkRef(fStyles[index].get()); |
191 } | 191 } |
192 | 192 |
193 /** Find the typeface in this style set that most closely matches the given
pattern. | 193 /** Find the typeface in this style set that most closely matches the given
pattern. |
194 * TODO: consider replacing with SkStyleSet_Indirect::matchStyle(); | 194 * TODO: consider replacing with SkStyleSet_Indirect::matchStyle(); |
195 * this simpler version using match_score() passes all our tests. | 195 * this simpler version using match_score() passes all our tests. |
196 */ | 196 */ |
197 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) SK_OVERRIDE
{ | 197 SkTypeface_AndroidSystem* matchStyle(const SkFontStyle& pattern) override { |
198 if (0 == fStyles.count()) { | 198 if (0 == fStyles.count()) { |
199 return NULL; | 199 return NULL; |
200 } | 200 } |
201 SkTypeface_AndroidSystem* closest = fStyles[0]; | 201 SkTypeface_AndroidSystem* closest = fStyles[0]; |
202 int minScore = std::numeric_limits<int>::max(); | 202 int minScore = std::numeric_limits<int>::max(); |
203 for (int i = 0; i < fStyles.count(); ++i) { | 203 for (int i = 0; i < fStyles.count(); ++i) { |
204 SkFontStyle style = this->style(i); | 204 SkFontStyle style = this->style(i); |
205 int score = match_score(pattern, style); | 205 int score = match_score(pattern, style); |
206 if (score < minScore) { | 206 if (score < minScore) { |
207 closest = fStyles[i]; | 207 closest = fStyles[i]; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 } | 262 } |
263 this->buildNameToFamilyMap(families); | 263 this->buildNameToFamilyMap(families); |
264 this->findDefaultFont(); | 264 this->findDefaultFont(); |
265 families.deleteAll(); | 265 families.deleteAll(); |
266 } | 266 } |
267 | 267 |
268 protected: | 268 protected: |
269 /** Returns not how many families we have, but how many unique names | 269 /** Returns not how many families we have, but how many unique names |
270 * exist among the families. | 270 * exist among the families. |
271 */ | 271 */ |
272 int onCountFamilies() const SK_OVERRIDE { | 272 int onCountFamilies() const override { |
273 return fNameToFamilyMap.count(); | 273 return fNameToFamilyMap.count(); |
274 } | 274 } |
275 | 275 |
276 void onGetFamilyName(int index, SkString* familyName) const SK_OVERRIDE { | 276 void onGetFamilyName(int index, SkString* familyName) const override { |
277 if (index < 0 || fNameToFamilyMap.count() <= index) { | 277 if (index < 0 || fNameToFamilyMap.count() <= index) { |
278 familyName->reset(); | 278 familyName->reset(); |
279 return; | 279 return; |
280 } | 280 } |
281 familyName->set(fNameToFamilyMap[index].name); | 281 familyName->set(fNameToFamilyMap[index].name); |
282 } | 282 } |
283 | 283 |
284 SkFontStyleSet* onCreateStyleSet(int index) const SK_OVERRIDE { | 284 SkFontStyleSet* onCreateStyleSet(int index) const override { |
285 if (index < 0 || fNameToFamilyMap.count() <= index) { | 285 if (index < 0 || fNameToFamilyMap.count() <= index) { |
286 return NULL; | 286 return NULL; |
287 } | 287 } |
288 return SkRef(fNameToFamilyMap[index].styleSet); | 288 return SkRef(fNameToFamilyMap[index].styleSet); |
289 } | 289 } |
290 | 290 |
291 SkFontStyleSet* onMatchFamily(const char familyName[]) const SK_OVERRIDE { | 291 SkFontStyleSet* onMatchFamily(const char familyName[]) const override { |
292 if (!familyName) { | 292 if (!familyName) { |
293 return NULL; | 293 return NULL; |
294 } | 294 } |
295 SkAutoAsciiToLC tolc(familyName); | 295 SkAutoAsciiToLC tolc(familyName); |
296 for (int i = 0; i < fNameToFamilyMap.count(); ++i) { | 296 for (int i = 0; i < fNameToFamilyMap.count(); ++i) { |
297 if (fNameToFamilyMap[i].name.equals(tolc.lc())) { | 297 if (fNameToFamilyMap[i].name.equals(tolc.lc())) { |
298 return SkRef(fNameToFamilyMap[i].styleSet); | 298 return SkRef(fNameToFamilyMap[i].styleSet); |
299 } | 299 } |
300 } | 300 } |
301 // TODO: eventually we should not need to name fallback families. | 301 // TODO: eventually we should not need to name fallback families. |
302 for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) { | 302 for (int i = 0; i < fFallbackNameToFamilyMap.count(); ++i) { |
303 if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) { | 303 if (fFallbackNameToFamilyMap[i].name.equals(tolc.lc())) { |
304 return SkRef(fFallbackNameToFamilyMap[i].styleSet); | 304 return SkRef(fFallbackNameToFamilyMap[i].styleSet); |
305 } | 305 } |
306 } | 306 } |
307 return NULL; | 307 return NULL; |
308 } | 308 } |
309 | 309 |
310 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], | 310 virtual SkTypeface* onMatchFamilyStyle(const char familyName[], |
311 const SkFontStyle& style) const SK_OV
ERRIDE { | 311 const SkFontStyle& style) const overr
ide { |
312 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); | 312 SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName)); |
313 return sset->matchStyle(style); | 313 return sset->matchStyle(style); |
314 } | 314 } |
315 | 315 |
316 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, | 316 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
317 const SkFontStyle& style) const SK_OVER
RIDE { | 317 const SkFontStyle& style) const overrid
e { |
318 for (int i = 0; i < fFontStyleSets.count(); ++i) { | 318 for (int i = 0; i < fFontStyleSets.count(); ++i) { |
319 for (int j = 0; j < fFontStyleSets[i]->fStyles.count(); ++j) { | 319 for (int j = 0; j < fFontStyleSets[i]->fStyles.count(); ++j) { |
320 if (fFontStyleSets[i]->fStyles[j] == typeface) { | 320 if (fFontStyleSets[i]->fStyles[j] == typeface) { |
321 return fFontStyleSets[i]->matchStyle(style); | 321 return fFontStyleSets[i]->matchStyle(style); |
322 } | 322 } |
323 } | 323 } |
324 } | 324 } |
325 return NULL; | 325 return NULL; |
326 } | 326 } |
327 | 327 |
(...skipping 24 matching lines...) Expand all Loading... |
352 return face.detach(); | 352 return face.detach(); |
353 } | 353 } |
354 } | 354 } |
355 return NULL; | 355 return NULL; |
356 } | 356 } |
357 | 357 |
358 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], | 358 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], |
359 const SkFontStyle& style, | 359 const SkFontStyle& style, |
360 const char* bcp47[], | 360 const char* bcp47[], |
361 int bcp47Count, | 361 int bcp47Count, |
362 SkUnichar character) const S
K_OVERRIDE | 362 SkUnichar character) const o
verride |
363 { | 363 { |
364 // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascen
t/descent'. | 364 // The variant 'elegant' is 'not squashed', 'compact' is 'stays in ascen
t/descent'. |
365 // The variant 'default' means 'compact and elegant'. | 365 // The variant 'default' means 'compact and elegant'. |
366 // As a result, it is not possible to know the variant context from the
font alone. | 366 // As a result, it is not possible to know the variant context from the
font alone. |
367 // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request. | 367 // TODO: add 'is_elegant' and 'is_compact' bits to 'style' request. |
368 | 368 |
369 // The first time match anything elegant, second time anything not elega
nt. | 369 // The first time match anything elegant, second time anything not elega
nt. |
370 for (int elegant = 2; elegant --> 0;) { | 370 for (int elegant = 2; elegant --> 0;) { |
371 for (int bcp47Index = bcp47Count; bcp47Index --> 0;) { | 371 for (int bcp47Index = bcp47Count; bcp47Index --> 0;) { |
372 SkLanguage lang(bcp47[bcp47Index]); | 372 SkLanguage lang(bcp47[bcp47Index]); |
(...skipping 13 matching lines...) Expand all Loading... |
386 find_family_style_character(fFallbackNameToFamilyMap, | 386 find_family_style_character(fFallbackNameToFamilyMap, |
387 style, SkToBool(elegant), | 387 style, SkToBool(elegant), |
388 SkString(), character); | 388 SkString(), character); |
389 if (matchingTypeface) { | 389 if (matchingTypeface) { |
390 return matchingTypeface; | 390 return matchingTypeface; |
391 } | 391 } |
392 } | 392 } |
393 return NULL; | 393 return NULL; |
394 } | 394 } |
395 | 395 |
396 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const SK_OVERRIDE { | 396 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
397 return this->createFromStream(new SkMemoryStream(data), ttcIndex); | 397 return this->createFromStream(new SkMemoryStream(data), ttcIndex); |
398 } | 398 } |
399 | 399 |
400 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const SK_OVERR
IDE { | 400 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
401 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); | 401 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); |
402 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; | 402 return stream.get() ? this->createFromStream(stream.detach(), ttcIndex)
: NULL; |
403 } | 403 } |
404 | 404 |
405 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t SK_OVERRIDE { | 405 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
406 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 406 SkAutoTDelete<SkStreamAsset> stream(bareStream); |
407 bool isFixedPitch; | 407 bool isFixedPitch; |
408 SkFontStyle style; | 408 SkFontStyle style; |
409 SkString name; | 409 SkString name; |
410 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch))
{ | 410 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch))
{ |
411 return NULL; | 411 return NULL; |
412 } | 412 } |
413 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex, | 413 return SkNEW_ARGS(SkTypeface_AndroidStream, (stream.detach(), ttcIndex, |
414 style, isFixedPitch, name))
; | 414 style, isFixedPitch, name))
; |
415 } | 415 } |
416 | 416 |
417 | 417 |
418 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 418 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
419 unsigned styleBits) const SK_OVER
RIDE { | 419 unsigned styleBits) const overrid
e { |
420 SkFontStyle style = SkFontStyle(styleBits); | 420 SkFontStyle style = SkFontStyle(styleBits); |
421 | 421 |
422 if (familyName) { | 422 if (familyName) { |
423 // On Android, we must return NULL when we can't find the requested | 423 // On Android, we must return NULL when we can't find the requested |
424 // named typeface so that the system/app can provide their own recov
ery | 424 // named typeface so that the system/app can provide their own recov
ery |
425 // mechanism. On other platforms we'd provide a typeface from the | 425 // mechanism. On other platforms we'd provide a typeface from the |
426 // default family instead. | 426 // default family instead. |
427 return this->onMatchFamilyStyle(familyName, style); | 427 return this->onMatchFamilyStyle(familyName, style); |
428 } | 428 } |
429 return fDefaultFamily->matchStyle(style); | 429 return fDefaultFamily->matchStyle(style); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 { | 540 { |
541 gTestFontsXml = fontsXml; | 541 gTestFontsXml = fontsXml; |
542 gTestFallbackFontsXml = fallbackFontsXml; | 542 gTestFallbackFontsXml = fallbackFontsXml; |
543 gTestBasePath = basePath; | 543 gTestBasePath = basePath; |
544 SkASSERT(gTestFontsXml); | 544 SkASSERT(gTestFontsXml); |
545 SkASSERT(gTestFallbackFontsXml); | 545 SkASSERT(gTestFallbackFontsXml); |
546 SkASSERT(gTestBasePath); | 546 SkASSERT(gTestBasePath); |
547 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", | 547 SkDEBUGF(("Test BasePath: %s Fonts: %s FallbackFonts: %s\n", |
548 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); | 548 gTestBasePath, gTestFontsXml, gTestFallbackFontsXml)); |
549 } | 549 } |
OLD | NEW |