xref: /aosp_15_r20/external/skia/modules/skparagraph/include/TypefaceFontProvider.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 // Copyright 2019 Google LLC.
2 #ifndef TypefaceFontProvider_DEFINED
3 #define TypefaceFontProvider_DEFINED
4 
5 #include "include/core/SkFontMgr.h"
6 #include "include/core/SkFontStyle.h"
7 #include "include/core/SkStream.h"
8 #include "include/core/SkString.h"
9 #include "include/private/base/SkTArray.h"
10 #include "src/core/SkTHash.h"
11 
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 
16 namespace skia {
17 namespace textlayout {
18 
19 class TypefaceFontStyleSet : public SkFontStyleSet {
20 public:
21     explicit TypefaceFontStyleSet(const SkString& familyName);
22 
23     int count() override;
24     void getStyle(int index, SkFontStyle*, SkString* name) override;
25     sk_sp<SkTypeface> createTypeface(int index) override;
26     sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) override;
27 
getFamilyName()28     SkString getFamilyName() const { return fFamilyName; }
getAlias()29     SkString getAlias() const { return fAlias; }
30     void appendTypeface(sk_sp<SkTypeface> typeface);
31 
32 private:
33     skia_private::TArray<sk_sp<SkTypeface>> fStyles;
34     SkString fFamilyName;
35     SkString fAlias;
36 };
37 
38 class TypefaceFontProvider : public SkFontMgr {
39 public:
40     size_t registerTypeface(sk_sp<SkTypeface> typeface);
41     size_t registerTypeface(sk_sp<SkTypeface> typeface, const SkString& alias);
42 
43     int onCountFamilies() const override;
44 
45     void onGetFamilyName(int index, SkString* familyName) const override;
46 
47     sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const override;
48 
49     sk_sp<SkFontStyleSet> onCreateStyleSet(int) const override;
50     sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[], const SkFontStyle& pattern) const override;
onMatchFamilyStyleCharacter(const char[],const SkFontStyle &,const char * [],int,SkUnichar)51     sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char[], const SkFontStyle&,
52                                                   const char*[], int,
53                                                   SkUnichar) const override {
54         return nullptr;
55     }
56 
onMakeFromData(sk_sp<SkData>,int)57     sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int) const override { return nullptr; }
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,int)58     sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int) const override {
59         return nullptr;
60     }
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,const SkFontArguments &)61     sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
62                                            const SkFontArguments&) const override {
63         return nullptr;
64     }
onMakeFromFile(const char[],int)65     sk_sp<SkTypeface> onMakeFromFile(const char[], int) const override {
66         return nullptr;
67     }
68 
69     sk_sp<SkTypeface> onLegacyMakeTypeface(const char[], SkFontStyle) const override;
70 
71 private:
72     skia_private::THashMap<SkString, sk_sp<TypefaceFontStyleSet>> fRegisteredFamilies;
73     skia_private::TArray<SkString> fFamilyNames;
74 };
75 
76 }  // namespace textlayout
77 }  // namespace skia
78 
79 #endif  // TypefaceFontProvider_DEFINED
80