xref: /aosp_15_r20/external/skia/modules/skparagraph/include/FontCollection.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 #ifndef FontCollection_DEFINED
3 #define FontCollection_DEFINED
4 
5 #include <memory>
6 #include <optional>
7 #include <set>
8 #include "include/core/SkFontMgr.h"
9 #include "include/core/SkRefCnt.h"
10 #include "include/core/SkSpan.h"
11 #include "modules/skparagraph/include/FontArguments.h"
12 #include "modules/skparagraph/include/ParagraphCache.h"
13 #include "modules/skparagraph/include/TextStyle.h"
14 #include "src/core/SkTHash.h"
15 
16 namespace skia {
17 namespace textlayout {
18 
19 class TextStyle;
20 class Paragraph;
21 class FontCollection : public SkRefCnt {
22 public:
23     FontCollection();
24 
25     size_t getFontManagersCount() const;
26 
27     void setAssetFontManager(sk_sp<SkFontMgr> fontManager);
28     void setDynamicFontManager(sk_sp<SkFontMgr> fontManager);
29     void setTestFontManager(sk_sp<SkFontMgr> fontManager);
30     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager);
31     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const char defaultFamilyName[]);
32     void setDefaultFontManager(sk_sp<SkFontMgr> fontManager, const std::vector<SkString>& defaultFamilyNames);
33 
getFallbackManager()34     sk_sp<SkFontMgr> getFallbackManager() const { return fDefaultFontManager; }
35 
36     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle);
37     std::vector<sk_sp<SkTypeface>> findTypefaces(const std::vector<SkString>& familyNames, SkFontStyle fontStyle, const std::optional<FontArguments>& fontArgs);
38 
39     sk_sp<SkTypeface> defaultFallback(SkUnichar unicode, SkFontStyle fontStyle, const SkString& locale);
40     sk_sp<SkTypeface> defaultEmojiFallback(SkUnichar emojiStart, SkFontStyle fontStyle, const SkString& locale);
41     sk_sp<SkTypeface> defaultFallback();
42 
43     void disableFontFallback();
44     void enableFontFallback();
fontFallbackEnabled()45     bool fontFallbackEnabled() { return fEnableFontFallback; }
46 
getParagraphCache()47     ParagraphCache* getParagraphCache() { return &fParagraphCache; }
48 
49     void clearCaches();
50 
51 private:
52     std::vector<sk_sp<SkFontMgr>> getFontManagerOrder() const;
53 
54     sk_sp<SkTypeface> matchTypeface(const SkString& familyName, SkFontStyle fontStyle);
55 
56     struct FamilyKey {
FamilyKeyFamilyKey57         FamilyKey(const std::vector<SkString>& familyNames, SkFontStyle style, const std::optional<FontArguments>& args)
58                 : fFamilyNames(familyNames), fFontStyle(style), fFontArguments(args) {}
59 
FamilyKeyFamilyKey60         FamilyKey() {}
61 
62         std::vector<SkString> fFamilyNames;
63         SkFontStyle fFontStyle;
64         std::optional<FontArguments> fFontArguments;
65 
66         bool operator==(const FamilyKey& other) const;
67 
68         struct Hasher {
69             size_t operator()(const FamilyKey& key) const;
70         };
71     };
72 
73     bool fEnableFontFallback;
74     skia_private::THashMap<FamilyKey, std::vector<sk_sp<SkTypeface>>, FamilyKey::Hasher> fTypefaces;
75     sk_sp<SkFontMgr> fDefaultFontManager;
76     sk_sp<SkFontMgr> fAssetFontManager;
77     sk_sp<SkFontMgr> fDynamicFontManager;
78     sk_sp<SkFontMgr> fTestFontManager;
79 
80     std::vector<SkString> fDefaultFamilyNames;
81     ParagraphCache fParagraphCache;
82 };
83 }  // namespace textlayout
84 }  // namespace skia
85 
86 #endif  // FontCollection_DEFINED
87