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