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 <mutex> 10 #include <vector> 11 12 #include "FontInfo_ohos.h" 13 #include "HmSymbolConfig_ohos.h" 14 #include "SkFontDescriptor.h" 15 #include "SkFontHost_FreeType_common.h" 16 #include "SkFontStyle.h" 17 #include "SkStream.h" 18 #include "SkString.h" 19 #include "SkTypeface_ohos.h" 20 #include "SkTypes.h" 21 22 /*! 23 * Error code definition 24 */ 25 namespace ErrorCode { 26 27 enum { 28 NO_ERROR = 0, // no error 29 ERROR_CONFIG_NOT_FOUND, // the configuration document is not found 30 ERROR_CONFIG_FORMAT_NOT_SUPPORTED, // the formation of configuration is not supported 31 ERROR_CONFIG_MISSING_TAG, // missing tag in the configuration 32 ERROR_CONFIG_INVALID_VALUE_TYPE, // invalid value type in the configuration 33 ERROR_FONT_NOT_EXIST, // the font file is not exist 34 ERROR_FONT_INVALID_STREAM, // the stream is not recognized 35 ERROR_FONT_NO_STREAM, // no stream in the font data 36 ERROR_FAMILY_NOT_FOUND, // the family name is not found in the system 37 ERROR_NO_AVAILABLE_FAMILY, // no available family in the system 38 ERROR_DIR_NOT_FOUND, // the directory is not exist 39 ERROR_CONFIG_FUN_NOT_DEFINED, // the symbol load config func is not register 40 41 ERROR_TYPE_COUNT, 42 }; 43 } /* namespace ErrorCode */ 44 45 constexpr size_t RANGE_SIZE = 11; 46 constexpr size_t UNICODE_RANGE_SIZE = 332; 47 using UnicodeRange = std::array<uint32_t, RANGE_SIZE>; 48 49 /*! 50 * \brief To parse the font configuration document and manage the system fonts 51 */ 52 class FontConfig_OHOS { 53 public: 54 enum FontType : uint32_t { Generic = 0, Fallback, NumOfFontType }; 55 56 // to map the json document's font object 57 struct FontJson { 58 uint32_t type = 0; 59 uint32_t slant = 0; 60 uint32_t index = 0; 61 uint32_t weight = 400; 62 std::string alias; 63 std::string family; 64 std::string lang; 65 std::string file; 66 UnicodeRange range { UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, 67 UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX }; 68 }; 69 70 struct Font { 71 FontType type = FontType::Generic; 72 // slant: 0 - normal, 1 - italic, 2 - oblique 73 uint32_t slant = 0; 74 // the ttc font index, only valid for ttc font 75 uint32_t index = 0; 76 // only valid for the font with alias 77 uint32_t weight = 0; 78 std::string alias; 79 // essential for the every font 80 std::string family; 81 // only valid for the fallback font 82 std::string lang; 83 // all the typefaces of this font 84 std::vector<sk_sp<SkTypeface_OHOS>> typefaces; 85 86 // may be redundant move 87 explicit Font(FontJson& info); 88 }; 89 90 explicit FontConfig_OHOS(const SkTypeface_FreeType::Scanner& fontScanner, const char* fname = nullptr); 91 virtual ~FontConfig_OHOS() = default; 92 SkTypeface* matchFallback(SkUnichar character, const SkFontStyle& style) const; 93 SkTypeface* matchFallback(size_t index, SkUnichar character, const SkFontStyle& style) const; 94 std::vector<size_t> matchFallbackByBCP47(std::function<int(const std::string&)>) const; 95 int getFamilyCount() const; 96 int getDefaultFamily(SkString& familyName) const; 97 int getFamilyName(size_t index, SkString& familyName) const; 98 size_t getTypefaceCount(size_t styleIndex, bool isFallback = false) const; 99 bool getStyleIndex(const char* familyName, bool& isFallback, size_t& index) const; 100 sk_sp<SkTypeface_OHOS> getFallbackTypeface(const SkString& familyName, const SkFontStyle& style) const; forAll(std::function<void (Font &)> func)101 void forAll(std::function<void(Font&)> func) 102 { 103 fFontCollection.forAll(func); 104 } 105 106 sk_sp<SkTypeface_OHOS> getTypefaceSP(size_t styleIndex, size_t index, bool isFallback = false) const; 107 SkTypeface_OHOS* getTypeface(size_t styleIndex, size_t index, bool isFallback = false) const; 108 SkTypeface_OHOS* getTypeface(size_t styleIndex, const SkFontStyle& style, bool isFallback = false) const; 109 110 static sk_sp<SkTypeface_OHOS> matchFontStyle( 111 const std::vector<sk_sp<SkTypeface_OHOS>>& typefaceSet, const SkFontStyle& pattern); 112 113 private: 114 struct FontCollection { 115 std::vector<Font> fFallback; 116 std::vector<Font> fGeneric; 117 std::unordered_map<std::string, std::pair<size_t, FontType>> fIndexMap; 118 std::array<std::vector<size_t>, UNICODE_RANGE_SIZE> fRangeToIndex; 119 120 void emplaceFont(FontJson&& fj, sk_sp<SkTypeface_OHOS>&& typeface); 121 bool getIndexByFamilyName(const std::string& family, std::pair<size_t, FontType>& res) const; 122 const std::vector<Font>& getSet(bool isFallback) const; 123 void forAll(std::function<void(Font&)> func); 124 } fFontCollection; 125 126 std::vector<std::string> fFontDir; // the directories where the fonts are 127 const SkTypeface_FreeType::Scanner& fFontScanner; 128 mutable std::mutex fFontMutex; 129 static const std::map<std::pair<uint32_t, uint32_t>, int16_t> fRangeMap; 130 131 int parseConfig(const char* fname); 132 int checkConfigFile(const char* fname, Json::Value& root); 133 int parseFontDir(const char* fname, const Json::Value& root); 134 int parseFonts(const Json::Value& root); 135 136 int loadFont(const char* fname, FontJson& font, sk_sp<SkTypeface_OHOS>& typeface); 137 void loadHMSymbol(); 138 static bool containRange(const UnicodeRange& range, size_t index); 139 static void sortTypefaceSet(std::vector<sk_sp<SkTypeface_OHOS>>& typefaceSet); 140 static uint32_t getFontStyleDifference(const SkFontStyle& style1, const SkFontStyle& style2); 141 static int16_t charRangeIndex(SkUnichar unicode); 142 FontConfig_OHOS(const FontConfig_OHOS&) = delete; 143 FontConfig_OHOS& operator=(const FontConfig_OHOS&) = delete; 144 FontConfig_OHOS(FontConfig_OHOS&&) = delete; 145 FontConfig_OHOS& operator=(FontConfig_OHOS&&) = delete; 146 int checkProductFile(const char* fname); 147 bool judgeFileExist(); 148 }; 149 150 #endif /* FONTCONFIG_OHOS_H */ 151