1 /*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 * 2021.9.9 SkFontMgr_config_parser on previewer of ohos.
7 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved.
8 */
9
10 #ifndef SkFontMgr_config_parser_DEFINED
11 #define SkFontMgr_config_parser_DEFINED
12
13 #include <climits>
14 #include <string>
15
16 #include "include/core/SkFontMgr.h"
17 #include "include/core/SkString.h"
18 #include "include/core/SkTypes.h"
19 #include "include/private/SkTArray.h"
20 #include "include/private/SkTDArray.h"
21 #include "include/private/SkTHash.h"
22
23 /** \class SkLanguage
24
25 The SkLanguage class represents a human written language, and is used by
26 text draw operations to determine which glyph to draw when drawing
27 characters with variants (ie Han-derived characters).
28 */
29 class SkLanguage {
30 public:
SkLanguage()31 SkLanguage() { }
SkLanguage(const SkString & tag)32 SkLanguage(const SkString& tag) : fTag(tag) { }
SkLanguage(const char * tag)33 SkLanguage(const char* tag) : fTag(tag) { }
SkLanguage(const char * tag,size_t len)34 SkLanguage(const char* tag, size_t len) : fTag(tag, len) { }
SkLanguage(const SkLanguage & b)35 SkLanguage(const SkLanguage& b) : fTag(b.fTag) { }
36 ~SkLanguage() = default;
37
38 /** Gets a BCP 47 language identifier for this SkLanguage.
39 @return a BCP 47 language identifier representing this language
40 */
getTag()41 const SkString& getTag() const
42 {
43 return fTag;
44 }
45
46 /** Performs BCP 47 fallback to return an SkLanguage one step more general.
47 @return an SkLanguage one step more general
48 */
49 SkLanguage getParent() const;
50
51 bool operator==(const SkLanguage& b) const
52 {
53 return fTag == b.fTag;
54 }
55 bool operator!=(const SkLanguage& b) const
56 {
57 return fTag != b.fTag;
58 }
59 SkLanguage& operator=(const SkLanguage& b)
60 {
61 fTag = b.fTag;
62 return *this;
63 }
64
65 private:
66 //! BCP 47 language identifier
67 SkString fTag;
68 };
69
70 enum FontVariants {
71 kDefault_FontVariant = 0x01,
72 kCompact_FontVariant = 0x02,
73 kElegant_FontVariant = 0x04,
74 kLast_FontVariant = kElegant_FontVariant,
75 };
76 typedef uint32_t FontVariant;
77
78 // Must remain trivially movable (can be memmoved).
79 struct FontFileInfo {
FontFileInfoFontFileInfo80 FontFileInfo() : fIndex(0), fWeight(0), fStyle(Style::kAuto) { }
81
82 SkString fFileName;
83 int fIndex;
84 int fWeight;
85 enum class Style { kAuto, kNormal, kItalic } fStyle;
86 SkTArray<SkFontArguments::VariationPosition::Coordinate, true> fVariationDesignPosition;
87 };
88
89 /**
90 * A font family provides one or more names for a collection of fonts, each of
91 * which has a different style (normal, italic) or weight (thin, light, bold,
92 * etc).
93 * Some fonts may occur in compact variants for use in the user interface.
94 * Android distinguishes "fallback" fonts to support non-ASCII character sets.
95 */
96 struct FontFamily {
FontFamilyFontFamily97 FontFamily(const SkString& basePath, bool isFallbackFont)
98 : fVariant(kDefault_FontVariant)
99 , fOrder(-1)
100 , fIsFallbackFont(isFallbackFont)
101 , fBasePath(basePath)
102 { }
103
104 SkTArray<SkString, true> fNames;
105 SkTArray<FontFileInfo, true> fFonts;
106 SkTArray<SkLanguage, true> fLanguages;
107 SkTHashMap<SkString, std::unique_ptr<FontFamily>> fallbackFamilies;
108 FontVariant fVariant;
109 int fOrder; // internal to the parser, not useful to users.
110 bool fIsFallbackFont;
111 SkString fFallbackFor;
112 const SkString fBasePath;
113 };
114
115 class SkFontMgr_Config_Parser {
116 public:
117 SkFontMgr_Config_Parser() = default;
118 ~SkFontMgr_Config_Parser() = default;
119
GetInstance()120 static SkFontMgr_Config_Parser& GetInstance()
121 {
122 static SkFontMgr_Config_Parser instance;
123 return instance;
124 }
125
126 /** Parses system font configuration files and appends result to fontFamilies. */
127 void GetSystemFontFamilies(SkTDArray<FontFamily *> &fontFamilies);
128
129 };
130
131 /** Parses a null terminated string into an integer type, checking for overflow.
132 * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def
133 *
134 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
135 */
parse_non_negative_integer(const char * s,T * value)136 template<typename T> static bool parse_non_negative_integer(const char *s, T *value)
137 {
138 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
139
140 if (*s == '\0') {
141 return false;
142 }
143
144 const T nMax = std::numeric_limits<T>::max() / 10;
145 const T dMax = std::numeric_limits<T>::max() - (nMax * 10);
146 T n = 0;
147 for (; *s; ++s) {
148 // Check if digit
149 if (*s < '0' || '9' < *s) {
150 return false;
151 }
152 T d = *s - '0';
153 // Check for overflow
154 if (n > nMax || (n == nMax && d > dMax)) {
155 return false;
156 }
157 n = (n * 10) + d;
158 }
159 *value = n;
160 return true;
161 }
162
163 /** Parses a null terminated string into a signed fixed point value with bias N.
164 *
165 * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def ,
166 * but may start with '.' and does not support 'e'. '-?((:digit:+(.:digit:+)?)|(.:digit:+))'
167 *
168 * Checks for overflow.
169 * Low bit rounding is not defined (is currently truncate).
170 * Bias (N) required to allow for the sign bit and 4 bits of integer.
171 *
172 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
173 */
parse_fixed(const char * s,T * value)174 template<int N, typename T> static bool parse_fixed(const char *s, T *value)
175 {
176 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
177 static_assert(std::numeric_limits<T>::is_signed, "T_must_be_signed");
178 static_assert(sizeof(T) * CHAR_BIT - N >= 5, "N_must_leave_four_bits_plus_sign");
179
180 bool negate = false;
181 if (*s == '-') {
182 ++s;
183 negate = true;
184 }
185 if (*s == '\0') {
186 return false;
187 }
188
189 const T nMax = (std::numeric_limits<T>::max() >> N) / 10;
190 const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10);
191 T n = 0;
192 T frac = 0;
193 for (; *s; ++s) {
194 // Check if digit
195 if (*s < '0' || '9' < *s) {
196 // If it wasn't a digit, check if it is a '.' followed by something.
197 if (*s != '.' || s[1] == '\0') {
198 return false;
199 }
200 // Find the end, verify digits.
201 for (++s; *s; ++s) {
202 if (*s < '0' || '9' < *s) {
203 return false;
204 }
205 }
206 // Read back toward the '.'.
207 for (--s; *s != '.'; --s) {
208 T d = *s - '0';
209 frac = (frac + (d << N)) / 10; // This requires four bits overhead.
210 }
211 break;
212 }
213 T d = *s - '0';
214 // Check for overflow
215 if (n > nMax || (n == nMax && d > dMax)) {
216 return false;
217 }
218 n = (n * 10) + d;
219 }
220 if (negate) {
221 n = -n;
222 frac = -frac;
223 }
224 *value = SkLeftShift(n, N) + frac;
225 return true;
226 }
227
228 #endif /* SkFontMgr_config_parser_DEFINED */
229