2 * Copyright (c) 2014-present, Facebook, Inc.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
10 #include "Yoga-internal.h"
12 // This struct is an helper model to hold the data for step 4 of flexbox
13 // algo, which is collecting the flex items in a line.
15 // - itemsOnLine: Number of items which can fit in a line considering the
16 // available Inner dimension, the flex items computed flexbasis and their
17 // margin. It may be different than the difference between start and end
18 // indicates because we skip over absolute-positioned items.
20 // - sizeConsumedOnCurrentLine: It is accumulation of the dimensions and margin
21 // of all the children on the current line. This will be used in order to either
22 // set the dimensions of the node if none already exist or to compute the
23 // remaining space left for the flexible children.
25 // - totalFlexGrowFactors: total flex grow factors of flex items which are to be
26 // layed in the current line
28 // - totalFlexShrinkFactors: total flex shrink factors of flex items which are
29 // to be layed in the current line
31 // - endOfLineIndex: Its the end index of the last flex item which was examined
32 // and it may or may not be part of the current line(as it may be absolutely
33 // positioned or inculding it may have caused to overshoot availableInnerDim)
35 // - relativeChildren: Maintain a vector of the child nodes that can shrink
38 struct YGCollectFlexItemsRowValues {
40 float sizeConsumedOnCurrentLine;
41 float totalFlexGrowFactors;
42 float totalFlexShrinkScaledFactors;
43 uint32_t endOfLineIndex;
44 std::vector<YGNodeRef> relativeChildren;
45 float remainingFreeSpace;
46 // The size of the mainDim for the row after considering size, padding, margin
47 // and border of flex items. This is used to calculate maxLineDim after going
48 // through all the rows to decide on the main axis size of owner.
50 // The size of the crossDim for the row after considering size, padding,
51 // margin and border of flex items. Used for calculating containers crossSize.
55 bool YGValueEqual(const YGValue a, const YGValue b);
57 // This custom float equality function returns true if either absolute
58 // difference between two floats is less than 0.0001f or both are undefined.
59 bool YGFloatsEqual(const float a, const float b);
61 // We need custom max function, since we want that, if one argument is
62 // YGUndefined then the max funtion should return the other argument as the max
63 // value. We wouldn't have needed a custom max function if YGUndefined was NAN
64 // as fmax has the same behaviour, but with NAN we cannot use `-ffast-math`
66 float YGFloatMax(const float a, const float b);
68 YGFloatOptional YGFloatOptionalMax(
69 const YGFloatOptional& op1,
70 const YGFloatOptional& op2);
72 // We need custom min function, since we want that, if one argument is
73 // YGUndefined then the min funtion should return the other argument as the min
74 // value. We wouldn't have needed a custom min function if YGUndefined was NAN
75 // as fmin has the same behaviour, but with NAN we cannot use `-ffast-math`
77 float YGFloatMin(const float a, const float b);
79 // This custom float comparision function compares the array of float with
80 // YGFloatsEqual, as the default float comparision operator will not work(Look
81 // at the comments of YGFloatsEqual function).
82 template <std::size_t size>
83 bool YGFloatArrayEqual(
84 const std::array<float, size>& val1,
85 const std::array<float, size>& val2) {
87 for (std::size_t i = 0; i < size && areEqual; ++i) {
88 areEqual = YGFloatsEqual(val1[i], val2[i]);
93 // This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise
94 float YGFloatSanitize(const float& val);
96 // This function unwraps optional and returns YGUndefined if not defined or
98 // TODO: Get rid off this function
99 float YGUnwrapFloatOptional(const YGFloatOptional& op);
101 YGFlexDirection YGFlexDirectionCross(
102 const YGFlexDirection flexDirection,
103 const YGDirection direction);
105 inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) {
106 return flexDirection == YGFlexDirectionRow ||
107 flexDirection == YGFlexDirectionRowReverse;
110 inline YGFloatOptional YGResolveValue(const YGValue value, const float ownerSize) {
111 switch (value.unit) {
112 case YGUnitUndefined:
114 return YGFloatOptional();
116 return YGFloatOptional(value.value);
118 return YGFloatOptional(
119 static_cast<float>(value.value * ownerSize * 0.01));
121 return YGFloatOptional();
124 inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) {
125 return flexDirection == YGFlexDirectionColumn ||
126 flexDirection == YGFlexDirectionColumnReverse;
129 inline YGFlexDirection YGResolveFlexDirection(
130 const YGFlexDirection flexDirection,
131 const YGDirection direction) {
132 if (direction == YGDirectionRTL) {
133 if (flexDirection == YGFlexDirectionRow) {
134 return YGFlexDirectionRowReverse;
135 } else if (flexDirection == YGFlexDirectionRowReverse) {
136 return YGFlexDirectionRow;
140 return flexDirection;
143 static inline YGFloatOptional YGResolveValueMargin(
145 const float ownerSize) {
146 return value.unit == YGUnitAuto ? YGFloatOptional(0)
147 : YGResolveValue(value, ownerSize);