1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FONT_CONFIG_H 17 #define FONT_CONFIG_H 18 19 #include <algorithm> 20 #include <cstring> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 27 struct cJSON; 28 namespace OHOS { 29 namespace Rosen { 30 namespace TextEngine { 31 class FontConfig { 32 public: 33 explicit FontConfig(const char* fname = nullptr); 34 virtual ~FontConfig() = default; 35 virtual void Dump() const; 36 std::vector<std::string> GetFontSet() const; 37 38 protected: 39 int ParseConfig(const char* fname); 40 int ParseFont(const cJSON* root); 41 static cJSON* CheckConfigFile(const char* fname); 42 static char* GetFileData(const char* fname, int& size); 43 44 private: 45 std::vector<std::string> fontSet_; 46 std::string rootPath_; 47 }; 48 49 typedef struct AdjustInfo { 50 int origValue; 51 int newValue; 52 } AdjustInfo; 53 using AdjustSet = std::vector<AdjustInfo>; 54 55 typedef struct AliasInfo { 56 std::string familyName; 57 int weight; 58 } AliasInfo; 59 using AliasSet = std::vector<AliasInfo>; 60 61 typedef struct FallbackInfo { 62 std::string familyName; 63 std::string font; 64 } FallbackInfo; 65 using FallbackInfoSet = std::vector<FallbackInfo>; 66 67 typedef struct FallbackGroup { 68 std::string groupName; 69 FallbackInfoSet fallbackInfoSet; 70 } FallbackGroup; 71 using FallbackGroupSet = std::vector<FallbackGroup>; 72 73 typedef struct FontGenericInfo { 74 std::string familyName; 75 AliasSet aliasSet; 76 AdjustSet adjustSet; 77 } FontGenericInfo; 78 using GenericSet = std::vector<FontGenericInfo>; 79 80 typedef struct FontConfigJsonInfo { 81 std::vector<std::string> fontDirSet; 82 GenericSet genericSet; 83 FallbackGroupSet fallbackGroupSet; 84 } FontConfigJsonInfo; 85 86 using FontFileMap = std::unordered_map<std::string, std::string>; 87 using FullNameToPath = std::unordered_map<std::string, std::pair<size_t, std::string>>; 88 89 class FontConfigJson : public FontConfig { 90 public: 91 FontConfigJson() = default; 92 int ParseFile(const char* fname = nullptr); 93 int ParseFontFileMap(const char* fname = nullptr); 94 template<typename T> 95 static int ParseInstallConfig(const char* fontPath, T& fontPathList); GetFontConfigJsonInfo()96 std::shared_ptr<FontConfigJsonInfo> GetFontConfigJsonInfo() 97 { 98 return fontPtr; 99 } GetFontFileMap()100 std::shared_ptr<FontFileMap> GetFontFileMap() 101 { 102 return fontFileMap; 103 } 104 // for test 105 void Dump() const override; 106 107 private: 108 int ParseConfigList(const char* fname); 109 int ParseConfigListPath(const char* fname); 110 int ParseFontMap(const cJSON* root, const char* key); 111 int ParseDir(const cJSON* root); 112 int ParseFonts(const cJSON* root); 113 void AnalyseFont(const cJSON* root); 114 static int ParseInstallFont(const cJSON* root, std::vector<std::string>& fontPathList); 115 static int ParseInstallFont(const cJSON* root, FullNameToPath& fontPathList); 116 static void ParseFullName(const cJSON* root, std::vector<std::string>& fullNameList); 117 void DumpFontDir() const; 118 void DumpGeneric() const; 119 void DumpForbak() const; 120 void DumpAlias(const AliasSet &aliasSet) const; 121 void DumpAjdust(const AdjustSet &adjustSet) const; 122 void DumpFontFileMap() const; 123 struct FontJson { 124 int type = 0; 125 int weight = 0; 126 std::string alias; 127 std::string family; 128 std::string lang; 129 }; 130 void EmplaceFontJson(const FontJson &fontJson); 131 132 std::shared_ptr<FontConfigJsonInfo> fontPtr = nullptr; 133 std::shared_ptr<FontFileMap> fontFileMap = nullptr; 134 // because of some memory bugs, this place use shared ptr 135 std::shared_ptr<std::unordered_map<std::string, size_t>> indexMap = nullptr; 136 }; 137 } // namespace TextEngine 138 } // namespace Rosen 139 } // namespace OHOS 140 #endif /* FONT_CONFIG_H */ 141