SkFontMgr_android_parser.h 6.7 KB

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