From 59637dd6f928756c124b18d3e8afde1bb5a178a9 Mon Sep 17 00:00:00 2001 From: Ben Wagner Date: Mon, 5 Oct 2020 18:25:03 -0400 Subject: [PATCH] Add SkTypeface::getPostScriptName. Bug: skia:10234 Change-Id: Idfa1261e36174a4b4223b8eb62c872448fc58b14 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/322680 Reviewed-by: Herb Derby Commit-Queue: Ben Wagner --- include/core/SkTypeface.h | 8 ++++++++ src/core/SkTypeface.cpp | 7 +++++++ src/core/SkTypeface_remote.h | 3 +++ src/ports/SkFontHost_FreeType.cpp | 17 +++++++++++++++++ src/ports/SkFontHost_FreeType_common.h | 1 + src/ports/SkFontHost_win.cpp | 1 + src/ports/SkTypeface_mac_ct.cpp | 11 +++++++++++ src/ports/SkTypeface_mac_ct.h | 1 + src/ports/SkTypeface_win_dw.cpp | 19 +++++++++++++++++++ src/ports/SkTypeface_win_dw.h | 1 + src/utils/SkCustomTypeface.cpp | 5 +++++ tests/FontMgrTest.cpp | 1 + tests/TypefaceTest.cpp | 16 ++++++++++++++++ tools/fonts/RandomScalerContext.cpp | 4 ++++ tools/fonts/RandomScalerContext.h | 17 +++++++++-------- tools/fonts/TestEmptyTypeface.h | 7 ++++--- tools/fonts/TestSVGTypeface.cpp | 2 ++ tools/fonts/TestSVGTypeface.h | 11 ++++++----- tools/fonts/TestTypeface.cpp | 2 ++ tools/fonts/TestTypeface.h | 7 ++++--- 20 files changed, 122 insertions(+), 19 deletions(-) diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h index 20b36ebd9a..ca139aef7d 100644 --- a/include/core/SkTypeface.h +++ b/include/core/SkTypeface.h @@ -300,6 +300,13 @@ public: */ void getFamilyName(SkString* name) const; + /** + * Return the PostScript name for this typeface. + * Value may change based on variation parameters. + * Returns false if no PostScript name is available. + */ + bool getPostScriptName(SkString* name) const; + /** * Return a stream for the contents of the font data, or NULL on failure. * If ttcIndex is not null, it is set to the TrueTypeCollection index @@ -387,6 +394,7 @@ protected: * This name may or may not be produced by the family name iterator. */ virtual void onGetFamilyName(SkString* familyName) const = 0; + virtual bool onGetPostScriptName(SkString*) const = 0; /** Returns an iterator over the family names in the font. */ virtual LocalizedStrings* onCreateFamilyNameIterator() const = 0; diff --git a/src/core/SkTypeface.cpp b/src/core/SkTypeface.cpp index 19de9891b2..e674db01de 100644 --- a/src/core/SkTypeface.cpp +++ b/src/core/SkTypeface.cpp @@ -63,6 +63,9 @@ protected: void onGetFamilyName(SkString* familyName) const override { familyName->reset(); } + bool onGetPostScriptName(SkString*) const override { + return false; + } SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { return new EmptyLocalizedStrings; } @@ -332,6 +335,10 @@ void SkTypeface::getFamilyName(SkString* name) const { this->onGetFamilyName(name); } +bool SkTypeface::getPostScriptName(SkString* name) const { + return this->onGetPostScriptName(name); +} + void SkTypeface::getGlyphToUnicodeMap(SkUnichar* dst) const { sk_bzero(dst, sizeof(SkUnichar) * this->countGlyphs()); } diff --git a/src/core/SkTypeface_remote.h b/src/core/SkTypeface_remote.h index 5dca39c225..ba0a2b40cd 100644 --- a/src/core/SkTypeface_remote.h +++ b/src/core/SkTypeface_remote.h @@ -78,6 +78,9 @@ protected: // Used by SkStrikeCache::DumpMemoryStatistics. *familyName = ""; } + bool onGetPostScriptName(SkString*) const override { + SK_ABORT("Should never be called."); + } SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { SK_ABORT("Should never be called."); } diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp index 34bcf184f5..f73b5fefc7 100644 --- a/src/ports/SkFontHost_FreeType.cpp +++ b/src/ports/SkFontHost_FreeType.cpp @@ -696,6 +696,23 @@ void SkTypeface_FreeType::getPostScriptGlyphNames(SkString* dstArray) const { } } +bool SkTypeface_FreeType::onGetPostScriptName(SkString* skPostScriptName) const { + AutoFTAccess fta(this); + FT_Face face = fta.face(); + if (!face) { + return false; + } + + const char* ftPostScriptName = FT_Get_Postscript_Name(face); + if (!ftPostScriptName) { + return false; + } + if (skPostScriptName) { + *skPostScriptName = ftPostScriptName; + } + return true; +} + /////////////////////////////////////////////////////////////////////////// static bool bothZero(SkScalar a, SkScalar b) { diff --git a/src/ports/SkFontHost_FreeType_common.h b/src/ports/SkFontHost_FreeType_common.h index 919cff0a0f..c9aea8d544 100644 --- a/src/ports/SkFontHost_FreeType_common.h +++ b/src/ports/SkFontHost_FreeType_common.h @@ -106,6 +106,7 @@ protected: void getGlyphToUnicodeMap(SkUnichar*) const override; std::unique_ptr onGetAdvancedMetrics() const override; void getPostScriptGlyphNames(SkString* dstArray) const override; + bool onGetPostScriptName(SkString*) const override; int onGetUPEM() const override; bool onGetKerningPairAdjustments(const uint16_t glyphs[], int count, int32_t adjustments[]) const override; diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp index 1208143d76..6a0b3c9691 100644 --- a/src/ports/SkFontHost_win.cpp +++ b/src/ports/SkFontHost_win.cpp @@ -285,6 +285,7 @@ protected: void getPostScriptGlyphNames(SkString*) const override; int onGetUPEM() const override; void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override { return false; } SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const override diff --git a/src/ports/SkTypeface_mac_ct.cpp b/src/ports/SkTypeface_mac_ct.cpp index 8be4142424..3b70f360f8 100644 --- a/src/ports/SkTypeface_mac_ct.cpp +++ b/src/ports/SkTypeface_mac_ct.cpp @@ -1096,6 +1096,17 @@ void SkTypeface_Mac::onGetFamilyName(SkString* familyName) const { get_str(CTFontCopyFamilyName(fFontRef.get()), familyName); } +bool SkTypeface_Mac::onGetPostScriptName(SkString* skPostScriptName) const { + SkUniqueCFRef ctPostScriptName(CTFontCopyPostScriptName(fFontRef.get())); + if (!ctPostScriptName) { + return false; + } + if (skPostScriptName) { + SkStringFromCFString(ctPostScriptName.get(), skPostScriptName); + } + return true; +} + void SkTypeface_Mac::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const { SkString tmpStr; diff --git a/src/ports/SkTypeface_mac_ct.h b/src/ports/SkTypeface_mac_ct.h index abe6333da4..71828ec0ff 100644 --- a/src/ports/SkTypeface_mac_ct.h +++ b/src/ports/SkTypeface_mac_ct.h @@ -96,6 +96,7 @@ protected: int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const override; void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetTableTags(SkFontTableTag tags[]) const override; size_t onGetTableData(SkFontTableTag, size_t offset, size_t length, void* data) const override; diff --git a/src/ports/SkTypeface_win_dw.cpp b/src/ports/SkTypeface_win_dw.cpp index a46c042643..849de7ed61 100644 --- a/src/ports/SkTypeface_win_dw.cpp +++ b/src/ports/SkTypeface_win_dw.cpp @@ -54,6 +54,25 @@ void DWriteFontTypeface::onGetFamilyName(SkString* familyName) const { sk_get_locale_string(familyNames.get(), nullptr/*fMgr->fLocaleName.get()*/, familyName); } +bool DWriteFontTypeface::onGetPostScriptName(SkString* skPostScriptName) const { + SkString localSkPostScriptName; + SkTScopedComPtr postScriptNames; + BOOL exists = FALSE; + if (FAILED(fDWriteFont->GetInformationalStrings( + DWRITE_INFORMATIONAL_STRING_POSTSCRIPT_NAME, + &postScriptNames, + &exists)) || + !exists || + FAILED(sk_get_locale_string(postScriptNames.get(), nullptr, &localSkPostScriptName))) + { + return false; + } + if (skPostScriptName) { + *skPostScriptName = localSkPostScriptName; + } + return true; +} + void DWriteFontTypeface::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const { // Get the family name. diff --git a/src/ports/SkTypeface_win_dw.h b/src/ports/SkTypeface_win_dw.h index c631fb887e..75e27e6443 100644 --- a/src/ports/SkTypeface_win_dw.h +++ b/src/ports/SkTypeface_win_dw.h @@ -134,6 +134,7 @@ protected: void getPostScriptGlyphNames(SkString*) const override; int onGetUPEM() const override; void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], int coordinateCount) const override; diff --git a/src/utils/SkCustomTypeface.cpp b/src/utils/SkCustomTypeface.cpp index 5649b475a7..debffa3637 100644 --- a/src/utils/SkCustomTypeface.cpp +++ b/src/utils/SkCustomTypeface.cpp @@ -62,6 +62,7 @@ private: void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; std::unique_ptr onOpenStream(int*) const override; @@ -173,6 +174,10 @@ void SkUserTypeface::onGetFamilyName(SkString* familyName) const { *familyName = ""; } +bool SkUserTypeface::onGetPostScriptName(SkString*) const { + return false; +} + SkTypeface::LocalizedStrings* SkUserTypeface::onCreateFamilyNameIterator() const { return nullptr; } diff --git a/tests/FontMgrTest.cpp b/tests/FontMgrTest.cpp index 866437d337..a1faa2c4a7 100644 --- a/tests/FontMgrTest.cpp +++ b/tests/FontMgrTest.cpp @@ -154,6 +154,7 @@ DEF_TEST(FontMgr_MatchStyleCSS3, reporter) { void onGetFamilyName(SkString* familyName) const override { familyName->reset(); } + bool onGetPostScriptName(SkString*) const override { return false; } SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { return new EmptyLocalizedStrings; } diff --git a/tests/TypefaceTest.cpp b/tests/TypefaceTest.cpp index f885d0696c..4501e7a64d 100644 --- a/tests/TypefaceTest.cpp +++ b/tests/TypefaceTest.cpp @@ -100,6 +100,22 @@ DEF_TEST(TypefaceStyle, reporter) { } } +DEF_TEST(TypefacePostScriptName, reporter) { + sk_sp typeface(MakeResourceAsTypeface("fonts/Em.ttf")); + if (!typeface) { + // Not all SkFontMgr can MakeFromStream(). + return; + } + + SkString postScriptName; + bool hasName = typeface->getPostScriptName(&postScriptName); + bool hasName2 = typeface->getPostScriptName(nullptr); + REPORTER_ASSERT(reporter, hasName == hasName2); + if (hasName) { + REPORTER_ASSERT(reporter, postScriptName == SkString("Em")); + } +} + DEF_TEST(TypefaceRoundTrip, reporter) { sk_sp typeface(MakeResourceAsTypeface("fonts/7630.otf")); if (!typeface) { diff --git a/tools/fonts/RandomScalerContext.cpp b/tools/fonts/RandomScalerContext.cpp index 1fe188a2a6..5dd1ae5c53 100644 --- a/tools/fonts/RandomScalerContext.cpp +++ b/tools/fonts/RandomScalerContext.cpp @@ -193,6 +193,10 @@ void SkRandomTypeface::onGetFamilyName(SkString* familyName) const { fProxy->getFamilyName(familyName); } +bool SkRandomTypeface::onGetPostScriptName(SkString* postScriptName) const { + return fProxy->getPostScriptName(postScriptName); +} + SkTypeface::LocalizedStrings* SkRandomTypeface::onCreateFamilyNameIterator() const { return fProxy->createFamilyNameIterator(); } diff --git a/tools/fonts/RandomScalerContext.h b/tools/fonts/RandomScalerContext.h index 2601fd4209..a3e6b9feee 100644 --- a/tools/fonts/RandomScalerContext.h +++ b/tools/fonts/RandomScalerContext.h @@ -20,24 +20,25 @@ class SkRandomTypeface : public SkTypeface { public: SkRandomTypeface(sk_sp proxy, const SkPaint&, bool fakeit); - SkTypeface* proxy() const { return fProxy.get(); } + SkTypeface* proxy() const { return fProxy.get(); } const SkPaint& paint() const { return fPaint; } protected: - SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, - const SkDescriptor*) const override; - void onFilterRec(SkScalerContextRec*) const override; - void getGlyphToUnicodeMap(SkUnichar*) const override; + SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, + const SkDescriptor*) const override; + void onFilterRec(SkScalerContextRec*) const override; + void getGlyphToUnicodeMap(SkUnichar*) const override; std::unique_ptr onGetAdvancedMetrics() const override; - std::unique_ptr onOpenStream(int* ttcIndex) const override; + std::unique_ptr onOpenStream(int* ttcIndex) const override; sk_sp onMakeClone(const SkFontArguments& args) const override; - void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const override; + void onGetFontDescriptor(SkFontDescriptor*, bool* isLocal) const override; void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override; int onCountGlyphs() const override; int onGetUPEM() const override; - void onGetFamilyName(SkString* familyName) const override; + void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; void getPostScriptGlyphNames(SkString*) const override; diff --git a/tools/fonts/TestEmptyTypeface.h b/tools/fonts/TestEmptyTypeface.h index a340ffec44..5e08a32922 100644 --- a/tools/fonts/TestEmptyTypeface.h +++ b/tools/fonts/TestEmptyTypeface.h @@ -21,7 +21,7 @@ protected: TestEmptyTypeface() : SkTypeface(SkFontStyle(), true) {} std::unique_ptr onOpenStream(int* ttcIndex) const override { return nullptr; } - sk_sp onMakeClone(const SkFontArguments& args) const override { + sk_sp onMakeClone(const SkFontArguments& args) const override { return sk_ref_sp(this); } SkScalerContext* onCreateScalerContext(const SkScalerContextEffects& effects, @@ -30,11 +30,11 @@ protected: sk_ref_sp(const_cast(this)), effects, desc); } - void onFilterRec(SkScalerContextRec*) const override {} + void onFilterRec(SkScalerContextRec*) const override {} std::unique_ptr onGetAdvancedMetrics() const override { return nullptr; } - void onGetFontDescriptor(SkFontDescriptor*, bool*) const override {} + void onGetFontDescriptor(SkFontDescriptor*, bool*) const override {} void onCharsToGlyphs(const SkUnichar* chars, int count, SkGlyphID glyphs[]) const override { sk_bzero(glyphs, count * sizeof(glyphs[0])); } @@ -47,6 +47,7 @@ protected: bool next(SkTypeface::LocalizedString*) override { return false; } }; void onGetFamilyName(SkString* familyName) const override { familyName->reset(); } + bool onGetPostScriptName(SkString*) const override { return false; } SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override { return new EmptyLocalizedStrings; } diff --git a/tools/fonts/TestSVGTypeface.cpp b/tools/fonts/TestSVGTypeface.cpp index d3f25a0762..5c19493882 100644 --- a/tools/fonts/TestSVGTypeface.cpp +++ b/tools/fonts/TestSVGTypeface.cpp @@ -159,6 +159,8 @@ void TestSVGTypeface::onCharsToGlyphs(const SkUnichar uni[], int count, SkGlyphI void TestSVGTypeface::onGetFamilyName(SkString* familyName) const { *familyName = fName; } +bool TestSVGTypeface::onGetPostScriptName(SkString*) const { return false; } + SkTypeface::LocalizedStrings* TestSVGTypeface::onCreateFamilyNameIterator() const { SkString familyName(fName); SkString language("und"); // undetermined diff --git a/tools/fonts/TestSVGTypeface.h b/tools/fonts/TestSVGTypeface.h index 44d3af216f..bb0b9e9fe7 100644 --- a/tools/fonts/TestSVGTypeface.h +++ b/tools/fonts/TestSVGTypeface.h @@ -80,10 +80,10 @@ public: protected: void exportTtxCommon(SkWStream*, const char* type, const SkTArray* = nullptr) const; - SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, - const SkDescriptor* desc) const override; - void onFilterRec(SkScalerContextRec* rec) const override; - void getGlyphToUnicodeMap(SkUnichar*) const override; + SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, + const SkDescriptor* desc) const override; + void onFilterRec(SkScalerContextRec* rec) const override; + void getGlyphToUnicodeMap(SkUnichar*) const override; std::unique_ptr onGetAdvancedMetrics() const override; std::unique_ptr onOpenStream(int* ttcIndex) const override { return nullptr; } @@ -102,7 +102,8 @@ protected: int onGetUPEM() const override { return fUpem; } - void onGetFamilyName(SkString* familyName) const override; + void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[], diff --git a/tools/fonts/TestTypeface.cpp b/tools/fonts/TestTypeface.cpp index fa4ba9d38e..c6927197c5 100644 --- a/tools/fonts/TestTypeface.cpp +++ b/tools/fonts/TestTypeface.cpp @@ -137,6 +137,8 @@ void TestTypeface::onCharsToGlyphs(const SkUnichar* uni, int count, SkGlyphID gl void TestTypeface::onGetFamilyName(SkString* familyName) const { *familyName = fTestFont->fName; } +bool TestTypeface::onGetPostScriptName(SkString*) const { return false; } + SkTypeface::LocalizedStrings* TestTypeface::onCreateFamilyNameIterator() const { SkString familyName(fTestFont->fName); SkString language("und"); // undetermined diff --git a/tools/fonts/TestTypeface.h b/tools/fonts/TestTypeface.h index bd3c92397e..8ed037b69d 100644 --- a/tools/fonts/TestTypeface.h +++ b/tools/fonts/TestTypeface.h @@ -72,8 +72,8 @@ public: protected: SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&, const SkDescriptor* desc) const override; - void onFilterRec(SkScalerContextRec* rec) const override; - void getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const override; + void onFilterRec(SkScalerContextRec* rec) const override; + void getGlyphToUnicodeMap(SkUnichar* glyphToUnicode) const override; std::unique_ptr onGetAdvancedMetrics() const override; std::unique_ptr onOpenStream(int* ttcIndex) const override { return nullptr; } @@ -92,7 +92,8 @@ protected: int onGetUPEM() const override { return 2048; } - void onGetFamilyName(SkString* familyName) const override; + void onGetFamilyName(SkString* familyName) const override; + bool onGetPostScriptName(SkString*) const override; SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override; int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],