Simplify SkFontMgr classes for sk_sp (flutter/engine#40627)

These subclasses were previously made very generic to work with both the
pre-sk_sp version of SkFontMgr and the using-sk_sp version of SkFontMgr.
Now that SkFontMgr uses sk_sp for return types, simplify the subclasses.
This commit is contained in:
bungeman
2023-03-27 16:06:07 -04:00
committed by GitHub
parent 33dd135fc0
commit 13b279990a
9 changed files with 34 additions and 62 deletions

View File

@@ -40,14 +40,13 @@ std::string AssetManagerFontProvider::GetFamilyName(int index) const {
}
// |FontAssetProvider|
SkFontStyleSet* AssetManagerFontProvider::MatchFamily(
sk_sp<SkFontStyleSet> AssetManagerFontProvider::MatchFamily(
const std::string& family_name) {
auto found = registered_families_.find(CanonicalFamilyName(family_name));
if (found == registered_families_.end()) {
return nullptr;
}
sk_sp<SkFontStyleSet> font_style_set = found->second;
return font_style_set.release();
return found->second;
}
void AssetManagerFontProvider::RegisterAsset(const std::string& family_name,

View File

@@ -78,7 +78,7 @@ class AssetManagerFontProvider : public txt::FontAssetProvider {
std::string GetFamilyName(int index) const override;
// |FontAssetProvider|
SkFontStyleSet* MatchFamily(const std::string& family_name) override;
sk_sp<SkFontStyleSet> MatchFamily(const std::string& family_name) override;
private:
std::shared_ptr<AssetManager> asset_manager_;

View File

@@ -40,34 +40,33 @@ void AssetFontManager::onGetFamilyName(int index, SkString* familyName) const {
familyName->set(font_provider_->GetFamilyName(index).c_str());
}
auto AssetFontManager::onCreateStyleSet(int index) const
-> OnCreateStyleSetRet {
sk_sp<SkFontStyleSet> AssetFontManager::onCreateStyleSet(int index) const {
FML_DCHECK(false);
return nullptr;
}
auto AssetFontManager::onMatchFamily(const char family_name_string[]) const
-> OnMatchFamilyRet {
sk_sp<SkFontStyleSet> AssetFontManager::onMatchFamily(
const char family_name_string[]) const {
std::string family_name(family_name_string);
return OnMatchFamilyRet(font_provider_->MatchFamily(family_name));
return font_provider_->MatchFamily(family_name);
}
auto AssetFontManager::onMatchFamilyStyle(const char familyName[],
const SkFontStyle& style) const
-> OnMatchFamilyStyleRet {
SkFontStyleSet* font_style_set =
sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyle(
const char familyName[],
const SkFontStyle& style) const {
sk_sp<SkFontStyleSet> font_style_set =
font_provider_->MatchFamily(std::string(familyName));
if (font_style_set == nullptr)
return nullptr;
return font_style_set->matchStyle(style);
}
auto AssetFontManager::onMatchFamilyStyleCharacter(const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const
-> OnMatchFamilyStyleCharacterRet {
sk_sp<SkTypeface> AssetFontManager::onMatchFamilyStyleCharacter(
const char familyName[],
const SkFontStyle&,
const char* bcp47[],
int bcp47Count,
SkUnichar character) const {
return nullptr;
}

View File

@@ -36,10 +36,7 @@ class AssetFontManager : public SkFontMgr {
protected:
// |SkFontMgr|
using OnMatchFamilyRet =
decltype((std::declval<SkFontMgr>().*
(&AssetFontManager::onMatchFamily))(std::declval<char[]>()));
OnMatchFamilyRet onMatchFamily(const char familyName[]) const override;
sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override;
std::unique_ptr<FontAssetProvider> font_provider_;
@@ -51,28 +48,14 @@ class AssetFontManager : public SkFontMgr {
void onGetFamilyName(int index, SkString* familyName) const override;
// |SkFontMgr|
using OnCreateStyleSetRet = decltype((
std::declval<SkFontMgr>().*(&AssetFontManager::onCreateStyleSet))(0));
OnCreateStyleSetRet onCreateStyleSet(int index) const override;
sk_sp<SkFontStyleSet> onCreateStyleSet(int index) const override;
// |SkFontMgr|
using OnMatchFamilyStyleRet = decltype((
std::declval<SkFontMgr>().*(&AssetFontManager::onMatchFamilyStyle))(
std::declval<char[]>(),
std::declval<SkFontStyle>()));
OnMatchFamilyStyleRet onMatchFamilyStyle(const char familyName[],
const SkFontStyle&) const override;
sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[],
const SkFontStyle&) const override;
// |SkFontMgr|
using OnMatchFamilyStyleCharacterRet =
decltype((std::declval<SkFontMgr>().*
(&AssetFontManager::onMatchFamilyStyleCharacter))(
std::declval<char[]>(),
std::declval<SkFontStyle>(),
std::declval<const char*[]>(),
0,
0));
OnMatchFamilyStyleCharacterRet onMatchFamilyStyleCharacter(
sk_sp<SkTypeface> onMatchFamilyStyleCharacter(
const char familyName[],
const SkFontStyle&,
const char* bcp47[],

View File

@@ -29,7 +29,7 @@ class FontAssetProvider {
virtual size_t GetFamilyCount() const = 0;
virtual std::string GetFamilyName(int index) const = 0;
virtual SkFontStyleSet* MatchFamily(const std::string& family_name) = 0;
virtual sk_sp<SkFontStyleSet> MatchFamily(const std::string& family_name) = 0;
protected:
static std::string CanonicalFamilyName(std::string family_name);

View File

@@ -27,8 +27,8 @@ TestFontManager::TestFontManager(
TestFontManager::~TestFontManager() = default;
auto TestFontManager::onMatchFamily(const char family_name[]) const
-> OnMatchFamilyRet {
sk_sp<SkFontStyleSet> TestFontManager::onMatchFamily(
const char family_name[]) const {
// 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);

View File

@@ -40,10 +40,7 @@ class TestFontManager : public AssetFontManager {
private:
std::vector<std::string> test_font_family_names_;
using OnMatchFamilyRet =
decltype((std::declval<AssetFontManager>().*
(&TestFontManager::onMatchFamily))(std::declval<char[]>()));
OnMatchFamilyRet onMatchFamily(const char family_name[]) const override;
sk_sp<SkFontStyleSet> onMatchFamily(const char family_name[]) const override;
FML_DISALLOW_COPY_AND_ASSIGN(TestFontManager);
};

View File

@@ -37,14 +37,13 @@ std::string TypefaceFontAssetProvider::GetFamilyName(int index) const {
}
// |FontAssetProvider|
SkFontStyleSet* TypefaceFontAssetProvider::MatchFamily(
sk_sp<SkFontStyleSet> TypefaceFontAssetProvider::MatchFamily(
const std::string& family_name) {
auto found = registered_families_.find(CanonicalFamilyName(family_name));
if (found == registered_families_.end()) {
return nullptr;
}
sk_sp<TypefaceFontStyleSet> font_style_set = found->second;
return font_style_set.release();
return found->second;
}
void TypefaceFontAssetProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
@@ -104,16 +103,15 @@ void TypefaceFontStyleSet::getStyle(int index,
}
}
auto TypefaceFontStyleSet::createTypeface(int i) -> CreateTypefaceRet {
sk_sp<SkTypeface> TypefaceFontStyleSet::createTypeface(int i) {
size_t index = i;
if (index >= typefaces_.size()) {
return nullptr;
}
return CreateTypefaceRet(SkRef(typefaces_[index].get()));
return typefaces_[index];
}
auto TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern)
-> MatchStyleRet {
sk_sp<SkTypeface> TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) {
return matchStyleCSS3(pattern);
}

View File

@@ -42,14 +42,10 @@ class TypefaceFontStyleSet : public SkFontStyleSet {
void getStyle(int index, SkFontStyle* style, SkString* name) override;
// |SkFontStyleSet|
using CreateTypefaceRet =
decltype(std::declval<SkFontStyleSet>().createTypeface(0));
CreateTypefaceRet createTypeface(int index) override;
sk_sp<SkTypeface> createTypeface(int index) override;
// |SkFontStyleSet|
using MatchStyleRet = decltype(std::declval<SkFontStyleSet>().matchStyle(
std::declval<SkFontStyle>()));
MatchStyleRet matchStyle(const SkFontStyle& pattern) override;
sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override;
private:
std::vector<sk_sp<SkTypeface>> typefaces_;
@@ -74,7 +70,7 @@ class TypefaceFontAssetProvider : public FontAssetProvider {
std::string GetFamilyName(int index) const override;
// |FontAssetProvider|
SkFontStyleSet* MatchFamily(const std::string& family_name) override;
sk_sp<SkFontStyleSet> MatchFamily(const std::string& family_name) override;
private:
std::unordered_map<std::string, sk_sp<TypefaceFontStyleSet>>