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