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 geFallbackManager()32 sk_sp<SkFontMgr> geFallbackManager() const { return fDefaultFontManager; } 33 34 sk_sp<SkTypeface> matchTypeface(const char familyName[], SkFontStyle fontStyle); 35 sk_sp<SkTypeface> matchDefaultTypeface(SkFontStyle fontStyle); 36 sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale); 37 38 void disableFontFallback(); fontFallbackEnabled()39 bool fontFallbackEnabled() { return fEnableFontFallback; } 40 getParagraphCache()41 ParagraphCache* getParagraphCache() { return &fParagraphCache; } 42 43 private: 44 std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const; 45 46 struct FamilyKey { FamilyKeyFamilyKey47 FamilyKey(const char family[], const char loc[], SkFontStyle style) 48 : fFontFamily(family), fLocale(loc), fFontStyle(style) {} 49 FamilyKeyFamilyKey50 FamilyKey() {} 51 52 SkString fFontFamily; 53 SkString fLocale; 54 SkFontStyle fFontStyle; 55 56 bool operator==(const FamilyKey& other) const; 57 58 struct Hasher { 59 size_t operator()(const FamilyKey& key) const; 60 }; 61 }; 62 63 bool fEnableFontFallback; 64 SkTHashMap<FamilyKey, sk_sp<SkTypeface>, FamilyKey::Hasher> fTypefaces; 65 sk_sp<SkFontMgr> fDefaultFontManager; 66 sk_sp<SkFontMgr> fAssetFontManager; 67 sk_sp<SkFontMgr> fDynamicFontManager; 68 sk_sp<SkFontMgr> fTestFontManager; 69 70 const char* fDefaultFamilyName; 71 ParagraphCache fParagraphCache; 72 }; 73 } // namespace textlayout 74 } // namespace skia 75 76 #endif // FontCollection_DEFINED 77