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