// Copyright 2019 Google LLC. #ifndef FontCollection_DEFINED #define FontCollection_DEFINED #include #include #include #include #include #include #include "include/core/SkFontMgr.h" #include "include/core/SkRefCnt.h" #include "modules/skparagraph/include/FontArguments.h" #include "modules/skparagraph/include/ParagraphCache.h" #include "modules/skparagraph/include/TextStyle.h" #include "include/private/SkTHash.h" #include "drawing.h" namespace skia { namespace textlayout { class TextStyle; class Paragraph; class FontCollection : public SkRefCnt { public: FontCollection(); size_t getFontManagersCount() const; #ifndef USE_SKIA_TXT void setAssetFontManager(sk_sp fontManager); void setDynamicFontManager(sk_sp fontManager); void setTestFontManager(sk_sp fontManager); void setDefaultFontManager(sk_sp fontManager); void setDefaultFontManager(sk_sp fontManager, const char defaultFamilyName[]); void setDefaultFontManager(sk_sp fontManager, const std::vector& defaultFamilyNames); sk_sp getFallbackManager() const { std::shared_lock readLock(mutex_); return fDefaultFontManager; } std::vector> findTypefaces(const std::vector& familyNames, SkFontStyle fontStyle); std::vector> findTypefaces(const std::vector& familyNames, SkFontStyle fontStyle, const std::optional& fontArgs); sk_sp defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale); sk_sp defaultFallback(); #else void setAssetFontManager(std::shared_ptr fontManager); void setDynamicFontManager(std::shared_ptr fontManager); void setTestFontManager(std::shared_ptr fontManager); void setDefaultFontManager(std::shared_ptr fontManager); void setDefaultFontManager(std::shared_ptr fontManager, const char defaultFamilyName[]); void setDefaultFontManager(std::shared_ptr fontManager, const std::vector& defaultFamilyNames); std::shared_ptr getFallbackManager() const { std::shared_lock readLock(mutex_); return fDefaultFontManager; } std::vector> findTypefaces( const std::vector& familyNames, RSFontStyle fontStyle); std::vector> findTypefaces( const std::vector& familyNames, RSFontStyle fontStyle, const std::optional& fontArgs); std::shared_ptr defaultFallback(SkUnichar unicode, RSFontStyle fontStyle, const SkString& locale); std::shared_ptr defaultFallback(); #endif #ifndef USE_SKIA_TXT sk_sp CloneTypeface(sk_sp typeface, const std::optional& fontArgs); #else std::shared_ptr CloneTypeface(std::shared_ptr typeface, const std::optional& fontArgs); #endif void disableFontFallback(); void enableFontFallback(); bool fontFallbackEnabled() { std::shared_lock readLock(mutex_); return fEnableFontFallback; } ParagraphCache* getParagraphCache() { std::shared_lock readLock(mutex_); return &fParagraphCache; } void clearCaches(); #ifdef OHOS_SUPPORT // set fIsAdpaterTextHeightEnabled with once_flag. static void SetAdapterTextHeightEnabled(bool adapterTextHeightEnabled) { static std::once_flag flag; std::call_once(flag, [adapterTextHeightEnabled]() { fIsAdpaterTextHeightEnabled = adapterTextHeightEnabled; }); } static bool IsAdapterTextHeightEnabled() { return fIsAdpaterTextHeightEnabled; } #endif private: #ifndef USE_SKIA_TXT std::vector> getFontManagerOrder() const; sk_sp matchTypeface(const SkString& familyName, SkFontStyle fontStyle); #else std::vector> getFontManagerOrder() const; std::shared_ptr matchTypeface(const SkString& familyName, RSFontStyle fontStyle); #endif struct FamilyKey { #ifndef USE_SKIA_TXT FamilyKey(const std::vector& familyNames, SkFontStyle style, const std::optional& args) : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {} #else FamilyKey( const std::vector& familyNames, RSFontStyle style, const std::optional& args) : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {} #endif FamilyKey() {} std::vector fFamilyNames; #ifndef USE_SKIA_TXT SkFontStyle fFontStyle; #else RSFontStyle fFontStyle; #endif std::optional fFontArguments; bool operator==(const FamilyKey& other) const; struct Hasher { size_t operator()(const FamilyKey& key) const; }; }; #ifdef OHOS_SUPPORT static bool fIsAdpaterTextHeightEnabled; #endif bool fEnableFontFallback; #ifndef USE_SKIA_TXT SkTHashMap>, FamilyKey::Hasher> fTypefaces; sk_sp fDefaultFontManager; sk_sp fAssetFontManager; sk_sp fDynamicFontManager; sk_sp fTestFontManager; #else std::unordered_map>, FamilyKey::Hasher> fTypefaces; std::shared_ptr fDefaultFontManager; std::shared_ptr fAssetFontManager; std::shared_ptr fDynamicFontManager; std::shared_ptr fTestFontManager; #endif std::mutex fMutex; std::vector fDefaultFamilyNames; ParagraphCache fParagraphCache; mutable std::shared_mutex mutex_; }; } // namespace textlayout } // namespace skia #endif // FontCollection_DEFINED