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

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

Issue 2343933002: Revert of SkFontData to use smart pointers. (Closed)
Patch Set: Created 4 years, 3 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 | « src/ports/SkFontMgr_FontConfigInterface.cpp ('k') | src/ports/SkFontMgr_custom.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 "SkData.h"
11 #include "SkFixed.h" 11 #include "SkFixed.h"
12 #include "SkFontDescriptor.h" 12 #include "SkFontDescriptor.h"
13 #include "SkFontHost_FreeType_common.h" 13 #include "SkFontHost_FreeType_common.h"
14 #include "SkFontMgr.h" 14 #include "SkFontMgr.h"
15 #include "SkFontMgr_android.h" 15 #include "SkFontMgr_android.h"
16 #include "SkFontMgr_android_parser.h" 16 #include "SkFontMgr_android_parser.h"
17 #include "SkFontStyle.h" 17 #include "SkFontStyle.h"
18 #include "SkMakeUnique.h"
19 #include "SkOSFile.h" 18 #include "SkOSFile.h"
20 #include "SkPaint.h" 19 #include "SkPaint.h"
21 #include "SkRefCnt.h" 20 #include "SkRefCnt.h"
22 #include "SkString.h" 21 #include "SkString.h"
23 #include "SkStream.h" 22 #include "SkStream.h"
24 #include "SkTArray.h" 23 #include "SkTArray.h"
25 #include "SkTDArray.h" 24 #include "SkTDArray.h"
26 #include "SkTSearch.h" 25 #include "SkTSearch.h"
27 #include "SkTemplates.h" 26 #include "SkTemplates.h"
28 #include "SkTypefaceCache.h" 27 #include "SkTypefaceCache.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 , fIndex(index) 66 , fIndex(index)
68 , fAxes(axes, axesCount) 67 , fAxes(axes, axesCount)
69 , fLang(lang) 68 , fLang(lang)
70 , fVariantStyle(variantStyle) 69 , fVariantStyle(variantStyle)
71 , fFile(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) { 70 , fFile(cacheFontFiles ? sk_fopen(fPathName.c_str(), kRead_SkFILE_Flag) : nullptr) {
72 if (cacheFontFiles) { 71 if (cacheFontFiles) {
73 SkASSERT(fFile); 72 SkASSERT(fFile);
74 } 73 }
75 } 74 }
76 75
77 std::unique_ptr<SkStreamAsset> makeStream() const { 76 SkStreamAsset* createStream() const {
78 if (fFile) { 77 if (fFile) {
79 sk_sp<SkData> data(SkData::MakeFromFILE(fFile)); 78 sk_sp<SkData> data(SkData::MakeFromFILE(fFile));
80 return data ? skstd::make_unique<SkMemoryStream>(std::move(data)) : nullptr; 79 return data ? new SkMemoryStream(std::move(data)) : nullptr;
81 } 80 }
82 return SkStream::MakeFromFile(fPathName.c_str()); 81 return SkStream::NewFromFile(fPathName.c_str());
83 } 82 }
84 83
85 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override { 84 virtual void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) co nst override {
86 SkASSERT(desc); 85 SkASSERT(desc);
87 SkASSERT(serialize); 86 SkASSERT(serialize);
88 desc->setFamilyName(fFamilyName.c_str()); 87 desc->setFamilyName(fFamilyName.c_str());
89 desc->setStyle(this->fontStyle()); 88 desc->setStyle(this->fontStyle());
90 *serialize = false; 89 *serialize = false;
91 } 90 }
92 SkStreamAsset* onOpenStream(int* ttcIndex) const override { 91 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
93 *ttcIndex = fIndex; 92 *ttcIndex = fIndex;
94 return this->makeStream().release(); 93 return this->createStream();
95 } 94 }
96 std::unique_ptr<SkFontData> onMakeFontData() const override { 95 SkFontData* onCreateFontData() const override {
97 return skstd::make_unique<SkFontData>(this->makeStream(), fIndex, 96 return new SkFontData(this->createStream(), fIndex, fAxes.begin(), fAxes .count());
98 fAxes.begin(), fAxes.count());
99 } 97 }
100 98
101 const SkString fPathName; 99 const SkString fPathName;
102 int fIndex; 100 int fIndex;
103 const SkSTArray<4, SkFixed, true> fAxes; 101 const SkSTArray<4, SkFixed, true> fAxes;
104 const SkLanguage fLang; 102 const SkLanguage fLang;
105 const FontVariant fVariantStyle; 103 const FontVariant fVariantStyle;
106 SkAutoTCallVProc<FILE, sk_fclose> fFile; 104 SkAutoTCallVProc<FILE, sk_fclose> fFile;
107 105
108 typedef SkTypeface_Android INHERITED; 106 typedef SkTypeface_Android INHERITED;
109 }; 107 };
110 108
111 class SkTypeface_AndroidStream : public SkTypeface_Android { 109 class SkTypeface_AndroidStream : public SkTypeface_Android {
112 public: 110 public:
113 SkTypeface_AndroidStream(std::unique_ptr<SkFontData> data, 111 SkTypeface_AndroidStream(SkFontData* data,
114 const SkFontStyle& style, 112 const SkFontStyle& style,
115 bool isFixedPitch, 113 bool isFixedPitch,
116 const SkString& familyName) 114 const SkString& familyName)
117 : INHERITED(style, isFixedPitch, familyName) 115 : INHERITED(style, isFixedPitch, familyName)
118 , fData(std::move(data)) 116 , fData(data)
119 { } 117 { }
120 118
121 virtual void onGetFontDescriptor(SkFontDescriptor* desc, 119 virtual void onGetFontDescriptor(SkFontDescriptor* desc,
122 bool* serialize) const override { 120 bool* serialize) const override {
123 SkASSERT(desc); 121 SkASSERT(desc);
124 SkASSERT(serialize); 122 SkASSERT(serialize);
125 desc->setFamilyName(fFamilyName.c_str()); 123 desc->setFamilyName(fFamilyName.c_str());
126 *serialize = true; 124 *serialize = true;
127 } 125 }
128 126
129 SkStreamAsset* onOpenStream(int* ttcIndex) const override { 127 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
130 *ttcIndex = fData->getIndex(); 128 *ttcIndex = fData->getIndex();
131 return fData->getStream()->duplicate(); 129 return fData->duplicateStream();
132 } 130 }
133 131
134 std::unique_ptr<SkFontData> onMakeFontData() const override { 132 SkFontData* onCreateFontData() const override {
135 return skstd::make_unique<SkFontData>(*fData); 133 return new SkFontData(*fData.get());
136 } 134 }
137 135
138 private: 136 private:
139 const std::unique_ptr<const SkFontData> fData; 137 const SkAutoTDelete<const SkFontData> fData;
140 typedef SkTypeface_Android INHERITED; 138 typedef SkTypeface_Android INHERITED;
141 }; 139 };
142 140
143 class SkFontStyleSet_Android : public SkFontStyleSet { 141 class SkFontStyleSet_Android : public SkFontStyleSet {
144 typedef SkTypeface_FreeType::Scanner Scanner; 142 typedef SkTypeface_FreeType::Scanner Scanner;
145 143
146 public: 144 public:
147 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner, 145 explicit SkFontStyleSet_Android(const FontFamily& family, const Scanner& sca nner,
148 const bool cacheFontFiles) { 146 const bool cacheFontFiles) {
149 const SkString* cannonicalFamilyName = nullptr; 147 const SkString* cannonicalFamilyName = nullptr;
150 if (family.fNames.count() > 0) { 148 if (family.fNames.count() > 0) {
151 cannonicalFamilyName = &family.fNames[0]; 149 cannonicalFamilyName = &family.fNames[0];
152 } 150 }
153 // TODO? make this lazy 151 // TODO? make this lazy
154 for (int i = 0; i < family.fFonts.count(); ++i) { 152 for (int i = 0; i < family.fFonts.count(); ++i) {
155 const FontFileInfo& fontFile = family.fFonts[i]; 153 const FontFileInfo& fontFile = family.fFonts[i];
156 154
157 SkString pathName(family.fBasePath); 155 SkString pathName(family.fBasePath);
158 pathName.append(fontFile.fFileName); 156 pathName.append(fontFile.fFileName);
159 157
160 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(pathN ame.c_str()); 158 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(pathName.c_str( )));
161 if (!stream) { 159 if (!stream.get()) {
162 SkDEBUGF(("Requested font file %s does not exist or cannot be op ened.\n", 160 SkDEBUGF(("Requested font file %s does not exist or cannot be op ened.\n",
163 pathName.c_str())); 161 pathName.c_str()));
164 continue; 162 continue;
165 } 163 }
166 164
167 const int ttcIndex = fontFile.fIndex; 165 const int ttcIndex = fontFile.fIndex;
168 SkString familyName; 166 SkString familyName;
169 SkFontStyle style; 167 SkFontStyle style;
170 bool isFixedWidth; 168 bool isFixedWidth;
171 Scanner::AxisDefinitions axisDefinitions; 169 Scanner::AxisDefinitions axisDefinitions;
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 } 403 }
406 } 404 }
407 return nullptr; 405 return nullptr;
408 } 406 }
409 407
410 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { 408 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
411 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn dex); 409 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn dex);
412 } 410 }
413 411
414 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { 412 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
415 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path); 413 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
416 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr; 414 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
417 } 415 }
418 416
419 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons t override { 417 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons t override {
420 std::unique_ptr<SkStreamAsset> stream(bareStream); 418 SkAutoTDelete<SkStreamAsset> stream(bareStream);
421 bool isFixedPitch; 419 bool isFixedPitch;
422 SkFontStyle style; 420 SkFontStyle style;
423 SkString name; 421 SkString name;
424 if (!fScanner.scanFont(stream.get(), ttcIndex, &name, &style, &isFixedPi tch, nullptr)) { 422 if (!fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch, n ullptr)) {
425 return nullptr; 423 return nullptr;
426 } 424 }
427 auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0); 425 SkFontData* data(new SkFontData(stream.release(), ttcIndex, nullptr, 0)) ;
428 return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch , name); 426 return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
429 } 427 }
430 428
431 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param s) const override { 429 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param s) const override {
432 using Scanner = SkTypeface_FreeType::Scanner; 430 using Scanner = SkTypeface_FreeType::Scanner;
433 std::unique_ptr<SkStreamAsset> stream(s); 431 SkAutoTDelete<SkStreamAsset> stream(s);
434 bool isFixedPitch; 432 bool isFixedPitch;
435 SkFontStyle style; 433 SkFontStyle style;
436 SkString name; 434 SkString name;
437 Scanner::AxisDefinitions axisDefinitions; 435 Scanner::AxisDefinitions axisDefinitions;
438 if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(), 436 if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &styl e, &isFixedPitch,
439 &name, &style, &isFixedPitch, &axisDefinitions)) 437 &axisDefinitions))
440 { 438 {
441 return nullptr; 439 return nullptr;
442 } 440 }
443 441
444 int paramAxisCount; 442 int paramAxisCount;
445 const FontParameters::Axis* paramAxes = params.getAxes(&paramAxisCount); 443 const FontParameters::Axis* paramAxes = params.getAxes(&paramAxisCount);
446 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); 444 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
447 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a xisValues, name); 445 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a xisValues, name);
448 446
449 auto data = skstd::make_unique<SkFontData>(std::move(stream), params.get CollectionIndex(), 447 SkFontData* data(new SkFontData(stream.release(), params.getCollectionIn dex(),
450 axisValues.get(), axisDefinit ions.count()); 448 axisValues.get(), axisDefinitions.count( )));
451 return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch , name); 449 return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
452 } 450 }
453 451
454 SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> data) const ove rride { 452 SkTypeface* onCreateFromFontData(SkFontData* data) const override {
455 SkStreamAsset* stream(data->getStream()); 453 SkStreamAsset* stream(data->getStream());
456 bool isFixedPitch; 454 bool isFixedPitch;
457 SkFontStyle style; 455 SkFontStyle style;
458 SkString name; 456 SkString name;
459 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixed Pitch, nullptr)) { 457 if (!fScanner.scanFont(stream, data->getIndex(), &name, &style, &isFixed Pitch, nullptr)) {
460 return nullptr; 458 return nullptr;
461 } 459 }
462 return new SkTypeface_AndroidStream(std::move(data), style, isFixedPitch , name); 460 return new SkTypeface_AndroidStream(data, style, isFixedPitch, name);
463 } 461 }
464 462
465 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl e) const override { 463 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl e) const override {
466 if (familyName) { 464 if (familyName) {
467 // On Android, we must return nullptr when we can't find the request ed 465 // On Android, we must return nullptr when we can't find the request ed
468 // named typeface so that the system/app can provide their own recov ery 466 // named typeface so that the system/app can provide their own recov ery
469 // mechanism. On other platforms we'd provide a typeface from the 467 // mechanism. On other platforms we'd provide a typeface from the
470 // default family instead. 468 // default family instead.
471 return this->onMatchFamilyStyle(familyName, style); 469 return this->onMatchFamilyStyle(familyName, style);
472 } 470 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ; 539 SkASSERT(custom->fSystemFontUse < SK_ARRAY_COUNT(gSystemFontUseStrings)) ;
542 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" , 540 SkDEBUGF(("SystemFontUse: %s BasePath: %s Fonts: %s FallbackFonts: %s\n" ,
543 gSystemFontUseStrings[custom->fSystemFontUse], 541 gSystemFontUseStrings[custom->fSystemFontUse],
544 custom->fBasePath, 542 custom->fBasePath,
545 custom->fFontsXml, 543 custom->fFontsXml,
546 custom->fFallbackFontsXml)); 544 custom->fFallbackFontsXml));
547 } 545 }
548 546
549 return new SkFontMgr_Android(custom); 547 return new SkFontMgr_Android(custom);
550 } 548 }
OLDNEW
« no previous file with comments | « src/ports/SkFontMgr_FontConfigInterface.cpp ('k') | src/ports/SkFontMgr_custom.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698