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