xref: /aosp_15_r20/external/skia/include/core/SkFontMgr.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkFontMgr_DEFINED
9 #define SkFontMgr_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkTypes.h"
13 
14 #include <memory>
15 
16 class SkData;
17 class SkFontStyle;
18 class SkStreamAsset;
19 class SkString;
20 class SkTypeface;
21 struct SkFontArguments;
22 
23 class SK_API SkFontStyleSet : public SkRefCnt {
24 public:
25     virtual int count() = 0;
26     virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
27     virtual sk_sp<SkTypeface> createTypeface(int index) = 0;
28     virtual sk_sp<SkTypeface> matchStyle(const SkFontStyle& pattern) = 0;
29 
30     static sk_sp<SkFontStyleSet> CreateEmpty();
31 
32 protected:
33     sk_sp<SkTypeface> matchStyleCSS3(const SkFontStyle& pattern);
34 };
35 
36 class SK_API SkFontMgr : public SkRefCnt {
37 public:
38     int countFamilies() const;
39     void getFamilyName(int index, SkString* familyName) const;
40     sk_sp<SkFontStyleSet> createStyleSet(int index) const;
41 
42     /**
43      *  The caller must call unref() on the returned object.
44      *  Never returns NULL; will return an empty set if the name is not found.
45      *
46      *  Passing nullptr as the parameter will return the default system family.
47      *  Note that most systems don't have a default system family, so passing nullptr will often
48      *  result in the empty set.
49      *
50      *  It is possible that this will return a style set not accessible from
51      *  createStyleSet(int) due to hidden or auto-activated fonts.
52      */
53     sk_sp<SkFontStyleSet> matchFamily(const char familyName[]) const;
54 
55     /**
56      *  Find the closest matching typeface to the specified familyName and style
57      *  and return a ref to it. The caller must call unref() on the returned
58      *  object. Will return nullptr if no 'good' match is found.
59      *
60      *  Passing |nullptr| as the parameter for |familyName| will return the
61      *  default system font.
62      *
63      *  It is possible that this will return a style set not accessible from
64      *  createStyleSet(int) or matchFamily(const char[]) due to hidden or
65      *  auto-activated fonts.
66      */
67     sk_sp<SkTypeface> matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
68 
69     /**
70      *  Use the system fallback to find a typeface for the given character.
71      *  Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
72      *  so it is fine to just pass a ISO 639 here.
73      *
74      *  Will return NULL if no family can be found for the character
75      *  in the system fallback.
76      *
77      *  Passing |nullptr| as the parameter for |familyName| will return the
78      *  default system font.
79      *
80      *  bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
81      *  most significant. If no specified bcp47 codes match, any font with the
82      *  requested character will be matched.
83      */
84     sk_sp<SkTypeface> matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
85                                                 const char* bcp47[], int bcp47Count,
86                                                 SkUnichar character) const;
87 
88     /**
89      *  Create a typeface for the specified data and TTC index (pass 0 for none)
90      *  or NULL if the data is not recognized. The caller must call unref() on
91      *  the returned object if it is not null.
92      */
93     sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const;
94 
95     /**
96      *  Create a typeface for the specified stream and TTC index
97      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
98      *  must call unref() on the returned object if it is not null.
99      */
100     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const;
101 
102     /* Experimental, API subject to change. */
103     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const;
104 
105     /**
106      *  Create a typeface for the specified fileName and TTC index
107      *  (pass 0 for none) or NULL if the file is not found, or its contents are
108      *  not recognized. The caller must call unref() on the returned object
109      *  if it is not null.
110      */
111     sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const;
112 
113     sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const;
114 
115     /* Returns an empty font manager without any typeface dependencies */
116     static sk_sp<SkFontMgr> RefEmpty();
117 
118 protected:
119     virtual int onCountFamilies() const = 0;
120     virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
121     virtual sk_sp<SkFontStyleSet> onCreateStyleSet(int index)const  = 0;
122 
123     /** May return NULL if the name is not found. */
124     virtual sk_sp<SkFontStyleSet> onMatchFamily(const char familyName[]) const = 0;
125 
126     virtual sk_sp<SkTypeface> onMatchFamilyStyle(const char familyName[],
127                                                  const SkFontStyle&) const = 0;
128     virtual sk_sp<SkTypeface> onMatchFamilyStyleCharacter(const char familyName[],
129                                                           const SkFontStyle&,
130                                                           const char* bcp47[], int bcp47Count,
131                                                           SkUnichar character) const = 0;
132 
133     virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0;
134     virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
135                                                     int ttcIndex) const = 0;
136     virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
137                                                    const SkFontArguments&) const = 0;
138     virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0;
139 
140     virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0;
141 };
142 
143 #endif
144