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_round_rect.h"
17
18 #include "drawing_canvas_utils.h"
19
20 #include "utils/round_rect.h"
21
22 using namespace OHOS;
23 using namespace Rosen;
24 using namespace Drawing;
25
CastToRect(const OH_Drawing_Rect & cRect)26 static const Rect& CastToRect(const OH_Drawing_Rect& cRect)
27 {
28 return reinterpret_cast<const Rect&>(cRect);
29 }
30
CastToRoundRect(OH_Drawing_RoundRect * cRoundRect)31 static RoundRect* CastToRoundRect(OH_Drawing_RoundRect* cRoundRect)
32 {
33 return reinterpret_cast<RoundRect*>(cRoundRect);
34 }
35
OH_Drawing_RoundRectCreate(const OH_Drawing_Rect * cRect,float xRad,float yRad)36 OH_Drawing_RoundRect* OH_Drawing_RoundRectCreate(const OH_Drawing_Rect* cRect, float xRad, float yRad)
37 {
38 if (cRect == nullptr) {
39 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
40 return nullptr;
41 }
42 return (OH_Drawing_RoundRect*)new RoundRect(CastToRect(*cRect), xRad, yRad);
43 }
44
CCornerPosCastToCornerPos(OH_Drawing_CornerPos pos)45 static RoundRect::CornerPos CCornerPosCastToCornerPos(OH_Drawing_CornerPos pos)
46 {
47 RoundRect::CornerPos roundPos = RoundRect::CornerPos::TOP_LEFT_POS;
48 switch (pos) {
49 case CORNER_POS_TOP_LEFT:
50 roundPos = RoundRect::CornerPos::TOP_LEFT_POS;
51 break;
52 case CORNER_POS_TOP_RIGHT:
53 roundPos = RoundRect::CornerPos::TOP_RIGHT_POS;
54 break;
55 case CORNER_POS_BOTTOM_RIGHT:
56 roundPos = RoundRect::CornerPos::BOTTOM_RIGHT_POS;
57 break;
58 case CORNER_POS_BOTTOM_LEFT:
59 roundPos = RoundRect::CornerPos::BOTTOM_LEFT_POS;
60 break;
61 default:
62 break;
63 }
64 return roundPos;
65 }
66
OH_Drawing_RoundRectSetCorner(OH_Drawing_RoundRect * cRoundRect,OH_Drawing_CornerPos pos,OH_Drawing_Corner_Radii radiusXY)67 void OH_Drawing_RoundRectSetCorner(OH_Drawing_RoundRect* cRoundRect, OH_Drawing_CornerPos pos,
68 OH_Drawing_Corner_Radii radiusXY)
69 {
70 RoundRect* rounRect = CastToRoundRect(cRoundRect);
71 if (rounRect == nullptr) {
72 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
73 return;
74 }
75 if (pos < CORNER_POS_TOP_LEFT || pos > CORNER_POS_BOTTOM_LEFT) {
76 g_drawingErrorCode = OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE;
77 return;
78 }
79 rounRect->SetCornerRadius(CCornerPosCastToCornerPos(pos), radiusXY.x, radiusXY.y);
80 }
81
OH_Drawing_RoundRectGetCorner(OH_Drawing_RoundRect * cRoundRect,OH_Drawing_CornerPos pos)82 OH_Drawing_Corner_Radii OH_Drawing_RoundRectGetCorner(OH_Drawing_RoundRect* cRoundRect, OH_Drawing_CornerPos pos)
83 {
84 RoundRect* rounRect = CastToRoundRect(cRoundRect);
85 if (rounRect == nullptr) {
86 g_drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
87 return {0, 0};
88 }
89 if (pos < CORNER_POS_TOP_LEFT || pos > CORNER_POS_BOTTOM_LEFT) {
90 g_drawingErrorCode = OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE;
91 return {0, 0};
92 }
93 Point radiusXY = rounRect->GetCornerRadius(CCornerPosCastToCornerPos(pos));
94 return {radiusXY.GetX(), radiusXY.GetY()};
95 }
96
OH_Drawing_RoundRectDestroy(OH_Drawing_RoundRect * cRoundRect)97 void OH_Drawing_RoundRectDestroy(OH_Drawing_RoundRect* cRoundRect)
98 {
99 if (!cRoundRect) {
100 return;
101 }
102 delete CastToRoundRect(cRoundRect);
103 }
104
OH_Drawing_RoundRectOffset(OH_Drawing_RoundRect * cRoundRect,float dx,float dy)105 OH_Drawing_ErrorCode OH_Drawing_RoundRectOffset(OH_Drawing_RoundRect* cRoundRect, float dx, float dy)
106 {
107 RoundRect* rounRect = CastToRoundRect(cRoundRect);
108 if (rounRect == nullptr) {
109 return OH_DRAWING_ERROR_INVALID_PARAMETER;
110 }
111 rounRect->Offset(dx, dy);
112 return OH_DRAWING_SUCCESS;
113 }