1 // Copyright (c) 2023 Huawei Device Co., Ltd. All rights reserved 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef FONTCONFIG_OHOS_H 6 #define FONTCONFIG_OHOS_H 7 8 #include <json/json.h> 9 #include <vector> 10 #include <mutex> 11 12 #include "SkFontDescriptor.h" 13 #include "SkFontHost_FreeType_common.h" 14 #include "SkFontStyle.h" 15 #include "SkStream.h" 16 #include "SkString.h" 17 #include "SkTypes.h" 18 19 #include "FontInfo_ohos.h" 20 #include "SkTypeface_ohos.h" 21 22 #include "HmSymbolConfig_ohos.h" 23 24 #ifdef ENABLE_DEBUG 25 26 #define LOGE(fmt, args...) \ 27 printf("E %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) 28 #define LOGI(fmt, args...) \ 29 printf("I %s:%d - " fmt, __FUNCTION__, __LINE__, ##args) 30 #define LOGW(fmt, args...) \ 31 printf("W %s:%d %s - " fmt, basename(__FILE__), __LINE__, __FUNCTION__, ##args) 32 33 #else 34 35 #define LOGE SkDEBUGF 36 #define LOGI SkDEBUGF 37 #define LOGW SkDEBUGF 38 39 #endif 40 41 struct FontInfo; 42 struct FallbackInfo; 43 struct GenericFamily; 44 struct FallbackSetPos; 45 46 using TypefaceSet = std::vector<sk_sp<SkTypeface_OHOS>>; 47 using GenericFamilySet = std::vector<std::unique_ptr<GenericFamily>>; 48 using FallbackSet = std::vector<std::unique_ptr<FallbackInfo>>; 49 using FallbackForMap = SkTHashMap<SkString, FallbackSetPos>; 50 using NamesMap = SkTHashMap<SkString, int>; 51 using Coordinate = SkFontArguments::VariationPosition::Coordinate; 52 using AxisDefinitions = SkTypeface_FreeType::Scanner::AxisDefinitions; 53 54 /*! 55 * Error code definition 56 */ 57 namespace ErrorCode { 58 59 enum { 60 NO_ERROR = 0, // no error 61 ERROR_CONFIG_NOT_FOUND, // the configuration document is not found 62 ERROR_CONFIG_FORMAT_NOT_SUPPORTED, // the formation of configuration is not supported 63 ERROR_CONFIG_MISSING_TAG, // missing tag in the configuration 64 ERROR_CONFIG_INVALID_VALUE_TYPE, // invalid value type in the configuration 65 ERROR_FONT_NOT_EXIST, // the font file is not exist 66 ERROR_FONT_INVALID_STREAM, // the stream is not recognized 67 ERROR_FONT_NO_STREAM, // no stream in the font data 68 ERROR_FAMILY_NOT_FOUND, // the family name is not found in the system 69 ERROR_NO_AVAILABLE_FAMILY, // no available family in the system 70 ERROR_DIR_NOT_FOUND, // the directory is not exist 71 72 ERROR_TYPE_COUNT, 73 }; 74 75 } /* namespace ErrorCode */ 76 77 /*! 78 * \brief To manage the related information of a 'fallbackFor' family name 79 */ 80 struct FallbackSetPos { 81 unsigned int index; // the index of the first font style set in the fallback set for a specified family name 82 unsigned int count; // the count of font style sets for a specified family name 83 }; 84 85 /*! 86 * \brief To manage the information for a generic family item 87 */ 88 struct GenericFamily { 89 SkString familyName; // the specified family name of the font style set 90 std::shared_ptr<TypefaceSet> typefaceSet; // the typeface set of the font style set 91 virtual ~GenericFamily() = default; 92 }; 93 94 /*! 95 * \brief To manage the information for a fallback family item 96 */ 97 struct FallbackInfo : GenericFamily { 98 SkString langs; // the language for which the font style set is 99 }; 100 101 /*! 102 * \brief To parse the font configuration document and manage the system fonts 103 */ 104 class FontConfig_OHOS { 105 public: 106 explicit FontConfig_OHOS(const SkTypeface_FreeType::Scanner& fontScanner, 107 const char* fname = nullptr); 108 virtual ~FontConfig_OHOS() = default; 109 const FallbackForMap& getFallbackForMap() const; 110 const FallbackSet& getFallbackSet() const; 111 int getFamilyCount() const; 112 int getDefaultFamily(SkString* familyName) const; 113 int getFamilyName(int index, SkString* familyName) const; 114 int getTypefaceCount(int styleIndex, bool isFallback = false) const; 115 int getStyleIndex(const char* familyName, bool& isFallback) const; 116 117 SkTypeface_OHOS* getTypeface(int styleIndex, int index, bool isFallback = false) const; 118 SkTypeface_OHOS* getTypeface(int styleIndex, const SkFontStyle& style, 119 bool isFallback = false) const; 120 121 #if ENABLE_DEBUG 122 void dumpFont(const FontInfo& font) const; 123 void dumpGeneric() const; 124 void dumpFallback() const; 125 #endif 126 bool hasError(int err, const SkString& text) const; 127 int getErrorCount() const; 128 129 static sk_sp<SkTypeface_OHOS> matchFontStyle(const TypefaceSet& typefaceSet, const SkFontStyle& pattern); 130 131 static const char* errToString(int err); 132 private: 133 const uint32_t defaultColorHexLen = 9; 134 const uint32_t defaultColorStrLen = 7; 135 const uint32_t hexFlag = 16; 136 const uint32_t twoBytesBitsLen = 16; 137 const uint32_t oneByteBitsLen = 8; 138 139 struct AliasInfo; 140 struct AdjustInfo; 141 struct VariationInfo; 142 struct TtcIndexInfo; 143 using AliasMap = SkTHashMap<SkString, std::vector<AliasInfo>>; 144 using AjdustMap = SkTHashMap<SkString, std::vector<AdjustInfo>>; 145 using VariationMap = SkTHashMap<SkString, std::vector<VariationInfo>>; 146 using TtcIndexMap = SkTHashMap<SkString, TtcIndexInfo>; 147 148 /*! 149 * \brief To manage the adjust information 150 */ 151 struct AdjustInfo { 152 int origValue; // the real value of the font weight 153 int newValue; // the specified value of weight for a font 154 }; 155 156 /*! 157 * \brief To manage the alias information of 158 */ 159 struct AliasInfo { 160 int pos; // the index of a font style set in generic family list. 161 int weight; // the weight of the font style set. 0 means no specified weight 162 }; 163 164 /*! 165 * \brief To manage the variation information 166 */ 167 struct VariationInfo { VariationInfoVariationInfo168 VariationInfo() : weight(-1), width(-1), slant(-1){} 169 std::vector<Coordinate> axis; // the axis set such as 'wght', 'wdth' and 'slnt'. 170 int weight; // the value of mapping weight 171 int width; // the value of mapping width 172 int slant; // the value of mapping slant 173 }; 174 175 /*! 176 * \brief To manage the 'index' information for ttc fonts 177 */ 178 struct TtcIndexInfo { 179 SkString familyName; // the family name of the first typeface in a ttc font 180 int ttcIndex; // the index of a typeface in a ttc font 181 }; 182 183 /*! 184 * \brief To manage the information of errors happened 185 */ 186 struct ErrorInfo { ErrorInfoErrorInfo187 ErrorInfo(int err, const char* text) : err(err), text(SkString(text)){} ErrorInfoErrorInfo188 ErrorInfo(int err, SkString& text) : err(err), text(std::move(text)){} 189 int err; // error id 190 SkString text; // the part with error 191 }; 192 193 std::vector<SkString> fontDirSet; // the directories where the fonts are 194 195 FallbackForMap fallbackForMap; // a hash table to save the fallbackFor pairs 196 GenericFamilySet genericFamilySet; // the font style set list of generic family 197 FallbackSet fallbackSet; // the font style set list of fallback family 198 199 NamesMap genericNames; // a map to store the index of a family for generic family 200 NamesMap fallbackNames; // a map to store the index of a family for fallback family 201 202 std::vector<ErrorInfo> errSet; // the errors happened 203 AliasMap aliasMap; // to save alias information temporarily 204 AjdustMap adjustMap; // to save adjust information temporarily 205 VariationMap variationMap; // to save variation information temporarily 206 TtcIndexMap ttcIndexMap; // to save 'index' information temporarily 207 208 mutable std::mutex fontMutex; 209 210 int parseConfig(const char* fname); 211 int checkConfigFile(const char* fname, Json::Value& root); 212 int parseFontDir(const char* fname, const Json::Value& root); 213 int parseGeneric(const Json::Value& root); 214 int parseFallback(const Json::Value& root); 215 int parseFallbackItem(const Json::Value& root); 216 int parseAlias(const Json::Value& root, std::vector<AliasInfo>& aliasSet); 217 int parseAdjust(const Json::Value& root, std::vector<AdjustInfo>& adjustSet); 218 int parseVariation(const Json::Value& root, std::vector<VariationInfo>& variationSet); 219 int parseTtcIndex(const Json::Value& root, const SkString& familyName); 220 void getAxisValues(const AxisDefinitions& axisDefinitions, 221 const VariationInfo& variation, FontInfo& font) const; 222 bool insertTtcFont(int count, FontInfo& font); 223 bool insertVariableFont(const AxisDefinitions& axisDefinitions, FontInfo& font); 224 TypefaceSet* getTypefaceSet(const SkString& familyName, SkString& specifiedName) const; 225 226 int loadFont(const SkTypeface_FreeType::Scanner& scanner, const char* fname); 227 int scanFonts(const SkTypeface_FreeType::Scanner& fontScanner); 228 void resetGenericValue(); 229 void buildSubTypefaceSet(const std::shared_ptr<TypefaceSet>& typefaceSet, 230 std::shared_ptr<TypefaceSet>& subSet, const SkString& familyName, int weight); 231 void resetFallbackValue(); 232 int logErrInfo(int err, const char* key, Json::ValueType expected = Json::nullValue, 233 Json::ValueType actual = Json::nullValue); 234 static void sortTypefaceSet(std::shared_ptr<TypefaceSet>& typefaceSet); 235 static uint32_t getFontStyleDifference(const SkFontStyle& style1, const SkFontStyle& style2); 236 static char* getFileData(const char* fname, int& size); 237 FontConfig_OHOS(const FontConfig_OHOS&) = delete; 238 FontConfig_OHOS& operator = (const FontConfig_OHOS&) = delete; 239 FontConfig_OHOS(FontConfig_OHOS&&) = delete; 240 FontConfig_OHOS& operator = (FontConfig_OHOS&&) = delete; 241 int checkProductFile(const char* fname); 242 bool judgeFileExist(); 243 }; 244 245 #endif /* FONTCONFIG_OHOS_H */ 246