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 #include "c/drawing_matrix.h"
17
18 #include "utils/matrix.h"
19
20 using namespace OHOS;
21 using namespace Rosen;
22 using namespace Drawing;
23
CastToMatrix(OH_Drawing_Matrix * cMatrix)24 static Matrix* CastToMatrix(OH_Drawing_Matrix* cMatrix)
25 {
26 return reinterpret_cast<Matrix*>(cMatrix);
27 }
28
OH_Drawing_MatrixCreate()29 OH_Drawing_Matrix* OH_Drawing_MatrixCreate()
30 {
31 return (OH_Drawing_Matrix*)new Matrix();
32 }
33
OH_Drawing_MatrixCreateRotation(float deg,float x,float y)34 OH_Drawing_Matrix* OH_Drawing_MatrixCreateRotation(float deg, float x, float y)
35 {
36 Matrix* matrix = new Matrix();
37 if (matrix == nullptr) {
38 return nullptr;
39 }
40 matrix->Rotate(deg, x, y);
41 return (OH_Drawing_Matrix*)matrix;
42 }
43
OH_Drawing_MatrixCreateScale(float sx,float sy,float px,float py)44 OH_Drawing_Matrix* OH_Drawing_MatrixCreateScale(float sx, float sy, float px, float py)
45 {
46 Matrix* matrix = new Matrix();
47 if (matrix == nullptr) {
48 return nullptr;
49 }
50 matrix->Scale(sx, sy, px, py);
51 return (OH_Drawing_Matrix*)matrix;
52 }
53
OH_Drawing_MatrixCreateTranslation(float dx,float dy)54 OH_Drawing_Matrix* OH_Drawing_MatrixCreateTranslation(float dx, float dy)
55 {
56 Matrix* matrix = new Matrix();
57 if (matrix == nullptr) {
58 return nullptr;
59 }
60 matrix->Translate(dx, dy);
61 return (OH_Drawing_Matrix*)matrix;
62 }
63
OH_Drawing_MatrixSetMatrix(OH_Drawing_Matrix * cMatrix,float scaleX,float skewX,float transX,float skewY,float scaleY,float transY,float persp0,float persp1,float persp2)64 void OH_Drawing_MatrixSetMatrix(OH_Drawing_Matrix* cMatrix, float scaleX, float skewX, float transX,
65 float skewY, float scaleY, float transY, float persp0, float persp1, float persp2)
66 {
67 Matrix* matrix = CastToMatrix(cMatrix);
68 if (matrix == nullptr) {
69 return;
70 }
71 matrix->SetMatrix(scaleX, skewX, transX, skewY, scaleY, transY, persp0, persp1, persp2);
72 }
73
OH_Drawing_MatrixPreConcat(OH_Drawing_Matrix * cMatrix,OH_Drawing_Matrix * other)74 void OH_Drawing_MatrixPreConcat(OH_Drawing_Matrix* cMatrix, OH_Drawing_Matrix* other)
75 {
76 Matrix* matrix = CastToMatrix(cMatrix);
77 if (matrix == nullptr) {
78 return;
79 }
80 Matrix* otherMatrix = CastToMatrix(other);
81 if (otherMatrix == nullptr) {
82 return;
83 }
84 matrix->PreConcat(*otherMatrix);
85 }
86
OH_Drawing_MatrixRotate(OH_Drawing_Matrix * cMatrix,float degree,float px,float py)87 void OH_Drawing_MatrixRotate(OH_Drawing_Matrix* cMatrix, float degree, float px, float py)
88 {
89 Matrix* matrix = CastToMatrix(cMatrix);
90 if (matrix == nullptr) {
91 return;
92 }
93 matrix->Rotate(degree, px, py);
94 }
95
OH_Drawing_MatrixTranslate(OH_Drawing_Matrix * cMatrix,float dx,float dy)96 void OH_Drawing_MatrixTranslate(OH_Drawing_Matrix* cMatrix, float dx, float dy)
97 {
98 Matrix* matrix = CastToMatrix(cMatrix);
99 if (matrix == nullptr) {
100 return;
101 }
102 matrix->Translate(dx, dy);
103 }
104
OH_Drawing_MatrixScale(OH_Drawing_Matrix * cMatrix,float sx,float sy,float px,float py)105 void OH_Drawing_MatrixScale(OH_Drawing_Matrix* cMatrix, float sx, float sy, float px, float py)
106 {
107 Matrix* matrix = CastToMatrix(cMatrix);
108 if (matrix == nullptr) {
109 return;
110 }
111 matrix->Scale(sx, sy, px, py);
112 }
113
OH_Drawing_MatrixInvert(OH_Drawing_Matrix * cMatrix,OH_Drawing_Matrix * inverse)114 bool OH_Drawing_MatrixInvert(OH_Drawing_Matrix* cMatrix, OH_Drawing_Matrix* inverse)
115 {
116 Matrix* matrix = CastToMatrix(cMatrix);
117 if (matrix == nullptr) {
118 return false;
119 }
120 Matrix* inverseMatrix = CastToMatrix(inverse);
121 if (inverseMatrix == nullptr) {
122 return false;
123 }
124 return matrix->Invert(*inverseMatrix);
125 }
126
OH_Drawing_MatrixIsEqual(OH_Drawing_Matrix * cMatrix,OH_Drawing_Matrix * other)127 bool OH_Drawing_MatrixIsEqual(OH_Drawing_Matrix* cMatrix, OH_Drawing_Matrix* other)
128 {
129 Matrix* matrix = CastToMatrix(cMatrix);
130 if (matrix == nullptr) {
131 return false;
132 }
133 Matrix* otherMatrix = CastToMatrix(other);
134 if (otherMatrix == nullptr) {
135 return false;
136 }
137 return (*matrix == *otherMatrix);
138 }
139
OH_Drawing_MatrixIsIdentity(OH_Drawing_Matrix * cMatrix)140 bool OH_Drawing_MatrixIsIdentity(OH_Drawing_Matrix* cMatrix)
141 {
142 Matrix* matrix = CastToMatrix(cMatrix);
143 if (matrix == nullptr) {
144 return false;
145 }
146 return matrix->IsIdentity();
147 }
148
OH_Drawing_MatrixDestroy(OH_Drawing_Matrix * cMatrix)149 void OH_Drawing_MatrixDestroy(OH_Drawing_Matrix* cMatrix)
150 {
151 delete CastToMatrix(cMatrix);
152 }
153