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 LIB_HW_SYMBOL_CONFIG_H_
17 #define LIB_HW_SYMBOL_CONFIG_H_
18 
19 #include <unordered_map>
20 #include <mutex>
21 #include <json/json.h>
22 
23 #include "include/core/SkStream.h"
24 #include "include/core/SkString.h"
25 #include "include/core/SkTypes.h"
26 #include "include/core/HMSymbol.h"
27 
28 class SK_API HmSymbolConfig_OHOS
29 {
30 public:
31     static HmSymbolConfig_OHOS* GetInstance();
32 
33     SymbolLayersGroups GetSymbolLayersGroups(uint16_t glyphId);
34 
35     std::vector<std::vector<PiecewiseParameter>> GetGroupParameters(AnimationType type, uint16_t groupSum,
36         uint16_t animationMode = 0, CommonSubType commonSubType = CommonSubType::DOWN);
37 
38     int ParseConfigOfHmSymbol(const char* fname, SkString fontDir);
39 
40     bool GetHmSymbolEnable();
41 
GetInit() const42     bool GetInit() const
43     {
44         return isInit_;
45     }
46 
SetInit(const bool init)47     void SetInit(const bool init)
48     {
49         isInit_ = init;
50     }
51 
Lock()52     void Lock()
53     {
54         hmSymbolMut_.lock();
55     }
56 
Unlock()57     void Unlock()
58     {
59         hmSymbolMut_.unlock();
60     }
61 
Clear()62     void Clear()
63     {
64         hmSymbolConfig_.clear();
65         animationInfos_.clear();
66         isInit_ = false;
67     }
68 
69 private:
HmSymbolConfig_OHOS()70     HmSymbolConfig_OHOS() {}
71     HmSymbolConfig_OHOS(const HmSymbolConfig_OHOS& hsc) = delete;
72     HmSymbolConfig_OHOS& operator=(const HmSymbolConfig_OHOS& hsc) = delete;
73     HmSymbolConfig_OHOS(const HmSymbolConfig_OHOS&& hsc) = delete;
74     HmSymbolConfig_OHOS& operator=(const HmSymbolConfig_OHOS&& hsc) = delete;
75 
76     std::unordered_map<uint16_t, SymbolLayersGroups> hmSymbolConfig_;
77     std::unordered_map<AnimationType, AnimationInfo> animationInfos_;
78     std::mutex hmSymbolMut_;
79     bool isInit_ = false;
80 
81     const uint32_t defaultColorHexLen = 9;
82     const uint32_t defaultColorStrLen = 7;
83     const uint32_t hexFlag = 16;
84     const uint32_t twoBytesBitsLen = 16;
85     const uint32_t oneByteBitsLen = 8;
86 
87     int CheckConfigFile(const char* fname, Json::Value& root);
88 
89     uint32_t EncodeAnimationAttribute(uint16_t groupSum, uint16_t animationMode, CommonSubType commonSubType);
90 
91     void ParseSymbolAnimations(const Json::Value& root,
92         std::unordered_map<AnimationType, AnimationInfo>* animationInfos);
93     void ParseSymbolAnimationParas(const Json::Value& root,
94         std::map<uint32_t, AnimationPara>& animationParas);
95     void ParseSymbolAnimationPara(const Json::Value& root, AnimationPara& animationPara);
96     void ParseSymbolGroupParas(const Json::Value& root, std::vector<std::vector<PiecewiseParameter>>& groupParameters);
97     void ParseSymbolPiecewisePara(const Json::Value& root, PiecewiseParameter& piecewiseParameter);
98     void ParseSymbolCurveArgs(const Json::Value& root, std::map<std::string, float>& curveArgs);
99     void ParseSymbolProperties(const Json::Value& root, std::map<std::string, std::vector<float>>& properties);
100 
101     void ParseSymbolLayersGrouping(const Json::Value& root);
102     void ParseOneSymbol(const Json::Value& root, std::unordered_map<uint16_t, SymbolLayersGroups>* hmSymbolConfig);
103     void ParseLayers(const Json::Value& root, std::vector<std::vector<size_t>>& layers);
104     void ParseRenderModes(const Json::Value& root, std::map<SymbolRenderingStrategy,
105         std::vector<RenderGroup>>& renderModesGroups);
106     void ParseComponets(const Json::Value& root, std::vector<size_t>& components);
107     void ParseRenderGroups(const Json::Value& root, std::vector<RenderGroup>& renderGroups);
108     void ParseGroupIndexes(const Json::Value& root, std::vector<GroupInfo>& groupInfos);
109     void ParseLayerOrMaskIndexes(const Json::Value& root, std::vector<size_t>& indexes);
110     void ParseDefaultColor(const char* defaultColorStr, RenderGroup& renderGroup);
111     void ParseAnimationSettings(const Json::Value& root, std::vector<AnimationSetting>& animationSettings);
112     void ParseAnimationSetting(const Json::Value& root, AnimationSetting& animationSetting);
113     void ParseAnimationType(const std::string& animationTypeStr, AnimationType& animationType);
114     void ParseAnimationTypes(const Json::Value& root, std::vector<AnimationType>& animationTypes);
115     void ParseGroupSettings(const Json::Value& root, std::vector<GroupSetting>& groupSettings);
116     void ParseGroupSetting(const Json::Value& root, GroupSetting& groupSetting);
117     void ParseSymbolCommonSubType(const char* subTypeStr, CommonSubType& commonSubType);
118 
119     void ParseOneSymbolNativeCase(const char* key, const Json::Value& root, SymbolLayersGroups& symbolLayersGroups,
120         uint16_t& nativeGlyphId);
121     void ParseOneSymbolLayerCase(const char* key, const Json::Value& root, SymbolLayersGroups& symbolLayersGroups);
122     void ParseOneSymbolRenderCase(const char* key, const Json::Value& root, SymbolLayersGroups& symbolLayersGroups);
123     void ParseOneSymbolAnimateCase(const char* key, const Json::Value& root, SymbolLayersGroups& symbolLayersGroups);
124 };
125 
126 #endif