2 * Copyright 2011 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkFontMgr_android_parser_DEFINED
9 #define SkFontMgr_android_parser_DEFINED
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"
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).
30 SkLanguage(const SkString& tag) : fTag(tag) { }
31 SkLanguage(const char* tag) : fTag(tag) { }
32 SkLanguage(const char* tag, size_t len) : fTag(tag, len) { }
33 SkLanguage(const SkLanguage& b) : fTag(b.fTag) { }
35 /** Gets a BCP 47 language identifier for this SkLanguage.
36 @return a BCP 47 language identifier representing this language
38 const SkString& getTag() const { return fTag; }
40 /** Performs BCP 47 fallback to return an SkLanguage one step more general.
41 @return an SkLanguage one step more general
43 SkLanguage getParent() const;
45 bool operator==(const SkLanguage& b) const {
46 return fTag == b.fTag;
48 bool operator!=(const SkLanguage& b) const {
49 return fTag != b.fTag;
51 SkLanguage& operator=(const SkLanguage& b) {
57 //! BCP 47 language identifier
62 kDefault_FontVariant = 0x01,
63 kCompact_FontVariant = 0x02,
64 kElegant_FontVariant = 0x04,
65 kLast_FontVariant = kElegant_FontVariant,
67 typedef uint32_t FontVariant;
69 // Must remain trivially movable (can be memmoved).
71 FontFileInfo() : fIndex(0), fWeight(0), fStyle(Style::kAuto) { }
76 enum class Style { kAuto, kNormal, kItalic } fStyle;
77 SkTArray<SkFontArguments::VariationPosition::Coordinate, true> fVariationDesignPosition;
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,
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.
88 FontFamily(const SkString& basePath, bool isFallbackFont)
89 : fVariant(kDefault_FontVariant)
91 , fIsFallbackFont(isFallbackFont)
95 SkTArray<SkString, true> fNames;
96 SkTArray<FontFileInfo, true> fFonts;
97 SkTArray<SkLanguage, true> fLanguages;
98 SkTHashMap<SkString, std::unique_ptr<FontFamily>> fallbackFamilies;
100 int fOrder; // internal to the parser, not useful to users.
101 bool fIsFallbackFont;
102 SkString fFallbackFor;
103 const SkString fBasePath;
106 namespace SkFontMgr_Android_Parser {
108 /** Parses system font configuration files and appends result to fontFamilies. */
109 void GetSystemFontFamilies(SkTDArray<FontFamily*>& fontFamilies);
111 /** Parses font configuration files and appends result to fontFamilies. */
112 void GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies,
113 const SkString& basePath,
114 const char* fontsXml,
115 const char* fallbackFontsXml,
116 const char* langFallbackFontsDir = nullptr);
118 } // namespace SkFontMgr_Android_Parser
121 /** Parses a null terminated string into an integer type, checking for overflow.
122 * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def
124 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
126 template <typename T> bool parse_non_negative_integer(const char* s, T* value) {
127 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
133 const T nMax = std::numeric_limits<T>::max() / 10;
134 const T dMax = std::numeric_limits<T>::max() - (nMax * 10);
138 if (*s < '0' || '9' < *s) {
142 // Check for overflow
143 if (n > nMax || (n == nMax && d > dMax)) {
152 /** Parses a null terminated string into a signed fixed point value with bias N.
154 * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def ,
155 * but may start with '.' and does not support 'e'. '-?((:digit:+(.:digit:+)?)|(.:digit:+))'
157 * Checks for overflow.
158 * Low bit rounding is not defined (is currently truncate).
159 * Bias (N) required to allow for the sign bit and 4 bits of integer.
161 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
163 template <int N, typename T> bool parse_fixed(const char* s, T* value) {
164 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
165 static_assert(std::numeric_limits<T>::is_signed, "T_must_be_signed");
166 static_assert(sizeof(T) * CHAR_BIT - N >= 5, "N_must_leave_four_bits_plus_sign");
177 const T nMax = (std::numeric_limits<T>::max() >> N) / 10;
178 const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10);
183 if (*s < '0' || '9' < *s) {
184 // If it wasn't a digit, check if it is a '.' followed by something.
185 if (*s != '.' || s[1] == '\0') {
188 // Find the end, verify digits.
190 if (*s < '0' || '9' < *s) {
194 // Read back toward the '.'.
195 for (--s; *s != '.'; --s) {
197 frac = (frac + (d << N)) / 10; // This requires four bits overhead.
202 // Check for overflow
203 if (n > nMax || (n == nMax && d > dMax)) {
212 *value = SkLeftShift(n, N) + frac;
216 #endif /* SkFontMgr_android_parser_DEFINED */