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/edge.h"
17
18 namespace OHOS::Ace {
19 namespace {
20
GetValueInPx(const Dimension & value,double dipScale)21 double GetValueInPx(const Dimension& value, double dipScale)
22 {
23 if (value.Unit() == DimensionUnit::VP || value.Unit() == DimensionUnit::FP) {
24 return value.Value() * dipScale;
25 } else {
26 return value.Value();
27 }
28 }
29
30 } // namespace
31
32 const Edge Edge::NONE = Edge(0.0, 0.0, 0.0, 0.0);
33
FromString(const std::string & value,Edge & edge)34 bool Edge::FromString(const std::string& value, Edge& edge)
35 {
36 bool result = true;
37 std::vector<std::string> offsets;
38 StringUtils::StringSplitter(value, ' ', offsets);
39 switch (offsets.size()) {
40 case 1: // example: 1px
41 edge.top_ = StringUtils::StringToDimension(offsets[0]);
42 edge.right_ = StringUtils::StringToDimension(offsets[0]);
43 edge.bottom_ = StringUtils::StringToDimension(offsets[0]);
44 edge.left_ = StringUtils::StringToDimension(offsets[0]);
45 break;
46 case 2: // example: 1px 2px
47 edge.top_ = StringUtils::StringToDimension(offsets[0]);
48 edge.right_ = StringUtils::StringToDimension(offsets[1]);
49 edge.bottom_ = StringUtils::StringToDimension(offsets[0]);
50 edge.left_ = StringUtils::StringToDimension(offsets[1]);
51 break;
52 case 3: // example: 1px 2px 3px
53 edge.top_ = StringUtils::StringToDimension(offsets[0]);
54 edge.right_ = StringUtils::StringToDimension(offsets[1]);
55 edge.bottom_ = StringUtils::StringToDimension(offsets[2]);
56 edge.left_ = StringUtils::StringToDimension(offsets[1]);
57 break;
58 case 4: // example: 1px 2px 3px 4px
59 edge.top_ = StringUtils::StringToDimension(offsets[0]);
60 edge.right_ = StringUtils::StringToDimension(offsets[1]);
61 edge.bottom_ = StringUtils::StringToDimension(offsets[2]);
62 edge.left_ = StringUtils::StringToDimension(offsets[3]);
63 break;
64 default:
65 result = false;
66 break;
67 }
68 return result;
69 }
70
IsValid() const71 bool Edge::IsValid() const
72 {
73 return left_.Value() >= 0.0 && top_.Value() >= 0.0 && right_.Value() >= 0.0 && bottom_.Value() >= 0.0;
74 }
75
IsEffective() const76 bool Edge::IsEffective() const
77 {
78 return left_.Value() > 0.0 || top_.Value() > 0.0 || right_.Value() > 0.0 || bottom_.Value() > 0.0;
79 }
80
GetLayoutSizeInPx(double dipScale) const81 Size Edge::GetLayoutSizeInPx(double dipScale) const
82 {
83 double width = GetValueInPx(left_, dipScale) + GetValueInPx(right_, dipScale);
84 double height = GetValueInPx(top_, dipScale) + GetValueInPx(bottom_, dipScale);
85 return Size(width, height);
86 }
87
GetOffsetInPx(double dipScale) const88 Offset Edge::GetOffsetInPx(double dipScale) const
89 {
90 return Offset(GetValueInPx(left_, dipScale), GetValueInPx(top_, dipScale));
91 }
92
HorizontalInPx(double dipScale) const93 double Edge::HorizontalInPx(double dipScale) const
94 {
95 return GetValueInPx(left_, dipScale) + GetValueInPx(right_, dipScale);
96 }
97
VerticalInPx(double dipScale) const98 double Edge::VerticalInPx(double dipScale) const
99 {
100 return GetValueInPx(top_, dipScale) + GetValueInPx(bottom_, dipScale);
101 }
102
103 } // namespace OHOS::Ace