• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #if defined(CROSS_PLATFORM)
111 /** Parses system font configuration files and appends result to fontFamilies. */
112 void GetSystemFontFamiliesForSymbol(SkTDArray<FontFamily *> &fontFamilies);
113 #endif
114 
115 /** Parses font configuration files and appends result to fontFamilies. */
116 void GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies,
117                            const SkString& basePath,
118                            const char* fontsXml,
119                            const char* fallbackFontsXml,
120                            const char* langFallbackFontsDir = nullptr);
121 
122 }  // namespace SkFontMgr_Android_Parser
123 
124 
125 /** Parses a null terminated string into an integer type, checking for overflow.
126  *  http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def
127  *
128  *  If the string cannot be parsed into 'value', returns false and does not change 'value'.
129  */
parse_non_negative_integer(const char * s,T * value)130 template <typename T> bool parse_non_negative_integer(const char* s, T* value) {
131     static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
132 
133     if (*s == '\0') {
134         return false;
135     }
136 
137     const T nMax = std::numeric_limits<T>::max() / 10;
138     const T dMax = std::numeric_limits<T>::max() - (nMax * 10);
139     T n = 0;
140     for (; *s; ++s) {
141         // Check if digit
142         if (*s < '0' || '9' < *s) {
143             return false;
144         }
145         T d = *s - '0';
146         // Check for overflow
147         if (n > nMax || (n == nMax && d > dMax)) {
148             return false;
149         }
150         n = (n * 10) + d;
151     }
152     *value = n;
153     return true;
154 }
155 
156 /** Parses a null terminated string into a signed fixed point value with bias N.
157  *
158  *  Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def ,
159  *  but may start with '.' and does not support 'e'. '-?((:digit:+(.:digit:+)?)|(.:digit:+))'
160  *
161  *  Checks for overflow.
162  *  Low bit rounding is not defined (is currently truncate).
163  *  Bias (N) required to allow for the sign bit and 4 bits of integer.
164  *
165  *  If the string cannot be parsed into 'value', returns false and does not change 'value'.
166  */
parse_fixed(const char * s,T * value)167 template <int N, typename T> bool parse_fixed(const char* s, T* value) {
168     static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
169     static_assert(std::numeric_limits<T>::is_signed, "T_must_be_signed");
170     static_assert(sizeof(T) * CHAR_BIT - N >= 5, "N_must_leave_four_bits_plus_sign");
171 
172     bool negate = false;
173     if (*s == '-') {
174         ++s;
175         negate = true;
176     }
177     if (*s == '\0') {
178         return false;
179     }
180 
181     const T nMax = (std::numeric_limits<T>::max() >> N) / 10;
182     const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10);
183     T n = 0;
184     T frac = 0;
185     for (; *s; ++s) {
186         // Check if digit
187         if (*s < '0' || '9' < *s) {
188             // If it wasn't a digit, check if it is a '.' followed by something.
189             if (*s != '.' || s[1] == '\0') {
190                 return false;
191             }
192             // Find the end, verify digits.
193             for (++s; *s; ++s) {
194                 if (*s < '0' || '9' < *s) {
195                     return false;
196                 }
197             }
198             // Read back toward the '.'.
199             for (--s; *s != '.'; --s) {
200                 T d = *s - '0';
201                 frac = (frac + (d << N)) / 10; // This requires four bits overhead.
202             }
203             break;
204         }
205         T d = *s - '0';
206         // Check for overflow
207         if (n > nMax || (n == nMax && d > dMax)) {
208             return false;
209         }
210         n = (n * 10) + d;
211     }
212     if (negate) {
213         n = -n;
214         frac = -frac;
215     }
216     *value = SkLeftShift(n, N) + frac;
217     return true;
218 }
219 
220 #endif /* SkFontMgr_android_parser_DEFINED */
221