• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 Google LLC.
2 #ifndef FontCollection_DEFINED
3 #define FontCollection_DEFINED
4 
5 #include <memory>
6 #include <optional>
7 #include <set>
8 #ifdef ENABLE_TEXT_ENHANCE
9 #include <mutex>
10 #include <shared_mutex>
11 #include <unordered_map>
12 #include "drawing.h"
13 #endif
14 #include "include/core/SkFontMgr.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkSpan.h"
17 #include "modules/skparagraph/include/FontArguments.h"
18 #include "modules/skparagraph/include/ParagraphCache.h"
19 #include "modules/skparagraph/include/TextStyle.h"
20 #include "src/core/SkTHash.h"
21 
22 namespace skia {
23 namespace textlayout {
24 
25 class TextStyle;
26 class Paragraph;
27 class FontCollection : public SkRefCnt {
28 public:
29     FontCollection();
30 
31     size_t getFontManagersCount() const;
32 
33 #ifdef ENABLE_TEXT_ENHANCE
34     void setAssetFontManager(std::shared_ptr<RSFontMgr> fontManager);
35     void setDynamicFontManager(std::shared_ptr<RSFontMgr> fontManager);
36     void setTestFontManager(std::shared_ptr<RSFontMgr> fontManager);
37     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager);
38     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager, const char defaultFamilyName[]);
39     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);
40 
getFallbackManager()41     std::shared_ptr<RSFontMgr> getFallbackManager() const
42     {
43         std::shared_lock<std::shared_mutex> readLock(mutex_);
44         return fDefaultFontManager;
45     }
46 
47     std::vector<std::shared_ptr<RSTypeface>> findTypefaces(
48         const std::vector<SkString>& familyNames, RSFontStyle fontStyle);
49     std::vector<std::shared_ptr<RSTypeface>> findTypefaces(
50         const std::vector<SkString>& familyNames, RSFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
51 
52     std::shared_ptr<RSTypeface> defaultFallback(SkUnichar unicode, RSFontStyle fontStyle, const SkString& locale);
53     std::shared_ptr<RSTypeface> defaultFallback();
54     std::shared_ptr<RSTypeface> CloneTypeface(std::shared_ptr<RSTypeface> typeface,
55         const std::optional<FontArguments>& fontArgs);
56 #else
57     void setAssetFontManager(sk_sp<SkFontMgr> fontManager);
58     void setDynamicFontManager(sk_sp<SkFontMgr> fontManager);
59     void setTestFontManager(sk_sp<SkFontMgr> fontManager);
60     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager);
61     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const char defaultFamilyName[]);
62     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);
63 
getFallbackManager()64     sk_sp<SkFontMgr> getFallbackManager() const { return fDefaultFontManager; }
65 
66     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle);
67     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
68 
69     sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale);
70     sk_sp<SkTypeface> defaultEmojiFallback(SkUnichar emojiStart, SkFontStyle fontStyle, const SkString& locale);
71     sk_sp<SkTypeface> defaultFallback();
72 #endif
73 
74     void disableFontFallback();
75     void enableFontFallback();
fontFallbackEnabled()76     bool fontFallbackEnabled() { return fEnableFontFallback; }
77 
getParagraphCache()78     ParagraphCache* getParagraphCache() { return &fParagraphCache; }
79 
80     void clearCaches();
81 
82 #ifdef ENABLE_TEXT_ENHANCE
83     // set fIsAdpaterTextHeightEnabled with once_flag.
SetAdapterTextHeightEnabled(bool adapterTextHeightEnabled)84     static void SetAdapterTextHeightEnabled(bool adapterTextHeightEnabled)
85     {
86         static std::once_flag flag;
87         std::call_once(flag, [adapterTextHeightEnabled]() {
88             fIsAdpaterTextHeightEnabled = adapterTextHeightEnabled;
89         });
90     }
91 
IsAdapterTextHeightEnabled()92     static bool IsAdapterTextHeightEnabled()
93     {
94         return fIsAdpaterTextHeightEnabled;
95     }
96 #endif
97 private:
98 #ifdef ENABLE_TEXT_ENHANCE
99     std::vector<std::shared_ptr<RSFontMgr>> getFontManagerOrder() const;
100 
101     std::shared_ptr<RSTypeface> matchTypeface(const SkString& familyName, RSFontStyle fontStyle);
102 
103     void updateTypefacesMatch(std::vector<std::shared_ptr<RSTypeface>>& typefaces,
104         RSFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
105 #else
106     std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const;
107 
108     sk_sp<SkTypeface> matchTypeface(const SkString& familyName, SkFontStyle fontStyle);
109 #endif
110 
111     struct FamilyKey {
112 #ifdef ENABLE_TEXT_ENHANCE
FamilyKeyFamilyKey113         FamilyKey(
114             const std::vector<SkString>& familyNames, RSFontStyle style, const std::optional<FontArguments>& args)
115                 : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}
116 #else
117         FamilyKey(const std::vector<SkString>& familyNames, SkFontStyle style, const std::optional<FontArguments>& args)
118                 : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}
119 #endif
120 
FamilyKeyFamilyKey121         FamilyKey() {}
122 
123         std::vector<SkString> fFamilyNames;
124 #ifdef ENABLE_TEXT_ENHANCE
125         RSFontStyle fFontStyle;
126 #else
127         SkFontStyle fFontStyle;
128 #endif
129         std::optional<FontArguments> fFontArguments;
130 
131         bool operator==(const FamilyKey& other) const;
132 
133         struct Hasher {
134             size_t operator()(const FamilyKey& key) const;
135         };
136     };
137 #ifdef ENABLE_TEXT_ENHANCE
138     static bool fIsAdpaterTextHeightEnabled;
139 #endif
140 
141     bool fEnableFontFallback;
142 #ifdef ENABLE_TEXT_ENHANCE
143     std::unordered_map<FamilyKey, std::vector<std::shared_ptr<RSTypeface>>, FamilyKey::Hasher> fTypefaces;
144     std::shared_ptr<RSFontMgr> fDefaultFontManager;
145     std::shared_ptr<RSFontMgr> fAssetFontManager;
146     std::shared_ptr<RSFontMgr> fDynamicFontManager;
147     std::shared_ptr<RSFontMgr> fTestFontManager;
148 #else
149     skia_private::THashMap<FamilyKey, std::vector<sk_sp<SkTypeface>>, FamilyKey::Hasher> fTypefaces;
150     sk_sp<SkFontMgr> fDefaultFontManager;
151     sk_sp<SkFontMgr> fAssetFontManager;
152     sk_sp<SkFontMgr> fDynamicFontManager;
153     sk_sp<SkFontMgr> fTestFontManager;
154 #endif
155     std::vector<SkString> fDefaultFamilyNames;
156     ParagraphCache fParagraphCache;
157 
158 #ifdef ENABLE_TEXT_ENHANCE
159     std::mutex fMutex;
160     mutable std::shared_mutex mutex_;
161 #endif
162 };
163 }  // namespace textlayout
164 }  // namespace skia
165 
166 #endif  // FontCollection_DEFINED
167