1 /*
2 * Copyright (c) 2023-2024 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 "drawing_rect.h"
17
18 #include "drawing_canvas_utils.h"
19 #include "array_mgr.h"
20 #include "utils/rect.h"
21
22 using namespace OHOS;
23 using namespace Rosen;
24 using namespace Drawing;
25
CastToRect(OH_Drawing_Rect * cRect)26 static Rect* CastToRect(OH_Drawing_Rect* cRect)
27 {
28 return reinterpret_cast<Rect*>(cRect);
29 }
30
OH_Drawing_RectCreate(float left,float top,float right,float bottom)31 OH_Drawing_Rect* OH_Drawing_RectCreate(float left, float top, float right, float bottom)
32 {
33 return (OH_Drawing_Rect*)new Rect(left, top, right, bottom);
34 }
35
OH_Drawing_RectIntersect(OH_Drawing_Rect * cRect,const OH_Drawing_Rect * other)36 bool OH_Drawing_RectIntersect(OH_Drawing_Rect* cRect, const OH_Drawing_Rect* other)
37 {
38 Rect* rect = CastToRect(cRect);
39 const Rect* otherRect = reinterpret_cast<const Rect*>(other);
40 if (rect == nullptr || otherRect == nullptr) {
41 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
42 return false;
43 }
44 return rect->Intersect(*otherRect);
45 }
46
OH_Drawing_RectJoin(OH_Drawing_Rect * cRect,const OH_Drawing_Rect * other)47 bool OH_Drawing_RectJoin(OH_Drawing_Rect* cRect, const OH_Drawing_Rect* other)
48 {
49 Rect* rect = CastToRect(cRect);
50 const Rect* otherRect = reinterpret_cast<const Rect*>(other);
51 if (rect == nullptr || otherRect == nullptr) {
52 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
53 return false;
54 }
55 return rect->Join(*otherRect);
56 }
57
OH_Drawing_RectSetTop(OH_Drawing_Rect * cRect,float top)58 void OH_Drawing_RectSetTop(OH_Drawing_Rect* cRect, float top)
59 {
60 Rect* rect = CastToRect(cRect);
61 if (rect == nullptr) {
62 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
63 return;
64 }
65 rect->SetTop(top);
66 }
67
OH_Drawing_RectSetBottom(OH_Drawing_Rect * cRect,float bottom)68 void OH_Drawing_RectSetBottom(OH_Drawing_Rect* cRect, float bottom)
69 {
70 Rect* rect = CastToRect(cRect);
71 if (rect == nullptr) {
72 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
73 return;
74 }
75 rect->SetBottom(bottom);
76 }
77
OH_Drawing_RectSetLeft(OH_Drawing_Rect * cRect,float left)78 void OH_Drawing_RectSetLeft(OH_Drawing_Rect* cRect, float left)
79 {
80 Rect* rect = CastToRect(cRect);
81 if (rect == nullptr) {
82 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
83 return;
84 }
85 rect->SetLeft(left);
86 }
87
OH_Drawing_RectSetRight(OH_Drawing_Rect * cRect,float right)88 void OH_Drawing_RectSetRight(OH_Drawing_Rect* cRect, float right)
89 {
90 Rect* rect = CastToRect(cRect);
91 if (rect == nullptr) {
92 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
93 return;
94 }
95 rect->SetRight(right);
96 }
97
OH_Drawing_RectGetTop(OH_Drawing_Rect * cRect)98 float OH_Drawing_RectGetTop(OH_Drawing_Rect* cRect)
99 {
100 Rect* rect = CastToRect(cRect);
101 if (rect == nullptr) {
102 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
103 return 0.f;
104 }
105 return rect->GetTop();
106 }
107
OH_Drawing_RectGetBottom(OH_Drawing_Rect * cRect)108 float OH_Drawing_RectGetBottom(OH_Drawing_Rect* cRect)
109 {
110 Rect* rect = CastToRect(cRect);
111 if (rect == nullptr) {
112 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
113 return 0.f;
114 }
115 return rect->GetBottom();
116 }
117
OH_Drawing_RectGetLeft(OH_Drawing_Rect * cRect)118 float OH_Drawing_RectGetLeft(OH_Drawing_Rect* cRect)
119 {
120 Rect* rect = CastToRect(cRect);
121 if (rect == nullptr) {
122 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
123 return 0.f;
124 }
125 return rect->GetLeft();
126 }
127
OH_Drawing_RectGetRight(OH_Drawing_Rect * cRect)128 float OH_Drawing_RectGetRight(OH_Drawing_Rect* cRect)
129 {
130 Rect* rect = CastToRect(cRect);
131 if (rect == nullptr) {
132 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
133 return 0.f;
134 }
135 return rect->GetRight();
136 }
137
OH_Drawing_RectGetHeight(OH_Drawing_Rect * cRect)138 float OH_Drawing_RectGetHeight(OH_Drawing_Rect* cRect)
139 {
140 Rect* rect = CastToRect(cRect);
141 if (rect == nullptr) {
142 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
143 return 0.f;
144 }
145 return rect->GetHeight();
146 }
147
OH_Drawing_RectGetWidth(OH_Drawing_Rect * cRect)148 float OH_Drawing_RectGetWidth(OH_Drawing_Rect* cRect)
149 {
150 Rect* rect = CastToRect(cRect);
151 if (rect == nullptr) {
152 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
153 return 0.f;
154 }
155 return rect->GetWidth();
156 }
157
OH_Drawing_RectCopy(OH_Drawing_Rect * sRect,OH_Drawing_Rect * dRect)158 void OH_Drawing_RectCopy(OH_Drawing_Rect* sRect, OH_Drawing_Rect* dRect)
159 {
160 Rect* rectSrc = CastToRect(dRect);
161 if (rectSrc == nullptr) {
162 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
163 return;
164 }
165
166 Rect* rectDst = CastToRect(sRect);
167 if (rectDst == nullptr) {
168 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
169 return;
170 }
171
172 rectSrc->SetLeft(rectDst->GetLeft());
173 rectSrc->SetTop(rectDst->GetTop());
174 rectSrc->SetRight(rectDst->GetRight());
175 rectSrc->SetBottom(rectDst->GetBottom());
176 }
177
OH_Drawing_RectDestroy(OH_Drawing_Rect * cRect)178 void OH_Drawing_RectDestroy(OH_Drawing_Rect* cRect)
179 {
180 if (!cRect) {
181 return;
182 }
183 delete CastToRect(cRect);
184 }
185
OH_Drawing_RectCreateArray(size_t size)186 OH_Drawing_Array* OH_Drawing_RectCreateArray(size_t size)
187 {
188 if ((size == 0) || (size > (std::numeric_limits<uint16_t>::max() + 1))) {
189 return nullptr;
190 }
191 ObjectArray *obj = new(std::nothrow) ObjectArray();
192 if (obj == nullptr) {
193 return nullptr;
194 }
195 obj->num = size;
196 obj->addr = reinterpret_cast<OH_Drawing_Rect*>(new Rect[size]);
197 if (obj->addr == nullptr) {
198 return nullptr;
199 }
200 obj->type = ObjectType::DRAWING_RECT;
201 return reinterpret_cast<OH_Drawing_Array*>(obj);
202 }
203
OH_Drawing_RectGetArraySize(OH_Drawing_Array * rectArray,size_t * pSize)204 OH_Drawing_ErrorCode OH_Drawing_RectGetArraySize(OH_Drawing_Array* rectArray, size_t* pSize)
205 {
206 if (rectArray == nullptr || pSize == nullptr) {
207 return OH_DRAWING_ERROR_INVALID_PARAMETER;
208 }
209 ObjectArray *obj = reinterpret_cast<ObjectArray*>(rectArray);
210 if (obj->type != ObjectType::DRAWING_RECT) {
211 return OH_DRAWING_ERROR_INVALID_PARAMETER;
212 }
213 *pSize = obj->num;
214 return OH_DRAWING_SUCCESS;
215 }
216
OH_Drawing_RectGetArrayElement(OH_Drawing_Array * rectArray,size_t index,OH_Drawing_Rect ** rect)217 OH_Drawing_ErrorCode OH_Drawing_RectGetArrayElement(OH_Drawing_Array* rectArray, size_t index,
218 OH_Drawing_Rect** rect)
219 {
220 if (rectArray == nullptr || rect == nullptr) {
221 return OH_DRAWING_ERROR_INVALID_PARAMETER;
222 }
223 ObjectArray *obj = reinterpret_cast<ObjectArray*>(rectArray);
224 if (index >= obj->num || obj->type != ObjectType::DRAWING_RECT || obj->addr == nullptr) {
225 return OH_DRAWING_ERROR_INVALID_PARAMETER;
226 }
227 *rect = (OH_Drawing_Rect*)&(reinterpret_cast<Rect*>(obj->addr)[index]);
228 return OH_DRAWING_SUCCESS;
229 }
230
OH_Drawing_RectDestroyArray(OH_Drawing_Array * rectArray)231 OH_Drawing_ErrorCode OH_Drawing_RectDestroyArray(OH_Drawing_Array* rectArray)
232 {
233 if (rectArray == nullptr) {
234 return OH_DRAWING_ERROR_INVALID_PARAMETER;
235 }
236 ObjectArray *obj = reinterpret_cast<ObjectArray*>(rectArray);
237 if (obj->type != ObjectType::DRAWING_RECT || obj->addr == nullptr) {
238 return OH_DRAWING_ERROR_INVALID_PARAMETER;
239 }
240 delete[] reinterpret_cast<Rect*>(obj->addr);
241 delete obj;
242 return OH_DRAWING_SUCCESS;
243 }
244