Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: src/ports/SkFontMgr_android.cpp

Issue 1673373003: Add support for caching font files in the Android SkFontMgr. (Closed) Base URL: https://chromium.googlesource.com/skia.git@master
Patch Set: Add enclosing brackets for if statement. Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/ports/SkFontMgr_android.h ('k') | src/ports/SkFontMgr_android_factory.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
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
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 : fIsolated(custom ? custom->fIsolated : false)
327 {
309 SkTDArray<FontFamily*> families; 328 SkTDArray<FontFamily*> families;
310 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) { 329 if (custom && SkFontMgr_Android_CustomFonts::kPreferSystem != custom->fS ystemFontUse) {
311 SkString base(custom->fBasePath); 330 SkString base(custom->fBasePath);
312 SkFontMgr_Android_Parser::GetCustomFontFamilies( 331 SkFontMgr_Android_Parser::GetCustomFontFamilies(
313 families, base, custom->fFontsXml, custom->fFallbackFontsXml); 332 families, base, custom->fFontsXml, custom->fFallbackFontsXml);
314 } 333 }
315 if (!custom || 334 if (!custom ||
316 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse)) 335 (custom && SkFontMgr_Android_CustomFonts::kOnlyCustom != custom->fSy stemFontUse))
317 { 336 {
318 SkFontMgr_Android_Parser::GetSystemFontFamilies(families); 337 SkFontMgr_Android_Parser::GetSystemFontFamilies(families);
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
530 549
531 SkTypeface_FreeType::Scanner fScanner; 550 SkTypeface_FreeType::Scanner fScanner;
532 551
533 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets; 552 SkTArray<SkAutoTUnref<SkFontStyleSet_Android>, true> fFontStyleSets;
534 SkFontStyleSet* fDefaultFamily; 553 SkFontStyleSet* fDefaultFamily;
535 SkTypeface* fDefaultTypeface; 554 SkTypeface* fDefaultTypeface;
536 555
537 SkTDArray<NameToFamily> fNameToFamilyMap; 556 SkTDArray<NameToFamily> fNameToFamilyMap;
538 SkTDArray<NameToFamily> fFallbackNameToFamilyMap; 557 SkTDArray<NameToFamily> fFallbackNameToFamilyMap;
539 558
559 const bool fIsolated;
bungeman-skia 2016/02/12 15:13:47 Interestingly, we no longer need to store this val
Khushal 2016/02/12 18:24:12 Yup, I missed it in when I was removing the variab
560
540 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) { 561 void buildNameToFamilyMap(SkTDArray<FontFamily*> families) {
541 for (int i = 0; i < families.count(); i++) { 562 for (int i = 0; i < families.count(); i++) {
542 FontFamily& family = *families[i]; 563 FontFamily& family = *families[i];
543 564
544 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap; 565 SkTDArray<NameToFamily>* nameToFamily = &fNameToFamilyMap;
545 if (family.fIsFallbackFont) { 566 if (family.fIsFallbackFont) {
546 nameToFamily = &fFallbackNameToFamilyMap; 567 nameToFamily = &fFallbackNameToFamilyMap;
547 568
548 if (0 == family.fNames.count()) { 569 if (0 == family.fNames.count()) {
549 SkString& fallbackName = family.fNames.push_back(); 570 SkString& fallbackName = family.fNames.push_back();
550 fallbackName.printf("%.2x##fallback", i); 571 fallbackName.printf("%.2x##fallback", i);
551 } 572 }
552 } 573 }
553 574
554 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner); 575 SkFontStyleSet_Android* newSet = new SkFontStyleSet_Android(family, fScanner,
576 fIsolate d);
555 if (0 == newSet->count()) { 577 if (0 == newSet->count()) {
556 delete newSet; 578 delete newSet;
557 continue; 579 continue;
558 } 580 }
559 fFontStyleSets.push_back().reset(newSet); 581 fFontStyleSets.push_back().reset(newSet);
560 582
561 for (int j = 0; j < family.fNames.count(); j++) { 583 for (int j = 0; j < family.fNames.count(); j++) {
562 NameToFamily* nextEntry = nameToFamily->append(); 584 NameToFamily* nextEntry = nameToFamily->append();
563 new (&nextEntry->name) SkString(family.fNames[j]); 585 new (&nextEntry->name) SkString(family.fNames[j]);
564 nextEntry->styleSet = newSet; 586 nextEntry->styleSet = newSet;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
605 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; 627 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ;
606 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , 628 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" ,
607 gSystemFontUseStrings[custom->fSystemFontUse], 629 gSystemFontUseStrings[custom->fSystemFontUse],
608 custom->fBasePath, 630 custom->fBasePath,
609 custom->fFontsXml, 631 custom->fFontsXml,
610 custom->fFallbackFontsXml)); 632 custom->fFallbackFontsXml));
611 } 633 }
612 634
613 return new SkFontMgr_Android(custom); 635 return new SkFontMgr_Android(custom);
614 } 636 }
OLDNEW
« no previous file with comments | « include/ports/SkFontMgr_android.h ('k') | src/ports/SkFontMgr_android_factory.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698