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