• 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 <unordered_map>
10 #include "include/core/SkFontMgr.h"
11 #include "include/core/SkRefCnt.h"
12 #include "modules/skparagraph/include/FontArguments.h"
13 #include "modules/skparagraph/include/ParagraphCache.h"
14 #include "modules/skparagraph/include/TextStyle.h"
15 #include "include/private/SkTHash.h"
16 #include "drawing.h"
17 
18 namespace skia {
19 namespace textlayout {
20 
21 class TextStyle;
22 class Paragraph;
23 class FontCollection : public SkRefCnt {
24 public:
25     FontCollection();
26 
27     size_t getFontManagersCount() const;
28 
29 #ifndef USE_SKIA_TXT
30     void setAssetFontManager(sk_sp<SkFontMgr> fontManager);
31     void setDynamicFontManager(sk_sp<SkFontMgr> fontManager);
32     void setTestFontManager(sk_sp<SkFontMgr> fontManager);
33     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager);
34     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const char defaultFamilyName[]);
35     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);
36 
getFallbackManager()37     sk_sp<SkFontMgr> getFallbackManager() const { return fDefaultFontManager; }
38 
39     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle);
40     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
41 
42     sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale);
43     sk_sp<SkTypeface> defaultFallback();
44 #else
45     void setAssetFontManager(std::shared_ptr<RSFontMgr> fontManager);
46     void setDynamicFontManager(std::shared_ptr<RSFontMgr> fontManager);
47     void setTestFontManager(std::shared_ptr<RSFontMgr> fontManager);
48     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager);
49     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager, const char defaultFamilyName[]);
50     void setDefaultFontManager(std::shared_ptr<RSFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);
51 
getFallbackManager()52     std::shared_ptr<RSFontMgr> getFallbackManager() const { return fDefaultFontManager; }
53 
54     std::vector<std::shared_ptr<RSTypeface>> findTypefaces(
55         const std::vector<SkString>& familyNames, RSFontStyle fontStyle);
56     std::vector<std::shared_ptr<RSTypeface>> findTypefaces(
57         const std::vector<SkString>& familyNames, RSFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
58 
59     std::shared_ptr<RSTypeface> defaultFallback(SkUnichar unicode, RSFontStyle fontStyle, const SkString& locale);
60     std::shared_ptr<RSTypeface> defaultFallback();
61 #endif
62 
63 #ifndef USE_SKIA_TXT
64     sk_sp<SkTypeface> CloneTypeface(sk_sp<SkTypeface> typeface,
65         const std::optional<FontArguments>& fontArgs);
66 #else
67     std::shared_ptr<RSTypeface> CloneTypeface(std::shared_ptr<RSTypeface> typeface,
68         const std::optional<FontArguments>& fontArgs);
69 #endif
70 
71     void disableFontFallback();
72     void enableFontFallback();
fontFallbackEnabled()73     bool fontFallbackEnabled() { return fEnableFontFallback; }
74 
getParagraphCache()75     ParagraphCache* getParagraphCache() { return &fParagraphCache; }
76 
77     void clearCaches();
78 
79 private:
80 #ifndef USE_SKIA_TXT
81     std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const;
82 
83     sk_sp<SkTypeface> matchTypeface(const SkString& familyName, SkFontStyle fontStyle);
84 #else
85     std::vector<std::shared_ptr<RSFontMgr>> getFontManagerOrder() const;
86 
87     std::shared_ptr<RSTypeface> matchTypeface(const SkString& familyName, RSFontStyle fontStyle);
88 #endif
89 
90     struct FamilyKey {
91 #ifndef USE_SKIA_TXT
FamilyKeyFamilyKey92         FamilyKey(const std::vector<SkString>& familyNames, SkFontStyle style, const std::optional<FontArguments>& args)
93                 : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}
94 #else
95         FamilyKey(
96             const std::vector<SkString>& familyNames, RSFontStyle style, const std::optional<FontArguments>& args)
97                 : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}
98 #endif
99 
FamilyKeyFamilyKey100         FamilyKey() {}
101 
102         std::vector<SkString> fFamilyNames;
103 #ifndef USE_SKIA_TXT
104         SkFontStyle fFontStyle;
105 #else
106         RSFontStyle fFontStyle;
107 #endif
108         std::optional<FontArguments> fFontArguments;
109 
110         bool operator==(const FamilyKey& other) const;
111 
112         struct Hasher {
113             size_t operator()(const FamilyKey& key) const;
114         };
115     };
116 
117     bool fEnableFontFallback;
118 #ifndef USE_SKIA_TXT
119     SkTHashMap<FamilyKey, std::vector<sk_sp<SkTypeface>>, FamilyKey::Hasher> fTypefaces;
120     SkLRUCache<uint32_t, sk_sp<SkTypeface>> fVarTypefaces;
121     sk_sp<SkFontMgr> fDefaultFontManager;
122     sk_sp<SkFontMgr> fAssetFontManager;
123     sk_sp<SkFontMgr> fDynamicFontManager;
124     sk_sp<SkFontMgr> fTestFontManager;
125 #else
126     std::unordered_map<FamilyKey, std::vector<std::shared_ptr<RSTypeface>>, FamilyKey::Hasher> fTypefaces;
127     SkLRUCache<uint32_t, std::shared_ptr<RSTypeface>> fVarTypefaces;
128     std::shared_ptr<RSFontMgr> fDefaultFontManager;
129     std::shared_ptr<RSFontMgr> fAssetFontManager;
130     std::shared_ptr<RSFontMgr> fDynamicFontManager;
131     std::shared_ptr<RSFontMgr> fTestFontManager;
132 #endif
133     std::mutex fMutex;
134     std::vector<SkString> fDefaultFamilyNames;
135     ParagraphCache fParagraphCache;
136 };
137 }  // namespace textlayout
138 }  // namespace skia
139 
140 #endif  // FontCollection_DEFINED
141