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