Flutter Engine
The Flutter Engine
SkFontMgr_android_parser.h
Go to the documentation of this file.
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
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*/
29public:
31 SkLanguage(const SkString& tag) : fTag(tag) { }
32 SkLanguage(const char* tag) : fTag(tag) { }
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 */
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
54private:
55 //! BCP 47 language identifier
56 SkString fTag;
57};
58
64};
65typedef uint32_t FontVariant;
66
67// Must remain trivially movable (can be memmoved).
69 FontFileInfo() : fIndex(0), fWeight(0), fStyle(Style::kAuto) { }
70
72 int fIndex;
74 enum class Style { kAuto, kNormal, kItalic } fStyle;
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 */
86struct FontFamily {
87 FontFamily(const SkString& basePath, bool isFallbackFont)
89 , fOrder(-1)
90 , fIsFallbackFont(isFallbackFont)
91 , fBasePath(basePath)
92 { }
93
99 int fOrder; // internal to the parser, not useful to users.
103};
104
106
107/** Parses system font configuration files and appends result to fontFamilies. */
109
110/** Parses font configuration files and appends result to 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 */
125template <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 */
162template <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 */
@ kItalic
bool parse_non_negative_integer(const char *s, T *value)
bool parse_fixed(const char *s, T *value)
uint32_t FontVariant
@ kDefault_FontVariant
@ kCompact_FontVariant
@ kLast_FontVariant
@ kElegant_FontVariant
static constexpr int32_t SkLeftShift(int32_t value, int32_t shift)
Definition: SkMath.h:37
static bool is_integer(SkScalar x)
#define N
Definition: beziers.cpp:19
SkLanguage getParent() const
SkLanguage & operator=(const SkLanguage &)=default
const SkString & getTag() const
SkLanguage(const char *tag)
bool operator!=(const SkLanguage &b) const
SkLanguage(const char *tag, size_t len)
bool operator==(const SkLanguage &b) const
SkLanguage(const SkString &tag)
SkLanguage(const SkLanguage &)=default
@ kNormal
Default priority level.
Definition: embedder.h:262
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
static bool b
struct MyStruct s
uint8_t value
static float max(float r, float g, float b)
Definition: hsl.cpp:49
void GetCustomFontFamilies(SkTDArray< FontFamily * > &fontFamilies, const SkString &basePath, const char *fontsXml, const char *fallbackFontsXml, const char *langFallbackFontsDir=nullptr)
void GetSystemFontFamilies(SkTDArray< FontFamily * > &fontFamilies)
static bool is_signed(const Type &type)
#define T
Definition: precompiler.cc:65
skia_private::TArray< SkString, true > fNames
FontFamily(const SkString &basePath, bool isFallbackFont)
const SkString fBasePath
skia_private::THashMap< SkString, std::unique_ptr< FontFamily > > fallbackFamilies
skia_private::TArray< SkLanguage, true > fLanguages
skia_private::TArray< FontFileInfo, true > fFonts
enum FontFileInfo::Style fStyle
skia_private::TArray< SkFontArguments::VariationPosition::Coordinate, true > fVariationDesignPosition