djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 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 SKFONTCONFIGPARSER_ANDROID_H_ |
| 9 | #define SKFONTCONFIGPARSER_ANDROID_H_ |
| 10 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 11 | #include "SkFontMgr_android.h" |
djsollen@google.com | 9a70f34 | 2013-06-25 18:07:45 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 13 | #include "SkTDArray.h" |
| 14 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 15 | #include <climits> |
| 16 | #include <limits> |
| 17 | |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 18 | /** \class SkLanguage |
| 19 | |
| 20 | The SkLanguage class represents a human written language, and is used by |
| 21 | text draw operations to determine which glyph to draw when drawing |
| 22 | characters with variants (ie Han-derived characters). |
| 23 | */ |
| 24 | class SkLanguage { |
| 25 | public: |
| 26 | SkLanguage() { } |
| 27 | SkLanguage(const SkString& tag) : fTag(tag) { } |
| 28 | SkLanguage(const char* tag) : fTag(tag) { } |
| 29 | SkLanguage(const char* tag, size_t len) : fTag(tag, len) { } |
| 30 | SkLanguage(const SkLanguage& b) : fTag(b.fTag) { } |
| 31 | |
| 32 | /** Gets a BCP 47 language identifier for this SkLanguage. |
| 33 | @return a BCP 47 language identifier representing this language |
| 34 | */ |
| 35 | const SkString& getTag() const { return fTag; } |
| 36 | |
| 37 | /** Performs BCP 47 fallback to return an SkLanguage one step more general. |
| 38 | @return an SkLanguage one step more general |
| 39 | */ |
| 40 | SkLanguage getParent() const; |
| 41 | |
| 42 | bool operator==(const SkLanguage& b) const { |
| 43 | return fTag == b.fTag; |
| 44 | } |
| 45 | bool operator!=(const SkLanguage& b) const { |
| 46 | return fTag != b.fTag; |
| 47 | } |
| 48 | SkLanguage& operator=(const SkLanguage& b) { |
| 49 | fTag = b.fTag; |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | //! BCP 47 language identifier |
| 55 | SkString fTag; |
| 56 | }; |
| 57 | |
| 58 | enum FontVariants { |
| 59 | kDefault_FontVariant = 0x01, |
| 60 | kCompact_FontVariant = 0x02, |
| 61 | kElegant_FontVariant = 0x04, |
| 62 | kLast_FontVariant = kElegant_FontVariant, |
| 63 | }; |
| 64 | typedef uint32_t FontVariant; |
| 65 | |
bungeman | b5d0ce6 | 2015-02-13 11:49:07 -0800 | [diff] [blame] | 66 | // Must remain trivially movable (can be memmoved). |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 67 | struct FontFileInfo { |
bungeman | e85a754 | 2015-04-17 13:51:08 -0700 | [diff] [blame] | 68 | FontFileInfo() : fIndex(0), fWeight(0), fStyle(Style::kAuto) { } |
bungeman | 8d84c99 | 2014-07-24 08:05:09 -0700 | [diff] [blame] | 69 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 70 | SkString fFileName; |
| 71 | int fIndex; |
| 72 | int fWeight; |
bungeman | e85a754 | 2015-04-17 13:51:08 -0700 | [diff] [blame] | 73 | enum class Style { kAuto, kNormal, kItalic } fStyle; |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 74 | struct Axis { |
| 75 | Axis() : fTag(SkSetFourByteTag('\0','\0','\0','\0')), fValue(0) { } |
| 76 | SkFourByteTag fTag; |
| 77 | SkFixed fValue; |
| 78 | }; |
bungeman | a9f1db7 | 2015-05-27 13:10:02 -0700 | [diff] [blame] | 79 | SkTArray<Axis, true> fAxes; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 83 | * A font family provides one or more names for a collection of fonts, each of |
| 84 | * which has a different style (normal, italic) or weight (thin, light, bold, |
| 85 | * etc). |
| 86 | * Some fonts may occur in compact variants for use in the user interface. |
| 87 | * Android distinguishes "fallback" fonts to support non-ASCII character sets. |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 88 | */ |
| 89 | struct FontFamily { |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 90 | FontFamily(const SkString& basePath, bool isFallbackFont) |
djsollen | 3b62554 | 2014-08-14 06:29:02 -0700 | [diff] [blame] | 91 | : fVariant(kDefault_FontVariant) |
tomhudson | d3ddea2 | 2014-08-11 11:28:00 -0700 | [diff] [blame] | 92 | , fOrder(-1) |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 93 | , fIsFallbackFont(isFallbackFont) |
| 94 | , fBasePath(basePath) |
| 95 | { } |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 96 | |
bungeman | b5d0ce6 | 2015-02-13 11:49:07 -0800 | [diff] [blame] | 97 | SkTArray<SkString, true> fNames; |
| 98 | SkTArray<FontFileInfo, true> fFonts; |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 99 | SkLanguage fLanguage; |
| 100 | FontVariant fVariant; |
| 101 | int fOrder; // internal to SkFontConfigParser |
| 102 | bool fIsFallbackFont; |
| 103 | const SkString fBasePath; |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | namespace SkFontConfigParser { |
| 107 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 108 | /** Parses system font configuration files and appends result to fontFamilies. */ |
| 109 | void GetSystemFontFamilies(SkTDArray<FontFamily*>& fontFamilies); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 110 | |
bungeman | 7fa87cd | 2015-02-06 07:59:19 -0800 | [diff] [blame] | 111 | /** Parses font configuration files and appends result to fontFamilies. */ |
| 112 | void GetCustomFontFamilies(SkTDArray<FontFamily*>& fontFamilies, |
| 113 | const SkString& basePath, |
| 114 | const char* fontsXml, |
bungeman | c3c6943 | 2015-02-11 07:18:51 -0800 | [diff] [blame] | 115 | const char* fallbackFontsXml, |
| 116 | const char* langFallbackFontsDir = NULL); |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 117 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 118 | } // SkFontConfigParser namespace |
| 119 | |
bungeman | 41868fe | 2015-05-20 09:21:04 -0700 | [diff] [blame] | 120 | |
| 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 |
| 123 | * |
| 124 | * If the string cannot be parsed into 'value', returns false and does not change 'value'. |
| 125 | */ |
| 126 | template <typename T> static bool parse_non_negative_integer(const char* s, T* value) { |
| 127 | SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); |
| 128 | |
| 129 | if (*s == '\0') { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | const T nMax = std::numeric_limits<T>::max() / 10; |
| 134 | const T dMax = std::numeric_limits<T>::max() - (nMax * 10); |
| 135 | T n = 0; |
| 136 | for (; *s; ++s) { |
| 137 | // Check if digit |
| 138 | if (*s < '0' || '9' < *s) { |
| 139 | return false; |
| 140 | } |
| 141 | T d = *s - '0'; |
| 142 | // Check for overflow |
| 143 | if (n > nMax || (n == nMax && d > dMax)) { |
| 144 | return false; |
| 145 | } |
| 146 | n = (n * 10) + d; |
| 147 | } |
| 148 | *value = n; |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | /** Parses a null terminated string into a signed fixed point value with bias N. |
| 153 | * |
| 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:+))' |
| 156 | * |
| 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. |
| 160 | * |
| 161 | * If the string cannot be parsed into 'value', returns false and does not change 'value'. |
| 162 | */ |
| 163 | template <int N, typename T> static bool parse_fixed(const char* s, T* value) { |
| 164 | SK_COMPILE_ASSERT(std::numeric_limits<T>::is_integer, T_must_be_integer); |
| 165 | SK_COMPILE_ASSERT(std::numeric_limits<T>::is_signed, T_must_be_signed); |
| 166 | SK_COMPILE_ASSERT(sizeof(T) * CHAR_BIT - N >= 5, N_must_leave_four_bits_plus_sign); |
| 167 | |
| 168 | bool negate = false; |
| 169 | if (*s == '-') { |
| 170 | ++s; |
| 171 | negate = true; |
| 172 | } |
| 173 | if (*s == '\0') { |
| 174 | return false; |
| 175 | } |
| 176 | |
| 177 | const T nMax = (std::numeric_limits<T>::max() >> N) / 10; |
| 178 | const T dMax = (std::numeric_limits<T>::max() >> N) - (nMax * 10); |
| 179 | T n = 0; |
| 180 | T frac = 0; |
| 181 | for (; *s; ++s) { |
| 182 | // Check if digit |
| 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') { |
| 186 | return false; |
| 187 | } |
| 188 | // Find the end, verify digits. |
| 189 | for (++s; *s; ++s) { |
| 190 | if (*s < '0' || '9' < *s) { |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | // Read back toward the '.'. |
| 195 | for (--s; *s != '.'; --s) { |
| 196 | T d = *s - '0'; |
| 197 | frac = (frac + (d << N)) / 10; // This requires four bits overhead. |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | T d = *s - '0'; |
| 202 | // Check for overflow |
| 203 | if (n > nMax || (n == nMax && d > dMax)) { |
| 204 | return false; |
| 205 | } |
| 206 | n = (n * 10) + d; |
| 207 | } |
| 208 | if (negate) { |
| 209 | n = -n; |
| 210 | frac = -frac; |
| 211 | } |
| 212 | *value = (n << N) + frac; |
| 213 | return true; |
| 214 | } |
| 215 | |
djsollen@google.com | bfae9d3 | 2013-05-21 16:53:50 +0000 | [diff] [blame] | 216 | #endif /* SKFONTCONFIGPARSER_ANDROID_H_ */ |