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(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) { | |
71 if (cacheFontFiles) { | |
72 SkASSERT(fFile); | |
73 } | |
74 } | |
75 | |
76 SkStreamAsset* createStream() const { | |
77 if (fFile) { | |
78 SkData* data = SkData::NewFromFILE(fFile); | |
79 return data ? new SkMemoryStream(data) : nullptr; | |
80 } | |
81 return SkStream::NewFromFile(fPathName.c_str()); | |
82 } | |
67 | 83 |
68 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override { | 84 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override { |
69 SkASSERT(desc); | 85 SkASSERT(desc); |
70 SkASSERT(serialize); | 86 SkASSERT(serialize); |
71 desc->setFamilyName(fFamilyName.c_str()); | 87 desc->setFamilyName(fFamilyName.c_str()); |
72 *serialize = false; | 88 *serialize = false; |
73 } | 89 } |
74 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 90 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
75 *ttcIndex = fIndex; | 91 *ttcIndex = fIndex; |
76 return SkStream::NewFromFile(fPathName.c_str()); | 92 return this->createStream(); |
77 } | 93 } |
78 SkFontData* onCreateFontData() const override { | 94 SkFontData* onCreateFontData() const override { |
79 return new SkFontData(SkStream::NewFromFile(fPathName.c_str()), fIndex, | 95 return new SkFontData(this->createStream(), fIndex, fAxes.begin(), fAxes .count()); |
80 fAxes.begin(), fAxes.count()); | |
81 } | 96 } |
82 | 97 |
83 const SkString fPathName; | 98 const SkString fPathName; |
84 int fIndex; | 99 int fIndex; |
85 const SkSTArray<4, SkFixed, true> fAxes; | 100 const SkSTArray<4, SkFixed, true> fAxes; |
86 const SkLanguage fLang; | 101 const SkLanguage fLang; |
87 const FontVariant fVariantStyle; | 102 const FontVariant fVariantStyle; |
103 SkAutoTCallVProc<FILE, sk_fclose> fFile; | |
88 | 104 |
89 typedef SkTypeface_Android INHERITED; | 105 typedef SkTypeface_Android INHERITED; |
90 }; | 106 }; |
91 | 107 |
92 class SkTypeface_AndroidStream : public SkTypeface_Android { | 108 class SkTypeface_AndroidStream : public SkTypeface_Android { |
93 public: | 109 public: |
94 SkTypeface_AndroidStream(SkFontData* data, | 110 SkTypeface_AndroidStream(SkFontData* data, |
95 const SkFontStyle& style, | 111 const SkFontStyle& style, |
96 bool isFixedPitch, | 112 bool isFixedPitch, |
97 const SkString& familyName) | 113 const SkString& familyName) |
(...skipping 20 matching lines...) Expand all Loading... | |
118 | 134 |
119 private: | 135 private: |
120 const SkAutoTDelete<const SkFontData> fData; | 136 const SkAutoTDelete<const SkFontData> fData; |
121 typedef SkTypeface_Android INHERITED; | 137 typedef SkTypeface_Android INHERITED; |
122 }; | 138 }; |
123 | 139 |
124 class SkFontStyleSet_Android : public SkFontStyleSet { | 140 class SkFontStyleSet_Android : public SkFontStyleSet { |
125 typedef SkTypeface_FreeType::Scanner Scanner; | 141 typedef SkTypeface_FreeType::Scanner Scanner; |
126 | 142 |
127 public: | 143 public: |
128 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner) { | 144 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner, |
145 const bool cacheFontFiles) { | |
129 const SkString* cannonicalFamilyName = nullptr; | 146 const SkString* cannonicalFamilyName = nullptr; |
130 if (family.fNames.count() > 0) { | 147 if (family.fNames.count() > 0) { |
131 cannonicalFamilyName = &family.fNames[0]; | 148 cannonicalFamilyName = &family.fNames[0]; |
132 } | 149 } |
133 // TODO? make this lazy | 150 // TODO? make this lazy |
134 for (int i = 0; i < family.fFonts.count(); ++i) { | 151 for (int i = 0; i < family.fFonts.count(); ++i) { |
135 const FontFileInfo& fontFile = family.fFonts[i]; | 152 const FontFileInfo& fontFile = family.fFonts[i]; |
136 | 153 |
137 SkString pathName(family.fBasePath); | 154 SkString pathName(family.fBasePath); |
138 pathName.append(fontFile.fFileName); | 155 pathName.append(fontFile.fFileName); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 } | 236 } |
220 if (!found) { | 237 if (!found) { |
221 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\ n", | 238 SkDEBUGF(("Requested font axis not found: %s '%c%c%c%c'\ n", |
222 familyName.c_str(), (skTag >> 24) & 0xFF, | 239 familyName.c_str(), (skTag >> 24) & 0xFF, |
223 (skTag >> 16) & 0xFF, (skTag >> 8) & 0xFF, ( skTag)&0xFF)); | 240 (skTag >> 16) & 0xFF, (skTag >> 8) & 0xFF, ( skTag)&0xFF)); |
224 } | 241 } |
225 } | 242 } |
226 ) | 243 ) |
227 | 244 |
228 fStyles.push_back().reset(new SkTypeface_AndroidSystem( | 245 fStyles.push_back().reset(new SkTypeface_AndroidSystem( |
229 pathName, ttcIndex, axisValues.get(), axisDefinitions.count( ), style, | 246 pathName, cacheFontFiles, ttcIndex, axisValues.get(), axisDe finitions.count(), |
230 isFixedWidth, familyName, lang, variant)); | 247 style, isFixedWidth, familyName, lang, variant)); |
231 } | 248 } |
232 } | 249 } |
233 | 250 |
234 int count() override { | 251 int count() override { |
235 return fStyles.count(); | 252 return fStyles.count(); |
236 } | 253 } |
237 void getStyle(int index, SkFontStyle* style, SkString* name) override { | 254 void getStyle(int index, SkFontStyle* style, SkString* name) override { |
238 if (index < 0 || fStyles.count() <= index) { | 255 if (index < 0 || fStyles.count() <= index) { |
239 return; | 256 return; |
240 } | 257 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 * (non-replicated) set of typefaces. | 315 * (non-replicated) set of typefaces. |
299 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. | 316 * SkTDict<> doesn't let us do index-based lookup, so we write our own mapping. |
300 */ | 317 */ |
301 struct NameToFamily { | 318 struct NameToFamily { |
302 SkString name; | 319 SkString name; |
303 SkFontStyleSet_Android* styleSet; | 320 SkFontStyleSet_Android* styleSet; |
304 }; | 321 }; |
305 | 322 |
306 class SkFontMgr_Android : public SkFontMgr { | 323 class SkFontMgr_Android : public SkFontMgr { |
307 public: | 324 public: |
308 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) { | 325 SkFontMgr_Android(const SkFontMgr_Android_CustomFonts* custom) |
326 { | |
bungeman-skia
2016/02/12 18:59:35
nit: this '{' should go on the previous line (no n
Khushal
2016/02/12 19:34:16
Done.
| |
309 SkTDArray<FontFamily*> families; | 327 SkTDArray<FontFamily*> families; |
310 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) { | 328 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) { |
311 SkString base(custom->fBasePath); | 329 SkString base(custom->fBasePath); |
312 SkFontMgr_Android_Parser::GetCustomFontFamilies( | 330 SkFontMgr_Android_Parser::GetCustomFontFamilies( |
313 families, base, custom->fFontsXml, custom->fFallbackFontsXml); | 331 families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
314 } | 332 } |
315 if (!custom || | 333 if (!custom || |
316 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse)) | 334 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse)) |
317 { | 335 { |
318 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); | 336 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); |
319 } | 337 } |
320 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fS ystemFontUse) { | 338 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem == custom->fS ystemFontUse) { |
321 SkString base(custom->fBasePath); | 339 SkString base(custom->fBasePath); |
322 SkFontMgr_Android_Parser::GetCustomFontFamilies( | 340 SkFontMgr_Android_Parser::GetCustomFontFamilies( |
323 families, base, custom->fFontsXml, custom->fFallbackFontsXml); | 341 families, base, custom->fFontsXml, custom->fFallbackFontsXml); |
324 } | 342 } |
325 this->buildNameToFamilyMap(families); | 343 this->buildNameToFamilyMap(families, custom ? custom->fIsolated : false) ; |
326 this->findDefaultFont(); | 344 this->findDefaultFont(); |
327 families.deleteAll(); | 345 families.deleteAll(); |
328 } | 346 } |
329 | 347 |
330 protected: | 348 protected: |
331 /** Returns not how many families we have, but how many unique names | 349 /** Returns not how many families we have, but how many unique names |
332 * exist among the families. | 350 * exist among the families. |
333 */ | 351 */ |
334 int onCountFamilies() const override { | 352 int onCountFamilies() const override { |
335 return fNameToFamilyMap.count(); | 353 return fNameToFamilyMap.count(); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
530 | 548 |
531 SkTypeface_FreeType::Scanner fScanner; | 549 SkTypeface_FreeType::Scanner fScanner; |
532 | 550 |
533 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; | 551 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; |
534 SkFontStyleSet* fDefaultFamily; | 552 SkFontStyleSet* fDefaultFamily; |
535 SkTypeface* fDefaultTypeface; | 553 SkTypeface* fDefaultTypeface; |
536 | 554 |
537 SkTDArray<NameToFamily> fNameToFamilyMap; | 555 SkTDArray<NameToFamily> fNameToFamilyMap; |
538 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; | 556 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; |
539 | 557 |
540 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { | 558 void buildNameToFamilyMap(SkTDArray<FontFamily*> families, const bool isolat ed) { |
541 for (int i = 0; i < families.count(); i++) { | 559 for (int i = 0; i < families.count(); i++) { |
542 FontFamily& family = *families[i]; | 560 FontFamily& family = *families[i]; |
543 | 561 |
544 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; | 562 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; |
545 if (family.fIsFallbackFont) { | 563 if (family.fIsFallbackFont) { |
546 nameToFamily = &fFallbackNameToFamilyMap; | 564 nameToFamily = &fFallbackNameToFamilyMap; |
547 | 565 |
548 if (0 == family.fNames.count()) { | 566 if (0 == family.fNames.count()) { |
549 SkString& fallbackName = family.fNames.push_back(); | 567 SkString& fallbackName = family.fNames.push_back(); |
550 fallbackName.printf("%.2x##fallback", i); | 568 fallbackName.printf("%.2x##fallback", i); |
551 } | 569 } |
552 } | 570 } |
553 | 571 |
554 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner); | 572 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner, isolated); |
555 if (0 == newSet->count()) { | 573 if (0 == newSet->count()) { |
556 delete newSet; | 574 delete newSet; |
557 continue; | 575 continue; |
558 } | 576 } |
559 fFontStyleSets.push_back().reset(newSet); | 577 fFontStyleSets.push_back().reset(newSet); |
560 | 578 |
561 for (int j = 0; j < family.fNames.count(); j++) { | 579 for (int j = 0; j < family.fNames.count(); j++) { |
562 NameToFamily* nextEntry = nameToFamily->append(); | 580 NameToFamily* nextEntry = nameToFamily->append(); |
563 new (&nextEntry->name) SkString(family.fNames[j]); | 581 new (&nextEntry->name) SkString(family.fNames[j]); |
564 nextEntry->styleSet = newSet; | 582 nextEntry->styleSet = newSet; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
605 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; | 623 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; |
606 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , | 624 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , |
607 gSystemFontUseStrings[custom->fSystemFontUse], | 625 gSystemFontUseStrings[custom->fSystemFontUse], |
608 custom->fBasePath, | 626 custom->fBasePath, |
609 custom->fFontsXml, | 627 custom->fFontsXml, |
610 custom->fFallbackFontsXml)); | 628 custom->fFallbackFontsXml)); |
611 } | 629 } |
612 | 630 |
613 return new SkFontMgr_Android(custom); | 631 return new SkFontMgr_Android(custom); |
614 } | 632 } |
OLD | NEW |