1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ 6 #define FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ 7 8 #include <memory> 9 #include <string> 10 #include <unordered_map> 11 #include <vector> 12 13 #include "flutter/assets/asset_manager.h" 14 #include "flutter/fml/macros.h" 15 #include "third_party/skia/include/core/SkFontMgr.h" 16 #include "third_party/skia/include/core/SkTypeface.h" 17 #include "txt/font_asset_provider.h" 18 19 namespace flutter { 20 21 class AssetManagerFontStyleSet : public SkFontStyleSet { 22 public: 23 AssetManagerFontStyleSet(std::shared_ptr<AssetManager> asset_manager); 24 25 ~AssetManagerFontStyleSet() override; 26 27 void registerAsset(std::string asset); 28 29 // |SkFontStyleSet| 30 int count() override; 31 32 // |SkFontStyleSet| 33 void getStyle(int index, SkFontStyle*, SkString* style) override; 34 35 // |SkFontStyleSet| 36 SkTypeface* createTypeface(int index) override; 37 38 // |SkFontStyleSet| 39 SkTypeface* matchStyle(const SkFontStyle& pattern) override; 40 41 private: 42 std::shared_ptr<AssetManager> asset_manager_; 43 44 struct TypefaceAsset { 45 TypefaceAsset(std::string a); 46 47 TypefaceAsset(const TypefaceAsset& other); 48 49 ~TypefaceAsset(); 50 51 std::string asset; 52 sk_sp<SkTypeface> typeface; 53 }; 54 std::vector<TypefaceAsset> assets_; 55 56 FML_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontStyleSet); 57 }; 58 59 class AssetManagerFontProvider : public txt::FontAssetProvider { 60 public: 61 AssetManagerFontProvider(std::shared_ptr<AssetManager> asset_manager); 62 63 ~AssetManagerFontProvider() override; 64 65 void RegisterAsset(std::string family_name, std::string asset); 66 67 // |FontAssetProvider| 68 size_t GetFamilyCount() const override; 69 70 // |FontAssetProvider| 71 std::string GetFamilyName(int index) const override; 72 73 // |FontAssetProvider| 74 SkFontStyleSet* MatchFamily(const std::string& family_name) override; 75 76 private: 77 std::shared_ptr<AssetManager> asset_manager_; 78 std::unordered_map<std::string, sk_sp<AssetManagerFontStyleSet>> 79 registered_families_; 80 std::vector<std::string> family_names_; 81 82 FML_DISALLOW_COPY_AND_ASSIGN(AssetManagerFontProvider); 83 }; 84 85 } // namespace flutter 86 87 #endif // FLUTTER_LIB_UI_TEXT_ASSET_MANAGER_FONT_PROVIDER_H_ 88