Add SkTypeface::getPostScriptName.

Bug: skia:10234
Change-Id: Idfa1261e36174a4b4223b8eb62c872448fc58b14
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/322680
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
This commit is contained in:
Ben Wagner 2020-10-05 18:25:03 -04:00 committed by Skia Commit-Bot
parent d6cfe72549
commit 59637dd6f9
20 changed files with 122 additions and 19 deletions

View File

@ -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;

View File

@ -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());
}

View File

@ -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.");
}

View File

@ -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) {

View File

@ -106,6 +106,7 @@ protected:
void getGlyphToUnicodeMap(SkUnichar*) const override;
std::unique_ptr<SkAdvancedTypefaceMetrics> 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;

View File

@ -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

View File

@ -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<CFStringRef> 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;

View File

@ -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;

View File

@ -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<IDWriteLocalizedStrings> 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.

View File

@ -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;

View File

@ -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<SkStreamAsset> 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;
}

View File

@ -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;
}

View File

@ -100,6 +100,22 @@ DEF_TEST(TypefaceStyle, reporter) {
}
}
DEF_TEST(TypefacePostScriptName, reporter) {
sk_sp<SkTypeface> 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<SkTypeface> typeface(MakeResourceAsTypeface("fonts/7630.otf"));
if (!typeface) {

View File

@ -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();
}

View File

@ -38,6 +38,7 @@ protected:
int onGetUPEM() const override;
void onGetFamilyName(SkString* familyName) const override;
bool onGetPostScriptName(SkString*) const override;
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
void getPostScriptGlyphNames(SkString*) const override;

View File

@ -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;
}

View File

@ -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

View File

@ -103,6 +103,7 @@ protected:
int onGetUPEM() const override { return fUpem; }
void onGetFamilyName(SkString* familyName) const override;
bool onGetPostScriptName(SkString*) const override;
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],

View File

@ -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

View File

@ -93,6 +93,7 @@ protected:
int onGetUPEM() const override { return 2048; }
void onGetFamilyName(SkString* familyName) const override;
bool onGetPostScriptName(SkString*) const override;
SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const override;
int onGetVariationDesignPosition(SkFontArguments::VariationPosition::Coordinate coordinates[],