• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "font_config.h"
17 
18 #include <dirent.h>
19 #include <fstream>
20 #include <libgen.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24 
25 #include "securec.h"
26 #include "texgine/utils/exlog.h"
27 
28 namespace OHOS {
29 namespace Rosen {
30 namespace TextEngine {
31 #define SUCCESSED 0
32 #define FAILED 1
33 
34 const std::string DEFAULT_DIR = "/system/fonts/";
35 
FontConfig(const char * fname)36 FontConfig::FontConfig(const char* fname)
37 {
38     int err = ParseConfig(fname);
39     if (err != 0) {
40         LOGSO_FUNC_LINE(ERROR) << "parse config err";
41     }
42 }
43 
GetFileData(const char * fname,int & size)44 char* FontConfig::GetFileData(const char* fname, int& size)
45 {
46     char realPath[PATH_MAX] = {0};
47     if (fname == nullptr || realpath(fname, realPath) == NULL) {
48         LOGSO_FUNC_LINE(ERROR) << "path or realPath is NULL";
49         return nullptr;
50     }
51     std::ifstream file(fname);
52     if (file.good()) {
53         FILE* fp = fopen(fname, "r");
54         if (fp == nullptr) {
55             return nullptr;
56         }
57         fseek(fp, 0L, SEEK_END);
58         size = ftell(fp) + 1;
59         rewind(fp);
60         char* data = static_cast<char*>(malloc(size));
61         if (data == nullptr) {
62             fclose(fp);
63             return nullptr;
64         }
65         if (memset_s(data, size, 0, size) != EOK) {
66             LOGSO_FUNC_LINE(ERROR) << "memset failed";
67             free(data);
68             data = nullptr;
69             fclose(fp);
70             return nullptr;
71         }
72         (void)fread(data, size, 1, fp);
73         fclose(fp);
74         return data;
75     }
76 
77     return nullptr;
78 }
79 
CheckConfigFile(const char * fname,Json::Value & root) const80 int FontConfig::CheckConfigFile(const char* fname, Json::Value& root) const
81 {
82     int size = 0;
83     char* data = GetFileData(fname, size);
84     if (data == nullptr) {
85         LOGSO_FUNC_LINE(ERROR) << "data is null";
86         return FAILED;
87     }
88     JSONCPP_STRING errs;
89     Json::CharReaderBuilder charReaderBuilder;
90     std::unique_ptr<Json::CharReader> jsonReader(charReaderBuilder.newCharReader());
91     bool isJson = jsonReader->parse(data, data + size, &root, &errs);
92     free(data);
93     data = nullptr;
94 
95     if (!isJson || !errs.empty()) {
96         LOGSO_FUNC_LINE(ERROR) << "not json or errs no empty";
97         return FAILED;
98     }
99     return SUCCESSED;
100 }
101 
ParseFont(const Json::Value & root)102 int FontConfig::ParseFont(const Json::Value& root)
103 {
104     for (unsigned int i = 0; i < root.size(); ++i) {
105         if (root[i].isString()) {
106             fontSet_.emplace_back(DEFAULT_DIR + root[i].asString());
107         }
108     }
109     return SUCCESSED;
110 }
111 
ParseConfig(const char * fname)112 int FontConfig::ParseConfig(const char* fname)
113 {
114     if (fname == nullptr) {
115         LOGSO_FUNC_LINE(ERROR) << "fname is null";
116         return FAILED;
117     }
118     Json::Value root;
119     int err = CheckConfigFile(fname, root);
120     if (err != 0) {
121         LOGSO_FUNC_LINE(ERROR) << "check config file failed";
122         return err;
123     }
124     const char* tag = "font";
125     if (root.isMember(tag)) {
126         if (root[tag].isArray()) {
127             ParseFont(root[tag]);
128         } else {
129             LOGSO_FUNC_LINE(ERROR) << "not array";
130             return FAILED;
131         }
132     } else {
133         LOGSO_FUNC_LINE(ERROR) << "not member";
134         return FAILED;
135     }
136 
137     return SUCCESSED;
138 }
139 
Dump() const140 void FontConfig::Dump() const
141 {
142     for (auto it : fontSet_) {
143         LOGSO_FUNC_LINE(INFO) << "fname:" << it;
144     }
145 }
146 
GetFontSet() const147 std::vector<std::string> FontConfig::GetFontSet() const
148 {
149     return fontSet_;
150 }
151 } // namespace TextEngine
152 } // namespace Rosen
153 } // namespace OHOS