1 /*
2  * Copyright (c) 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_region.h"
17 
18 #include "drawing_canvas_utils.h"
19 
20 #include "utils/region.h"
21 
22 using namespace OHOS;
23 using namespace Rosen;
24 using namespace Drawing;
25 
CastToPath(const OH_Drawing_Path * cPath)26 static const Path* CastToPath(const OH_Drawing_Path* cPath)
27 {
28     return reinterpret_cast<const Path*>(cPath);
29 }
30 
CastToRegion(OH_Drawing_Region * cRegion)31 static Region* CastToRegion(OH_Drawing_Region* cRegion)
32 {
33     return reinterpret_cast<Region*>(cRegion);
34 }
35 
CastToRect(const OH_Drawing_Rect * cRect)36 static const Rect* CastToRect(const OH_Drawing_Rect* cRect)
37 {
38     return reinterpret_cast<const Rect*>(cRect);
39 }
40 
OH_Drawing_RegionCreate()41 OH_Drawing_Region* OH_Drawing_RegionCreate()
42 {
43     return (OH_Drawing_Region*)new Region();
44 }
45 
OH_Drawing_RegionContains(OH_Drawing_Region * cRegion,int32_t x,int32_t y)46 bool OH_Drawing_RegionContains(OH_Drawing_Region* cRegion, int32_t x, int32_t y)
47 {
48     Region* region = CastToRegion(cRegion);
49     if (region == nullptr) {
50         g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
51         return false;
52     }
53     return region->Contains(x, y);
54 }
55 
OH_Drawing_RegionOp(OH_Drawing_Region * cRegion,const OH_Drawing_Region * cDst,OH_Drawing_RegionOpMode op)56 bool OH_Drawing_RegionOp(OH_Drawing_Region* cRegion, const OH_Drawing_Region* cDst, OH_Drawing_RegionOpMode op)
57 {
58     Region* region = CastToRegion(cRegion);
59     Region* dst = CastToRegion(const_cast<OH_Drawing_Region*>(cDst));
60     if (region == nullptr || dst == nullptr) {
61         g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
62         return false;
63     }
64     if (op < REGION_OP_MODE_DIFFERENCE || op > REGION_OP_MODE_REPLACE) {
65         g_drawingErrorCode = OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE;
66         return false;
67     }
68     return region->Op(*dst, static_cast<RegionOp>(op));
69 }
70 
OH_Drawing_RegionSetRect(OH_Drawing_Region * cRegion,const OH_Drawing_Rect * cRect)71 bool OH_Drawing_RegionSetRect(OH_Drawing_Region* cRegion, const OH_Drawing_Rect* cRect)
72 {
73     const Rect* rect = CastToRect(cRect);
74     Region* region = CastToRegion(cRegion);
75     if (region == nullptr || rect == nullptr) {
76         g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
77         return false;
78     }
79     int left = rect->GetLeft();
80     int right = rect->GetRight();
81     int top = rect->GetTop();
82     int bottom = rect->GetBottom();
83     RectI rectI(left, top, right, bottom);
84     return region->SetRect(rectI);
85 }
86 
OH_Drawing_RegionSetPath(OH_Drawing_Region * cRegion,const OH_Drawing_Path * cPath,const OH_Drawing_Region * cClip)87 bool OH_Drawing_RegionSetPath(OH_Drawing_Region* cRegion, const OH_Drawing_Path* cPath, const OH_Drawing_Region* cClip)
88 {
89     Region* region = CastToRegion(cRegion);
90     const Path* path = CastToPath(cPath);
91     Region* clip = CastToRegion(const_cast<OH_Drawing_Region*>(cClip));
92     if (region == nullptr || path == nullptr || clip == nullptr) {
93         g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
94         return false;
95     }
96     return region->SetPath(*path, *clip);
97 }
98 
OH_Drawing_RegionDestroy(OH_Drawing_Region * cRegion)99 void OH_Drawing_RegionDestroy(OH_Drawing_Region* cRegion)
100 {
101     if (cRegion == nullptr) {
102         return;
103     }
104     delete CastToRegion(cRegion);
105 }
106