1 /*
2 * Copyright (c) 2025 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 "custom_symbol_config.h"
17
18 #include <fstream>
19 #include <json/json.h>
20
21 #include "symbol_resource/symbol_config_parser.h"
22
23 namespace OHOS {
24 namespace Rosen {
25 namespace Symbol {
26
GetInstance()27 CustomSymbolConfig* CustomSymbolConfig::GetInstance()
28 {
29 static CustomSymbolConfig singleton;
30 return &singleton;
31 }
32
ParseConfig(const std::string & familyName,const uint8_t * data,size_t datalen)33 LoadSymbolErrorCode CustomSymbolConfig::ParseConfig(const std::string &familyName,
34 const uint8_t *data, size_t datalen)
35 {
36 if (data == nullptr) {
37 return LoadSymbolErrorCode::LOAD_FAILED;
38 }
39
40 if (symbolConfig_.find(familyName) != symbolConfig_.end()) {
41 return LoadSymbolErrorCode::SUCCESS;
42 }
43
44 std::string srcString(data, data + datalen);
45 Json::Value root;
46 Json::Reader jsonReader;
47 bool isJson = jsonReader.parse(srcString, root);
48 if (!isJson) {
49 return LoadSymbolErrorCode::JSON_ERROR;
50 }
51
52 std::unique_lock<std::shared_mutex> lock(mutex_);
53 std::unordered_map<uint16_t, RSSymbolLayersGroups> symbolConfigGroup;
54 std::unordered_map<OHOS::Rosen::Drawing::DrawingAnimationType, OHOS::Rosen::Drawing::DrawingAnimationInfo>
55 animationInfos;
56 LoadSymbolErrorCode result = LoadSymbolErrorCode::JSON_ERROR;
57
58 if (SymbolConfigParser::ParseSymbolConfig(root, symbolConfigGroup, animationInfos)) {
59 symbolConfig_.emplace(familyName, symbolConfigGroup);
60 result = LoadSymbolErrorCode::SUCCESS;
61 }
62 root.clear();
63 return result;
64 }
65
GetSymbolLayersGroups(const std::string & familyName,uint16_t glyphId)66 std::optional<RSSymbolLayersGroups> CustomSymbolConfig::GetSymbolLayersGroups(const std::string &familyName,
67 uint16_t glyphId)
68 {
69 std::shared_lock<std::shared_mutex> lock(mutex_);
70 auto it = symbolConfig_.find(familyName);
71 if (it == symbolConfig_.end()) {
72 return std::nullopt;
73 }
74
75 std::unordered_map<uint16_t, RSSymbolLayersGroups> &layersInfoMap = it->second;
76 auto infoIter = layersInfoMap.find(glyphId);
77 if (infoIter == layersInfoMap.end()) {
78 return std::nullopt;
79 }
80 return infoIter->second;
81 }
82
83 } // namespace Symbol
84 } // namespace Rosen
85 } // namespace OHOS