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