1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_CONTEXT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_CONTEXT_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "base/memory/ace_type.h"
23 #include "frameworks/core/components/svg/parse/svg_node.h"
24 #include "frameworks/core/components/svg/render_svg_base.h"
25 
26 namespace OHOS::Ace {
27 
28 using AttrMap = std::unordered_map<std::string, std::string>;
29 using ClassStyleMap = std::unordered_map<std::string, AttrMap>;
30 
31 class SvgNode;
32 
33 class SvgContext : public AceType {
34     DECLARE_ACE_TYPE(SvgContext, AceType);
35 
36 public:
37     SvgContext() = default;
~SvgContext()38     ~SvgContext()
39     {
40         idMapper_.clear();
41     }
42 
Push(const std::string & value,const RefPtr<SvgNode> & svgNode)43     void Push(const std::string& value, const RefPtr<SvgNode>& svgNode)
44     {
45         idMapper_.emplace(value, svgNode);
46     }
47 
GetSvgNodeById(const std::string & id)48     RefPtr<SvgNode> GetSvgNodeById(const std::string& id) const
49     {
50         auto item = idMapper_.find(id);
51         if (item != idMapper_.end()) {
52             return item->second;
53         }
54         return nullptr;
55     }
56 
PushStyle(const std::string & styleName,const std::pair<std::string,std::string> & attrPair)57     void PushStyle(const std::string& styleName, const std::pair<std::string, std::string>& attrPair)
58     {
59         const auto& arrMapIter = styleMap_.find(styleName);
60         if (arrMapIter == styleMap_.end()) {
61             AttrMap attrMap;
62             attrMap.emplace(attrPair);
63             styleMap_.emplace(std::make_pair(styleName, attrMap));
64         } else {
65             if (arrMapIter->second.find(attrPair.first) != arrMapIter->second.end()) {
66                 arrMapIter->second.erase(attrPair.first);
67             }
68             arrMapIter->second.emplace(attrPair);
69         }
70     }
71 
GetAttrMap(const std::string & key)72     const AttrMap& GetAttrMap(const std::string& key) const
73     {
74         auto styleClassIter = styleMap_.find(key);
75         if (styleClassIter != styleMap_.end()) {
76             return styleClassIter->second;
77         } else {
78             static AttrMap emptyMap;
79             return emptyMap;
80         }
81     }
82 
addMaskReferUrl(std::string maskId)83     void addMaskReferUrl(std::string maskId)
84     {
85         if (maskId.empty()) {
86             return;
87         }
88         urlMaskIdSet_.emplace(maskId);
89     }
90 
IfIdInMaskReferSet(std::string maskId)91     bool IfIdInMaskReferSet(std::string maskId)
92     {
93         if (maskId.empty()) {
94             return false;
95         }
96         return urlMaskIdSet_.find(maskId) != urlMaskIdSet_.end();
97     }
98 
SetSvgRoot(const RefPtr<RenderSvgBase> & renderNode)99     void SetSvgRoot(const RefPtr<RenderSvgBase>& renderNode)
100     {
101         root_ = renderNode;
102     }
103 
GetSvgRoot()104     const RefPtr<RenderSvgBase>& GetSvgRoot() const
105     {
106         return root_;
107     }
108 
109 private:
110     std::unordered_map<std::string, RefPtr<SvgNode>> idMapper_;
111     std::set<std::string> urlMaskIdSet_;
112     ClassStyleMap styleMap_;
113     RefPtr<RenderSvgBase> root_ = nullptr;
114 };
115 
116 } // namespace OHOS::Ace
117 
118 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_PARSE_SVG_CONTEXT_H