[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / third-party / yoga / Utils.h
1 /**
2  * Copyright (c) 2014-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7
8 #pragma once
9 #include "YGNode.h"
10 #include "Yoga-internal.h"
11
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.
14 //
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.
19 //
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.
24 //
25 // - totalFlexGrowFactors: total flex grow factors of flex items which are to be
26 // layed in the current line
27 //
28 // - totalFlexShrinkFactors: total flex shrink factors of flex items which are
29 // to be layed in the current line
30 //
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)
34 //
35 // - relativeChildren: Maintain a vector of the child nodes that can shrink
36 // and/or grow.
37
38 struct YGCollectFlexItemsRowValues {
39   uint32_t itemsOnLine;
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.
49   float mainDim;
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.
52   float crossDim;
53 };
54
55 bool YGValueEqual(const YGValue a, const YGValue b);
56
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);
60
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`
65 // compiler flag.
66 float YGFloatMax(const float a, const float b);
67
68 YGFloatOptional YGFloatOptionalMax(
69     const YGFloatOptional& op1,
70     const YGFloatOptional& op2);
71
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`
76 // compiler flag.
77 float YGFloatMin(const float a, const float b);
78
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) {
86   bool areEqual = true;
87   for (std::size_t i = 0; i < size && areEqual; ++i) {
88     areEqual = YGFloatsEqual(val1[i], val2[i]);
89   }
90   return areEqual;
91 }
92
93 // This function returns 0 if YGFloatIsUndefined(val) is true and val otherwise
94 float YGFloatSanitize(const float& val);
95
96 // This function unwraps optional and returns YGUndefined if not defined or
97 // op.value otherwise
98 // TODO: Get rid off this function
99 float YGUnwrapFloatOptional(const YGFloatOptional& op);
100
101 YGFlexDirection YGFlexDirectionCross(
102     const YGFlexDirection flexDirection,
103     const YGDirection direction);
104
105 inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) {
106   return flexDirection == YGFlexDirectionRow ||
107       flexDirection == YGFlexDirectionRowReverse;
108 }
109
110 inline YGFloatOptional YGResolveValue(const YGValue value, const float ownerSize) {
111   switch (value.unit) {
112     case YGUnitUndefined:
113     case YGUnitAuto:
114       return YGFloatOptional();
115     case YGUnitPoint:
116       return YGFloatOptional(value.value);
117     case YGUnitPercent:
118       return YGFloatOptional(
119           static_cast<float>(value.value * ownerSize * 0.01));
120   }
121   return YGFloatOptional();
122 }
123
124 inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) {
125   return flexDirection == YGFlexDirectionColumn ||
126       flexDirection == YGFlexDirectionColumnReverse;
127 }
128
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;
137     }
138   }
139
140   return flexDirection;
141 }
142
143 static inline YGFloatOptional YGResolveValueMargin(
144     const YGValue value,
145     const float ownerSize) {
146   return value.unit == YGUnitAuto ? YGFloatOptional(0)
147                                   : YGResolveValue(value, ownerSize);
148 }