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