1 /*
2  * Copyright (c) 2023 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 "texgine_rect.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace TextEngine {
TexgineRect()21 TexgineRect::TexgineRect(): rect_(std::make_shared<RSRect>())
22 {
23     fBottom_ = &rect_->bottom_;
24     fLeft_ = &rect_->left_;
25     fTop_ = &rect_->top_;
26     fRight_ = &rect_->right_;
27 }
28 
MakeLTRB(float left,float top,float right,float bottom)29 TexgineRect TexgineRect::MakeLTRB(float left, float top, float right, float bottom)
30 {
31     auto rect = std::make_shared<TexgineRect>();
32     rect->SetRect(RSRect { left, top, right, bottom });
33     return *rect;
34 }
35 
MakeXYWH(float x,float y,float w,float h)36 TexgineRect TexgineRect::MakeXYWH(float x, float y, float w, float h)
37 {
38     auto rect = std::make_shared<TexgineRect>();
39     rect->SetRect(RSRect { x, y, x + w, y + h });
40     return *rect;
41 }
42 
MakeWH(float w,float h)43 TexgineRect TexgineRect::MakeWH(float w, float h)
44 {
45     auto rect = std::make_shared<TexgineRect>();
46     rect->SetRect(RSRect { 0, 0, w, h });
47     return *rect;
48 }
49 
MakeRRect(float x,float y,float w,float h,const SkVector skRadii[4])50 TexgineRect TexgineRect::MakeRRect(float x, float y, float w, float h, const SkVector skRadii[4])
51 {
52     auto rect = std::make_shared<TexgineRect>();
53     RSRect rsRect = { x, y, x + w, y + h };
54     RSPoint leftTop(skRadii[0].x(), skRadii[0].y()); // skRadii[0] is leftTop corner
55     RSPoint rightTop(skRadii[1].x(), skRadii[1].y()); // skRadii[1] is rightTop corner
56     RSPoint rightBottom(skRadii[2].x(), skRadii[2].y()); // skRadii[2] is rightBottom corner
57     RSPoint leftBottom(skRadii[3].x(), skRadii[3].y()); // skRadii[3] is leftBottom corner
58     std::vector<RSPoint> radiusXY = { leftTop, rightTop, rightBottom, leftBottom };
59     rect->SetRRect(RSRoundRect { rsRect, radiusXY });
60     return *rect;
61 }
62 
GetRect() const63 std::shared_ptr<RSRect> TexgineRect::GetRect() const
64 {
65     return rect_;
66 }
67 
GetRRect() const68 std::shared_ptr<RSRoundRect> TexgineRect::GetRRect() const
69 {
70     return rrect_;
71 }
72 
SetRect(const RSRect & rect)73 void TexgineRect::SetRect(const RSRect &rect)
74 {
75     *rect_ = rect;
76     fBottom_ = &rect_->bottom_;
77     fLeft_ = &rect_->left_;
78     fTop_ = &rect_->top_;
79     fRight_ = &rect_->right_;
80 }
81 
SetRRect(const RSRoundRect & rrect)82 void TexgineRect::SetRRect(const RSRoundRect &rrect)
83 {
84     rrect_ = std::make_shared<RSRoundRect>(std::move(rrect));
85 }
86 } // namespace TextEngine
87 } // namespace Rosen
88 } // namespace OHOS
89