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 51 PaddingPropertyF ConvertToPaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, 52 float percentReference = -1.0f, bool roundPixel = true); 53 54 MarginPropertyF ConvertToMarginPropertyF(const std::unique_ptr<MarginProperty>& margin, 55 const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true); 56 57 MarginPropertyF ConvertToMarginPropertyF(const MarginProperty& margin, const ScaleProperty& scaleProperty, 58 float percentReference = -1.0f, bool roundPixel = true); 59 60 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const std::unique_ptr<BorderWidthProperty>& borderWidth, 61 const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true); 62 63 BorderWidthPropertyF ConvertToBorderWidthPropertyF(const BorderWidthProperty& borderWidth, 64 const ScaleProperty& scaleProperty, float percentReference = -1.0f, bool roundPixel = true); 65 66 void UpdatePaddingPropertyF(const PaddingProperty& padding, const ScaleProperty& scaleProperty, const SizeF& selfSize, 67 PaddingPropertyF& paddingValue); 68 69 ACE_FORCE_EXPORT void AddPaddingToSize(const PaddingPropertyF& padding, SizeF& size); 70 71 ACE_FORCE_EXPORT void MinusPaddingToSize(const PaddingPropertyF& padding, SizeF& size); 72 73 ACE_FORCE_EXPORT void MinusPaddingToNonNegativeSize(const PaddingPropertyF& padding, SizeF& size); 74 75 void AddPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size); 76 77 void MinusPaddingToSize(const PaddingPropertyF& padding, OptionalSizeF& size); 78 79 float GetCrossAxisSize(const SizeF& size, Axis axis); 80 81 float GetMainAxisOffset(const OffsetF& offset, Axis axis); 82 83 float GetMainAxisSize(const SizeF& size, Axis axis); 84 85 void SetCrossAxisSize(float value, Axis axis, SizeF& size); 86 87 std::optional<float> GetCrossAxisSize(const OptionalSizeF& size, Axis axis); 88 89 std::optional<float> GetMainAxisSize(const OptionalSizeF& size, Axis axis); 90 91 void SetCrossAxisSize(float value, Axis axis, OptionalSizeF& size); 92 93 void SetMainAxisSize(float value, Axis axis, OptionalSizeF& size); 94 95 void UpdateOptionSizeByMaxOrMinCalcLayoutConstraint(OptionalSizeF& frameSize, 96 const std::optional<CalcSize>& calcLayoutConstraintMaxMinSize, const SizeT<float> percentReference, bool IsMaxSize); 97 98 ACE_FORCE_EXPORT PaddingProperty ConvertToCalcPaddingProperty(const std::optional<CalcDimension>& top, 99 const std::optional<CalcDimension>& bottom, const std::optional<CalcDimension>& left, 100 const std::optional<CalcDimension>& right); 101 102 /** 103 * @brief Create node IdealSize. 104 * 105 * @param layoutConstraint the constraint of current node. 106 * @param axis the axis of this node. 107 * @param measureType the measure info. 108 * @param usingMaxSize When the component cannot confirm the size, it decides whether to use the max or min value. 109 * @return SizeF the node size info. 110 */ 111 ACE_FORCE_EXPORT SizeF CreateIdealSize( 112 const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool usingMaxSize); 113 114 /** 115 * @brief Create node IdealSize. 116 * 117 * @param layoutConstraint the constraint of current node. 118 * @param axis the axis of this node. 119 * @param measureType the measure info. 120 * @return SizeF the node size info. 121 */ 122 ACE_FORCE_EXPORT OptionalSizeF CreateIdealSize( 123 const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType); 124 125 /** 126 * @brief Create node IdealSize by percent reference. 127 * 128 * @param layoutConstraint the constraint of current node. 129 * @param axis the axis of this node. 130 * @param measureType the measure info. 131 * @param needToConstrain constraint the result idealSize or not by min and max size in layoutConstraint. 132 * @return SizeF the node size info. 133 */ 134 OptionalSizeF CreateIdealSizeByPercentRef( 135 const LayoutConstraintF& layoutConstraint, Axis axis, MeasureType measureType, bool needToConstrain = false, 136 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