1 // Copyright 2019 Google LLC. 2 #ifndef FontCollection_DEFINED 3 #define FontCollection_DEFINED 4 5 #include <memory> 6 #include <set> 7 #include "include/core/SkFontMgr.h" 8 #include "include/core/SkRefCnt.h" 9 #include "include/private/SkTHash.h" 10 #include "modules/skparagraph/include/ParagraphCache.h" 11 #include "modules/skparagraph/include/TextStyle.h" 12 13 namespace skia { 14 namespace textlayout { 15 16 class TextStyle; 17 class Paragraph; 18 class FontCollection : public SkRefCnt { 19 public: 20 FontCollection(); 21 22 ~FontCollection() = default; 23 24 size_t getFontManagersCount() const; 25 26 void setAssetFontManager(sk_sp<SkFontMgr> fontManager); 27 void setDynamicFontManager(sk_sp<SkFontMgr> fontManager); 28 void setTestFontManager(sk_sp<SkFontMgr> fontManager); 29 void setDefaultFontManager(sk_sp<SkFontMgr> fontManager); 30 void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const char defaultFamilyName[]); 31 getFallbackManager()32 sk_sp<SkFontMgr> getFallbackManager() const { return fDefaultFontManager; } 33 34 std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle); 35 36 sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale); 37 sk_sp<SkTypeface> defaultFallback(); 38 39 void disableFontFallback(); 40 void enableFontFallback(); fontFallbackEnabled()41 bool fontFallbackEnabled() { return fEnableFontFallback; } 42 getParagraphCache()43 ParagraphCache* getParagraphCache() { return &fParagraphCache; } 44 45 private: 46 std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const; 47 48 sk_sp<SkTypeface> matchTypeface(const SkString& familyName, SkFontStyle fontStyle); 49 50 struct FamilyKey { FamilyKeyFamilyKey51 FamilyKey(const std::vector<SkString>& familyNames, SkFontStyle style) 52 : fFamilyNames(familyNames), fFontStyle(style) {} 53 FamilyKeyFamilyKey54 FamilyKey() {} 55 56 std::vector<SkString> fFamilyNames; 57 SkFontStyle fFontStyle; 58 59 bool operator==(const FamilyKey& other) const; 60 61 struct Hasher { 62 size_t operator()(const FamilyKey& key) const; 63 }; 64 }; 65 66 bool fEnableFontFallback; 67 SkTHashMap<FamilyKey, std::vector<sk_sp<SkTypeface>>, FamilyKey::Hasher> fTypefaces; 68 sk_sp<SkFontMgr> fDefaultFontManager; 69 sk_sp<SkFontMgr> fAssetFontManager; 70 sk_sp<SkFontMgr> fDynamicFontManager; 71 sk_sp<SkFontMgr> fTestFontManager; 72 73 SkString fDefaultFamilyName; 74 ParagraphCache fParagraphCache; 75 }; 76 } // namespace textlayout 77 } // namespace skia 78 79 #endif // FontCollection_DEFINED 80