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 ROUND_RECT_H
17 #define ROUND_RECT_H
18
19 #include <vector>
20
21 #include "utils/drawing_macros.h"
22 #include "utils/point.h"
23 #include "utils/rect.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 class DRAWING_API RoundRect {
29 public:
30 enum CornerPos : int {
31 TOP_LEFT_POS,
32 TOP_RIGHT_POS,
33 BOTTOM_RIGHT_POS,
34 BOTTOM_LEFT_POS,
35 CORNER_NUMBER
36 };
37
38 inline RoundRect() noexcept;
~RoundRect()39 inline ~RoundRect() {}
40
41 inline RoundRect(const RoundRect& roundRect) noexcept;
42 inline RoundRect(const Rect& r, scalar xRad, scalar yRad) noexcept;
43 inline RoundRect(const Rect& r, const std::vector<Point>& radiusXY) noexcept;
44
45 inline void SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY);
46 inline Point GetCornerRadius(CornerPos pos) const;
47
48 inline void SetRect(const Rect& rect);
49 inline Rect GetRect() const;
50
51 inline void Offset(scalar dx, scalar dy);
52
53 void AdjustRadiiX(double limit, double scale, CornerPos cornerPosA, CornerPos cornerPosB);
54 void AdjustRadiiY(double limit, double scale, CornerPos cornerPosA, CornerPos cornerPosB);
55 bool ClampToZero();
56 bool ScaleRadii();
57
58 private:
59 Rect rect_;
60 // Four radii are stored: top-left/top-right/bottom-left/bottom-right corner radii.
61 Point radiusXY_[CORNER_NUMBER] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
62 };
63
RoundRect()64 inline RoundRect::RoundRect() noexcept : rect_() {}
65
RoundRect(const RoundRect & roundRect)66 inline RoundRect::RoundRect(const RoundRect& roundRect) noexcept : RoundRect()
67 {
68 rect_ = roundRect.rect_;
69 for (int i = 0; i < CORNER_NUMBER; ++i) {
70 radiusXY_[i] = roundRect.radiusXY_[i];
71 }
72 }
73
RoundRect(const Rect & r,scalar xRad,scalar yRad)74 inline RoundRect::RoundRect(const Rect& r, scalar xRad, scalar yRad) noexcept : RoundRect()
75 {
76 rect_ = r;
77 for (int i = 0; i < CORNER_NUMBER; ++i) {
78 radiusXY_[i].SetX(xRad);
79 radiusXY_[i].SetY(yRad);
80 }
81 }
82
RoundRect(const Rect & r,const std::vector<Point> & radiusXY)83 inline RoundRect::RoundRect(const Rect& r, const std::vector<Point>& radiusXY) noexcept : RoundRect()
84 {
85 rect_ = r;
86 for (size_t i = 0; i < CORNER_NUMBER && i < radiusXY.size(); ++i) {
87 radiusXY_[i] = radiusXY[i];
88 }
89 ScaleRadii();
90 }
91
SetCornerRadius(CornerPos pos,scalar radiusX,scalar radiusY)92 inline void RoundRect::SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY)
93 {
94 radiusXY_[pos].SetX(radiusX);
95 radiusXY_[pos].SetY(radiusY);
96 }
97
GetCornerRadius(CornerPos pos)98 inline Point RoundRect::GetCornerRadius(CornerPos pos) const
99 {
100 return radiusXY_[pos];
101 }
102
SetRect(const Rect & rect)103 inline void RoundRect::SetRect(const Rect& rect)
104 {
105 rect_ = rect;
106 }
107
GetRect()108 inline Rect RoundRect::GetRect() const
109 {
110 return rect_;
111 }
112
Offset(scalar dx,scalar dy)113 inline void RoundRect::Offset(scalar dx, scalar dy)
114 {
115 rect_.Offset(dx, dy);
116 }
117 } // namespace Drawing
118 } // namespace Rosen
119 } // namespace OHOS
120 #endif