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