Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Simplify SkFontMgr classes for sk_sp #40627

Merged
merged 2 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/ui/text/asset_manager_font_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/text/asset_manager_font_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
29 changes: 14 additions & 15 deletions third_party/txt/src/txt/asset_font_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
27 changes: 5 additions & 22 deletions third_party/txt/src/txt/asset_font_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;

Expand All @@ -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[],
Expand Down
2 changes: 1 addition & 1 deletion third_party/txt/src/txt/font_asset_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions third_party/txt/src/txt/test_font_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions third_party/txt/src/txt/test_font_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
12 changes: 5 additions & 7 deletions third_party/txt/src/txt/typeface_font_asset_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 3 additions & 7 deletions third_party/txt/src/txt/typeface_font_asset_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -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>>
Expand Down