Use Skia's matchStyleCSS3 to find bundled asset typefaces matching a font style (flutter/engine#13848)
This will improve font matching in SkParagraph for fonts that are bundled as assets within the app. Libtxt was using Minikin's FontFamily class to select the closest matching font, but SkParagraph will rely on the matchStyle implementation in the asset font manager.
This commit is contained in:
@@ -56,7 +56,8 @@ void AssetManagerFontProvider::RegisterAsset(std::string family_name,
|
||||
if (family_it == registered_families_.end()) {
|
||||
family_names_.push_back(family_name);
|
||||
auto value = std::make_pair(
|
||||
canonical_name, sk_make_sp<AssetManagerFontStyleSet>(asset_manager_));
|
||||
canonical_name,
|
||||
sk_make_sp<AssetManagerFontStyleSet>(asset_manager_, family_name));
|
||||
family_it = registered_families_.emplace(value).first;
|
||||
}
|
||||
|
||||
@@ -64,8 +65,9 @@ void AssetManagerFontProvider::RegisterAsset(std::string family_name,
|
||||
}
|
||||
|
||||
AssetManagerFontStyleSet::AssetManagerFontStyleSet(
|
||||
std::shared_ptr<AssetManager> asset_manager)
|
||||
: asset_manager_(asset_manager) {}
|
||||
std::shared_ptr<AssetManager> asset_manager,
|
||||
std::string family_name)
|
||||
: asset_manager_(asset_manager), family_name_(family_name) {}
|
||||
|
||||
AssetManagerFontStyleSet::~AssetManagerFontStyleSet() = default;
|
||||
|
||||
@@ -78,9 +80,15 @@ int AssetManagerFontStyleSet::count() {
|
||||
}
|
||||
|
||||
void AssetManagerFontStyleSet::getStyle(int index,
|
||||
SkFontStyle*,
|
||||
SkString* style) {
|
||||
FML_DCHECK(false);
|
||||
SkFontStyle* style,
|
||||
SkString* name) {
|
||||
FML_DCHECK(index < static_cast<int>(assets_.size()));
|
||||
if (style) {
|
||||
*style = assets_[index].typeface->fontStyle();
|
||||
}
|
||||
if (name) {
|
||||
*name = family_name_.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
SkTypeface* AssetManagerFontStyleSet::createTypeface(int i) {
|
||||
@@ -112,14 +120,7 @@ SkTypeface* AssetManagerFontStyleSet::createTypeface(int i) {
|
||||
}
|
||||
|
||||
SkTypeface* AssetManagerFontStyleSet::matchStyle(const SkFontStyle& pattern) {
|
||||
if (assets_.empty())
|
||||
return nullptr;
|
||||
|
||||
for (const TypefaceAsset& asset : assets_)
|
||||
if (asset.typeface && asset.typeface->fontStyle() == pattern)
|
||||
return SkRef(asset.typeface.get());
|
||||
|
||||
return SkRef(assets_[0].typeface.get());
|
||||
return matchStyleCSS3(pattern);
|
||||
}
|
||||
|
||||
AssetManagerFontStyleSet::TypefaceAsset::TypefaceAsset(std::string a)
|
||||
|
||||
@@ -20,7 +20,8 @@ namespace flutter {
|
||||
|
||||
class AssetManagerFontStyleSet : public SkFontStyleSet {
|
||||
public:
|
||||
AssetManagerFontStyleSet(std::shared_ptr<AssetManager> asset_manager);
|
||||
AssetManagerFontStyleSet(std::shared_ptr<AssetManager> asset_manager,
|
||||
std::string family_name);
|
||||
|
||||
~AssetManagerFontStyleSet() override;
|
||||
|
||||
@@ -40,6 +41,7 @@ class AssetManagerFontStyleSet : public SkFontStyleSet {
|
||||
|
||||
private:
|
||||
std::shared_ptr<AssetManager> asset_manager_;
|
||||
std::string family_name_;
|
||||
|
||||
struct TypefaceAsset {
|
||||
TypefaceAsset(std::string a);
|
||||
|
||||
Reference in New Issue
Block a user