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 cJSON* CheckConfigFile(const char* fname) const; 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::map<std::string, std::string>; 87 88 class FontConfigJson : public FontConfig { 89 public: 90 FontConfigJson() = default; 91 int ParseFile(const char* fname = nullptr); 92 int ParseFontFileMap(const char* fname = nullptr); 93 int ParseInstallConfig(const char* fontPath, std::vector<std::string>& fontPathList); GetFontConfigJsonInfo()94 std::shared_ptr<FontConfigJsonInfo> GetFontConfigJsonInfo() 95 { 96 return fontPtr; 97 } GetFontFileMap()98 std::shared_ptr<FontFileMap> GetFontFileMap() 99 { 100 return fontFileMap; 101 } 102 // for test 103 void Dump() const override; 104 105 private: 106 int ParseConfigList(const char* fname); 107 int ParseConfigListPath(const char* fname); 108 int ParseFontMap(const cJSON* root, const char* key); 109 int ParseDir(const cJSON* root); 110 int ParseFonts(const cJSON* root); 111 void AnalyseFont(const cJSON* root); 112 int ParseInstallFont(const cJSON* root, std::vector<std::string>& fontPathList); 113 void DumpFontDir() const; 114 void DumpGeneric() const; 115 void DumpForbak() const; 116 void DumpAlias(const AliasSet &aliasSet) const; 117 void DumpAjdust(const AdjustSet &adjustSet) const; 118 void DumpFontFileMap() const; 119 struct FontJson { 120 int type = 0; 121 int weight = 0; 122 std::string alias; 123 std::string family; 124 std::string lang; 125 }; 126 void EmplaceFontJson(const FontJson &fontJson); 127 128 std::shared_ptr<FontConfigJsonInfo> fontPtr = nullptr; 129 std::shared_ptr<FontFileMap> fontFileMap = nullptr; 130 // because of some memory bugs, this place use shared ptr 131 std::shared_ptr<std::unordered_map<std::string, size_t>> indexMap = nullptr; 132 }; 133 } // namespace TextEngine 134 } // namespace Rosen 135 } // namespace OHOS 136 #endif /* FONT_CONFIG_H */ 137