• 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 
ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty> & margin,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)153 MarginPropertyF ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty>& margin,
154     const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
155 {
156     return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference, roundPixel);
157 }
158 
ConvertToMarginPropertyF(const MarginProperty & margin,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)159 MarginPropertyF ConvertToMarginPropertyF(
160     const MarginProperty& margin, const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
161 {
162     return ConvertToPaddingPropertyF(margin, scaleProperty, percentReference, roundPixel);
163 }
164 
ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty> & borderWidth,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)165 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty>& borderWidth,
166     const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
167 {
168     if (!borderWidth) {
169         return {};
170     }
171     return ConvertToBorderWidthPropertyF(*borderWidth, scaleProperty, percentReference, roundPixel);
172 }
173 
ConvertToBorderWidthPropertyF(const BorderWidthProperty & borderWidth,const ScaleProperty & scaleProperty,float percentReference,bool roundPixel)174 BorderWidthPropertyF ConvertToBorderWidthPropertyF(
175     const BorderWidthProperty& borderWidth, const ScaleProperty& scaleProperty, float percentReference, bool roundPixel)
176 {
177     auto left = ConvertToPx(borderWidth.leftDimen, scaleProperty, percentReference);
178     auto right = ConvertToPx(borderWidth.rightDimen, scaleProperty, percentReference);
179     auto top = ConvertToPx(borderWidth.topDimen, scaleProperty, percentReference);
180     auto bottom = ConvertToPx(borderWidth.bottomDimen, scaleProperty, percentReference);
181     if (roundPixel && AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
182         if (left.has_value()) {
183             left = (GreatOrEqual(left.value(), 1.0f) || NearEqual(left.value(), 0.0f)) ? floor(left.value()) : 1.0f;
184         }
185         if (right.has_value()) {
186             right = (GreatOrEqual(right.value(), 1.0f) || NearEqual(right.value(), 0.0f)) ? floor(right.value()) : 1.0f;
187         }
188         if (top.has_value()) {
189             top = (GreatOrEqual(top.value(), 1.0f) || NearEqual(top.value(), 0.0f)) ? floor(top.value()) : 1.0f;
190         }
191         if (bottom.has_value()) {
192             bottom =
193                 (GreatOrEqual(bottom.value(), 1.0f) || NearEqual(bottom.value(), 0.0f)) ? floor(bottom.value()) : 1.0f;
194         }
195     }
196     return BorderWidthPropertyF { left, top, right, bottom };
197 }
198 
UpdatePaddingPropertyF(const PaddingProperty & padding,const ScaleProperty & scaleProperty,const SizeF & selfSize,PaddingPropertyF & paddingValue)199 void UpdatePaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, const SizeF& selfSize,
200     PaddingPropertyF& paddingValue)
201 {
202     auto left = ConvertToPx(padding.left, scaleProperty, selfSize.Width());
203     auto right = ConvertToPx(padding.right, scaleProperty, selfSize.Width());
204     auto top = ConvertToPx(padding.top, scaleProperty, selfSize.Height());
205     auto bottom = ConvertToPx(padding.bottom, scaleProperty, selfSize.Height());
206     if (left.has_value()) {
207         paddingValue.left = left;
208     }
209     if (right.has_value()) {
210         paddingValue.right = right;
211     }
212     if (top.has_value()) {
213         paddingValue.top = top;
214     }
215     if (bottom.has_value()) {
216         paddingValue.bottom = bottom;
217     }
218 }
219 
AddPaddingToSize(const PaddingPropertyF & padding,SizeF & size)220 void AddPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
221 {
222     size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
223 }
224 
MinusPaddingToSize(const PaddingPropertyF & padding,SizeF & size)225 void MinusPaddingToSize(const PaddingPropertyF& padding, SizeF& size)
226 {
227     size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
228 }
229 
MinusPaddingToNonNegativeSize(const PaddingPropertyF & padding,SizeF & size)230 void MinusPaddingToNonNegativeSize(const PaddingPropertyF& padding, SizeF& size)
231 {
232     size.MinusPaddingToNonNegative(padding.left, padding.right, padding.top, padding.bottom);
233 }
234 
AddPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)235 void AddPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
236 {
237     size.AddPadding(padding.left, padding.right, padding.top, padding.bottom);
238 }
239 
MinusPaddingToSize(const PaddingPropertyF & padding,OptionalSizeF & size)240 void MinusPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size)
241 {
242     size.MinusPadding(padding.left, padding.right, padding.top, padding.bottom);
243 }
244 
GetMainAxisOffset(const OffsetF & offset,Axis axis)245 float GetMainAxisOffset(const OffsetF& offset, Axis axis)
246 {
247     return axis == Axis::HORIZONTAL ? offset.GetX() : offset.GetY();
248 }
249 
GetMainAxisSize(const SizeF & size,Axis axis)250 float GetMainAxisSize(const SizeF& size, Axis axis)
251 {
252     return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
253 }
254 
GetCrossAxisSize(const SizeF & size,Axis axis)255 float GetCrossAxisSize(const SizeF& size, Axis axis)
256 {
257     return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
258 }
259 
SetCrossAxisSize(float value,Axis axis,SizeF & size)260 void SetCrossAxisSize(float value, Axis axis, SizeF& size)
261 {
262     if (axis == Axis::VERTICAL) {
263         size.SetWidth(value);
264         return;
265     }
266     size.SetHeight(value);
267 }
268 
GetMainAxisSize(const OptionalSizeF & size,Axis axis)269 std::optional<float> GetMainAxisSize(const OptionalSizeF& size, Axis axis)
270 {
271     return axis == Axis::HORIZONTAL ? size.Width() : size.Height();
272 }
273 
GetCrossAxisSize(const OptionalSizeF & size,Axis axis)274 std::optional<float> GetCrossAxisSize(const OptionalSizeF& size, Axis axis)
275 {
276     return axis == Axis::HORIZONTAL ? size.Height() : size.Width();
277 }
278 
SetCrossAxisSize(float value,Axis axis,OptionalSizeF & size)279 void SetCrossAxisSize(float value, Axis axis, OptionalSizeF& size)
280 {
281     if (axis == Axis::VERTICAL) {
282         size.SetWidth(value);
283         return;
284     }
285     size.SetHeight(value);
286 }
287 
SetMainAxisSize(float value,Axis axis,OptionalSizeF & size)288 void SetMainAxisSize(float value, Axis axis, OptionalSizeF& size)
289 {
290     if (axis == Axis::VERTICAL) {
291         size.SetHeight(value);
292         return;
293     }
294     size.SetWidth(value);
295 }
296 
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool usingMaxSize)297 SizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool usingMaxSize)
298 {
299     auto optional = CreateIdealSize(layoutConstraint, axis, measureType);
300     if (usingMaxSize) {
301         optional.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
302     } else {
303         optional.UpdateIllegalSizeWithCheck(layoutConstraint.minSize);
304     }
305     return optional.ConvertToSizeT();
306 }
307 
CreateIdealSize(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType)308 OptionalSizeF CreateIdealSize(const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType)
309 {
310     OptionalSizeF idealSize;
311     do {
312         // Use idea size first if it is valid.
313         idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
314         if (idealSize.IsValid()) {
315             break;
316         }
317 
318         if (measureType == MeasureType::MATCH_PARENT) {
319             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
320             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.maxSize);
321             break;
322         }
323 
324         if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
325             auto selfSize = GetCrossAxisSize(idealSize, axis);
326             if (!selfSize) {
327                 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
328                 if (parentCrossSize) {
329                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
330                 } else {
331                     parentCrossSize = GetCrossAxisSize(layoutConstraint.maxSize, axis);
332                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
333                 }
334             }
335             break;
336         }
337 
338         if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
339             auto selfSize = GetMainAxisSize(idealSize, axis);
340             auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
341             if (!selfSize) {
342                 if (parentMainSize) {
343                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
344                 } else {
345                     parentMainSize = GetMainAxisSize(layoutConstraint.maxSize, axis);
346                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
347                 }
348             }
349             break;
350         }
351     } while (false);
352     return idealSize;
353 }
354 
UpdateOptionSizeByCalcLayoutConstraint(const OptionalSize<float> & frameSize,const std::unique_ptr<MeasureProperty> & calcLayoutConstraint,const SizeT<float> percentReference)355 OptionalSizeF UpdateOptionSizeByCalcLayoutConstraint(const OptionalSize<float>& frameSize,
356     const std::unique_ptr<MeasureProperty>& calcLayoutConstraint, const SizeT<float> percentReference)
357 {
358     OptionalSizeF finalSize(frameSize.Width(), frameSize.Height());
359     if (!calcLayoutConstraint) {
360         return finalSize;
361     } else {
362         UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(
363             finalSize, calcLayoutConstraint->maxSize, percentReference, true);
364         UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(
365             finalSize, calcLayoutConstraint->minSize, percentReference, false);
366     }
367     return finalSize;
368 }
369 
UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF & frameSize,const std::optional<CalcSize> & calcLayoutConstraintMaxMinSize,const SizeT<float> percentReference,bool IsMaxSize)370 void UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize,
371     const std::optional<CalcSize>& calcLayoutConstraintMaxMinSize, const SizeT<float> percentReference, bool IsMaxSize)
372 {
373     auto scaleProperty = ScaleProperty::CreateScaleProperty();
374     if (!calcLayoutConstraintMaxMinSize.has_value()) {
375         return;
376     }
377     if (calcLayoutConstraintMaxMinSize->Width().has_value()) {
378         auto maxWidthPx = ConvertToPx(calcLayoutConstraintMaxMinSize->Width(), scaleProperty, percentReference.Width());
379         if (maxWidthPx.has_value()) {
380             if (IsMaxSize) {
381                 frameSize.SetWidth(std::min(maxWidthPx.value(), frameSize.Width().value_or(maxWidthPx.value())));
382             } else {
383                 frameSize.SetWidth(std::max(maxWidthPx.value(), frameSize.Width().value_or(maxWidthPx.value())));
384             }
385         }
386     }
387     if (calcLayoutConstraintMaxMinSize->Height().has_value()) {
388         auto maxHeightPx =
389             ConvertToPx(calcLayoutConstraintMaxMinSize->Height(), scaleProperty, percentReference.Height());
390         if (maxHeightPx.has_value()) {
391             if (IsMaxSize) {
392                 frameSize.SetHeight(std::min(maxHeightPx.value(), frameSize.Height().value_or(maxHeightPx.value())));
393             } else {
394                 frameSize.SetHeight(std::max(maxHeightPx.value(), frameSize.Height().value_or(maxHeightPx.value())));
395             }
396         }
397     }
398 }
399 
UpdateConstraintByRawConstraint(SizeF & validMinSize,SizeF & validMaxSize,const std::unique_ptr<MeasureProperty> & rawConstraint)400 void UpdateConstraintByRawConstraint(SizeF& validMinSize, SizeF& validMaxSize,
401     const std::unique_ptr<MeasureProperty>& rawConstraint)
402 {
403     if (rawConstraint->minSize) {
404         if (!rawConstraint->minSize.value().Width()) {
405             validMinSize.SetWidth(-1.0f);
406         }
407         if (!rawConstraint->minSize.value().Height()) {
408             validMinSize.SetHeight(-1.0f);
409         }
410     } else {
411         validMinSize = SizeF(-1.0f, -1.0f);
412     }
413     if (rawConstraint->maxSize) {
414         if (!rawConstraint->maxSize.value().Width()) {
415             validMaxSize.SetWidth(-1.0f);
416         }
417         if (!rawConstraint->maxSize.value().Height()) {
418             validMaxSize.SetHeight(-1.0f);
419         }
420     } else {
421         validMaxSize = SizeF(-1.0f, -1.0f);
422     }
423 }
424 
ApplyConstraint(OptionalSizeF & idealSize,const LayoutConstraintF & layoutConstraint,const std::unique_ptr<MeasureProperty> & rawConstraint)425 void ApplyConstraint(OptionalSizeF& idealSize, const LayoutConstraintF& layoutConstraint,
426     const std::unique_ptr<MeasureProperty>& rawConstraint)
427 {
428     auto validMinSize = layoutConstraint.minSize;
429     auto validMaxSize = layoutConstraint.maxSize;
430     if (rawConstraint) {
431         UpdateConstraintByRawConstraint(validMinSize, validMaxSize, rawConstraint);
432     }
433     idealSize.Constrain(validMinSize, validMaxSize,
434         PipelineBase::GetCurrentContext() &&
435             PipelineBase::GetCurrentContext()->GetMinPlatformVersion() >= PLATFORM_VERSION_TEN,
436         rawConstraint != nullptr);
437 }
438 
CreateIdealSizeByPercentRef(const LayoutConstraintF & layoutConstraint,Axis axis,MeasureType measureType,bool needToConstrain,const std::unique_ptr<MeasureProperty> & rawConstraint)439 OptionalSizeF CreateIdealSizeByPercentRef(
440     const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain,
441     const std::unique_ptr<MeasureProperty>& rawConstraint)
442 {
443     OptionalSizeF idealSize;
444     do {
445         // Use idea size first if it is valid.
446         idealSize.UpdateSizeWithCheck(layoutConstraint.selfIdealSize);
447         if (idealSize.IsValid()) {
448             break;
449         }
450 
451         if (measureType == MeasureType::MATCH_PARENT) {
452             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.parentIdealSize);
453             idealSize.UpdateIllegalSizeWithCheck(layoutConstraint.percentReference);
454             break;
455         }
456 
457         if (measureType == MeasureType::MATCH_PARENT_CROSS_AXIS) {
458             auto selfSize = GetCrossAxisSize(idealSize, axis);
459             if (!selfSize) {
460                 auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
461                 if (parentCrossSize) {
462                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
463                 } else {
464                     parentCrossSize = GetCrossAxisSize(layoutConstraint.percentReference, axis);
465                     SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
466                 }
467             }
468             break;
469         }
470 
471         if (measureType == MeasureType::MATCH_PARENT_MAIN_AXIS) {
472             auto selfSize = GetMainAxisSize(idealSize, axis);
473             auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
474             if (!selfSize) {
475                 if (parentMainSize) {
476                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
477                 } else {
478                     parentMainSize = GetMainAxisSize(layoutConstraint.percentReference, axis);
479                     SetMainAxisSize(parentMainSize.value(), axis, idealSize);
480                 }
481             }
482             break;
483         }
484     } while (false);
485     if (needToConstrain) {
486         ApplyConstraint(idealSize, layoutConstraint, rawConstraint);
487     }
488     return idealSize;
489 }
490 
ConstrainIdealSizeByLayoutPolicy(const LayoutConstraintF & layoutConstraint,uint8_t widthLayoutPolicy,uint8_t heightLayoutPolicy,Axis axis)491 OptionalSizeF ConstrainIdealSizeByLayoutPolicy(const LayoutConstraintF& layoutConstraint,
492     uint8_t widthLayoutPolicy, uint8_t heightLayoutPolicy, Axis axis)
493 {
494     bool isHorizontal = axis == Axis::HORIZONTAL;
495     bool mainAxisMatchParent = (isHorizontal ? widthLayoutPolicy : heightLayoutPolicy) ==
496                                static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT);
497     bool crossAxisMatchParent = (isHorizontal ? heightLayoutPolicy : widthLayoutPolicy) ==
498                                 static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT);
499     OptionalSizeF idealSize;
500     if (mainAxisMatchParent) {
501         auto parentMainSize = GetMainAxisSize(layoutConstraint.parentIdealSize, axis);
502         if (parentMainSize) {
503             SetMainAxisSize(parentMainSize.value(), axis, idealSize);
504         }
505     }
506     if (crossAxisMatchParent) {
507         auto parentCrossSize = GetCrossAxisSize(layoutConstraint.parentIdealSize, axis);
508         if (parentCrossSize) {
509             SetCrossAxisSize(parentCrossSize.value(), axis, idealSize);
510         }
511     }
512     return idealSize;
513 }
514 
CreateChildrenConstraint(SizeF & size,const PaddingPropertyF & padding)515 void CreateChildrenConstraint(SizeF& size, const PaddingPropertyF& padding)
516 {
517     float width = 0;
518     float height = 0;
519 
520     float paddingLeft = padding.left.has_value() ? padding.left.value() : 0;
521     float paddingRight = padding.right.has_value() ? padding.right.value() : 0;
522     float paddingTop = padding.top.has_value() ? padding.top.value() : 0;
523     float paddingBottom = padding.bottom.has_value() ? padding.bottom.value() : 0;
524     width += (paddingLeft + paddingRight);
525     height += (paddingTop + paddingBottom);
526 
527     size.SetHeight(size.Height() - height);
528     size.SetWidth(size.Width() - width);
529 }
530 
ConvertToCalcPaddingProperty(const std::optional<CalcDimension> & top,const std::optional<CalcDimension> & bottom,const std::optional<CalcDimension> & left,const std::optional<CalcDimension> & right)531 PaddingProperty ConvertToCalcPaddingProperty(const std::optional<CalcDimension>& top,
532     const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left,
533     const std::optional<CalcDimension>& right)
534 {
535     PaddingProperty paddings;
536     if (top.has_value()) {
537         if (top.value().Unit() == DimensionUnit::CALC) {
538             paddings.top =
539                 NG::CalcLength(top.value().IsNonNegative() ? top.value().CalcValue() : CalcDimension().CalcValue());
540         } else {
541             paddings.top = NG::CalcLength(top.value().IsNonNegative() ? top.value() : CalcDimension());
542         }
543     }
544     if (bottom.has_value()) {
545         if (bottom.value().Unit() == DimensionUnit::CALC) {
546             paddings.bottom = NG::CalcLength(
547                 bottom.value().IsNonNegative() ? bottom.value().CalcValue() : CalcDimension().CalcValue());
548         } else {
549             paddings.bottom = NG::CalcLength(bottom.value().IsNonNegative() ? bottom.value() : CalcDimension());
550         }
551     }
552     if (left.has_value()) {
553         if (left.value().Unit() == DimensionUnit::CALC) {
554             paddings.left =
555                 NG::CalcLength(left.value().IsNonNegative() ? left.value().CalcValue() : CalcDimension().CalcValue());
556         } else {
557             paddings.left = NG::CalcLength(left.value().IsNonNegative() ? left.value() : CalcDimension());
558         }
559     }
560     if (right.has_value()) {
561         if (right.value().Unit() == DimensionUnit::CALC) {
562             paddings.right =
563                 NG::CalcLength(right.value().IsNonNegative() ? right.value().CalcValue() : CalcDimension().CalcValue());
564         } else {
565             paddings.right = NG::CalcLength(right.value().IsNonNegative() ? right.value() : CalcDimension());
566         }
567     }
568     return paddings;
569 }
570 } // namespace OHOS::Ace::NG
571