Chromium Code Reviews| 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 "SkTypes.h" | 8 #include "SkTypes.h" |
| 9 | 9 |
| 10 #include "SkData.h" | |
| 10 #include "SkFixed.h" | 11 #include "SkFixed.h" |
| 11 #include "SkFontDescriptor.h" | 12 #include "SkFontDescriptor.h" |
| 12 #include "SkFontHost_FreeType_common.h" | 13 #include "SkFontHost_FreeType_common.h" |
| 13 #include "SkFontMgr.h" | 14 #include "SkFontMgr.h" |
| 14 #include "SkFontMgr_android.h" | 15 #include "SkFontMgr_android.h" |
| 15 #include "SkFontMgr_android_parser.h" | 16 #include "SkFontMgr_android_parser.h" |
| 16 #include "SkFontStyle.h" | 17 #include "SkFontStyle.h" |
| 18 #include "SkOSFile.h" | |
| 17 #include "SkPaint.h" | 19 #include "SkPaint.h" |
| 18 #include "SkRefCnt.h" | 20 #include "SkRefCnt.h" |
| 19 #include "SkString.h" | 21 #include "SkString.h" |
| 20 #include "SkStream.h" | 22 #include "SkStream.h" |
| 21 #include "SkTArray.h" | 23 #include "SkTArray.h" |
| 22 #include "SkTDArray.h" | 24 #include "SkTDArray.h" |
| 23 #include "SkTSearch.h" | 25 #include "SkTSearch.h" |
| 24 #include "SkTemplates.h" | 26 #include "SkTemplates.h" |
| 25 #include "SkTypefaceCache.h" | 27 #include "SkTypefaceCache.h" |
| 26 | 28 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 44 | 46 |
| 45 SkString fFamilyName; | 47 SkString fFamilyName; |
| 46 | 48 |
| 47 private: | 49 private: |
| 48 typedef SkTypeface_FreeType INHERITED; | 50 typedef SkTypeface_FreeType INHERITED; |
| 49 }; | 51 }; |
| 50 | 52 |
| 51 class SkTypeface_AndroidSystem : public SkTypeface_Android { | 53 class SkTypeface_AndroidSystem : public SkTypeface_Android { |
| 52 public: | 54 public: |
| 53 SkTypeface_AndroidSystem(const SkString& pathName, | 55 SkTypeface_AndroidSystem(const SkString& pathName, |
| 56 const bool cacheFontFiles, | |
| 54 int index, | 57 int index, |
| 55 const SkFixed* axes, int axesCount, | 58 const SkFixed* axes, int axesCount, |
| 56 const SkFontStyle& style, | 59 const SkFontStyle& style, |
| 57 bool isFixedPitch, | 60 bool isFixedPitch, |
| 58 const SkString& familyName, | 61 const SkString& familyName, |
| 59 const SkLanguage& lang, | 62 const SkLanguage& lang, |
| 60 FontVariant variantStyle) | 63 FontVariant variantStyle) |
| 61 : INHERITED(style, isFixedPitch, familyName) | 64 : INHERITED(style, isFixedPitch, familyName) |
| 62 , fPathName(pathName) | 65 , fPathName(pathName) |
| 63 , fIndex(index) | 66 , fIndex(index) |
| 64 , fAxes(axes, axesCount) | 67 , fAxes(axes, axesCount) |
| 65 , fLang(lang) | 68 , fLang(lang) |
| 66 , fVariantStyle(variantStyle) { } | 69 , fVariantStyle(variantStyle) |
| 70 , fFile( | |
|
bungeman-skia
2016/02/11 20:06:09
Skia allows up to 100 character lines, can probabl
Khushal
2016/02/12 04:54:07
Done.
| |
| 71 cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) | |
| 72 : nullptr) | |
| 73 , closeOnDestroy(fFile) { | |
| 74 if(cacheFontFiles) | |
|
bungeman-skia
2016/02/11 20:06:09
space between 'if' and '('.
Khushal
2016/02/12 04:54:07
Done.
| |
| 75 SkASSERT(fFile); | |
|
bungeman-skia
2016/02/11 20:06:09
four space indents, here and below
Khushal
2016/02/12 04:54:07
Done.
| |
| 76 } | |
| 77 | |
| 78 SkStreamAsset* createStream() const { | |
| 79 if (fFile) { | |
| 80 SkData* data = SkData::NewFromFILE(fFile); | |
| 81 return data ? new SkMemoryStream(data) : nullptr; | |
| 82 } | |
| 83 return SkStream::NewFromFile(fPathName.c_str()); | |
| 84 } | |
| 67 | 85 |
| 68 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override { | 86 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override { |
| 69 SkASSERT(desc); | 87 SkASSERT(desc); |
| 70 SkASSERT(serialize); | 88 SkASSERT(serialize); |
| 71 desc->setFamilyName(fFamilyName.c_str()); | 89 desc->setFamilyName(fFamilyName.c_str()); |
| 72 *serialize = false; | 90 *serialize = false; |
| 73 } | 91 } |
| 74 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 92 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 75 *ttcIndex = fIndex; | 93 *ttcIndex = fIndex; |
| 76 return SkStream::NewFromFile(fPathName.c_str()); | 94 return this->createStream(); |
| 77 } | 95 } |
| 78 SkFontData* onCreateFontData() const override { | 96 SkFontData* onCreateFontData() const override { |
| 79 return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, | 97 return new SkFontData(this->createStream(), fIndex, |
| 80 fAxes.begin(), fAxes.count()); | 98 fAxes.begin(), fAxes.count()); |
| 81 } | 99 } |
| 82 | 100 |
| 83 const SkString fPathName; | 101 const SkString fPathName; |
| 84 int fIndex; | 102 int fIndex; |
| 85 const SkSTArray<4, SkFixed, true> fAxes; | 103 const SkSTArray<4, SkFixed, true> fAxes; |
| 86 const SkLanguage fLang; | 104 const SkLanguage fLang; |
| 87 const FontVariant fVariantStyle; | 105 const FontVariant fVariantStyle; |
| 106 FILE* fFile; | |
| 107 SkAutoTCallVProc<FILE, sk_fclose> closeOnDestroy; | |
|
bungeman-skia
2016/02/11 20:06:09
This extra field isn't needed, SkAUtoTCallVProc is
Khushal
2016/02/12 04:54:07
Oh. Done. Thanks!
| |
| 88 | 108 |
| 89 typedef SkTypeface_Android INHERITED; | 109 typedef SkTypeface_Android INHERITED; |
| 90 }; | 110 }; |
| 91 | 111 |
| 92 class SkTypeface_AndroidStream : public SkTypeface_Android { | 112 class SkTypeface_AndroidStream : public SkTypeface_Android { |
| 93 public: | 113 public: |
| 94 SkTypeface_AndroidStream(SkFontData* data, | 114 SkTypeface_AndroidStream(SkFontData* data, |
| 95 const SkFontStyle& style, | 115 const SkFontStyle& style, |
| 96 bool isFixedPitch, | 116 bool isFixedPitch, |
| 97 const SkString& familyName) | 117 const SkString& familyName) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 118 | 138 |
| 119 private: | 139 private: |
| 120 const SkAutoTDelete<const SkFontData> fData; | 140 const SkAutoTDelete<const SkFontData> fData; |
| 121 typedef SkTypeface_Android INHERITED; | 141 typedef SkTypeface_Android INHERITED; |
| 122 }; | 142 }; |
| 123 | 143 |
| 124 class SkFontStyleSet_Android : public SkFontStyleSet { | 144 class SkFontStyleSet_Android : public SkFontStyleSet { |
| 125 typedef SkTypeface_FreeType::Scanner Scanner; | 145 typedef SkTypeface_FreeType::Scanner Scanner; |
| 126 | 146 |
| 127 public: | 147 public: |
| 128 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner) { | 148 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner, |
| 149 const bool cacheFontFiles) { | |
| 129 const SkString* cannonicalFamilyName = nullptr; | 150 const SkString* cannonicalFamilyName = nullptr; |
| 130 if (family.fNames.count() > 0) { | 151 if (family.fNames.count() > 0) { |
| 131 cannonicalFamilyName = &family.fNames[0]; | 152 cannonicalFamilyName = &family.fNames[0]; |
| 132 } | 153 } |
| 133 // TODO? make this lazy | 154 // TODO? make this lazy |
| 134 for (int i = 0; i < family.fFonts.count(); ++i) { | 155 for (int i = 0; i < family.fFonts.count(); ++i) { |
| 135 const FontFileInfo& fontFile = family.fFonts[i]; | 156 const FontFileInfo& fontFile = family.fFonts[i]; |
| 136 | 157 |
| 137 SkString pathName(family.fBasePath); | 158 SkString pathName(family.fBasePath); |
| 138 pathName.append(fontFile.fFileName); | 159 pathName.append(fontFile.fFileName); |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 } | 240 } |
| 220 if (!found) { | 241 if (!found) { |
| 221 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\ n", | 242 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\ n", |
| 222 familyName.c_str(), (skTag >> 24) & 0xFF, | 243 familyName.c_str(), (skTag >> 24) & 0xFF, |
| 223 (skTag >> 16) & 0xFF, (skTag >> 8) & 0xFF, ( skTag)&0xFF)); | 244 (skTag >> 16) & 0xFF, (skTag >> 8) & 0xFF, ( skTag)&0xFF)); |
| 224 } | 245 } |
| 225 } | 246 } |
| 226 ) | 247 ) |
| 227 | 248 |
| 228 fStyles.push_back().reset(new SkTypeface_AndroidSystem( | 249 fStyles.push_back().reset(new SkTypeface_AndroidSystem( |
| 229 pathName, ttcIndex, axisValues.get(), axisDefinitions.count( ), style, | 250 pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDe finitions.count(), style, |
|
bungeman-skia
2016/02/11 20:06:09
This line is a bit long (more than 100 characters)
Khushal
2016/02/12 04:54:07
Done.
| |
| 230 isFixedWidth, familyName, lang, variant)); | 251 isFixedWidth, familyName, lang, variant)); |
| 231 } | 252 } |
| 232 } | 253 } |
| 233 | 254 |
| 234 int count() override { | 255 int count() override { |
| 235 return fStyles.count(); | 256 return fStyles.count(); |
| 236 } | 257 } |
| 237 void getStyle(int index, SkFontStyle* style, SkString* name) override { | 258 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
| 238 if (index < 0 || fStyles.count() <= index) { | 259 if (index < 0 || fStyles.count() <= index) { |
| 239 return; | 260 return; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 * (non-replicated) set of typefaces. | 319 * (non-replicated) set of typefaces. |
| 299 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. | 320 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
| 300 */ | 321 */ |
| 301 struct NameToFamily { | 322 struct NameToFamily { |
| 302 SkString name; | 323 SkString name; |
| 303 SkFontStyleSet_Android* styleSet; | 324 SkFontStyleSet_Android* styleSet; |
| 304 }; | 325 }; |
| 305 | 326 |
| 306 class SkFontMgr_Android : public SkFontMgr { | 327 class SkFontMgr_Android : public SkFontMgr { |
| 307 public: | 328 public: |
| 308 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { | 329 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) |
| 330 : fIsolated(custom ? custom->fIsolated : false) | |
| 331 { | |
| 309 SkTDArray<FontFamily*> families; | 332 SkTDArray<FontFamily*> families; |
| 310 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) { | 333 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) { |
| 311 SkString base(custom->fBasePath); | 334 SkString base(custom->fBasePath); |
| 312 SkFontMgr_Android_Parser::GetCustomFontFamilies( | 335 SkFontMgr_Android_Parser::GetCustomFontFamilies( |
| 313 families, base, custom->fFontsXml, custom->fFallbackFontsXml); | 336 families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
| 314 } | 337 } |
| 315 if (!custom || | 338 if (!custom || |
| 316 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse)) | 339 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse)) |
| 317 { | 340 { |
| 318 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); | 341 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 513 | 536 |
| 514 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], | 537 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[], |
| 515 unsigned styleBits) const overrid e { | 538 unsigned styleBits) const overrid e { |
| 516 SkFontStyle style = SkFontStyle(styleBits); | 539 SkFontStyle style = SkFontStyle(styleBits); |
| 517 | 540 |
| 518 if (familyName) { | 541 if (familyName) { |
| 519 // On Android, we must return nullptr when we can't find the request ed | 542 // On Android, we must return nullptr when we can't find the request ed |
| 520 // named typeface so that the system/app can provide their own recov ery | 543 // named typeface so that the system/app can provide their own recov ery |
| 521 // mechanism. On other platforms we'd provide a typeface from the | 544 // mechanism. On other platforms we'd provide a typeface from the |
| 522 // default family instead. | 545 // default family instead. |
| 523 return this->onMatchFamilyStyle(familyName, style); | 546 // We only return the default family when the font manager is runnin g |
| 547 // as isolated since we don't expect the app to have a recovery mech anism. | |
| 548 SkTypeface* typeface = this->onMatchFamilyStyle(familyName, style); | |
| 549 if (typeface || !fIsolated) | |
|
bungeman-skia
2016/02/11 20:06:09
I'm going to have to take quite a look into this.
Khushal
2016/02/12 04:54:07
I tried reproducing this to trace back the calls.
bungeman-skia
2016/02/12 15:13:47
That is awesome, I was just building Chromium to t
| |
| 550 return typeface; | |
| 524 } | 551 } |
| 525 return fDefaultFamily->matchStyle(style); | 552 return fDefaultFamily->matchStyle(style); |
| 526 } | 553 } |
| 527 | 554 |
| 528 | 555 |
| 529 private: | 556 private: |
| 530 | 557 |
| 531 SkTypeface_FreeType::Scanner fScanner; | 558 SkTypeface_FreeType::Scanner fScanner; |
| 532 | 559 |
| 533 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 560 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
| 534 SkFontStyleSet* fDefaultFamily; | 561 SkFontStyleSet* fDefaultFamily; |
| 535 SkTypeface* fDefaultTypeface; | 562 SkTypeface* fDefaultTypeface; |
| 536 | 563 |
| 537 SkTDArray<NameToFamily> fNameToFamilyMap; | 564 SkTDArray<NameToFamily> fNameToFamilyMap; |
| 538 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; | 565 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
| 539 | 566 |
| 567 const bool fIsolated; | |
| 568 | |
| 540 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { | 569 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { |
| 541 for (int i = 0; i < families.count(); i++) { | 570 for (int i = 0; i < families.count(); i++) { |
| 542 FontFamily& family = *families[i]; | 571 FontFamily& family = *families[i]; |
| 543 | 572 |
| 544 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; | 573 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
| 545 if (family.fIsFallbackFont) { | 574 if (family.fIsFallbackFont) { |
| 546 nameToFamily = &fFallbackNameToFamilyMap; | 575 nameToFamily = &fFallbackNameToFamilyMap; |
| 547 | 576 |
| 548 if (0 == family.fNames.count()) { | 577 if (0 == family.fNames.count()) { |
| 549 SkString& fallbackName = family.fNames.push_back(); | 578 SkString& fallbackName = family.fNames.push_back(); |
| 550 fallbackName.printf("%.2x##fallback", i); | 579 fallbackName.printf("%.2x##fallback", i); |
| 551 } | 580 } |
| 552 } | 581 } |
| 553 | 582 |
| 554 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner); | 583 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner, fIsolated); |
|
bungeman-skia
2016/02/11 20:06:09
this line ran a bit long too.
Khushal
2016/02/12 04:54:07
Done.
| |
| 555 if (0 == newSet->count()) { | 584 if (0 == newSet->count()) { |
| 556 delete newSet; | 585 delete newSet; |
| 557 continue; | 586 continue; |
| 558 } | 587 } |
| 559 fFontStyleSets.push_back().reset(newSet); | 588 fFontStyleSets.push_back().reset(newSet); |
| 560 | 589 |
| 561 for (int j = 0; j < family.fNames.count(); j++) { | 590 for (int j = 0; j < family.fNames.count(); j++) { |
| 562 NameToFamily* nextEntry = nameToFamily->append(); | 591 NameToFamily* nextEntry = nameToFamily->append(); |
| 563 new (&nextEntry->name) SkString(family.fNames[j]); | 592 new (&nextEntry->name) SkString(family.fNames[j]); |
| 564 nextEntry->styleSet = newSet; | 593 nextEntry->styleSet = newSet; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 605 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; | 634 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; |
| 606 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , | 635 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , |
| 607 gSystemFontUseStrings[custom->fSystemFontUse], | 636 gSystemFontUseStrings[custom->fSystemFontUse], |
| 608 custom->fBasePath, | 637 custom->fBasePath, |
| 609 custom->fFontsXml, | 638 custom->fFontsXml, |
| 610 custom->fFallbackFontsXml)); | 639 custom->fFallbackFontsXml)); |
| 611 } | 640 } |
| 612 | 641 |
| 613 return new SkFontMgr_Android(custom); | 642 return new SkFontMgr_Android(custom); |
| 614 } | 643 } |
| OLD | NEW |