Implement more SkFontMgr APIs in AssetFontManager (flutter/engine#4591)
This commit is contained in:
@@ -24,7 +24,11 @@ AssetDataProvider::AssetDataProvider() = default;
|
||||
AssetDataProvider::~AssetDataProvider() = default;
|
||||
|
||||
size_t AssetDataProvider::GetFamilyCount() const {
|
||||
return registered_families_.size();
|
||||
return family_names_.size();
|
||||
}
|
||||
|
||||
const std::string& AssetDataProvider::GetFamilyName(int index) const {
|
||||
return family_names_[index];
|
||||
}
|
||||
|
||||
AssetFontStyleSet* AssetDataProvider::MatchFamily(
|
||||
@@ -44,18 +48,26 @@ void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
|
||||
SkString sk_family_name;
|
||||
typeface->getFamilyName(&sk_family_name);
|
||||
|
||||
if (sk_family_name.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string family_name(sk_family_name.c_str(), sk_family_name.size());
|
||||
RegisterTypeface(std::move(typeface), std::move(family_name));
|
||||
}
|
||||
|
||||
void AssetDataProvider::RegisterTypeface(sk_sp<SkTypeface> typeface,
|
||||
std::string family_name_alias) {
|
||||
registered_families_[std::move(family_name_alias)].registerTypeface(
|
||||
std::move(typeface));
|
||||
if (family_name_alias.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto family_it = registered_families_.find(family_name_alias);
|
||||
if (family_it == registered_families_.end()) {
|
||||
family_names_.push_back(family_name_alias);
|
||||
family_it = registered_families_
|
||||
.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(family_name_alias),
|
||||
std::forward_as_tuple())
|
||||
.first;
|
||||
}
|
||||
family_it->second.registerTypeface(std::move(typeface));
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
|
||||
@@ -33,6 +33,8 @@ class AssetDataProvider {
|
||||
|
||||
size_t GetFamilyCount() const;
|
||||
|
||||
const std::string& GetFamilyName(int index) const;
|
||||
|
||||
AssetFontStyleSet* MatchFamily(const std::string& family_name);
|
||||
|
||||
void RegisterTypeface(sk_sp<SkTypeface> typeface);
|
||||
@@ -42,6 +44,7 @@ class AssetDataProvider {
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, AssetFontStyleSet> registered_families_;
|
||||
std::vector<std::string> family_names_;
|
||||
|
||||
FXL_DISALLOW_COPY_AND_ASSIGN(AssetDataProvider);
|
||||
};
|
||||
|
||||
@@ -33,7 +33,7 @@ int AssetFontManager::onCountFamilies() const {
|
||||
}
|
||||
|
||||
void AssetFontManager::onGetFamilyName(int index, SkString* familyName) const {
|
||||
FXL_DCHECK(false);
|
||||
familyName->set(data_provider_->GetFamilyName(index).c_str());
|
||||
}
|
||||
|
||||
SkFontStyleSet* AssetFontManager::onCreateStyleSet(int index) const {
|
||||
@@ -47,10 +47,14 @@ SkFontStyleSet* AssetFontManager::onMatchFamily(
|
||||
return data_provider_->MatchFamily(family_name);
|
||||
}
|
||||
|
||||
SkTypeface* AssetFontManager::onMatchFamilyStyle(const char familyName[],
|
||||
const SkFontStyle&) const {
|
||||
FXL_DCHECK(false);
|
||||
return nullptr;
|
||||
SkTypeface* AssetFontManager::onMatchFamilyStyle(
|
||||
const char familyName[],
|
||||
const SkFontStyle& style) const {
|
||||
SkFontStyleSet* font_style_set =
|
||||
data_provider_->MatchFamily(std::string(familyName));
|
||||
if (font_style_set == nullptr)
|
||||
return nullptr;
|
||||
return font_style_set->matchStyle(style);
|
||||
}
|
||||
|
||||
SkTypeface* AssetFontManager::onMatchFamilyStyleCharacter(
|
||||
|
||||
@@ -35,8 +35,14 @@ SkTypeface* AssetFontStyleSet::createTypeface(int index) {
|
||||
}
|
||||
|
||||
SkTypeface* AssetFontStyleSet::matchStyle(const SkFontStyle& pattern) {
|
||||
FXL_DCHECK(false);
|
||||
return nullptr;
|
||||
if (typefaces_.empty())
|
||||
return nullptr;
|
||||
|
||||
for (const sk_sp<SkTypeface>& typeface : typefaces_)
|
||||
if (typeface->fontStyle() == pattern)
|
||||
return typeface.get();
|
||||
|
||||
return typefaces_[0].get();
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
|
||||
Reference in New Issue
Block a user