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