1 /*
2  * Copyright (c) 2022 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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_MEASURE_UTILS_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_MEASURE_UTILS_H
18 
19 #include <optional>
20 
21 #include "base/geometry/calc_dimension.h"
22 #include "base/geometry/ng/offset_t.h"
23 #include "base/geometry/ng/size_t.h"
24 #include "base/utils/macros.h"
25 #include "core/components_ng/property/border_property.h"
26 #include "core/components_ng/property/calc_length.h"
27 #include "core/components_ng/property/layout_constraint.h"
28 #include "core/components_ng/property/measure_property.h"
29 
30 namespace OHOS::Ace::NG {
31 std::optional<float> ConvertToPx(
32     const CalcLength& value, const ScaleProperty& scaleProperty, float percentReference = -1.0f);
33 
34 std::optional<float> ConvertToPx(
35     const std::optional<CalcLength>& value, const ScaleProperty& scaleProperty, float percentReference = -1.0f);
36 
37 std::optional<float> ConvertToPx(
38     const Dimension& dimension, const ScaleProperty& scaleProperty, float percentReference = -1.0f);
39 
40 SizeF ConvertToSize(
41     const CalcSize& size, const ScaleProperty& scaleProperty, const SizeF& percentReference = SizeF(-1.0f, -1.0f));
42 
43 OptionalSizeF ConvertToOptionalSize(
44     const CalcSize& size, const ScaleProperty& scaleProperty, const SizeF& percentReference = SizeF(-1.0f, -1.0f));
45 
46 SizeF ConstrainSize(const SizeF& size, const SizeF& minSize, const SizeF& maxSize);
47 
48 PaddingPropertyF ConvertToPaddingPropertyF(const std::unique_ptr<PaddingProperty>& padding,
49     const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true,
50     bool nonNegative = false);
51 
52 PaddingPropertyF ConvertToPaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty,
53     float percentReference = -1.0f, bool roundPixel = true, bool nonNegative = false);
54 
55 MarginPropertyF ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty>& margin,
56     const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true);
57 
58 MarginPropertyF ConvertToMarginPropertyF(const MarginProperty& margin, const ScaleProperty& scaleProperty,
59     float percentReference = -1.0f, bool roundPixel = true);
60 
61 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty>& borderWidth,
62     const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true);
63 
64 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const BorderWidthProperty& borderWidth,
65     const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true);
66 
67 void UpdatePaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, const SizeF& selfSize,
68     PaddingPropertyF& paddingValue);
69 
70 ACE_FORCE_EXPORT void AddPaddingToSize(const PaddingPropertyF& padding, SizeF& size);
71 
72 ACE_FORCE_EXPORT void MinusPaddingToSize(const PaddingPropertyF& padding, SizeF& size);
73 
74 ACE_FORCE_EXPORT void MinusPaddingToNonNegativeSize(const PaddingPropertyF& padding, SizeF& size);
75 
76 void AddPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size);
77 
78 void MinusPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size);
79 
80 float GetCrossAxisSize(const SizeF& size, Axis axis);
81 
82 float GetMainAxisOffset(const OffsetF& offset, Axis axis);
83 
84 float GetMainAxisSize(const SizeF& size, Axis axis);
85 
86 void SetCrossAxisSize(float value, Axis axis, SizeF& size);
87 
88 std::optional<float> GetCrossAxisSize(const OptionalSizeF& size, Axis axis);
89 
90 std::optional<float> GetMainAxisSize(const OptionalSizeF& size, Axis axis);
91 
92 void SetCrossAxisSize(float value, Axis axis, OptionalSizeF& size);
93 
94 void SetMainAxisSize(float value, Axis axis, OptionalSizeF& size);
95 
96 void UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize,
97     const std::optional<CalcSize>& calcLayoutConstraintMaxMinSize, const SizeT<float> percentReference, bool IsMaxSize);
98 
99 ACE_FORCE_EXPORT PaddingProperty ConvertToCalcPaddingProperty(const std::optional<CalcDimension>& top,
100     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
101     const std::optional<CalcDimension>& right);
102 
103 /**
104  * @brief Create node IdealSize.
105  *
106  * @param layoutConstraint the constraint of current node.
107  * @param axis the axis of this node.
108  * @param measureType the measure info.
109  * @param usingMaxSize When the component cannot confirm the size, it decides whether to use the max or min value.
110  * @return SizeF the node size info.
111  */
112 ACE_FORCE_EXPORT SizeF CreateIdealSize(
113     const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool usingMaxSize);
114 
115 /**
116  * @brief Create node IdealSize.
117  *
118  * @param layoutConstraint the constraint of current node.
119  * @param axis the axis of this node.
120  * @param measureType the measure info.
121  * @return SizeF the node size info.
122  */
123 ACE_FORCE_EXPORT OptionalSizeF CreateIdealSize(
124     const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType);
125 
126 /**
127  * @brief Create node IdealSize by percent reference.
128  *
129  * @param layoutConstraint the constraint of current node.
130  * @param axis the axis of this node.
131  * @param measureType the measure info.
132  * @param needToConstrain constraint the result idealSize or not by min and max size in layoutConstraint.
133  * @return SizeF the node size info.
134  */
135 OptionalSizeF CreateIdealSizeByPercentRef(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType,
136     bool needToConstrain = false, const std::unique_ptr<MeasureProperty>& rawConstraint = nullptr);
137 
138 /**
139  * @brief Create max size for children which is parent's max size minus margin and padding.
140  *
141  * @param size the max size of parent node.
142  * @param margin the margin property of this node.
143  * @param padding the padding property of this node.
144  */
145 void CreateChildrenConstraint(SizeF& size, const PaddingPropertyF& padding);
146 
147 /**
148  * @brief Calculate IdeaSize by maxSize and minSize, while keeping margin unaffected by margin.
149  *
150  * @param frameSize the current size of node.
151  * @param calcLayoutConstraint the calcLayoutConstraint of this node.
152  * @param percentReference the percentReference of this node.
153  */
154 OptionalSizeF UpdateOptionSizeByCalcLayoutConstraint(const OptionalSize<float>& frameSize,
155     const std::unique_ptr<MeasureProperty>& calcLayoutConstraint, const SizeT<float> percentReference);
156 } // namespace OHOS::Ace::NG
157 
158 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PROPERTIES_MEASURE_UTILS_H
159