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 #include "core/components_ng/property/measure_utils.h"
17
18 #include <memory>
19 #include <optional>
20
21 #include "base/geometry/ng/size_t.h"
22 #include "base/geometry/size.h"
23 #include "base/log/log.h"
24 #include "base/utils/utils.h"
25 #include "core/components_ng/property/measure_property.h"
26 #include "core/pipeline/pipeline_base.h"
27
28 namespace OHOS::Ace::NG {
29 namespace {
30 const static int32_t PLATFORM_VERSION_TEN = 10;
31 }
32
ConvertToSize(const CalcSize & size,const ScaleProperty & scaleProperty,const SizeF & percentReference)33 SizeF ConvertToSize(const CalcSize& size, const ScaleProperty& scaleProperty, const SizeF& percentReference)
34 {
35 auto width = ConvertToPx(size.Width(), scaleProperty, percentReference.Width());
36 auto height = ConvertToPx(size.Height(), scaleProperty, percentReference.Height());
37 return { width.value_or(-1.0f), height.value_or(-1.0f) };
38 }
39
ConvertToOptionalSize(const CalcSize & size,const ScaleProperty & scaleProperty,const SizeF & percentReference)40 OptionalSizeF ConvertToOptionalSize(
41 const CalcSize& size, const ScaleProperty& scaleProperty, const SizeF& percentReference)
42 {
43 auto width = ConvertToPx(size.Width(), scaleProperty, percentReference.Width());
44 auto height = ConvertToPx(size.Height(), scaleProperty, percentReference.Height());
45 return { width, height };
46 }
47
ConvertToPx(const CalcLength & value,const ScaleProperty & scaleProperty,float percentReference)48 std::optional<float> ConvertToPx(const CalcLength& value, const ScaleProperty& scaleProperty, float percentReference)
49 {
50 double result = -1.0;
51 if (!value.NormalizeToPx(
52 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
53 LOGE("fail to Convert CalcDimension To Px: %{public}f, %{public}f, %{public}f, %{public}f",
54 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference);
55 return std::nullopt;
56 }
57 return static_cast<float>(result);
58 }
59
ConvertToPx(const std::optional<CalcLength> & value,const ScaleProperty & scaleProperty,float percentReference)60 std::optional<float> ConvertToPx(
61 const std::optional<CalcLength>& value, const ScaleProperty& scaleProperty, float percentReference)
62 {
63 if (!value) {
64 return std::nullopt;
65 }
66 double result = -1.0;
67 if (!value.value().NormalizeToPx(
68 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
69 LOGE("optional: fail to Convert CalcDimension To Px: %{public}f, %{public}f, %{public}f, %{public}f",
70 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference);
71 return std::nullopt;
72 }
73 return static_cast<float>(result);
74 }
75
ConvertToPx(const Dimension & dimension,const ScaleProperty & scaleProperty,float percentReference)76 std::optional<float> ConvertToPx(const Dimension& dimension, const ScaleProperty& scaleProperty, float percentReference)
77 {
78 double result = -1.0;
79 if (!dimension.NormalizeToPx(
80 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
81 LOGE("fail to Convert dimension To Px: %{public}f, %{public}f, %{public}f, %{public}f", scaleProperty.vpScale,
82 scaleProperty.fpScale, scaleProperty.lpxScale, percentReference);
83 return std::nullopt;
84 }
85 return static_cast<float>(result);
86 }
87
ConvertToPx(const std::optional<Dimension> & dimension,const ScaleProperty & scaleProperty,float percentReference)88 std::optional<float> ConvertToPx(
89 const std::optional<Dimension>& dimension, const ScaleProperty& scaleProperty, float percentReference)
90 {
91 if (!dimension) {
92 return std::nullopt;
93 }
94 double result = -1.0;
95 if (!dimension.value().NormalizeToPx(
96 scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
97 LOGE("fail to Convert dimension To Px: %{public}f, %{public}f, %{public}f, %{public}f", scaleProperty.vpScale,
98 scaleProperty.fpScale, scaleProperty.lpxScale, percentReference);
99 return std::nullopt;
100 }
101 return static_cast<float>(result);
102 }
103
ConstrainSize(const SizeF & size,const SizeF & minSize,const SizeF & maxSize)104 SizeF ConstrainSize(const SizeF& size, const SizeF& minSize, const SizeF& maxSize)
105 {
106 float height = std::max(minSize.Height(), size.Height());
107 if (maxSize.Height() > 0) {
108 height = std::min(maxSize.Height(), height);
109 }
110 float width = std::max(minSize.Width(), size.Width());
111 if (maxSize.Width() > 0) {
112 width = std::min(maxSize.Width(), width);
113 }
114 return { width, height };
115 }
116
ConvertToPaddingPropertyF(const std::unique_ptr<PaddingProperty> & padding,const ScaleProperty & scaleProperty,float percentReference)117 PaddingPropertyF ConvertToPaddingPropertyF(
118 const std::unique_ptr<PaddingProperty>& padding, const ScaleProperty& scaleProperty, float percentReference)
119 {
120 if (!padding) {
121 return {};
122 }
123 return ConvertToPaddingPropertyF(*padding, scaleProperty, percentReference);
124 }
125
ConvertToPaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,float percentReference)126 PaddingPropertyF ConvertToPaddingPropertyF(
127 const PaddingProperty& padding, const ScaleProperty& scaleProperty, float percentReference)
128 {
129 auto left = ConvertToPx(padding.left, scaleProperty, percentReference);
130 auto right = ConvertToPx(padding.right, scaleProperty, percentReference);
131 auto top = ConvertToPx(padding.top, scaleProperty, percentReference);
132 auto bottom = ConvertToPx(padding.bottom, scaleProperty, percentReference);
133 return PaddingPropertyF { left, right, top, bottom };
134 }
135
ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty> & margin,const ScaleProperty & scaleProperty,float percentReference)136 MarginPropertyF ConvertToMarginPropertyF(
137 const std::unique_ptr<MarginProperty>& margin, const ScaleProperty& scaleProperty, float percentReference)
138 {
139 return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference);
140 }
141
ConvertToMarginPropertyF(const MarginProperty & margin,const ScaleProperty & scaleProperty,float percentReference)142 MarginPropertyF ConvertToMarginPropertyF(
143 const MarginProperty& margin, const ScaleProperty& scaleProperty, float percentReference)
144 {
145 return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference);
146 }
147
ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty> & borderWidth,const ScaleProperty & scaleProperty,float percentReference)148 BorderWidthPropertyF ConvertToBorderWidthPropertyF(
149 const std::unique_ptr<BorderWidthProperty>& borderWidth, const ScaleProperty& scaleProperty, float percentReference)
150 {
151 if (!borderWidth) {
152 return {};
153 }
154 return ConvertToBorderWidthPropertyF(*borderWidth, scaleProperty, percentReference);
155 }
156
ConvertToBorderWidthPropertyF(const BorderWidthProperty & borderWidth,const ScaleProperty & scaleProperty,float percentReference)157 BorderWidthPropertyF ConvertToBorderWidthPropertyF(
158 const BorderWidthProperty& borderWidth, const ScaleProperty& scaleProperty, float percentReference)
159 {
160 auto left = ConvertToPx(borderWidth.leftDimen, scaleProperty, percentReference);
161 auto right = ConvertToPx(borderWidth.rightDimen, scaleProperty, percentReference);
162 auto top = ConvertToPx(borderWidth.topDimen, scaleProperty, percentReference);
163 auto bottom = ConvertToPx(borderWidth.bottomDimen, scaleProperty, percentReference);
164
165 return BorderWidthPropertyF { left, top, right, bottom };
166 }
167
UpdatePaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,const SizeF & selfSize,PaddingPropertyF & paddingValue)168 void UpdatePaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, const SizeF& selfSize,
169 PaddingPropertyF& paddingValue)
170 {
171 auto left = ConvertToPx(padding.left, scaleProperty, selfSize.Width());
172 auto right = ConvertToPx(padding.right, scaleProperty, selfSize.Width());
173 auto top = ConvertToPx(padding.top, scaleProperty, selfSize.Height());
174 auto bottom = ConvertToPx(padding.bottom, scaleProperty, selfSize.Height());
175 if (left.has_value()) {
176 paddingValue.left = left;
177 }
178 if (right.has_value()) {
179 paddingValue.right = right;
180 }
181 if (top.has_value()) {
182 paddingValue.top = top;
183 }
184 if (bottom.has_value()) {
185 paddingValue.bottom = bottom;
186 }
187 }
188
AddPaddingToSize(const PaddingPropertyF & padding,SizeF & size)189 void AddPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
190 {
191 size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
192 }
193
MinusPaddingToSize(const PaddingPropertyF & padding,SizeF & size)194 void MinusPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
195 {
196 size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
197 }
198
AddPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)199 void AddPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
200 {
201 size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
202 }
203
MinusPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)204 void MinusPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
205 {
206 size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
207 }
208
GetMainAxisOffset(const OffsetF & offset,Axis axis)209 float GetMainAxisOffset(const OffsetF& offset, Axis axis)
210 {
211 return axis == Axis::HORIZONTAL ? offset.GetX() : offset.GetY();
212 }
213
GetMainAxisSize(const SizeF & size,Axis axis)214 float GetMainAxisSize(const SizeF& size, Axis axis)
215 {
216 return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
217 }
218
GetCrossAxisSize(const SizeF & size,Axis axis)219 float GetCrossAxisSize(const SizeF& size, Axis axis)
220 {
221 return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
222 }
223
SetCrossAxisSize(float value,Axis axis,SizeF & size)224 void SetCrossAxisSize(float value, Axis axis, SizeF& size)
225 {
226 if (axis == Axis::VERTICAL) {
227 size.SetWidth(value);
228 return;
229 }
230 size.SetHeight(value);
231 }
232
GetMainAxisSize(const OptionalSizeF & size,Axis axis)233 std::optional<float> GetMainAxisSize(const OptionalSizeF& size, Axis axis)
234 {
235 return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
236 }
237
GetCrossAxisSize(const OptionalSizeF & size,Axis axis)238 std::optional<float> GetCrossAxisSize(const OptionalSizeF& size, Axis axis)
239 {
240 return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
241 }
242
SetCrossAxisSize(float value,Axis axis,OptionalSizeF & size)243 void SetCrossAxisSize(float value, Axis axis, OptionalSizeF& size)
244 {
245 if (axis == Axis::VERTICAL) {
246 size.SetWidth(value);
247 return;
248 }
249 size.SetHeight(value);
250 }
251
SetMainAxisSize(float value,Axis axis,OptionalSizeF & size)252 void SetMainAxisSize(float value, Axis axis, OptionalSizeF& size)
253 {
254 if (axis == Axis::VERTICAL) {
255 size.SetHeight(value);
256 return;
257 }
258 size.SetWidth(value);
259 }
260
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool usingMaxSize)261 SizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool usingMaxSize)
262 {
263 auto optional = CreateIdealSize(layoutConstraint, axis, measureType);
264 if (usingMaxSize) {
265 optional.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
266 } else {
267 optional.UpdateIllegalSizeWithCheck(layoutConstraint.minSize);
268 }
269 return optional.ConvertToSizeT();
270 }
271
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType)272 OptionalSizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType)
273 {
274 OptionalSizeF idealSize;
275 do {
276 // Use idea size first if it is valid.
277 idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
278 if (idealSize.IsValid()) {
279 break;
280 }
281
282 if (measureType == MeasureType::MATCH_PARENT) {
283 idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
284 idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
285 break;
286 }
287
288 if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
289 auto selfSize = GetCrossAxisSize(idealSize, axis);
290 if (!selfSize) {
291 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
292 if (parentCrossSize) {
293 SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
294 } else {
295 parentCrossSize = GetCrossAxisSize(layoutConstraint.maxSize, axis);
296 SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
297 }
298 }
299 break;
300 }
301
302 if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
303 auto selfSize = GetMainAxisSize(idealSize, axis);
304 auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
305 if (!selfSize) {
306 if (parentMainSize) {
307 SetMainAxisSize(parentMainSize.value(), axis, idealSize);
308 } else {
309 parentMainSize = GetMainAxisSize(layoutConstraint.maxSize, axis);
310 SetMainAxisSize(parentMainSize.value(), axis, idealSize);
311 }
312 }
313 break;
314 }
315 } while (false);
316 return idealSize;
317 }
318
CreateIdealSizeByPercentRef(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool needToConstrain)319 OptionalSizeF CreateIdealSizeByPercentRef(
320 const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain)
321 {
322 OptionalSizeF idealSize;
323 do {
324 // Use idea size first if it is valid.
325 idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
326 if (idealSize.IsValid()) {
327 break;
328 }
329
330 if (measureType == MeasureType::MATCH_PARENT) {
331 idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
332 idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.percentReference);
333 break;
334 }
335
336 if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
337 auto selfSize = GetCrossAxisSize(idealSize, axis);
338 if (!selfSize) {
339 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
340 if (parentCrossSize) {
341 SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
342 } else {
343 parentCrossSize = GetCrossAxisSize(layoutConstraint.percentReference, axis);
344 SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
345 }
346 }
347 break;
348 }
349
350 if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
351 auto selfSize = GetMainAxisSize(idealSize, axis);
352 auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
353 if (!selfSize) {
354 if (parentMainSize) {
355 SetMainAxisSize(parentMainSize.value(), axis, idealSize);
356 } else {
357 parentMainSize = GetMainAxisSize(layoutConstraint.percentReference, axis);
358 SetMainAxisSize(parentMainSize.value(), axis, idealSize);
359 }
360 }
361 break;
362 }
363 } while (false);
364 if (needToConstrain) {
365 idealSize.Constrain(layoutConstraint.minSize, layoutConstraint.maxSize,
366 PipelineBase::GetCurrentContext() &&
367 PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN);
368 }
369 return idealSize;
370 }
371
CreateChildrenConstraint(SizeF & size,const PaddingPropertyF & padding)372 void CreateChildrenConstraint(SizeF& size, const PaddingPropertyF& padding)
373 {
374 float width = 0;
375 float height = 0;
376
377 float paddingLeft = padding.left.has_value() ? padding.left.value() : 0;
378 float paddingRight = padding.right.has_value() ? padding.right.value() : 0;
379 float paddingTop = padding.top.has_value() ? padding.top.value() : 0;
380 float paddingBottom = padding.bottom.has_value() ? padding.bottom.value() : 0;
381 width += (paddingLeft + paddingRight);
382 height += (paddingTop + paddingBottom);
383
384 size.SetHeight(size.Height() - height);
385 size.SetWidth(size.Width() - width);
386 }
387 } // namespace OHOS::Ace::NG
388