| OLD | NEW |
| 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 Loading... |
| 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) |
| 106 , fPath(path) | 107 , fPath(path) |
| 107 { } | 108 { } |
| 108 | 109 |
| 109 protected: | 110 protected: |
| 110 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 111 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 111 *ttcIndex = this->getIndex(); | 112 *ttcIndex = this->getIndex(); |
| 112 return SkStream::NewFromFile(fPath.c_str()); | 113 return SkStream::MakeFromFile(fPath.c_str()).release(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 private: | 116 private: |
| 116 SkString fPath; | 117 SkString fPath; |
| 117 | 118 |
| 118 typedef SkTypeface_Custom INHERITED; | 119 typedef SkTypeface_Custom INHERITED; |
| 119 }; | 120 }; |
| 120 | 121 |
| 121 /////////////////////////////////////////////////////////////////////////////// | 122 /////////////////////////////////////////////////////////////////////////////// |
| 122 | 123 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(¶mAxisCount); | 286 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); |
| 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 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(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 |
| 316 if (familyName) { | 315 if (familyName) { |
| 317 tf = this->onMatchFamilyStyle(familyName, style); | 316 tf = this->onMatchFamilyStyle(familyName, style); |
| 318 } | 317 } |
| 319 | 318 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 | 364 |
| 366 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner
, | 365 static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner
, |
| 367 const SkString& directory, const char* suff
ix, | 366 const SkString& directory, const char* suff
ix, |
| 368 SkFontMgr_Custom::Families* families) | 367 SkFontMgr_Custom::Families* families) |
| 369 { | 368 { |
| 370 SkOSFile::Iter iter(directory.c_str(), suffix); | 369 SkOSFile::Iter iter(directory.c_str(), suffix); |
| 371 SkString name; | 370 SkString name; |
| 372 | 371 |
| 373 while (iter.next(&name, false)) { | 372 while (iter.next(&name, false)) { |
| 374 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); | 373 SkString filename(SkOSPath::Join(directory.c_str(), name.c_str())); |
| 375 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str(
))); | 374 std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(filen
ame.c_str()); |
| 376 if (!stream.get()) { | 375 if (!stream) { |
| 377 SkDebugf("---- failed to open <%s>\n", filename.c_str()); | 376 SkDebugf("---- failed to open <%s>\n", filename.c_str()); |
| 378 continue; | 377 continue; |
| 379 } | 378 } |
| 380 | 379 |
| 381 int numFaces; | 380 int numFaces; |
| 382 if (!scanner.recognizedFont(stream, &numFaces)) { | 381 if (!scanner.recognizedFont(stream.get(), &numFaces)) { |
| 383 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); | 382 SkDebugf("---- failed to open <%s> as a font\n", filename.c_str(
)); |
| 384 continue; | 383 continue; |
| 385 } | 384 } |
| 386 | 385 |
| 387 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { | 386 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
| 388 bool isFixedPitch; | 387 bool isFixedPitch; |
| 389 SkString realname; | 388 SkString realname; |
| 390 SkFontStyle style = SkFontStyle(); // avoid uninitialized warnin
g | 389 SkFontStyle style = SkFontStyle(); // avoid uninitialized warnin
g |
| 391 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isF
ixedPitch, nullptr)) { | 390 if (!scanner.scanFont(stream.get(), faceIndex, |
| 391 &realname, &style, &isFixedPitch, nullptr)
) |
| 392 { |
| 392 SkDebugf("---- failed to open <%s> <%d> as a font\n", | 393 SkDebugf("---- failed to open <%s> <%d> as a font\n", |
| 393 filename.c_str(), faceIndex); | 394 filename.c_str(), faceIndex); |
| 394 continue; | 395 continue; |
| 395 } | 396 } |
| 396 | 397 |
| 397 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c
_str()); | 398 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c
_str()); |
| 398 if (nullptr == addTo) { | 399 if (nullptr == addTo) { |
| 399 addTo = new SkFontStyleSet_Custom(realname); | 400 addTo = new SkFontStyleSet_Custom(realname); |
| 400 families->push_back().reset(addTo); | 401 families->push_back().reset(addTo); |
| 401 } | 402 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 return families[i].get(); | 456 return families[i].get(); |
| 456 } | 457 } |
| 457 } | 458 } |
| 458 return nullptr; | 459 return nullptr; |
| 459 } | 460 } |
| 460 | 461 |
| 461 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner, | 462 static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner, |
| 462 const uint8_t* data, size_t size, int index, | 463 const uint8_t* data, size_t size, int index, |
| 463 SkFontMgr_Custom::Families* families) | 464 SkFontMgr_Custom::Families* families) |
| 464 { | 465 { |
| 465 SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, fals
e)); | 466 auto stream = skstd::make_unique<SkMemoryStream>(data, size, false); |
| 466 | 467 |
| 467 int numFaces; | 468 int numFaces; |
| 468 if (!scanner.recognizedFont(stream, &numFaces)) { | 469 if (!scanner.recognizedFont(stream.get(), &numFaces)) { |
| 469 SkDebugf("---- failed to open <%d> as a font\n", index); | 470 SkDebugf("---- failed to open <%d> as a font\n", index); |
| 470 return; | 471 return; |
| 471 } | 472 } |
| 472 | 473 |
| 473 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { | 474 for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) { |
| 474 bool isFixedPitch; | 475 bool isFixedPitch; |
| 475 SkString realname; | 476 SkString realname; |
| 476 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning | 477 SkFontStyle style = SkFontStyle(); // avoid uninitialized warning |
| 477 if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixed
Pitch, nullptr)) { | 478 if (!scanner.scanFont(stream.get(), faceIndex, |
| 479 &realname, &style, &isFixedPitch, nullptr)) |
| 480 { |
| 478 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac
eIndex); | 481 SkDebugf("---- failed to open <%d> <%d> as a font\n", index, fac
eIndex); |
| 479 return; | 482 return; |
| 480 } | 483 } |
| 481 | 484 |
| 482 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str
()); | 485 SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str
()); |
| 483 if (nullptr == addTo) { | 486 if (nullptr == addTo) { |
| 484 addTo = new SkFontStyleSet_Custom(realname); | 487 addTo = new SkFontStyleSet_Custom(realname); |
| 485 families->push_back().reset(addTo); | 488 families->push_back().reset(addTo); |
| 486 } | 489 } |
| 487 std::unique_ptr<SkFontData> data( | 490 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), | 491 addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data), |
| 490 style, isFixedPi
tch, | 492 style, isFixedPi
tch, |
| 491 true, realname))
; | 493 true, realname))
; |
| 492 } | 494 } |
| 493 } | 495 } |
| 494 | 496 |
| 495 const SkEmbeddedResourceHeader* fHeader; | 497 const SkEmbeddedResourceHeader* fHeader; |
| 496 }; | 498 }; |
| 497 | 499 |
| 498 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header)
{ | 500 SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header)
{ |
| (...skipping 12 matching lines...) Expand all Loading... |
| 511 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); | 513 SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString()); |
| 512 families->push_back().reset(family); | 514 families->push_back().reset(family); |
| 513 family->appendTypeface(sk_make_sp<SkTypeface_Empty>()); | 515 family->appendTypeface(sk_make_sp<SkTypeface_Empty>()); |
| 514 } | 516 } |
| 515 | 517 |
| 516 }; | 518 }; |
| 517 | 519 |
| 518 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { | 520 SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() { |
| 519 return new SkFontMgr_Custom(EmptyFontLoader()); | 521 return new SkFontMgr_Custom(EmptyFontLoader()); |
| 520 } | 522 } |
| OLD | NEW |