• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/common/ace_application_info.h"
19 #include "core/pipeline/pipeline_base.h"
20 
21 namespace OHOS::Ace::NG {
22 namespace {
23 const static int32_t PLATFORM_VERSION_TEN = 10;
24 }
25 
ConvertToSize(const CalcSize & size,const ScaleProperty & scaleProperty,const SizeF & percentReference,const std::pair<std::vector<std::string>,std::vector<std::string>> & calcRpnexp)26 SizeF ConvertToSize(const CalcSize& size, const ScaleProperty& scaleProperty, const SizeF& percentReference,
27     const std::pair<std::vector<std::string>, std::vector<std::string>>& calcRpnexp)
28 {
29     auto width = ConvertToPx(size.Width(), scaleProperty, percentReference.Width(), calcRpnexp.first);
30     auto height = ConvertToPx(size.Height(), scaleProperty, percentReference.Height(), calcRpnexp.second);
31     return { width.value_or(-1.0f), height.value_or(-1.0f) };
32 }
33 
ConvertToOptionalSize(const CalcSize & size,const ScaleProperty & scaleProperty,const SizeF & percentReference,const std::pair<std::vector<std::string>,std::vector<std::string>> & calcRpnexp)34 OptionalSizeF ConvertToOptionalSize(const CalcSize& size, const ScaleProperty& scaleProperty,
35     const SizeF& percentReference, const std::pair<std::vector<std::string>, std::vector<std::string>>& calcRpnexp)
36 {
37     auto width = ConvertToPx(size.Width(), scaleProperty, percentReference.Width(), calcRpnexp.first);
38     auto height = ConvertToPx(size.Height(), scaleProperty, percentReference.Height(), calcRpnexp.second);
39     return { width, height };
40 }
41 
ConvertToPx(const CalcLength & value,const ScaleProperty & scaleProperty,float percentReference,const std::vector<std::string> & rpnexp)42 std::optional<float> ConvertToPx(const CalcLength& value, const ScaleProperty& scaleProperty, float percentReference,
43     const std::vector<std::string>& rpnexp)
44 {
45     double result = -1.0;
46     if (!value.NormalizeToPx(
47         scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result, rpnexp)) {
48         return std::nullopt;
49     }
50     return static_cast<float>(result);
51 }
52 
ConvertToPx(const std::optional<CalcLength> & value,const ScaleProperty & scaleProperty,float percentReference,const std::vector<std::string> & rpnexp)53 std::optional<float> ConvertToPx(const std::optional<CalcLength>& value, const ScaleProperty& scaleProperty,
54     float percentReference, const std::vector<std::string>& rpnexp)
55 {
56     if (!value) {
57         return std::nullopt;
58     }
59     double result = -1.0;
60     if (!value.value().NormalizeToPx(
61             scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result, rpnexp)) {
62         return std::nullopt;
63     }
64     return static_cast<float>(result);
65 }
66 
ConvertToPx(const Dimension & dimension,const ScaleProperty & scaleProperty,float percentReference)67 std::optional<float> ConvertToPx(const Dimension& dimension, const ScaleProperty& scaleProperty, float percentReference)
68 {
69     double result = -1.0;
70     if (!dimension.NormalizeToPx(
71             scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
72         return std::nullopt;
73     }
74     return static_cast<float>(result);
75 }
76 
ConvertToPx(const std::optional<Dimension> & dimension,const ScaleProperty & scaleProperty,float percentReference)77 std::optional<float> ConvertToPx(
78     const std::optional<Dimension>& dimension, const ScaleProperty& scaleProperty, float percentReference)
79 {
80     if (!dimension) {
81         return std::nullopt;
82     }
83     double result = -1.0;
84     if (!dimension.value().NormalizeToPx(
85             scaleProperty.vpScale, scaleProperty.fpScale, scaleProperty.lpxScale, percentReference, result)) {
86         return std::nullopt;
87     }
88     return static_cast<float>(result);
89 }
90 
ConstrainSize(const SizeF & size,const SizeF & minSize,const SizeF & maxSize)91 SizeF ConstrainSize(const SizeF& size, const SizeF& minSize, const SizeF& maxSize)
92 {
93     float height = std::max(minSize.Height(), size.Height());
94     if (maxSize.Height() > 0) {
95         height = std::min(maxSize.Height(), height);
96     }
97     float width = std::max(minSize.Width(), size.Width());
98     if (maxSize.Width() > 0) {
99         width = std::min(maxSize.Width(), width);
100     }
101     return { width, height };
102 }
103 
ConvertToPaddingPropertyF(const std::unique_ptr<PaddingProperty> & padding,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel,bool nonNegative)104 PaddingPropertyF ConvertToPaddingPropertyF(const std::unique_ptr<PaddingProperty>& padding,
105     const ScaleProperty& scaleProperty, float percentReference, bool roundPixel, bool nonNegative)
106 {
107     if (!padding) {
108         return {};
109     }
110     return ConvertToPaddingPropertyF(*padding, scaleProperty, percentReference, roundPixel, nonNegative);
111 }
112 
ConvertToPaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel,bool nonNegative)113 PaddingPropertyF ConvertToPaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty,
114     float percentReference, bool roundPixel, bool nonNegative)
115 {
116     auto left = ConvertToPx(padding.left, scaleProperty, percentReference);
117     auto right = ConvertToPx(padding.right, scaleProperty, percentReference);
118     auto top = ConvertToPx(padding.top, scaleProperty, percentReference);
119     auto bottom = ConvertToPx(padding.bottom, scaleProperty, percentReference);
120     bool versionSatisfy =
121         AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE);
122     if (roundPixel && versionSatisfy) {
123         if (left.has_value()) {
124             left = floor(left.value());
125         }
126         if (right.has_value()) {
127             right = floor(right.value());
128         }
129         if (top.has_value()) {
130             top = floor(top.value());
131         }
132         if (bottom.has_value()) {
133             bottom = floor(bottom.value());
134         }
135     }
136     if (nonNegative && versionSatisfy) {
137         if (left.has_value()) {
138             left = std::max(left.value(), 0.0f);
139         }
140         if (right.has_value()) {
141             right = std::max(right.value(), 0.0f);
142         }
143         if (top.has_value()) {
144             top = std::max(top.value(), 0.0f);
145         }
146         if (bottom.has_value()) {
147             bottom = std::max(bottom.value(), 0.0f);
148         }
149     }
150     return PaddingPropertyF { left, right, top, bottom };
151 }
152 
ConvertWithResidueToPaddingPropertyF(const std::unique_ptr<PaddingProperty> & padding,const ScaleProperty & scaleProperty,const PaddingPropertyF & fract,float percentReference,bool nonNegative)153 PaddingPropertyF ConvertWithResidueToPaddingPropertyF(const std::unique_ptr<PaddingProperty>& padding,
154     const ScaleProperty& scaleProperty, const PaddingPropertyF& fract, float percentReference, bool nonNegative)
155 {
156     if (!padding) {
157         return {};
158     }
159     return ConvertWithResidueToPaddingPropertyF(*padding, scaleProperty, fract, percentReference, nonNegative);
160 }
161 
ConvertWithResidueToPaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,const PaddingPropertyF & fract,float percentReference,bool nonNegative)162 PaddingPropertyF ConvertWithResidueToPaddingPropertyF(const PaddingProperty& padding,
163     const ScaleProperty& scaleProperty, const PaddingPropertyF& fract, float percentReference, bool nonNegative)
164 {
165     auto left = ConvertToPx(padding.left, scaleProperty, percentReference);
166     auto right = ConvertToPx(padding.right, scaleProperty, percentReference);
167     auto top = ConvertToPx(padding.top, scaleProperty, percentReference);
168     auto bottom = ConvertToPx(padding.bottom, scaleProperty, percentReference);
169     if (left.has_value()) {
170         left = floor(left.value() + fract.left.value_or(0.0f));
171     }
172     if (right.has_value()) {
173         right = floor(right.value() + fract.right.value_or(0.0f));
174     }
175     if (top.has_value()) {
176         top = floor(top.value() + fract.top.value_or(0.0f));
177     }
178     if (bottom.has_value()) {
179         bottom = floor(bottom.value() + fract.bottom.value_or(0.0f));
180     }
181     if (nonNegative) {
182         if (left.has_value()) {
183             left = std::max(left.value(), 0.0f);
184         }
185         if (right.has_value()) {
186             right = std::max(right.value(), 0.0f);
187         }
188         if (top.has_value()) {
189             top = std::max(top.value(), 0.0f);
190         }
191         if (bottom.has_value()) {
192             bottom = std::max(bottom.value(), 0.0f);
193         }
194     }
195     return PaddingPropertyF { left, right, top, bottom };
196 }
197 
ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty> & margin,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)198 MarginPropertyF ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty>& margin,
199     const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
200 {
201     return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference, roundPixel);
202 }
203 
ConvertToMarginPropertyF(const MarginProperty & margin,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)204 MarginPropertyF ConvertToMarginPropertyF(
205     const MarginProperty& margin, const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
206 {
207     return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference, roundPixel);
208 }
209 
ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty> & borderWidth,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)210 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty>& borderWidth,
211     const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
212 {
213     if (!borderWidth) {
214         return {};
215     }
216     return ConvertToBorderWidthPropertyF(*borderWidth, scaleProperty, percentReference, roundPixel);
217 }
218 
ConvertToBorderWidthPropertyF(const BorderWidthProperty & borderWidth,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)219 BorderWidthPropertyF ConvertToBorderWidthPropertyF(
220     const BorderWidthProperty& borderWidth, const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
221 {
222     auto left = ConvertToPx(borderWidth.leftDimen, scaleProperty, percentReference);
223     auto right = ConvertToPx(borderWidth.rightDimen, scaleProperty, percentReference);
224     auto top = ConvertToPx(borderWidth.topDimen, scaleProperty, percentReference);
225     auto bottom = ConvertToPx(borderWidth.bottomDimen, scaleProperty, percentReference);
226     if (roundPixel && AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
227         if (left.has_value()) {
228             left = (GreatNotEqualCustomPrecision(left.value(), 1.0f) || NearEqual(left.value(), 0.0f))
229                        ? floor(left.value())
230                        : 1.0f;
231         }
232         if (right.has_value()) {
233             right = (GreatNotEqualCustomPrecision(right.value(), 1.0f) || NearEqual(right.value(), 0.0f))
234                         ? floor(right.value())
235                         : 1.0f;
236         }
237         if (top.has_value()) {
238             top = (GreatNotEqualCustomPrecision(top.value(), 1.0f) || NearEqual(top.value(), 0.0f)) ? floor(top.value())
239                                                                                                     : 1.0f;
240         }
241         if (bottom.has_value()) {
242             bottom = (GreatNotEqualCustomPrecision(bottom.value(), 1.0f) || NearEqual(bottom.value(), 0.0f))
243                          ? floor(bottom.value())
244                          : 1.0f;
245         }
246     }
247     return BorderWidthPropertyF { left, top, right, bottom };
248 }
249 
UpdatePaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,const SizeF & selfSize,PaddingPropertyF & paddingValue)250 void UpdatePaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, const SizeF& selfSize,
251     PaddingPropertyF& paddingValue)
252 {
253     auto left = ConvertToPx(padding.left, scaleProperty, selfSize.Width());
254     auto right = ConvertToPx(padding.right, scaleProperty, selfSize.Width());
255     auto top = ConvertToPx(padding.top, scaleProperty, selfSize.Height());
256     auto bottom = ConvertToPx(padding.bottom, scaleProperty, selfSize.Height());
257     if (left.has_value()) {
258         paddingValue.left = left;
259     }
260     if (right.has_value()) {
261         paddingValue.right = right;
262     }
263     if (top.has_value()) {
264         paddingValue.top = top;
265     }
266     if (bottom.has_value()) {
267         paddingValue.bottom = bottom;
268     }
269 }
270 
AddPaddingToSize(const PaddingPropertyF & padding,SizeF & size)271 void AddPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
272 {
273     size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
274 }
275 
MinusPaddingToSize(const PaddingPropertyF & padding,SizeF & size)276 void MinusPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
277 {
278     size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
279 }
280 
MinusPaddingToNonNegativeSize(const PaddingPropertyF & padding,SizeF & size)281 void MinusPaddingToNonNegativeSize(const PaddingPropertyF& padding, SizeF& size)
282 {
283     size.MinusPaddingToNonNegative(padding.left, padding.right, padding.top, padding.bottom);
284 }
285 
AddPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)286 void AddPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
287 {
288     size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
289 }
290 
MinusPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)291 void MinusPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
292 {
293     size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
294 }
295 
AdjacentExpandToRect(RectF & adjustingRect,PaddingPropertyF & frameExpand,RectF & frameRect)296 PaddingPropertyF AdjacentExpandToRect(RectF& adjustingRect, PaddingPropertyF& frameExpand, RectF& frameRect)
297 {
298     PaddingPropertyF filtered;
299     if (NearEqual(adjustingRect.Left(), frameRect.Left())) {
300         filtered.left = frameExpand.left;
301     }
302     if (NearEqual(adjustingRect.Top(), frameRect.Top())) {
303         filtered.top = frameExpand.top;
304     }
305     if (NearEqual(adjustingRect.Right(), frameRect.Right())) {
306         filtered.right = frameExpand.right;
307     }
308     if (NearEqual(adjustingRect.Bottom(), frameRect.Bottom())) {
309         filtered.bottom = frameExpand.bottom;
310     }
311     return filtered;
312 }
313 
GetMainAxisOffset(const OffsetF & offset,Axis axis)314 float GetMainAxisOffset(const OffsetF& offset, Axis axis)
315 {
316     return axis == Axis::HORIZONTAL ? offset.GetX() : offset.GetY();
317 }
318 
GetMainAxisSize(const SizeF & size,Axis axis)319 float GetMainAxisSize(const SizeF& size, Axis axis)
320 {
321     return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
322 }
323 
GetCrossAxisSize(const SizeF & size,Axis axis)324 float GetCrossAxisSize(const SizeF& size, Axis axis)
325 {
326     return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
327 }
328 
SetCrossAxisSize(float value,Axis axis,SizeF & size)329 void SetCrossAxisSize(float value, Axis axis, SizeF& size)
330 {
331     if (axis == Axis::VERTICAL) {
332         size.SetWidth(value);
333         return;
334     }
335     size.SetHeight(value);
336 }
337 
GetMainAxisSize(const OptionalSizeF & size,Axis axis)338 std::optional<float> GetMainAxisSize(const OptionalSizeF& size, Axis axis)
339 {
340     return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
341 }
342 
GetCrossAxisSize(const OptionalSizeF & size,Axis axis)343 std::optional<float> GetCrossAxisSize(const OptionalSizeF& size, Axis axis)
344 {
345     return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
346 }
347 
SetCrossAxisSize(float value,Axis axis,OptionalSizeF & size)348 void SetCrossAxisSize(float value, Axis axis, OptionalSizeF& size)
349 {
350     if (axis == Axis::VERTICAL) {
351         size.SetWidth(value);
352         return;
353     }
354     size.SetHeight(value);
355 }
356 
SetMainAxisSize(float value,Axis axis,OptionalSizeF & size)357 void SetMainAxisSize(float value, Axis axis, OptionalSizeF& size)
358 {
359     if (axis == Axis::VERTICAL) {
360         size.SetHeight(value);
361         return;
362     }
363     size.SetWidth(value);
364 }
365 
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool usingMaxSize)366 SizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool usingMaxSize)
367 {
368     auto optional = CreateIdealSize(layoutConstraint, axis, measureType);
369     if (usingMaxSize) {
370         optional.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
371     } else {
372         optional.UpdateIllegalSizeWithCheck(layoutConstraint.minSize);
373     }
374     return optional.ConvertToSizeT();
375 }
376 
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType)377 OptionalSizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType)
378 {
379     OptionalSizeF idealSize;
380     do {
381         // Use idea size first if it is valid.
382         idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
383         if (idealSize.IsValid()) {
384             break;
385         }
386 
387         if (measureType == MeasureType::MATCH_PARENT) {
388             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
389             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
390             break;
391         }
392 
393         if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
394             auto selfSize = GetCrossAxisSize(idealSize, axis);
395             if (!selfSize) {
396                 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
397                 if (parentCrossSize) {
398                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
399                 } else {
400                     parentCrossSize = GetCrossAxisSize(layoutConstraint.maxSize, axis);
401                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
402                 }
403             }
404             break;
405         }
406 
407         if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
408             auto selfSize = GetMainAxisSize(idealSize, axis);
409             auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
410             if (!selfSize) {
411                 if (parentMainSize) {
412                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
413                 } else {
414                     parentMainSize = GetMainAxisSize(layoutConstraint.maxSize, axis);
415                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
416                 }
417             }
418             break;
419         }
420     } while (false);
421     return idealSize;
422 }
423 
UpdateOptionSizeByCalcLayoutConstraint(const OptionalSize<float> & frameSize,const std::unique_ptr<MeasureProperty> & calcLayoutConstraint,const SizeT<float> percentReference)424 OptionalSizeF UpdateOptionSizeByCalcLayoutConstraint(const OptionalSize<float>& frameSize,
425     const std::unique_ptr<MeasureProperty>& calcLayoutConstraint, const SizeT<float> percentReference)
426 {
427     OptionalSizeF finalSize(frameSize.Width(), frameSize.Height());
428     if (!calcLayoutConstraint) {
429         return finalSize;
430     } else {
431         UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(
432             finalSize, calcLayoutConstraint->maxSize, percentReference, true);
433         UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(
434             finalSize, calcLayoutConstraint->minSize, percentReference, false);
435     }
436     return finalSize;
437 }
438 
UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF & frameSize,const std::optional<CalcSize> & calcLayoutConstraintMaxMinSize,const SizeT<float> percentReference,bool IsMaxSize)439 void UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize,
440     const std::optional<CalcSize>& calcLayoutConstraintMaxMinSize, const SizeT<float> percentReference, bool IsMaxSize)
441 {
442     auto scaleProperty = ScaleProperty::CreateScaleProperty();
443     if (!calcLayoutConstraintMaxMinSize.has_value()) {
444         return;
445     }
446     if (calcLayoutConstraintMaxMinSize->Width().has_value()) {
447         auto maxWidthPx = ConvertToPx(calcLayoutConstraintMaxMinSize->Width(), scaleProperty, percentReference.Width());
448         if (maxWidthPx.has_value()) {
449             if (IsMaxSize) {
450                 frameSize.SetWidth(std::min(maxWidthPx.value(), frameSize.Width().value_or(maxWidthPx.value())));
451             } else {
452                 frameSize.SetWidth(std::max(maxWidthPx.value(), frameSize.Width().value_or(maxWidthPx.value())));
453             }
454         }
455     }
456     if (calcLayoutConstraintMaxMinSize->Height().has_value()) {
457         auto maxHeightPx =
458             ConvertToPx(calcLayoutConstraintMaxMinSize->Height(), scaleProperty, percentReference.Height());
459         if (maxHeightPx.has_value()) {
460             if (IsMaxSize) {
461                 frameSize.SetHeight(std::min(maxHeightPx.value(), frameSize.Height().value_or(maxHeightPx.value())));
462             } else {
463                 frameSize.SetHeight(std::max(maxHeightPx.value(), frameSize.Height().value_or(maxHeightPx.value())));
464             }
465         }
466     }
467 }
468 
UpdateConstraintByRawConstraint(SizeF & validMinSize,SizeF & validMaxSize,const std::unique_ptr<MeasureProperty> & rawConstraint)469 void UpdateConstraintByRawConstraint(SizeF& validMinSize, SizeF& validMaxSize,
470     const std::unique_ptr<MeasureProperty>& rawConstraint)
471 {
472     if (rawConstraint->minSize) {
473         if (!rawConstraint->minSize.value().Width()) {
474             validMinSize.SetWidth(-1.0f);
475         }
476         if (!rawConstraint->minSize.value().Height()) {
477             validMinSize.SetHeight(-1.0f);
478         }
479     } else {
480         validMinSize = SizeF(-1.0f, -1.0f);
481     }
482     if (rawConstraint->maxSize) {
483         if (!rawConstraint->maxSize.value().Width()) {
484             validMaxSize.SetWidth(-1.0f);
485         }
486         if (!rawConstraint->maxSize.value().Height()) {
487             validMaxSize.SetHeight(-1.0f);
488         }
489     } else {
490         validMaxSize = SizeF(-1.0f, -1.0f);
491     }
492 }
493 
ApplyConstraint(OptionalSizeF & idealSize,const LayoutConstraintF & layoutConstraint,const std::unique_ptr<MeasureProperty> & rawConstraint)494 void ApplyConstraint(OptionalSizeF& idealSize, const LayoutConstraintF& layoutConstraint,
495     const std::unique_ptr<MeasureProperty>& rawConstraint)
496 {
497     auto validMinSize = layoutConstraint.minSize;
498     auto validMaxSize = layoutConstraint.maxSize;
499     if (rawConstraint) {
500         UpdateConstraintByRawConstraint(validMinSize, validMaxSize, rawConstraint);
501     }
502     idealSize.Constrain(validMinSize, validMaxSize,
503         PipelineBase::GetCurrentContext() &&
504             PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN,
505         rawConstraint != nullptr);
506 }
507 
CreateIdealSizeByPercentRef(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool needToConstrain,const std::unique_ptr<MeasureProperty> & rawConstraint)508 OptionalSizeF CreateIdealSizeByPercentRef(
509     const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain,
510     const std::unique_ptr<MeasureProperty>& rawConstraint)
511 {
512     OptionalSizeF idealSize;
513     do {
514         // Use idea size first if it is valid.
515         idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
516         if (idealSize.IsValid()) {
517             break;
518         }
519 
520         if (measureType == MeasureType::MATCH_PARENT) {
521             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
522             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.percentReference);
523             break;
524         }
525 
526         if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
527             auto selfSize = GetCrossAxisSize(idealSize, axis);
528             if (!selfSize) {
529                 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
530                 if (parentCrossSize) {
531                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
532                 } else {
533                     parentCrossSize = GetCrossAxisSize(layoutConstraint.percentReference, axis);
534                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
535                 }
536             }
537             break;
538         }
539 
540         if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
541             auto selfSize = GetMainAxisSize(idealSize, axis);
542             auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
543             if (!selfSize) {
544                 if (parentMainSize) {
545                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
546                 } else {
547                     parentMainSize = GetMainAxisSize(layoutConstraint.percentReference, axis);
548                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
549                 }
550             }
551             break;
552         }
553     } while (false);
554     if (needToConstrain) {
555         ApplyConstraint(idealSize, layoutConstraint, rawConstraint);
556     }
557     return idealSize;
558 }
559 
ConstrainIdealSizeByLayoutPolicy(const LayoutConstraintF & layoutConstraint,LayoutCalPolicy widthLayoutPolicy,LayoutCalPolicy heightLayoutPolicy,Axis axis)560 OptionalSizeF ConstrainIdealSizeByLayoutPolicy(const LayoutConstraintF& layoutConstraint,
561     LayoutCalPolicy widthLayoutPolicy, LayoutCalPolicy heightLayoutPolicy, Axis axis)
562 {
563     bool isHorizontal = axis == Axis::HORIZONTAL;
564     bool mainAxisMatchParent = (isHorizontal ? widthLayoutPolicy : heightLayoutPolicy) == LayoutCalPolicy::MATCH_PARENT;
565     bool crossAxisMatchParent =
566         (isHorizontal ? heightLayoutPolicy : widthLayoutPolicy) == LayoutCalPolicy::MATCH_PARENT;
567     OptionalSizeF idealSize;
568     if (mainAxisMatchParent) {
569         auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
570         if (parentMainSize) {
571             SetMainAxisSize(parentMainSize.value(), axis, idealSize);
572         }
573     }
574     if (crossAxisMatchParent) {
575         auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
576         if (parentCrossSize) {
577             SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
578         }
579     }
580     return idealSize;
581 }
582 
CalcLayoutPolicySingleSide(const std::optional<NG::LayoutPolicyProperty> & childLayoutPolicy,const std::unique_ptr<MeasureProperty> & childCalcLayoutConstraint,const std::optional<LayoutConstraintF> & parentConstraint,const MagicItemProperty & magicItemProperty)583 OptionalSizeF CalcLayoutPolicySingleSide(const std::optional<NG::LayoutPolicyProperty>& childLayoutPolicy,
584     const std::unique_ptr<MeasureProperty>& childCalcLayoutConstraint,
585     const std::optional<LayoutConstraintF>& parentConstraint, const MagicItemProperty& magicItemProperty)
586 {
587     OptionalSizeF result;
588     if (!parentConstraint.has_value()) {
589         return result;
590     }
591     if (!childLayoutPolicy.has_value() || !childLayoutPolicy->IsMatch()) {
592         return result;
593     }
594     if (!childCalcLayoutConstraint ||
595         (!childCalcLayoutConstraint->selfIdealSize.has_value() && !childCalcLayoutConstraint->minSize.has_value())) {
596         return result;
597     }
598     auto isWidthPolicy = childLayoutPolicy->IsWidthMatch();
599     auto isHeightPolicy = childLayoutPolicy->IsHeightMatch();
600 
601     if (childCalcLayoutConstraint->selfIdealSize.has_value()) {
602         auto selfSize = ConvertToOptionalSize(childCalcLayoutConstraint->selfIdealSize.value(),
603             parentConstraint->scaleProperty, parentConstraint->percentReference);
604         if (isHeightPolicy && selfSize.Width().has_value()) {
605             result.SetWidth(selfSize.Width().value());
606         }
607         if (isWidthPolicy && selfSize.Height().has_value()) {
608             result.SetHeight(selfSize.Height().value());
609         }
610         if (magicItemProperty.HasAspectRatio()) {
611             auto aspectRatio = magicItemProperty.GetAspectRatioValue();
612             if (result.Width().has_value() && GreatNotEqual(aspectRatio, 0.0f)) {
613                 result.SetHeight(result.Width().value() / aspectRatio);
614             }
615         }
616         UpdateSingleSideByMaxOrMinCalcLayoutConstraint(
617             result, childCalcLayoutConstraint->maxSize, parentConstraint, true);
618         UpdateSingleSideByMaxOrMinCalcLayoutConstraint(
619             result, childCalcLayoutConstraint->minSize, parentConstraint, false);
620     } else if (childCalcLayoutConstraint->minSize.has_value()) {
621         auto minsize = ConvertToOptionalSize(childCalcLayoutConstraint->minSize.value(),
622             parentConstraint->scaleProperty, parentConstraint->percentReference);
623         if (isHeightPolicy && minsize.Width().has_value()) {
624             result.SetWidth(minsize.Width().value());
625         }
626         if (isWidthPolicy && minsize.Height().has_value()) {
627             result.SetHeight(minsize.Height().value());
628         }
629     }
630     return result;
631 }
632 
UpdateSingleSideByMaxOrMinCalcLayoutConstraint(OptionalSizeF & frameSize,const std::optional<CalcSize> & calcLayoutConstraintMaxMinSize,const std::optional<LayoutConstraintF> & parentConstraint,bool isMaxSize)633 void UpdateSingleSideByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize,
634     const std::optional<CalcSize>& calcLayoutConstraintMaxMinSize,
635     const std::optional<LayoutConstraintF>& parentConstraint, bool isMaxSize)
636 {
637     if (!calcLayoutConstraintMaxMinSize.has_value() || frameSize.IsNull()) {
638         return;
639     }
640     if (calcLayoutConstraintMaxMinSize->Width().has_value() && frameSize.Width().has_value()) {
641         auto maxWidthPx = ConvertToPx(calcLayoutConstraintMaxMinSize->Width(), parentConstraint->scaleProperty,
642             parentConstraint->percentReference.Width());
643         if (maxWidthPx.has_value()) {
644             if (isMaxSize) {
645                 frameSize.SetWidth(std::min(maxWidthPx.value(), frameSize.Width().value()));
646             } else {
647                 frameSize.SetWidth(std::max(maxWidthPx.value(), frameSize.Width().value()));
648             }
649         }
650     }
651     if (calcLayoutConstraintMaxMinSize->Height().has_value() && frameSize.Height().has_value()) {
652         auto maxHeightPx = ConvertToPx(calcLayoutConstraintMaxMinSize->Height(), parentConstraint->scaleProperty,
653             parentConstraint->percentReference.Height());
654         if (maxHeightPx.has_value()) {
655             if (isMaxSize) {
656                 frameSize.SetHeight(std::min(maxHeightPx.value(), frameSize.Height().value()));
657             } else {
658                 frameSize.SetHeight(std::max(maxHeightPx.value(), frameSize.Height().value()));
659             }
660         }
661     }
662 }
663 
CreateChildrenConstraint(SizeF & size,const PaddingPropertyF & padding)664 void CreateChildrenConstraint(SizeF& size, const PaddingPropertyF& padding)
665 {
666     float width = 0;
667     float height = 0;
668 
669     float paddingLeft = padding.left.has_value() ? padding.left.value() : 0;
670     float paddingRight = padding.right.has_value() ? padding.right.value() : 0;
671     float paddingTop = padding.top.has_value() ? padding.top.value() : 0;
672     float paddingBottom = padding.bottom.has_value() ? padding.bottom.value() : 0;
673     width += (paddingLeft + paddingRight);
674     height += (paddingTop + paddingBottom);
675 
676     size.SetHeight(size.Height() - height);
677     size.SetWidth(size.Width() - width);
678 }
679 
ConvertToCalcPaddingProperty(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)680 PaddingProperty ConvertToCalcPaddingProperty(const std::optional<CalcDimension>& top,
681     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
682     const std::optional<CalcDimension>& right)
683 {
684     PaddingProperty paddings;
685     if (top.has_value()) {
686         if (top.value().Unit() == DimensionUnit::CALC) {
687             paddings.top =
688                 NG::CalcLength(top.value().IsNonNegative() ? top.value().CalcValue() : CalcDimension().CalcValue());
689         } else {
690             paddings.top = NG::CalcLength(top.value().IsNonNegative() ? top.value() : CalcDimension());
691         }
692     }
693     if (bottom.has_value()) {
694         if (bottom.value().Unit() == DimensionUnit::CALC) {
695             paddings.bottom = NG::CalcLength(
696                 bottom.value().IsNonNegative() ? bottom.value().CalcValue() : CalcDimension().CalcValue());
697         } else {
698             paddings.bottom = NG::CalcLength(bottom.value().IsNonNegative() ? bottom.value() : CalcDimension());
699         }
700     }
701     if (left.has_value()) {
702         if (left.value().Unit() == DimensionUnit::CALC) {
703             paddings.left =
704                 NG::CalcLength(left.value().IsNonNegative() ? left.value().CalcValue() : CalcDimension().CalcValue());
705         } else {
706             paddings.left = NG::CalcLength(left.value().IsNonNegative() ? left.value() : CalcDimension());
707         }
708     }
709     if (right.has_value()) {
710         if (right.value().Unit() == DimensionUnit::CALC) {
711             paddings.right =
712                 NG::CalcLength(right.value().IsNonNegative() ? right.value().CalcValue() : CalcDimension().CalcValue());
713         } else {
714             paddings.right = NG::CalcLength(right.value().IsNonNegative() ? right.value() : CalcDimension());
715         }
716     }
717     return paddings;
718 }
719 } // namespace OHOS::Ace::NG
720