• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/SkFontArguments.h"
12 #include "include/core/SkFontStyle.h"
13 #include "include/core/SkRefCnt.h"
14 #include "include/core/SkTypes.h"
15 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \
16     defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX)
17 #include <string>
18 #endif
19 
20 #ifdef OHOS_SUPPORT
21 #include <vector>
22 #endif
23 
24 #ifdef OHOS_SUPPORT
25 enum FontCheckCode {
26     SUCCESSED                  = 0, /** no error */
27     ERROR_PARSE_CONFIG_FAILED  = 1, /** failed to parse the JSON configuration file */
28     ERROR_TYPE_OTHER           = 2  /** other reasons, such as empty input parameters or other internal reasons */
29 };
30 
31 struct SkByteArray {
32     std::unique_ptr<uint8_t[]> strData = nullptr; // A byte array in UTF-16BE encoding
33     uint32_t strLen = 0;
34 };
35 #endif
36 
37 class SkData;
38 class SkFontData;
39 class SkStreamAsset;
40 class SkString;
41 class SkTypeface;
42 
43 class SK_API SkFontStyleSet : public SkRefCnt {
44 public:
45     virtual int count() = 0;
46     virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
47     virtual SkTypeface* createTypeface(int index) = 0;
48     virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
49 
50     static SkFontStyleSet* CreateEmpty();
51 
52 protected:
53     SkTypeface* matchStyleCSS3(const SkFontStyle& pattern);
54 
55 private:
56     using INHERITED = SkRefCnt;
57 };
58 
59 class SK_API SkFontMgr : public SkRefCnt {
60 public:
61     int countFamilies() const;
62     void getFamilyName(int index, SkString* familyName) const;
63     SkFontStyleSet* createStyleSet(int index) const;
64 
65 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \
66     defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX)
67     /**
68      * OHOS_Container font base path. It is empty when using OpenHarmony fonts.
69      */
70     static std::string containerFontPath;
71     /**
72      * Indicate the runtimeOS of preview(OHOS_Container and OHOS)
73      */
74     static std::string runtimeOS;
75 #endif
76     /**
77      *  The caller must call unref() on the returned object.
78      *  Never returns NULL; will return an empty set if the name is not found.
79      *
80      *  Passing nullptr as the parameter will return the default system family.
81      *  Note that most systems don't have a default system family, so passing nullptr will often
82      *  result in the empty set.
83      *
84      *  It is possible that this will return a style set not accessible from
85      *  createStyleSet(int) due to hidden or auto-activated fonts.
86      */
87     SkFontStyleSet* matchFamily(const char familyName[]) const;
88 
89     /**
90      *  Find the closest matching typeface to the specified familyName and style
91      *  and return a ref to it. The caller must call unref() on the returned
92      *  object. Will return nullptr if no 'good' match is found.
93      *
94      *  Passing |nullptr| as the parameter for |familyName| will return the
95      *  default system font.
96      *
97      *  It is possible that this will return a style set not accessible from
98      *  createStyleSet(int) or matchFamily(const char[]) due to hidden or
99      *  auto-activated fonts.
100      */
101     SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
102 
103     /**
104      *  Use the system fallback to find a typeface for the given character.
105      *  Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
106      *  so it is fine to just pass a ISO 639 here.
107      *
108      *  Will return NULL if no family can be found for the character
109      *  in the system fallback.
110      *
111      *  Passing |nullptr| as the parameter for |familyName| will return the
112      *  default system font.
113      *
114      *  bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
115      *  most significant. If no specified bcp47 codes match, any font with the
116      *  requested character will be matched.
117      */
118     SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
119                                           const char* bcp47[], int bcp47Count,
120                                           SkUnichar character) const;
121 
122     /**
123      *  Create a typeface for the specified data and TTC index (pass 0 for none)
124      *  or NULL if the data is not recognized. The caller must call unref() on
125      *  the returned object if it is not null.
126      */
127     sk_sp<SkTypeface> makeFromData(sk_sp<SkData>, int ttcIndex = 0) const;
128 
129     /**
130      *  Create a typeface for the specified stream and TTC index
131      *  (pass 0 for none) or NULL if the stream is not recognized. The caller
132      *  must call unref() on the returned object if it is not null.
133      */
134     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, int ttcIndex = 0) const;
135 
136     /* Experimental, API subject to change. */
137     sk_sp<SkTypeface> makeFromStream(std::unique_ptr<SkStreamAsset>, const SkFontArguments&) const;
138 
139     /**
140      *  Create a typeface for the specified fileName and TTC index
141      *  (pass 0 for none) or NULL if the file is not found, or its contents are
142      *  not recognized. The caller must call unref() on the returned object
143      *  if it is not null.
144      */
145     sk_sp<SkTypeface> makeFromFile(const char path[], int ttcIndex = 0) const;
146 
147     sk_sp<SkTypeface> legacyMakeTypeface(const char familyName[], SkFontStyle style) const;
148 
149 #ifdef OHOS_SUPPORT
150     std::vector<sk_sp<SkTypeface>> getSystemFonts();
151 #endif
152 
153     /** Return the default fontmgr. */
154     static sk_sp<SkFontMgr> RefDefault();
155 
156     /* Returns an empty font manager without any typeface dependencies */
157     static sk_sp<SkFontMgr> RefEmpty();
158 
159 #if defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_WIN) or defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_MAC) or \
160     defined(SK_BUILD_FONT_MGR_FOR_PREVIEW_LINUX)
161     /** Set the runtimeOS and container font base path */
SetFontMgrConfig(const std::string runtime,const std::string containerFontBasePath)162     static void SetFontMgrConfig(const std::string runtime, const std::string containerFontBasePath)
163     {
164         containerFontPath = containerFontBasePath;
165         runtimeOS = runtime;
166     }
167 #endif
168 
169 #ifdef OHOS_SUPPORT
170     /**
171      *  Adding a base class interface function to a subclass, generally doesn't go here
172      *  0 means valid
173      */
GetFontFullName(int fontFd,std::vector<SkByteArray> & fullnameVec)174     virtual int GetFontFullName(int fontFd, std::vector<SkByteArray> &fullnameVec)
175     {
176         return ERROR_TYPE_OTHER;
177     }
178     /**
179      *  Adding a base class interface function to a subclass, generally doesn't go here
180      *  0 means success
181      */
ParseInstallFontConfig(const std::string & configPath,std::vector<std::string> & fontPathVec)182     virtual int ParseInstallFontConfig(const std::string& configPath, std::vector<std::string>& fontPathVec)
183     {
184         return ERROR_PARSE_CONFIG_FAILED;
185     }
186 #endif
187 
188 protected:
189     virtual int onCountFamilies() const = 0;
190     virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
191     virtual SkFontStyleSet* onCreateStyleSet(int index)const  = 0;
192 
193     /** May return NULL if the name is not found. */
194     virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
195 
196     virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
197                                            const SkFontStyle&) const = 0;
198     virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
199                                                     const char* bcp47[], int bcp47Count,
200                                                     SkUnichar character) const = 0;
201 
202     virtual sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const = 0;
203     virtual sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
204                                                     int ttcIndex) const = 0;
205     virtual sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
206                                                    const SkFontArguments&) const = 0;
207     virtual sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const = 0;
208 
209     virtual sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const = 0;
210 
211     // this method is never called -- will be removed
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &)212     virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
213                                          const SkFontStyle&) const {
214         return nullptr;
215     }
216 
217 #ifdef OHOS_SUPPORT
218     virtual std::vector<sk_sp<SkTypeface>> onGetSystemFonts() const;
219 #endif
220 
221 private:
222 
223     /** Implemented by porting layer to return the default factory. */
224     static sk_sp<SkFontMgr> Factory();
225 
226     using INHERITED = SkRefCnt;
227 };
228 
229 #endif
230