From d347698e6875f98d64b974e3e718ed65e66fe339 Mon Sep 17 00:00:00 2001 From: bungeman Date: Thu, 23 Mar 2023 09:48:22 -0400 Subject: [PATCH] Make SkFontMgr subclasses flexible for sk_sp (flutter/engine#40556) Skia is changing SkFontMgr and SkFontStyleSet methods to consistently return sk_sp and sk_sp instead of SkTypeface* and SkFontStyleSet*. The pointers returned always needed to be SkSafeUnref'ed but with sk_sp this ownership is now explicit. Flutter subclasses both SkFontMgr and SkFontStyleSet and overrides affected methods. Normally Skia would roll out this change behind a build flag which would first be set in Flutter (to hold out the change), Skia then rolled into Flutter, then the build flag removed from Flutter (along with updating the subclasses). However, this is made quite difficult and slow because of the need to also be compatible with Flutter in other repositories at the same time. Instead, this change updates the subclasses to infer the correct return types in a way that will work both with and without the Skia change. After the Skia change is landed and rolled into Flutter the subclasses will be re-simplified to match the new method signatures. [0] https://skia-review.googlesource.com/c/skia/+/659856 --- .../ui/text/asset_manager_font_provider.cc | 7 ++-- .../lib/ui/text/asset_manager_font_provider.h | 8 +++- .../txt/src/txt/asset_font_manager.cc | 27 +++++++------- .../txt/src/txt/asset_font_manager.h | 37 ++++++++++++++----- .../txt/src/txt/test_font_manager.cc | 3 +- .../txt/src/txt/test_font_manager.h | 5 ++- .../src/txt/typeface_font_asset_provider.cc | 7 ++-- .../src/txt/typeface_font_asset_provider.h | 8 +++- 8 files changed, 68 insertions(+), 34 deletions(-) diff --git a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc index fe3d258cfd..2975406ced 100644 --- a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc +++ b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.cc @@ -97,7 +97,7 @@ void AssetManagerFontStyleSet::getStyle(int index, } } -SkTypeface* AssetManagerFontStyleSet::createTypeface(int i) { +auto AssetManagerFontStyleSet::createTypeface(int i) -> CreateTypefaceRet { size_t index = i; if (index >= assets_.size()) { return nullptr; @@ -126,10 +126,11 @@ SkTypeface* AssetManagerFontStyleSet::createTypeface(int i) { } } - return SkRef(asset.typeface.get()); + return CreateTypefaceRet(SkRef(asset.typeface.get())); } -SkTypeface* AssetManagerFontStyleSet::matchStyle(const SkFontStyle& pattern) { +auto AssetManagerFontStyleSet::matchStyle(const SkFontStyle& pattern) + -> MatchStyleRet { return matchStyleCSS3(pattern); } diff --git a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h index ba78ad2ecd..759ddcc924 100644 --- a/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h +++ b/engine/src/flutter/lib/ui/text/asset_manager_font_provider.h @@ -34,10 +34,14 @@ class AssetManagerFontStyleSet : public SkFontStyleSet { void getStyle(int index, SkFontStyle*, SkString* style) override; // |SkFontStyleSet| - SkTypeface* createTypeface(int index) override; + using CreateTypefaceRet = + decltype(std::declval().createTypeface(0)); + CreateTypefaceRet createTypeface(int index) override; // |SkFontStyleSet| - SkTypeface* matchStyle(const SkFontStyle& pattern) override; + using MatchStyleRet = decltype(std::declval().matchStyle( + std::declval())); + MatchStyleRet matchStyle(const SkFontStyle& pattern) override; private: std::shared_ptr asset_manager_; diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc index c5ba5d7acf..593a9bde3b 100644 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc +++ b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.cc @@ -40,20 +40,21 @@ void AssetFontManager::onGetFamilyName(int index, SkString* familyName) const { familyName->set(font_provider_->GetFamilyName(index).c_str()); } -SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const { +auto AssetFontManager::onCreateStyleSet(int index) const + -> OnCreateStyleSetRet { FML_DCHECK(false); return nullptr; } -SkFontStyleSet* AssetFontManager::onMatchFamily( - const char family_name_string[]) const { +auto AssetFontManager::onMatchFamily(const char family_name_string[]) const + -> OnMatchFamilyRet { std::string family_name(family_name_string); - return font_provider_->MatchFamily(family_name); + return OnMatchFamilyRet(font_provider_->MatchFamily(family_name)); } -SkTypeface* AssetFontManager::onMatchFamilyStyle( - const char familyName[], - const SkFontStyle& style) const { +auto AssetFontManager::onMatchFamilyStyle(const char familyName[], + const SkFontStyle& style) const + -> OnMatchFamilyStyleRet { SkFontStyleSet* font_style_set = font_provider_->MatchFamily(std::string(familyName)); if (font_style_set == nullptr) @@ -61,12 +62,12 @@ SkTypeface* AssetFontManager::onMatchFamilyStyle( return font_style_set->matchStyle(style); } -SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter( - const char familyName[], - const SkFontStyle&, - const char* bcp47[], - int bcp47Count, - SkUnichar character) const { +auto AssetFontManager::onMatchFamilyStyleCharacter(const char familyName[], + const SkFontStyle&, + const char* bcp47[], + int bcp47Count, + SkUnichar character) const + -> OnMatchFamilyStyleCharacterRet { return nullptr; } diff --git a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h index 41648a0a49..cc7692ed51 100644 --- a/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h +++ b/engine/src/flutter/third_party/txt/src/txt/asset_font_manager.h @@ -18,6 +18,7 @@ #define TXT_ASSET_FONT_MANAGER_H_ #include +#include #include "flutter/fml/macros.h" #include "third_party/skia/include/core/SkFontMgr.h" @@ -35,7 +36,10 @@ class AssetFontManager : public SkFontMgr { protected: // |SkFontMgr| - SkFontStyleSet* onMatchFamily(const char familyName[]) const override; + using OnMatchFamilyRet = + decltype((std::declval().* + (&AssetFontManager::onMatchFamily))(std::declval())); + OnMatchFamilyRet onMatchFamily(const char familyName[]) const override; std::unique_ptr font_provider_; @@ -47,18 +51,33 @@ class AssetFontManager : public SkFontMgr { void onGetFamilyName(int index, SkString* familyName) const override; // |SkFontMgr| - SkFontStyleSet* onCreateStyleSet(int index) const override; + using OnCreateStyleSetRet = decltype(( + std::declval().*(&AssetFontManager::onCreateStyleSet))(0)); + OnCreateStyleSetRet onCreateStyleSet(int index) const override; // |SkFontMgr| - SkTypeface* onMatchFamilyStyle(const char familyName[], - const SkFontStyle&) const override; + using OnMatchFamilyStyleRet = decltype(( + std::declval().*(&AssetFontManager::onMatchFamilyStyle))( + std::declval(), + std::declval())); + OnMatchFamilyStyleRet onMatchFamilyStyle(const char familyName[], + const SkFontStyle&) const override; // |SkFontMgr| - SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], - const SkFontStyle&, - const char* bcp47[], - int bcp47Count, - SkUnichar character) const override; + using OnMatchFamilyStyleCharacterRet = + decltype((std::declval().* + (&AssetFontManager::onMatchFamilyStyleCharacter))( + std::declval(), + std::declval(), + std::declval(), + 0, + 0)); + OnMatchFamilyStyleCharacterRet onMatchFamilyStyleCharacter( + const char familyName[], + const SkFontStyle&, + const char* bcp47[], + int bcp47Count, + SkUnichar character) const override; // |SkFontMgr| sk_sp onMakeFromData(sk_sp, int ttcIndex) const override; diff --git a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc index a23a8aa588..5a072ba9f0 100644 --- a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc +++ b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.cc @@ -27,7 +27,8 @@ TestFontManager::TestFontManager( TestFontManager::~TestFontManager() = default; -SkFontStyleSet* TestFontManager::onMatchFamily(const char family_name[]) const { +auto TestFontManager::onMatchFamily(const char family_name[]) const + -> OnMatchFamilyRet { // Find the requested name in the list, if not found, default to the first // font family in the test font family list. std::string requested_name(family_name); diff --git a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h index b1a7d58b99..cc6b8d0a84 100644 --- a/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h +++ b/engine/src/flutter/third_party/txt/src/txt/test_font_manager.h @@ -40,7 +40,10 @@ class TestFontManager : public AssetFontManager { private: std::vector test_font_family_names_; - SkFontStyleSet* onMatchFamily(const char family_name[]) const override; + using OnMatchFamilyRet = + decltype((std::declval().* + (&TestFontManager::onMatchFamily))(std::declval())); + OnMatchFamilyRet onMatchFamily(const char family_name[]) const override; FML_DISALLOW_COPY_AND_ASSIGN(TestFontManager); }; diff --git a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc index c62c43265d..48e8837a0b 100644 --- a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc +++ b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.cc @@ -104,15 +104,16 @@ void TypefaceFontStyleSet::getStyle(int index, } } -SkTypeface* TypefaceFontStyleSet::createTypeface(int i) { +auto TypefaceFontStyleSet::createTypeface(int i) -> CreateTypefaceRet { size_t index = i; if (index >= typefaces_.size()) { return nullptr; } - return SkRef(typefaces_[index].get()); + return CreateTypefaceRet(SkRef(typefaces_[index].get())); } -SkTypeface* TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) { +auto TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) + -> MatchStyleRet { return matchStyleCSS3(pattern); } diff --git a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h index a2c4912213..246162d476 100644 --- a/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h +++ b/engine/src/flutter/third_party/txt/src/txt/typeface_font_asset_provider.h @@ -42,10 +42,14 @@ class TypefaceFontStyleSet : public SkFontStyleSet { void getStyle(int index, SkFontStyle* style, SkString* name) override; // |SkFontStyleSet| - SkTypeface* createTypeface(int index) override; + using CreateTypefaceRet = + decltype(std::declval().createTypeface(0)); + CreateTypefaceRet createTypeface(int index) override; // |SkFontStyleSet| - SkTypeface* matchStyle(const SkFontStyle& pattern) override; + using MatchStyleRet = decltype(std::declval().matchStyle( + std::declval())); + MatchStyleRet matchStyle(const SkFontStyle& pattern) override; private: std::vector> typefaces_;