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 #include "core/components/common/properties/border.h"
17 
18 namespace OHOS::Ace {
19 
Border(const BorderEdge & left,const BorderEdge & top,const BorderEdge & right,const BorderEdge & bottom)20 Border::Border(const BorderEdge& left, const BorderEdge& top, const BorderEdge& right, const BorderEdge& bottom)
21     : left_(left), top_(top), right_(right), bottom_(bottom)
22 {}
23 
Border(const BorderImageEdge & leftImage,const BorderImageEdge & topImage,const BorderImageEdge & rightImage,const BorderImageEdge & bottomImage)24 Border::Border(const BorderImageEdge& leftImage, const BorderImageEdge& topImage,
25     const BorderImageEdge& rightImage, const BorderImageEdge& bottomImage)
26     : borderImageLeft_(leftImage), borderImageTop_(topImage),
27     borderImageRight_(rightImage), borderImageBottom_(bottomImage)
28 {}
29 
IsAllEqual() const30 bool Border::IsAllEqual() const
31 {
32     return (left_ == top_) && (top_ == right_) && (right_ == bottom_);
33 }
34 
HasValue() const35 bool Border::HasValue() const
36 {
37     return left_.HasValue() || top_.HasValue() || right_.HasValue() || bottom_.HasValue();
38 }
39 
HasRadius() const40 bool Border::HasRadius() const
41 {
42     return topLeftRadius_.HasValue() || topRightRadius_.HasValue() ||
43         bottomLeftRadius_.HasValue() || bottomRightRadius_.HasValue();
44 }
45 
GetOffset(double dipScale) const46 Offset Border::GetOffset(double dipScale) const
47 {
48     return Offset(left_.GetWidthInPx(dipScale), top_.GetWidthInPx(dipScale));
49 }
50 
HorizontalWidth(double dipScale) const51 double Border::HorizontalWidth(double dipScale) const
52 {
53     return left_.GetWidthInPx(dipScale) + right_.GetWidthInPx(dipScale);
54 }
55 
VerticalWidth(double dipScale) const56 double Border::VerticalWidth(double dipScale) const
57 {
58     return top_.GetWidthInPx(dipScale) + bottom_.GetWidthInPx(dipScale);
59 }
60 
GetLayoutSize(double dipScale) const61 Size Border::GetLayoutSize(double dipScale) const
62 {
63     return Size(HorizontalWidth(dipScale), VerticalWidth(dipScale));
64 }
65 
GetValidEdge() const66 BorderEdge Border::GetValidEdge() const
67 {
68     if (left_.HasValue()) {
69         return left_;
70     }
71     if (top_.HasValue()) {
72         return top_;
73     }
74     if (right_.HasValue()) {
75         return right_;
76     }
77     if (bottom_.HasValue()) {
78         return bottom_;
79     }
80     return BorderEdge();
81 }
82 
83 } // namespace OHOS::Ace
84