| OLD | NEW |
| 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 "SkAdvancedTypefaceMetrics.h" | 8 #include "SkAdvancedTypefaceMetrics.h" |
| 9 #include "SkDataTable.h" | 9 #include "SkDataTable.h" |
| 10 #include "SkFixed.h" | 10 #include "SkFixed.h" |
| 11 #include "SkFontDescriptor.h" | 11 #include "SkFontDescriptor.h" |
| 12 #include "SkFontHost_FreeType_common.h" | 12 #include "SkFontHost_FreeType_common.h" |
| 13 #include "SkFontMgr.h" | 13 #include "SkFontMgr.h" |
| 14 #include "SkFontStyle.h" | 14 #include "SkFontStyle.h" |
| 15 #include "SkMakeUnique.h" |
| 15 #include "SkMath.h" | 16 #include "SkMath.h" |
| 16 #include "SkMutex.h" | 17 #include "SkMutex.h" |
| 17 #include "SkOSFile.h" | 18 #include "SkOSFile.h" |
| 18 #include "SkRefCnt.h" | 19 #include "SkRefCnt.h" |
| 19 #include "SkStream.h" | 20 #include "SkStream.h" |
| 20 #include "SkString.h" | 21 #include "SkString.h" |
| 21 #include "SkTDArray.h" | 22 #include "SkTDArray.h" |
| 22 #include "SkTemplates.h" | 23 #include "SkTemplates.h" |
| 23 #include "SkTypeface.h" | 24 #include "SkTypeface.h" |
| 24 #include "SkTypefaceCache.h" | 25 #include "SkTypefaceCache.h" |
| (...skipping 373 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 } | 399 } |
| 399 | 400 |
| 400 FcPatternAddInteger(pattern, FC_WEIGHT, weight); | 401 FcPatternAddInteger(pattern, FC_WEIGHT, weight); |
| 401 FcPatternAddInteger(pattern, FC_WIDTH , width); | 402 FcPatternAddInteger(pattern, FC_WIDTH , width); |
| 402 FcPatternAddInteger(pattern, FC_SLANT , slant); | 403 FcPatternAddInteger(pattern, FC_SLANT , slant); |
| 403 } | 404 } |
| 404 | 405 |
| 405 class SkTypeface_stream : public SkTypeface_FreeType { | 406 class SkTypeface_stream : public SkTypeface_FreeType { |
| 406 public: | 407 public: |
| 407 /** @param data takes ownership of the font data.*/ | 408 /** @param data takes ownership of the font data.*/ |
| 408 SkTypeface_stream(SkFontData* data, const SkFontStyle& style, bool fixedWidt
h) | 409 SkTypeface_stream(std::unique_ptr<SkFontData> data, const SkFontStyle& style
, bool fixedWidth) |
| 409 : INHERITED(style, fixedWidth) | 410 : INHERITED(style, fixedWidth) |
| 410 , fData(data) | 411 , fData(std::move(data)) |
| 411 { }; | 412 { }; |
| 412 | 413 |
| 413 void onGetFamilyName(SkString* familyName) const override { | 414 void onGetFamilyName(SkString* familyName) const override { |
| 414 familyName->reset(); | 415 familyName->reset(); |
| 415 } | 416 } |
| 416 | 417 |
| 417 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 418 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
| 418 *serialize = true; | 419 *serialize = true; |
| 419 } | 420 } |
| 420 | 421 |
| 421 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 422 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 422 *ttcIndex = fData->getIndex(); | 423 *ttcIndex = fData->getIndex(); |
| 423 return fData->duplicateStream(); | 424 return fData->getStream()->duplicate(); |
| 424 } | 425 } |
| 425 | 426 |
| 426 SkFontData* onCreateFontData() const override { | 427 std::unique_ptr<SkFontData> onMakeFontData() const override { |
| 427 return new SkFontData(*fData.get()); | 428 return skstd::make_unique<SkFontData>(*fData); |
| 428 } | 429 } |
| 429 | 430 |
| 430 private: | 431 private: |
| 431 const SkAutoTDelete<const SkFontData> fData; | 432 const std::unique_ptr<const SkFontData> fData; |
| 432 | 433 |
| 433 typedef SkTypeface_FreeType INHERITED; | 434 typedef SkTypeface_FreeType INHERITED; |
| 434 }; | 435 }; |
| 435 | 436 |
| 436 class SkTypeface_fontconfig : public SkTypeface_FreeType { | 437 class SkTypeface_fontconfig : public SkTypeface_FreeType { |
| 437 public: | 438 public: |
| 438 /** @param pattern takes ownership of the reference. */ | 439 /** @param pattern takes ownership of the reference. */ |
| 439 static SkTypeface_fontconfig* Create(FcPattern* pattern) { | 440 static SkTypeface_fontconfig* Create(FcPattern* pattern) { |
| 440 return new SkTypeface_fontconfig(pattern); | 441 return new SkTypeface_fontconfig(pattern); |
| 441 } | 442 } |
| 442 mutable SkAutoFcPattern fPattern; | 443 mutable SkAutoFcPattern fPattern; |
| 443 | 444 |
| 444 void onGetFamilyName(SkString* familyName) const override { | 445 void onGetFamilyName(SkString* familyName) const override { |
| 445 *familyName = get_string(fPattern, FC_FAMILY); | 446 *familyName = get_string(fPattern, FC_FAMILY); |
| 446 } | 447 } |
| 447 | 448 |
| 448 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { | 449 void onGetFontDescriptor(SkFontDescriptor* desc, bool* serialize) const over
ride { |
| 449 FCLocker lock; | 450 FCLocker lock; |
| 450 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); | 451 desc->setFamilyName(get_string(fPattern, FC_FAMILY)); |
| 451 desc->setFullName(get_string(fPattern, FC_FULLNAME)); | 452 desc->setFullName(get_string(fPattern, FC_FULLNAME)); |
| 452 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); | 453 desc->setPostscriptName(get_string(fPattern, FC_POSTSCRIPT_NAME)); |
| 453 desc->setStyle(this->fontStyle()); | 454 desc->setStyle(this->fontStyle()); |
| 454 *serialize = false; | 455 *serialize = false; |
| 455 } | 456 } |
| 456 | 457 |
| 457 SkStreamAsset* onOpenStream(int* ttcIndex) const override { | 458 SkStreamAsset* onOpenStream(int* ttcIndex) const override { |
| 458 FCLocker lock; | 459 FCLocker lock; |
| 459 *ttcIndex = get_int(fPattern, FC_INDEX, 0); | 460 *ttcIndex = get_int(fPattern, FC_INDEX, 0); |
| 460 return SkStream::NewFromFile(get_string(fPattern, FC_FILE)); | 461 return SkStream::MakeFromFile(get_string(fPattern, FC_FILE)).release(); |
| 461 } | 462 } |
| 462 | 463 |
| 463 void onFilterRec(SkScalerContextRec* rec) const override { | 464 void onFilterRec(SkScalerContextRec* rec) const override { |
| 464 const FcMatrix* fcMatrix = get_matrix(fPattern, FC_MATRIX); | 465 const FcMatrix* fcMatrix = get_matrix(fPattern, FC_MATRIX); |
| 465 if (fcMatrix) { | 466 if (fcMatrix) { |
| 466 // fPost2x2 is column-major, left handed (y down). | 467 // fPost2x2 is column-major, left handed (y down). |
| 467 // FcMatrix is column-major, right handed (y up). | 468 // FcMatrix is column-major, right handed (y up). |
| 468 SkMatrix fm; | 469 SkMatrix fm; |
| 469 fm.setAll(fcMatrix->xx,-fcMatrix->xy, 0, | 470 fm.setAll(fcMatrix->xx,-fcMatrix->xy, 0, |
| 470 -fcMatrix->yx, fcMatrix->yy, 0, | 471 -fcMatrix->yx, fcMatrix->yy, 0, |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 871 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, | 872 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* typeface, |
| 872 const SkFontStyle& style) const overrid
e | 873 const SkFontStyle& style) const overrid
e |
| 873 { | 874 { |
| 874 //TODO: should the SkTypeface_fontconfig know its family? | 875 //TODO: should the SkTypeface_fontconfig know its family? |
| 875 const SkTypeface_fontconfig* fcTypeface = | 876 const SkTypeface_fontconfig* fcTypeface = |
| 876 static_cast<const SkTypeface_fontconfig*>(typeface); | 877 static_cast<const SkTypeface_fontconfig*>(typeface); |
| 877 return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY
), style); | 878 return this->matchFamilyStyle(get_string(fcTypeface->fPattern, FC_FAMILY
), style); |
| 878 } | 879 } |
| 879 | 880 |
| 880 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { | 881 SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) cons
t override { |
| 881 SkAutoTDelete<SkStreamAsset> stream(bareStream); | 882 std::unique_ptr<SkStreamAsset> stream(bareStream); |
| 882 const size_t length = stream->getLength(); | 883 const size_t length = stream->getLength(); |
| 883 if (length <= 0 || (1u << 30) < length) { | 884 if (length <= 0 || (1u << 30) < length) { |
| 884 return nullptr; | 885 return nullptr; |
| 885 } | 886 } |
| 886 | 887 |
| 887 SkFontStyle style; | 888 SkFontStyle style; |
| 888 bool isFixedWidth = false; | 889 bool isFixedWidth = false; |
| 889 if (!fScanner.scanFont(stream, ttcIndex, nullptr, &style, &isFixedWidth,
nullptr)) { | 890 if (!fScanner.scanFont(stream.get(), ttcIndex, nullptr, &style, &isFixed
Width, nullptr)) { |
| 890 return nullptr; | 891 return nullptr; |
| 891 } | 892 } |
| 892 | 893 |
| 893 return new SkTypeface_stream(new SkFontData(stream.release(), ttcIndex,
nullptr, 0), style, | 894 auto data = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex,
nullptr, 0); |
| 894 isFixedWidth); | 895 return new SkTypeface_stream(std::move(data), style, isFixedWidth); |
| 895 } | 896 } |
| 896 | 897 |
| 897 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { | 898 SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& param
s) const override { |
| 898 using Scanner = SkTypeface_FreeType::Scanner; | 899 using Scanner = SkTypeface_FreeType::Scanner; |
| 899 SkAutoTDelete<SkStreamAsset> stream(s); | 900 std::unique_ptr<SkStreamAsset> stream(s); |
| 900 bool isFixedPitch; | 901 bool isFixedPitch; |
| 901 SkFontStyle style; | 902 SkFontStyle style; |
| 902 SkString name; | 903 SkString name; |
| 903 Scanner::AxisDefinitions axisDefinitions; | 904 Scanner::AxisDefinitions axisDefinitions; |
| 904 if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &styl
e, &isFixedPitch, | 905 if (!fScanner.scanFont(stream.get(), params.getCollectionIndex(), |
| 905 &axisDefinitions)) | 906 &name, &style, &isFixedPitch, &axisDefinitions)) |
| 906 { | 907 { |
| 907 return nullptr; | 908 return nullptr; |
| 908 } | 909 } |
| 909 | 910 |
| 910 int paramAxisCount; | 911 int paramAxisCount; |
| 911 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); | 912 const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount); |
| 912 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); | 913 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count()); |
| 913 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); | 914 Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, a
xisValues, name); |
| 914 | 915 |
| 915 SkFontData* data(new SkFontData(stream.release(), params.getCollectionIn
dex(), | 916 auto data = skstd::make_unique<SkFontData>(std::move(stream), params.get
CollectionIndex(), |
| 916 axisValues.get(), axisDefinitions.count(
))); | 917 axisValues.get(), axisDefinit
ions.count()); |
| 917 return new SkTypeface_stream(data, style, isFixedPitch); | 918 return new SkTypeface_stream(std::move(data), style, isFixedPitch); |
| 918 } | 919 } |
| 919 | 920 |
| 920 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { | 921 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override { |
| 921 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn
dex); | 922 return this->createFromStream(new SkMemoryStream(sk_ref_sp(data)), ttcIn
dex); |
| 922 } | 923 } |
| 923 | 924 |
| 924 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ | 925 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override
{ |
| 925 return this->createFromStream(SkStream::NewFromFile(path), ttcIndex); | 926 return this->createFromStream(SkStream::MakeFromFile(path).release(), tt
cIndex); |
| 926 } | 927 } |
| 927 | 928 |
| 928 SkTypeface* onCreateFromFontData(SkFontData* fontData) const override { | 929 SkTypeface* onCreateFromFontData(std::unique_ptr<SkFontData> fontData) const
override { |
| 929 SkStreamAsset* stream(fontData->getStream()); | 930 SkStreamAsset* stream(fontData->getStream()); |
| 930 const size_t length = stream->getLength(); | 931 const size_t length = stream->getLength(); |
| 931 if (length <= 0 || (1u << 30) < length) { | 932 if (length <= 0 || (1u << 30) < length) { |
| 932 return nullptr; | 933 return nullptr; |
| 933 } | 934 } |
| 934 | 935 |
| 935 const int ttcIndex = fontData->getIndex(); | 936 const int ttcIndex = fontData->getIndex(); |
| 936 SkFontStyle style; | 937 SkFontStyle style; |
| 937 bool isFixedWidth = false; | 938 bool isFixedWidth = false; |
| 938 if (!fScanner.scanFont(stream, ttcIndex, nullptr, &style, &isFixedWidth,
nullptr)) { | 939 if (!fScanner.scanFont(stream, ttcIndex, nullptr, &style, &isFixedWidth,
nullptr)) { |
| 939 return nullptr; | 940 return nullptr; |
| 940 } | 941 } |
| 941 | 942 |
| 942 return new SkTypeface_stream(fontData, style, isFixedWidth); | 943 return new SkTypeface_stream(std::move(fontData), style, isFixedWidth); |
| 943 } | 944 } |
| 944 | 945 |
| 945 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { | 946 SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle styl
e) const override { |
| 946 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); | 947 SkAutoTUnref<SkTypeface> typeface(this->matchFamilyStyle(familyName, sty
le)); |
| 947 if (typeface.get()) { | 948 if (typeface.get()) { |
| 948 return typeface.release(); | 949 return typeface.release(); |
| 949 } | 950 } |
| 950 | 951 |
| 951 return this->matchFamilyStyle(nullptr, style); | 952 return this->matchFamilyStyle(nullptr, style); |
| 952 } | 953 } |
| 953 }; | 954 }; |
| 954 | 955 |
| 955 SK_API SkFontMgr* SkFontMgr_New_FontConfig(FcConfig* fc) { | 956 SK_API SkFontMgr* SkFontMgr_New_FontConfig(FcConfig* fc) { |
| 956 return new SkFontMgr_fontconfig(fc); | 957 return new SkFontMgr_fontconfig(fc); |
| 957 } | 958 } |
| OLD | NEW |