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 const Point& GetCornerRadius(CornerPos pos) const;
47 
48     inline void SetRect(const Rect& rect);
49     inline const Rect& GetRect() const;
50 
51     /**
52      * @brief Translates RoundRect by (dx, dy).
53      * @param dx  offset added to rect left and rect right
54      * @param dy  offset added to rect top and rect bottom
55      */
56     inline void Offset(scalar dx, scalar dy);
57     inline bool IsSimpleRoundRect() const;
58     inline scalar GetSimpleX() const;
59     inline scalar GetSimpleY() const;
60 
61     void AdjustRadiiX(double limit, double scale, CornerPos cornerPosA, CornerPos cornerPosB);
62     void AdjustRadiiY(double limit, double scale, CornerPos cornerPosA, CornerPos cornerPosB);
63     bool ClampToZero();
64 
65     /**
66      * @brief Returns true if the radii had to be scaled to fit rect.
67      */
68     bool ScaleRadii();
69 
70     void Dump(std::string& out) const;
71 
72 private:
73     Rect rect_;
74     // Four radii are stored: top-left/top-right/bottom-left/bottom-right corner radii.
75     Point radiusXY_[CORNER_NUMBER] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
76     bool isSimple_ = false;
77 };
78 
isSimple_(true)79 inline RoundRect::RoundRect() noexcept : rect_(), isSimple_(true) {}
80 
RoundRect()81 inline RoundRect::RoundRect(const RoundRect& roundRect) noexcept : RoundRect()
82 {
83     rect_ = roundRect.rect_;
84     for (int i = 0; i < CORNER_NUMBER; ++i) {
85         radiusXY_[i] = roundRect.radiusXY_[i];
86     }
87     isSimple_ = roundRect.isSimple_;
88 }
89 
RoundRect()90 inline RoundRect::RoundRect(const Rect& r, scalar xRad, scalar yRad) noexcept : RoundRect()
91 {
92     rect_ = r;
93     for (int i = 0; i < CORNER_NUMBER; ++i) {
94         radiusXY_[i].SetX(xRad);
95         radiusXY_[i].SetY(yRad);
96     }
97     isSimple_ = true;
98 }
99 
RoundRect()100 inline RoundRect::RoundRect(const Rect& r, const std::vector<Point>& radiusXY) noexcept : RoundRect()
101 {
102     rect_ = r;
103     for (int i = 0; i < CORNER_NUMBER && i < static_cast<int>(radiusXY.size()); ++i) {
104         radiusXY_[i] = radiusXY[i];
105     }
106     isSimple_ = false;
107     ScaleRadii();
108 }
109 
SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY)110 inline void RoundRect::SetCornerRadius(CornerPos pos, scalar radiusX, scalar radiusY)
111 {
112     radiusXY_[pos].SetX(radiusX);
113     radiusXY_[pos].SetY(radiusY);
114     isSimple_ = false;
115 }
116 
GetCornerRadius(CornerPos pos) const117 inline const Point& RoundRect::GetCornerRadius(CornerPos pos) const
118 {
119     return radiusXY_[pos];
120 }
121 
SetRect(const Rect& rect)122 inline void RoundRect::SetRect(const Rect& rect)
123 {
124     rect_ = rect;
125 }
126 
GetRect() const127 inline const Rect& RoundRect::GetRect() const
128 {
129     return rect_;
130 }
131 
Offset(scalar dx, scalar dy)132 inline void RoundRect::Offset(scalar dx, scalar dy)
133 {
134     rect_.Offset(dx, dy);
135 }
136 
IsSimpleRoundRect() const137 inline bool RoundRect::IsSimpleRoundRect() const
138 {
139     return isSimple_;
140 }
141 
GetSimpleX() const142 inline scalar RoundRect::GetSimpleX() const
143 {
144     return radiusXY_[RoundRect::TOP_LEFT_POS].GetX();
145 }
146 
GetSimpleY() const147 inline scalar RoundRect::GetSimpleY() const
148 {
149     return radiusXY_[RoundRect::TOP_LEFT_POS].GetY();
150 }
151 } // namespace Drawing
152 } // namespace Rosen
153 } // namespace OHOS
154 #endif