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