| 1 | /* |
| 2 | * Copyright (C) 2016 The Qt Company Ltd. |
| 3 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 4 | * |
| 5 | * SPDX-License-Identifier: MIT |
| 6 | */ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include "YGNode.h" |
| 11 | #include "Yoga-internal.h" |
| 12 | #include "CompactValue.h" |
| 13 | |
| 14 | // This struct is an helper model to hold the data for step 4 of flexbox algo, |
| 15 | // which is collecting the flex items in a line. |
| 16 | // |
| 17 | // - itemsOnLine: Number of items which can fit in a line considering the |
| 18 | // available Inner dimension, the flex items computed flexbasis and their |
| 19 | // margin. It may be different than the difference between start and end |
| 20 | // indicates because we skip over absolute-positioned items. |
| 21 | // |
| 22 | // - sizeConsumedOnCurrentLine: It is accumulation of the dimensions and margin |
| 23 | // of all the children on the current line. This will be used in order to |
| 24 | // either set the dimensions of the node if none already exist or to compute |
| 25 | // the remaining space left for the flexible children. |
| 26 | // |
| 27 | // - totalFlexGrowFactors: total flex grow factors of flex items which are to be |
| 28 | // laid in the current line |
| 29 | // |
| 30 | // - totalFlexShrinkFactors: total flex shrink factors of flex items which are |
| 31 | // to be laid in the current line |
| 32 | // |
| 33 | // - endOfLineIndex: Its the end index of the last flex item which was examined |
| 34 | // and it may or may not be part of the current line(as it may be absolutely |
| 35 | // positioned or including it may have caused to overshoot availableInnerDim) |
| 36 | // |
| 37 | // - relativeChildren: Maintain a vector of the child nodes that can shrink |
| 38 | // and/or grow. |
| 39 | |
| 40 | QT_YOGA_NAMESPACE_BEGIN |
| 41 | |
| 42 | struct YGCollectFlexItemsRowValues { |
| 43 | uint32_t itemsOnLine; |
| 44 | float sizeConsumedOnCurrentLine; |
| 45 | float totalFlexGrowFactors; |
| 46 | float totalFlexShrinkScaledFactors; |
| 47 | uint32_t endOfLineIndex; |
| 48 | std::vector<YGNodeRef> relativeChildren; |
| 49 | float remainingFreeSpace; |
| 50 | // The size of the mainDim for the row after considering size, padding, margin |
| 51 | // and border of flex items. This is used to calculate maxLineDim after going |
| 52 | // through all the rows to decide on the main axis size of owner. |
| 53 | float mainDim; |
| 54 | // The size of the crossDim for the row after considering size, padding, |
| 55 | // margin and border of flex items. Used for calculating containers crossSize. |
| 56 | float crossDim; |
| 57 | }; |
| 58 | |
| 59 | bool YGValueEqual(const YGValue& a, const YGValue& b); |
| 60 | inline bool YGValueEqual( |
| 61 | facebook::yoga::detail::CompactValue a, |
| 62 | facebook::yoga::detail::CompactValue b) { |
| 63 | return YGValueEqual(a: (YGValue) a, b: (YGValue) b); |
| 64 | } |
| 65 | |
| 66 | // This custom float equality function returns true if either absolute |
| 67 | // difference between two floats is less than 0.0001f or both are undefined. |
| 68 | bool YGFloatsEqual(const float a, const float b); |
| 69 | |
| 70 | bool YGDoubleEqual(const double a, const double b); |
| 71 | |
| 72 | float YGFloatMax(const float a, const float b); |
| 73 | |
| 74 | YGFloatOptional YGFloatOptionalMax( |
| 75 | const YGFloatOptional op1, |
| 76 | const YGFloatOptional op2); |
| 77 | |
| 78 | float YGFloatMin(const float a, const float b); |
| 79 | |
| 80 | // This custom float comparison function compares the array of float with |
| 81 | // YGFloatsEqual, as the default float comparison operator will not work(Look |
| 82 | // at the comments of YGFloatsEqual function). |
| 83 | template <std::size_t size> |
| 84 | bool YGFloatArrayEqual( |
| 85 | const std::array<float, size>& val1, |
| 86 | const std::array<float, size>& val2) { |
| 87 | bool areEqual = true; |
| 88 | for (std::size_t i = 0; i < size && areEqual; ++i) { |
| 89 | areEqual = YGFloatsEqual(val1[i], val2[i]); |
| 90 | } |
| 91 | return areEqual; |
| 92 | } |
| 93 | |
| 94 | // This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise |
| 95 | float YGFloatSanitize(const float val); |
| 96 | |
| 97 | YGFlexDirection YGFlexDirectionCross( |
| 98 | const YGFlexDirection flexDirection, |
| 99 | const YGDirection direction); |
| 100 | |
| 101 | inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) { |
| 102 | return flexDirection == YGFlexDirectionRow || |
| 103 | flexDirection == YGFlexDirectionRowReverse; |
| 104 | } |
| 105 | |
| 106 | inline YGFloatOptional YGResolveValue( |
| 107 | const YGValue value, |
| 108 | const float ownerSize) { |
| 109 | switch (value.unit) { |
| 110 | case YGUnitPoint: |
| 111 | return YGFloatOptional{value.value}; |
| 112 | case YGUnitPercent: |
| 113 | return YGFloatOptional{value.value * ownerSize * 0.01f}; |
| 114 | default: |
| 115 | return YGFloatOptional{}; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | inline YGFloatOptional YGResolveValue( |
| 120 | facebook::yoga::detail::CompactValue value, |
| 121 | float ownerSize) { |
| 122 | return YGResolveValue(value: (YGValue) value, ownerSize); |
| 123 | } |
| 124 | |
| 125 | inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) { |
| 126 | return flexDirection == YGFlexDirectionColumn || |
| 127 | flexDirection == YGFlexDirectionColumnReverse; |
| 128 | } |
| 129 | |
| 130 | inline YGFlexDirection YGResolveFlexDirection( |
| 131 | const YGFlexDirection flexDirection, |
| 132 | const YGDirection direction) { |
| 133 | if (direction == YGDirectionRTL) { |
| 134 | if (flexDirection == YGFlexDirectionRow) { |
| 135 | return YGFlexDirectionRowReverse; |
| 136 | } else if (flexDirection == YGFlexDirectionRowReverse) { |
| 137 | return YGFlexDirectionRow; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return flexDirection; |
| 142 | } |
| 143 | |
| 144 | inline YGFloatOptional YGResolveValueMargin( |
| 145 | facebook::yoga::detail::CompactValue value, |
| 146 | const float ownerSize) { |
| 147 | return value.isAuto() ? YGFloatOptional{0} : YGResolveValue(value, ownerSize); |
| 148 | } |
| 149 | |
| 150 | QT_YOGA_NAMESPACE_END |
| 151 | |