From 91ec75170b3251d8513fcfd4ecaf08d3a702896d Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Wed, 13 Nov 2019 17:44:25 -0800 Subject: [PATCH] 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. --- .../ui/text/asset_manager_font_provider.cc | 29 ++++++++++--------- .../lib/ui/text/asset_manager_font_provider.h | 4 ++- 2 files changed, 18 insertions(+), 15 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 fd019f23cc..07a4a66f2f 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 @@ -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(asset_manager_)); + canonical_name, + sk_make_sp(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 asset_manager) - : asset_manager_(asset_manager) {} + std::shared_ptr 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(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) 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 d24e5acb07..ef4d272664 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 @@ -20,7 +20,8 @@ namespace flutter { class AssetManagerFontStyleSet : public SkFontStyleSet { public: - AssetManagerFontStyleSet(std::shared_ptr asset_manager); + AssetManagerFontStyleSet(std::shared_ptr asset_manager, + std::string family_name); ~AssetManagerFontStyleSet() override; @@ -40,6 +41,7 @@ class AssetManagerFontStyleSet : public SkFontStyleSet { private: std::shared_ptr asset_manager_; + std::string family_name_; struct TypefaceAsset { TypefaceAsset(std::string a);