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 #include "frameworks/bridge/common/dom/dom_svg_animate.h"
17 
18 #include "frameworks/core/components/declaration/svg/svg_animate_declaration.h"
19 
20 namespace OHOS::Ace::Framework {
21 namespace {
22 
23 const char ANIMATION_FILL_MODE_FREEZE[] = "freeze";
24 const char ANIMATION_CALC_MODE_DISCRETE[] = "discrete";
25 const char ANIMATION_CALC_MODE_PACED[] = "paced";
26 const char ANIMATION_CALC_MODE_SPLINE[] = "spline";
27 
28 } // namespace
29 
DOMSvgAnimate(NodeId nodeId,const std::string & nodeName)30 DOMSvgAnimate::DOMSvgAnimate(NodeId nodeId, const std::string& nodeName) : DOMNode(nodeId, nodeName) {}
31 
PrepareSpecializedComponent()32 void DOMSvgAnimate::PrepareSpecializedComponent()
33 {
34     if (!animateComponent_) {
35         animateComponent_ = AceType::MakeRefPtr<SvgAnimateComponent>(std::to_string(GetNodeId()), GetTag());
36     }
37     SetAnimateAttrs();
38 }
39 
SetAnimateAttrs()40 void DOMSvgAnimate::SetAnimateAttrs()
41 {
42     auto declaration = AceType::DynamicCast<SvgAnimateDeclaration>(declaration_);
43     if (!declaration) {
44         return;
45     }
46     animateComponent_->SetBegin(declaration->GetBegin());
47     animateComponent_->SetDur(declaration->GetDur());
48     animateComponent_->SetEnd(declaration->GetEnd());
49     animateComponent_->SetRepeatCount(declaration->GetRepeatCount());
50     Fill fillMode = (declaration->GetFillMode() == ANIMATION_FILL_MODE_FREEZE ? Fill::FREEZE : Fill::REMOVE);
51     animateComponent_->SetFillMode(fillMode);
52     animateComponent_->SetCalcMode(ConvertCalcMode(declaration->GetCalcMode()));
53     animateComponent_->SetValues(declaration->GetValues());
54     animateComponent_->SetKeyTimes(declaration->GetKeyTimes());
55     animateComponent_->SetKeySplines(declaration->GetKeySplines());
56     animateComponent_->SetFrom(declaration->GetFrom());
57     animateComponent_->SetTo(declaration->GetTo());
58     animateComponent_->SetAttributeName(declaration->GetAttributeName());
59 
60     if (animateComponent_->GetSvgAnimateType() == SvgAnimateType::MOTION) {
61         animateComponent_->SetKeyPoints(declaration->GetKeyPoints());
62         animateComponent_->SetPath(declaration->GetPath());
63         animateComponent_->SetRotate(declaration->GetRotate());
64     }
65     if (animateComponent_->GetSvgAnimateType() == SvgAnimateType::TRANSFORM) {
66         animateComponent_->SetTransformType(declaration->GetTransformType());
67     }
68 }
69 
ConvertCalcMode(const std::string & val) const70 CalcMode DOMSvgAnimate::ConvertCalcMode(const std::string& val) const
71 {
72     CalcMode calcMode;
73     if (val == ANIMATION_CALC_MODE_DISCRETE) {
74         calcMode = CalcMode::DISCRETE;
75     } else if (val == ANIMATION_CALC_MODE_PACED) {
76         calcMode = CalcMode::PACED;
77     } else if (val == ANIMATION_CALC_MODE_SPLINE) {
78         calcMode = CalcMode::SPLINE;
79     } else {
80         calcMode = CalcMode::LINEAR;
81     }
82     return calcMode;
83 }
84 
85 } // namespace OHOS::Ace::Framework
86