1 /*
2 * Copyright (c) 2024 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 #include "font_config.h"
17 #include "font_hilog.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace Global {
22 namespace FontManager {
23 static const char *FONT_PATH = "fontfullpath";
24 static const char *FONT_FULL_NAME = "fullname";
25
InsertFontRecord(const std::string & fontPath,const std::vector<std::string> & fullNames)26 bool FontConfig::InsertFontRecord(const std::string &fontPath, const std::vector<std::string> &fullNames)
27 {
28 cJSON *jsonValue = cJSON_Parse(CheckConfigFile(ConfigPath_).c_str());
29 if (jsonValue == nullptr) {
30 FONT_LOGE("heck config file failed");
31 return false;
32 }
33 cJSON *fontList = cJSON_GetObjectItem(jsonValue, "fontlist");
34 if (fontList == nullptr) {
35 FONT_LOGE("Font Config file's format is incorrect");
36 cJSON_Delete(jsonValue);
37 return false;
38 }
39 cJSON *insertValue = ConstructCJSON(fontPath, fullNames);
40 if (insertValue == nullptr) {
41 cJSON_Delete(jsonValue);
42 return false;
43 }
44 cJSON_AddItemToArray(fontList, insertValue);
45 char *fileData = cJSON_Print(jsonValue);
46 cJSON_Delete(jsonValue);
47 return WriteToFile(fileData);
48 }
49
DeleteFontRecord(const std::string & fontPath)50 bool FontConfig::DeleteFontRecord(const std::string &fontPath)
51 {
52 std::lock_guard<std::mutex> lock(fontsMapLock_);
53 if (fontsMap_.size() == 0) {
54 fontsMap_ = GetFontsMap(ConfigPath_);
55 }
56 if (fontsMap_.find(fontPath) == fontsMap_.end()) {
57 return false;
58 }
59 fontsMap_.erase(fontPath);
60
61 cJSON *rootData = cJSON_CreateArray();
62 if (rootData == nullptr) {
63 return false;
64 }
65 for (const auto &info : fontsMap_) {
66 cJSON *item = cJSON_CreateObject();
67 if (item == nullptr) {
68 continue;
69 }
70 cJSON_AddItemToObject(item, FONT_PATH, cJSON_CreateString(info.first.c_str()));
71 cJSON *arrData = cJSON_CreateArray();
72 if (arrData == nullptr) {
73 cJSON_Delete(item);
74 continue;
75 }
76 for (const auto &name : info.second) {
77 cJSON_AddItemToArray(arrData, cJSON_CreateString(name.c_str()));
78 }
79 cJSON_AddItemToObject(item, FONT_FULL_NAME, arrData);
80 cJSON_AddItemToArray(rootData, item);
81 }
82 cJSON *cJsonData = cJSON_CreateObject();
83 if (cJsonData == nullptr) {
84 cJSON_Delete(rootData);
85 return false;
86 }
87 cJSON_AddItemToObject(cJsonData, "fontlist", rootData);
88 char *fileData = cJSON_Print(cJsonData);
89 cJSON_Delete(cJsonData);
90 return WriteToFile(fileData);
91 }
92
GetInstalledFontsNum()93 int FontConfig::GetInstalledFontsNum()
94 {
95 std::lock_guard<std::mutex> lock(fontsMapLock_);
96 if (fontsMap_.size() == 0) {
97 fontsMap_ = GetFontsMap(ConfigPath_);
98 }
99 return fontsMap_.size();
100 }
101
GetFontFileByName(const std::string & fullName)102 std::string FontConfig::GetFontFileByName(const std::string &fullName)
103 {
104 std::lock_guard<std::mutex> lock(fontsMapLock_);
105 if (fontsMap_.size() == 0) {
106 fontsMap_ = GetFontsMap(ConfigPath_);
107 }
108 for (const auto &font : fontsMap_) {
109 for (const auto &name : font.second) {
110 if (name == fullName) {
111 return font.first;
112 }
113 }
114 }
115 return "";
116 }
117
GetFileData(const std::string & filePath,long & size)118 char *FontConfig::GetFileData(const std::string &filePath, long &size)
119 {
120 std::lock_guard<std::mutex> lock(configLock_);
121 FILE *fp = std::fopen(filePath.c_str(), "r");
122 if (fp == nullptr) {
123 FONT_LOGE("failed open the filePath = %{public}s", filePath.c_str());
124 return nullptr;
125 }
126 std::fseek(fp, 0, SEEK_END);
127 long fileSize = ftell(fp);
128 if (fileSize < 0) {
129 FONT_LOGE("failed to get the file size for filePath = %{public}s", filePath.c_str());
130 (void)fclose(fp);
131 return nullptr;
132 }
133 size = fileSize + 1;
134
135 if (size <= 0) {
136 FONT_LOGE("invalid file size = %ld for filePath = %{public}s", size, filePath.c_str());
137 (void)fclose(fp);
138 return nullptr;
139 }
140 char *data = static_cast<char *>(malloc(size));
141 if (data == nullptr) {
142 FONT_LOGE("failed malloc in GetFileData for filePath = %{public}s", filePath.c_str());
143 (void)fclose(fp);
144 return nullptr;
145 }
146 memset_s(data, size, 0, size);
147 std::fseek(fp, 0, SEEK_SET);
148 size_t bytesRead = std::fread(data, 1, fileSize, fp);
149 if (bytesRead != static_cast<size_t>(fileSize)) {
150 FONT_LOGE("failed to read the full file content for filePath = %{public}s", filePath.c_str());
151 free(data);
152 (void)fclose(fp);
153 return nullptr;
154 }
155
156 (void)fclose(fp);
157 return data;
158 }
159
WriteToFile(char * fileData)160 bool FontConfig::WriteToFile(char *fileData)
161 {
162 std::lock_guard<std::mutex> lock(configLock_);
163 if (fileData == nullptr) {
164 return false;
165 }
166 FILE *fp = fopen(ConfigPath_.c_str(), "w");
167 if (fp == nullptr) {
168 FONT_LOGE("failed open the filePath = %{public}s", ConfigPath_.c_str());
169 free(fileData);
170 fileData = nullptr;
171 return false;
172 }
173
174 bool ret = true;
175 if (std::fwrite(fileData, sizeof(char), strlen(fileData), fp) != strlen(fileData)) {
176 FONT_LOGE("failed to write file");
177 ret = false;
178 }
179 if (fileData != nullptr) {
180 free(fileData);
181 fileData = nullptr;
182 }
183 (void)fclose(fp);
184 fp = nullptr;
185 return ret;
186 }
187
CheckConfigFile(const std::string & fontPath)188 std::string FontConfig::CheckConfigFile(const std::string &fontPath)
189 {
190 long size = 0;
191 char *data = GetFileData(fontPath, size);
192 if (data == nullptr) {
193 FONT_LOGE("data is NULL");
194 return "";
195 }
196 std::string pramsString;
197 pramsString.assign(data, size);
198 free(data);
199 data = nullptr;
200 return pramsString;
201 }
202
ConstructCJSON(const std::string & fontFullPath,const std::vector<std::string> & fullName)203 cJSON *FontConfig::ConstructCJSON(const std::string &fontFullPath, const std::vector<std::string> &fullName)
204 {
205 cJSON *jsonData = cJSON_CreateObject();
206 if (jsonData == nullptr) {
207 return nullptr;
208 }
209 cJSON_AddStringToObject(jsonData, FONT_PATH, fontFullPath.c_str());
210 cJSON *fullNameJson = cJSON_CreateArray();
211 if (fullNameJson == nullptr) {
212 cJSON_Delete(jsonData);
213 return nullptr;
214 }
215 for (const auto &name : fullName) {
216 cJSON_AddItemToArray(fullNameJson, cJSON_CreateString(name.c_str()));
217 }
218 cJSON_AddItemToObject(jsonData, FONT_FULL_NAME, fullNameJson);
219 return jsonData;
220 }
221
GetFontsMap(const std::string & fontPath)222 std::unordered_map<std::string, std::vector<std::string>> FontConfig::GetFontsMap(const std::string &fontPath)
223 {
224 cJSON *jsonData = cJSON_Parse(CheckConfigFile(fontPath).c_str());
225 if (jsonData == nullptr) {
226 FONT_LOGE("heck config file failed");
227 return std::unordered_map<std::string, std::vector<std::string>>();
228 }
229 cJSON *fontList = cJSON_GetObjectItem(jsonData, "fontlist");
230 if (fontList == nullptr) {
231 cJSON_Delete(jsonData);
232 return std::unordered_map<std::string, std::vector<std::string>>();
233 }
234 std::unordered_map<std::string, std::vector<std::string>> fontMap;
235 int fontSize = cJSON_GetArraySize(fontList);
236 for (int i = 0; i < fontSize; i++) {
237 cJSON *arrItem = cJSON_GetArrayItem(fontList, i);
238 cJSON *fullNameValue = cJSON_GetObjectItem(arrItem, FONT_FULL_NAME);
239 int fullNameSize = cJSON_GetArraySize(fullNameValue);
240 std::vector<std::string> fullNames;
241 for (int j = 0; j < fullNameSize; j++) {
242 cJSON *fullNameItem = cJSON_GetArrayItem(fullNameValue, j);
243 if (fullNameItem != nullptr && fullNameItem->valuestring != nullptr) {
244 fullNames.emplace_back(fullNameItem->valuestring);
245 }
246 }
247 cJSON *fontFullPathValue = cJSON_GetObjectItem(arrItem, FONT_PATH);
248 if (fontFullPathValue != nullptr && fontFullPathValue->valuestring != nullptr) {
249 fontMap.insert(std::make_pair(fontFullPathValue->valuestring, fullNames));
250 }
251 }
252 cJSON_Delete(jsonData);
253 return fontMap;
254 }
255 } // namespace FontManager
256 } // namespace Global
257 } // namespace OHOS