blob: d8de6ab1fd3259f2059f32d5e96b45e3ef73c5c5 [file] [log] [blame]
djsollen@google.combfae9d32013-05-21 16:53:50 +00001/*
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
bungemanc5308542015-06-23 13:25:46 -07008#ifndef SkFontMgr_android_parser_DEFINED
9#define SkFontMgr_android_parser_DEFINED
djsollen@google.combfae9d32013-05-21 16:53:50 +000010
bungeman47a1e962016-02-25 11:20:01 -080011#include "SkFontMgr.h"
djsollen@google.com9a70f342013-06-25 18:07:45 +000012#include "SkString.h"
bungemanf20488b2015-07-29 11:49:40 -070013#include "SkTArray.h"
djsollen@google.combfae9d32013-05-21 16:53:50 +000014#include "SkTDArray.h"
bungemanf20488b2015-07-29 11:49:40 -070015#include "SkTypes.h"
djsollen@google.combfae9d32013-05-21 16:53:50 +000016
bungeman41868fe2015-05-20 09:21:04 -070017#include <climits>
18#include <limits>
19
djsollen3b625542014-08-14 06:29:02 -070020/** \class SkLanguage
21
22 The SkLanguage class represents a human written language, and is used by
23 text draw operations to determine which glyph to draw when drawing
24 characters with variants (ie Han-derived characters).
25*/
26class SkLanguage {
27public:
28 SkLanguage() { }
29 SkLanguage(const SkString& tag) : fTag(tag) { }
30 SkLanguage(const char* tag) : fTag(tag) { }
31 SkLanguage(const char* tag, size_t len) : fTag(tag, len) { }
32 SkLanguage(const SkLanguage& b) : fTag(b.fTag) { }
33
34 /** Gets a BCP 47 language identifier for this SkLanguage.
35 @return a BCP 47 language identifier representing this language
36 */
37 const SkString& getTag() const { return fTag; }
38
39 /** Performs BCP 47 fallback to return an SkLanguage one step more general.
40 @return an SkLanguage one step more general
41 */
42 SkLanguage getParent() const;
43
44 bool operator==(const SkLanguage& b) const {
45 return fTag == b.fTag;
46 }
47 bool operator!=(const SkLanguage& b) const {
48 return fTag != b.fTag;
49 }
50 SkLanguage& operator=(const SkLanguage& b) {
51 fTag = b.fTag;
52 return *this;
53 }
54
55private:
56 //! BCP 47 language identifier
57 SkString fTag;
58};
59
60enum FontVariants {
61 kDefault_FontVariant = 0x01,
62 kCompact_FontVariant = 0x02,
63 kElegant_FontVariant = 0x04,
64 kLast_FontVariant = kElegant_FontVariant,
65};
66typedef uint32_t FontVariant;
67
bungemanb5d0ce62015-02-13 11:49:07 -080068// Must remain trivially movable (can be memmoved).
djsollen@google.combfae9d32013-05-21 16:53:50 +000069struct FontFileInfo {
bungemane85a7542015-04-17 13:51:08 -070070 FontFileInfo() : fIndex(0), fWeight(0), fStyle(Style::kAuto) { }
bungeman8d84c992014-07-24 08:05:09 -070071
bungeman7fa87cd2015-02-06 07:59:19 -080072 SkString fFileName;
73 int fIndex;
74 int fWeight;
bungemane85a7542015-04-17 13:51:08 -070075 enum class Style { kAuto, kNormal, kItalic } fStyle;
Ben Wagnerfc497342017-02-24 11:15:26 -050076 SkTArray<SkFontArguments::VariationPosition::Coordinate, true> fVariationDesignPosition;
djsollen@google.combfae9d32013-05-21 16:53:50 +000077};
78
79/**
tomhudsond3ddea22014-08-11 11:28:00 -070080 * 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.
djsollen@google.combfae9d32013-05-21 16:53:50 +000085 */
86struct FontFamily {
bungeman7fa87cd2015-02-06 07:59:19 -080087 FontFamily(const SkString& basePath, bool isFallbackFont)
djsollen3b625542014-08-14 06:29:02 -070088 : fVariant(kDefault_FontVariant)
tomhudsond3ddea22014-08-11 11:28:00 -070089 , fOrder(-1)
bungeman7fa87cd2015-02-06 07:59:19 -080090 , fIsFallbackFont(isFallbackFont)
91 , fBasePath(basePath)
92 { }
djsollen@google.combfae9d32013-05-21 16:53:50 +000093
bungemanb5d0ce62015-02-13 11:49:07 -080094 SkTArray<SkString, true> fNames;
95 SkTArray<FontFileInfo, true> fFonts;
Ben Wagneraee878d2017-08-10 13:49:41 -040096 SkTArray<SkLanguage, true> fLanguages;
bungeman7fa87cd2015-02-06 07:59:19 -080097 FontVariant fVariant;
bungemanc5308542015-06-23 13:25:46 -070098 int fOrder; // internal to the parser, not useful to users.
bungeman7fa87cd2015-02-06 07:59:19 -080099 bool fIsFallbackFont;
100 const SkString fBasePath;
djsollen@google.combfae9d32013-05-21 16:53:50 +0000101};
102
bungemanc5308542015-06-23 13:25:46 -0700103namespace SkFontMgr_Android_Parser {
djsollen@google.combfae9d32013-05-21 16:53:50 +0000104
bungeman7fa87cd2015-02-06 07:59:19 -0800105/** Parses system font configuration files and appends result to fontFamilies. */
106void GetSystemFontFamilies(SkTDArray<FontFamily*>& fontFamilies);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000107
bungeman7fa87cd2015-02-06 07:59:19 -0800108/** Parses font configuration files and appends result to fontFamilies. */
109void GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies,
110 const SkString& basePath,
111 const char* fontsXml,
bungemanc3c69432015-02-11 07:18:51 -0800112 const char* fallbackFontsXml,
halcanary96fcdcc2015-08-27 07:41:13 -0700113 const char* langFallbackFontsDir = nullptr);
djsollen@google.combfae9d32013-05-21 16:53:50 +0000114
bungemanc5308542015-06-23 13:25:46 -0700115} // SkFontMgr_Android_Parser namespace
djsollen@google.combfae9d32013-05-21 16:53:50 +0000116
bungeman41868fe2015-05-20 09:21:04 -0700117
118/** Parses a null terminated string into an integer type, checking for overflow.
119 * http://www.w3.org/TR/html-markup/datatypes.html#common.data.integer.non-negative-def
120 *
121 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
122 */
123template <typename T> static bool parse_non_negative_integer(const char* s, T* value) {
bungeman99fe8222015-08-20 07:57:51 -0700124 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
bungeman41868fe2015-05-20 09:21:04 -0700125
126 if (*s == '\0') {
127 return false;
128 }
129
130 const T nMax = std::numeric_limits<T>::max() / 10;
131 const T dMax = std::numeric_limits<T>::max() - (nMax * 10);
132 T n = 0;
133 for (; *s; ++s) {
134 // Check if digit
135 if (*s < '0' || '9' < *s) {
136 return false;
137 }
138 T d = *s - '0';
139 // Check for overflow
140 if (n > nMax || (n == nMax && d > dMax)) {
141 return false;
142 }
143 n = (n * 10) + d;
144 }
145 *value = n;
146 return true;
147}
148
149/** Parses a null terminated string into a signed fixed point value with bias N.
150 *
151 * Like http://www.w3.org/TR/html-markup/datatypes.html#common.data.float-def ,
152 * but may start with '.' and does not support 'e'. '-?((:digit:+(.:digit:+)?)|(.:digit:+))'
153 *
154 * Checks for overflow.
155 * Low bit rounding is not defined (is currently truncate).
156 * Bias (N) required to allow for the sign bit and 4 bits of integer.
157 *
158 * If the string cannot be parsed into 'value', returns false and does not change 'value'.
159 */
160template <int N, typename T> static bool parse_fixed(const char* s, T* value) {
bungeman99fe8222015-08-20 07:57:51 -0700161 static_assert(std::numeric_limits<T>::is_integer, "T_must_be_integer");
162 static_assert(std::numeric_limits<T>::is_signed, "T_must_be_signed");
163 static_assert(sizeof(T) * CHAR_BIT - N >= 5, "N_must_leave_four_bits_plus_sign");
bungeman41868fe2015-05-20 09:21:04 -0700164
165 bool negate = false;
166 if (*s == '-') {
167 ++s;
168 negate = true;
169 }
170 if (*s == '\0') {
171 return false;
172 }
173
174 const T nMax = (std::numeric_limits<T>::max() >> N) / 10;
175 const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10);
176 T n = 0;
177 T frac = 0;
178 for (; *s; ++s) {
179 // Check if digit
180 if (*s < '0' || '9' < *s) {
181 // If it wasn't a digit, check if it is a '.' followed by something.
182 if (*s != '.' || s[1] == '\0') {
183 return false;
184 }
185 // Find the end, verify digits.
186 for (++s; *s; ++s) {
187 if (*s < '0' || '9' < *s) {
188 return false;
189 }
190 }
191 // Read back toward the '.'.
192 for (--s; *s != '.'; --s) {
193 T d = *s - '0';
194 frac = (frac + (d << N)) / 10; // This requires four bits overhead.
195 }
196 break;
197 }
198 T d = *s - '0';
199 // Check for overflow
200 if (n > nMax || (n == nMax && d > dMax)) {
201 return false;
202 }
203 n = (n * 10) + d;
204 }
205 if (negate) {
206 n = -n;
207 frac = -frac;
208 }
caryclark3127c992015-12-09 12:02:30 -0800209 *value = SkLeftShift(n, N) + frac;
bungeman41868fe2015-05-20 09:21:04 -0700210 return true;
211}
212
bungemanc5308542015-06-23 13:25:46 -0700213#endif /* SkFontMgr_android_parser_DEFINED */