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_SVGTRANSFORM_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_SVGTRANSFORM_H
18 
19 #include <map>
20 #include <vector>
21 
22 #include "base/geometry/matrix4.h"
23 
24 namespace OHOS::Ace {
25 
26 struct TransformInfo {
27     Matrix4 matrix4;
28     Offset rotateCenter;
29     bool hasRotateCenter = false;
30 };
31 
32 class SvgTransform final {
33 public:
34     SvgTransform() = default;
35     ~SvgTransform() = default;
36 
37     // input: "rotate(-10, 50, 100) translate(-36, 45.5) skewX(40) skewY(40) scale(1, 0.5) matrix(1, 2, 3, 4, 5, 6)"
38     static Matrix4 CreateMatrix4(const std::string& transform);
39     static TransformInfo CreateInfoFromString(const std::string& transform);
40 
41     // input: "rotate(-10, 50, 100) translate(-36, 45.5) skewX(40) skewY(40) scale(1, 0.5) matrix(1, 2, 3, 4, 5, 6)"
42     // output: "{ { "rotate" : {-10, 50, 100} }, { "translate" : {-36, 45.5} }, { "skew" : {40, 50} },
43     //            { "scale", {1, 0.5} }, { "matrix", {1, 2, 3, 4, 5, 6} } }"
44     static std::map<std::string, std::vector<float>> CreateMap(const std::string& transform);
45 
46     static Matrix4 CreateMatrixFromMap(const std::map<std::string, std::vector<float>>& transform);
47     static TransformInfo CreateInfoFromMap(const std::map<std::string, std::vector<float>>& transform);
48 
49     static bool SetProperty(const std::string& type, const std::vector<float>& from, const std::vector<float>& to,
50         double value, std::map<std::string, std::vector<float>>& transformAttrs);
51 
52     static bool AlignmentValues(const std::string& type, std::vector<float>& from, std::vector<float>& to);
53 
54     static bool AlignmentFrame(const std::string& type, std::vector<float>& frame);
55 
56 private:
57     static void ApplyRotationPivot(Matrix4& mat, float x, float y);
58 };
59 
60 } // namespace OHOS::Ace
61 
62 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SVG_SVGTRANSFORM_H
63