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 <string>
20 #include <vector>
21 #include <include/core/SkString.h>
22 #include <map>
23 
24 struct cJSON;
25 namespace OHOS {
26 namespace Rosen {
27 namespace TextEngine {
28 class FontConfig {
29 public:
30     explicit FontConfig(const char* fname = nullptr);
31     virtual ~FontConfig() = default;
32     virtual void Dump() const;
33     std::vector<std::string> GetFontSet() const;
34 
35 protected:
36     int ParseConfig(const char* fname);
37     int ParseFont(const cJSON* root);
38     cJSON* CheckConfigFile(const char* fname) const;
39     static char* GetFileData(const char* fname, int& size);
40 
41 private:
42     std::vector<std::string> fontSet_;
43     std::string rootPath_;
44 };
45 
46 typedef struct AdjustInfo {
47     int origValue;
48     int newValue;
49 } AdjustInfo;
50 using AdjustSet = std::vector<AdjustInfo>;
51 
52 typedef struct AliasInfo {
53     std::string familyName;
54     int weight;
55 } AliasInfo;
56 using AliasSet = std::vector<AliasInfo>;
57 
58 typedef struct FallbackInfo {
59     std::string familyName;
60     std::string font;
61 } FallbackInfo;
62 using FallbackInfoSet = std::vector<FallbackInfo>;
63 
64 typedef struct FallbackGroup {
65     std::string groupName;
66     FallbackInfoSet fallbackInfoSet;
67 } FallbackGroup;
68 using FallbackGroupSet = std::vector<FallbackGroup>;
69 
70 typedef struct FontGenericInfo {
71     std::string familyName;
72     AliasSet aliasSet;
73     AdjustSet adjustSet;
74 } FontGenericInfo;
75 using GenericSet = std::vector<FontGenericInfo>;
76 
77 typedef struct FontConfigJsonInfo {
78     std::vector<std::string> fontDirSet;
79     GenericSet genericSet;
80     FallbackGroupSet fallbackGroupSet;
81 } FontConfigJsonInfo;
82 
83 using FontFileMap = std::map<std::string, std::string>;
84 
85 class FontConfigJson : public FontConfig {
86 public:
87     FontConfigJson() = default;
88     int ParseFile(const char* fname = nullptr);
89     int ParseFontFileMap(const char* fname = nullptr);
90     int ParseInstallConfig(const char* fontPath, std::vector<std::string>& fontPathList);
GetFontConfigJsonInfo()91     std::shared_ptr<FontConfigJsonInfo> GetFontConfigJsonInfo()
92     {
93         return fontPtr;
94     }
GetFontFileMap()95     std::shared_ptr<FontFileMap> GetFontFileMap()
96     {
97         return fontFileMap;
98     }
99     // for test
100     void Dump() const override;
101 
102 private:
103     int ParseConfigList(const char* fname);
104     int ParseConfigListPath(const char* fname);
105     int ParseGeneric(const cJSON* root, const char* key);
106     int ParseAlias(const cJSON* root, FontGenericInfo &genericInfo);
107     int ParseAdjust(const cJSON* root, FontGenericInfo &genericInfo);
108     int ParseFallback(const cJSON* root, const char* key);
109     int ParseFontMap(const cJSON* root, const char* key);
110     int ParseDir(const cJSON* root);
111     void AnalyseFontDir(const cJSON* root);
112     int ParseAdjustArr(const cJSON* arr, FontGenericInfo &genericInfo);
113     int ParseAliasArr(const cJSON* arr, FontGenericInfo &genericInfo);
114     int ParseInstallFont(const cJSON* root, std::vector<std::string>& fontPathList);
115     void DumpFontDir() const;
116     void DumpGeneric() const;
117     void DumpForbak() const;
118     void DumpAlias(const AliasSet &aliasSet) const;
119     void DumpAjdust(const AdjustSet &adjustSet) const;
120     void DumpFontFileMap() const;
121 
122     std::shared_ptr<FontConfigJsonInfo> fontPtr = nullptr;
123     std::shared_ptr<FontFileMap> fontFileMap = nullptr;
124 };
125 } // namespace TextEngine
126 } // namespace Rosen
127 } // namespace OHOS
128 #endif /* FONT_CONFIG_H */
129