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