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

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

Issue 2339273002: SkFontData to use smart pointers. (Closed)
Patch Set: Also Mac. 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
OLDNEW
1 /* 1 /*
2 * Copyright 2006 The Android Open Source Project 2 * Copyright 2006 The Android Open Source Project
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 "SkFontDescriptor.h" 8 #include "SkFontDescriptor.h"
9 #include "SkFontHost_FreeType_common.h" 9 #include "SkFontHost_FreeType_common.h"
10 #include "SkFontMgr.h" 10 #include "SkFontMgr.h"
11 #include "SkFontMgr_custom.h" 11 #include "SkFontMgr_custom.h"
12 #include "SkFontStyle.h" 12 #include "SkFontStyle.h"
13 #include "SkMakeUnique.h"
13 #include "SkOSFile.h" 14 #include "SkOSFile.h"
14 #include "SkRefCnt.h" 15 #include "SkRefCnt.h"
15 #include "SkStream.h" 16 #include "SkStream.h"
16 #include "SkString.h" 17 #include "SkString.h"
17 #include "SkTArray.h" 18 #include "SkTArray.h"
18 #include "SkTemplates.h" 19 #include "SkTemplates.h"
19 #include "SkTypeface.h" 20 #include "SkTypeface.h"
20 #include "SkTypefaceCache.h" 21 #include "SkTypefaceCache.h"
21 #include "SkTypes.h" 22 #include "SkTypes.h"
22 23
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData, 78 SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
78 const SkFontStyle& style, bool isFixedPitch, bool sysFont, 79 const SkFontStyle& style, bool isFixedPitch, bool sysFont,
79 const SkString familyName) 80 const SkString familyName)
80 : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex ()) 81 : INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex ())
81 , fData(std::move(fontData)) 82 , fData(std::move(fontData))
82 { } 83 { }
83 84
84 protected: 85 protected:
85 SkStreamAsset* onOpenStream(int* ttcIndex) const override { 86 SkStreamAsset* onOpenStream(int* ttcIndex) const override {
86 *ttcIndex = fData->getIndex(); 87 *ttcIndex = fData->getIndex();
87 return fData->duplicateStream(); 88 return fData->getStream()->duplicate();
88 } 89 }
89 90
90 SkFontData* onCreateFontData() const override { 91 std::unique_ptr<SkFontData> onMakeFontData() const override {
91 return new SkFontData(*fData.get()); 92 return skstd::make_unique<SkFontData>(*fData);
92 } 93 }
93 94
94 private: 95 private:
95 std::unique_ptr<const SkFontData> fData; 96 const std::unique_ptr<const SkFontData> fData;
96 97
97 typedef SkTypeface_Custom INHERITED; 98 typedef SkTypeface_Custom INHERITED;
98 }; 99 };
99 100
100 /** The file SkTypeface implementation for the custom font manager. */ 101 /** The file SkTypeface implementation for the custom font manager. */
101 class SkTypeface_File : public SkTypeface_Custom { 102 class SkTypeface_File : public SkTypeface_Custom {
102 public: 103 public:
103 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont, 104 SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
104 const SkString familyName, const char path[], int index) 105 const SkString familyName, const char path[], int index)
105 : INHERITED(style, isFixedPitch, sysFont, familyName, index) 106 : INHERITED(style, isFixedPitch, sysFont, familyName, index)
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { 264 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
264 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn dex); 265 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn dex);
265 } 266 }
266 267
267 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons t override { 268 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons t override {
268 return this->createFromStream(bareStream, FontParameters().setCollection Index(ttcIndex)); 269 return this->createFromStream(bareStream, FontParameters().setCollection Index(ttcIndex));
269 } 270 }
270 271
271 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param s) const override { 272 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param s) const override {
272 using Scanner = SkTypeface_FreeType::Scanner; 273 using Scanner = SkTypeface_FreeType::Scanner;
273 SkAutoTDelete<SkStreamAsset> stream(s); 274 std::unique_ptr<SkStreamAsset> stream(s);
274 bool isFixedPitch; 275 bool isFixedPitch;
275 SkFontStyle style; 276 SkFontStyle style;
276 SkString name; 277 SkString name;
277 Scanner::AxisDefinitions axisDefinitions; 278 Scanner::AxisDefinitions axisDefinitions;
278 if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &styl e, &isFixedPitch, 279 if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(),
279 &axisDefinitions)) 280 &name, &style, &isFixedPitch, &axisDefinitions))
280 { 281 {
281 return nullptr; 282 return nullptr;
282 } 283 }
283 284
284 int paramAxisCount; 285 int paramAxisCount;
285 const FontParameters::Axis* paramAxes = params.getAxes(&paramAxisCount); 286 const FontParameters::Axis* paramAxes = params.getAxes(&paramAxisCount);
286 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); 287 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
287 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a xisValues, name); 288 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a xisValues, name);
288 289
289 std::unique_ptr<SkFontData> data(new SkFontData(stream.release(), 290 auto data = skstd::make_unique<SkFontData>(std::move(stream), params.get CollectionIndex(),
290 params.getCollectionInde x(), 291 axisValues.get(), axisDefinit ions.count());
291 axisValues.get(), axisDe finitions.count()));
292 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false , name); 292 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false , name);
293 } 293 }
294 294
295 SkTypeface* onCreateFromFontData(SkFontData* data) const override { 295 SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> data) const ove rride {
296 bool isFixedPitch; 296 bool isFixedPitch;
297 SkFontStyle style; 297 SkFontStyle style;
298 SkString name; 298 SkString name;
299 if (!fScanner.scanFont(data->getStream(), data->getIndex(), 299 if (!fScanner.scanFont(data->getStream(), data->getIndex(),
300 &name, &style, &isFixedPitch, nullptr)) 300 &name, &style, &isFixedPitch, nullptr))
301 { 301 {
302 return nullptr; 302 return nullptr;
303 } 303 }
304 std::unique_ptr<SkFontData> unique_data(data); 304 return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false , name);
305 return new SkTypeface_Stream(std::move(unique_data), style, isFixedPitch , false, name);
306 } 305 }
307 306
308 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override { 307 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
309 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path)); 308 SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
310 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr; 309 return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
311 } 310 }
312 311
313 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl e) const override { 312 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl e) const override {
314 SkTypeface* tf = nullptr; 313 SkTypeface* tf = nullptr;
315 314
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 return families[i].get(); 454 return families[i].get();
456 } 455 }
457 } 456 }
458 return nullptr; 457 return nullptr;
459 } 458 }
460 459
461 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner, 460 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner,
462 const uint8_t* data, size_t size, int index, 461 const uint8_t* data, size_t size, int index,
463 SkFontMgr_Custom::Families* families) 462 SkFontMgr_Custom::Families* families)
464 { 463 {
465 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, fals e)); 464 auto stream = skstd::make_unique<SkMemoryStream>(data, size, false);
466 465
467 int numFaces; 466 int numFaces;
468 if (!scanner.recognizedFont(stream, &numFaces)) { 467 if (!scanner.recognizedFont(stream.get(), &numFaces)) {
469 SkDebugf("---- failed to open <%d> as a font\n", index); 468 SkDebugf("---- failed to open <%d> as a font\n", index);
470 return; 469 return;
471 } 470 }
472 471
473 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { 472 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
474 bool isFixedPitch; 473 bool isFixedPitch;
475 SkString realname; 474 SkString realname;
476 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning 475 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
477 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixed Pitch, nullptr)) { 476 if (!scanner.scanFont(stream.get(), faceIndex,
477 &realname, &style, &isFixedPitch, nullptr))
478 {
478 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac eIndex); 479 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac eIndex);
479 return; 480 return;
480 } 481 }
481 482
482 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str ()); 483 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str ());
483 if (nullptr == addTo) { 484 if (nullptr == addTo) {
484 addTo = new SkFontStyleSet_Custom(realname); 485 addTo = new SkFontStyleSet_Custom(realname);
485 families->push_back().reset(addTo); 486 families->push_back().reset(addTo);
486 } 487 }
487 std::unique_ptr<SkFontData> data( 488 auto data = skstd::make_unique<SkFontData>(std::move(stream), faceIn dex, nullptr, 0);
488 new SkFontData(stream.release(), faceIndex, nullptr, 0));
489 addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data), 489 addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data),
490 style, isFixedPi tch, 490 style, isFixedPi tch,
491 true, realname)) ; 491 true, realname)) ;
492 } 492 }
493 } 493 }
494 494
495 const SkEmbeddedResourceHeader* fHeader; 495 const SkEmbeddedResourceHeader* fHeader;
496 }; 496 };
497 497
498 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) { 498 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) {
(...skipping 12 matching lines...) Expand all
511 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); 511 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
512 families->push_back().reset(family); 512 families->push_back().reset(family);
513 family->appendTypeface(sk_make_sp<SkTypeface_Empty>()); 513 family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
514 } 514 }
515 515
516 }; 516 };
517 517
518 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { 518 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() {
519 return new SkFontMgr_Custom(EmptyFontLoader()); 519 return new SkFontMgr_Custom(EmptyFontLoader());
520 } 520 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698