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