Merge "Fix resource leaks in layouting." into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / third-party / yoga / Yoga.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
10 #include <assert.h>
11 #include <math.h>
12 #include <stdarg.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #ifndef __cplusplus
18 #include <stdbool.h>
19 #endif
20
21 /** Large positive number signifies that the property(float) is undefined.
22  *Earlier we used to have YGundefined as NAN, but the downside of this is that
23  *we can't use -ffast-math compiler flag as it assumes all floating-point
24  *calculation involve and result into finite numbers. For more information
25  *regarding -ffast-math compiler flag in clang, have a look at
26  *https://clang.llvm.org/docs/UsersManual.html#cmdoption-ffast-math
27  **/
28 #define YGUndefined 10E20F
29
30 #include "YGEnums.h"
31 #include "YGMacros.h"
32
33 YG_EXTERN_C_BEGIN
34
35 typedef struct YGSize {
36   float width;
37   float height;
38 } YGSize;
39
40 typedef struct YGValue {
41   float value;
42   YGUnit unit;
43 } YGValue;
44
45 extern const YGValue YGValueUndefined;
46 extern const YGValue YGValueAuto;
47
48 typedef struct YGConfig *YGConfigRef;
49
50 typedef struct YGNode* YGNodeRef;
51
52 typedef YGSize (*YGMeasureFunc)(YGNodeRef node,
53                                 float width,
54                                 YGMeasureMode widthMode,
55                                 float height,
56                                 YGMeasureMode heightMode);
57 typedef float (*YGBaselineFunc)(YGNodeRef node, const float width, const float height);
58 typedef void (*YGDirtiedFunc)(YGNodeRef node);
59 typedef void (*YGPrintFunc)(YGNodeRef node);
60 typedef int (*YGLogger)(const YGConfigRef config,
61                         const YGNodeRef node,
62                         YGLogLevel level,
63                         const char *format,
64                         va_list args);
65 typedef YGNodeRef (
66     *YGCloneNodeFunc)(YGNodeRef oldNode, YGNodeRef owner, int childIndex);
67
68 // YGNode
69 WIN_EXPORT YGNodeRef YGNodeNew(void);
70 WIN_EXPORT YGNodeRef YGNodeNewWithConfig(const YGConfigRef config);
71 WIN_EXPORT YGNodeRef YGNodeClone(const YGNodeRef node);
72 WIN_EXPORT void YGNodeFree(const YGNodeRef node);
73 WIN_EXPORT void YGNodeFreeRecursive(const YGNodeRef node);
74 WIN_EXPORT void YGNodeReset(const YGNodeRef node);
75 WIN_EXPORT int32_t YGNodeGetInstanceCount(void);
76
77 WIN_EXPORT void YGNodeInsertChild(const YGNodeRef node,
78                                   const YGNodeRef child,
79                                   const uint32_t index);
80
81 // This function inserts the child YGNodeRef as a children of the node received
82 // by parameter and set the Owner of the child object to null. This function is
83 // expected to be called when using Yoga in persistent mode in order to share a
84 // YGNodeRef object as a child of two different Yoga trees. The child YGNodeRef
85 // is expected to be referenced from its original owner and from a clone of its
86 // original owner.
87 WIN_EXPORT void YGNodeInsertSharedChild(
88     const YGNodeRef node,
89     const YGNodeRef child,
90     const uint32_t index);
91 WIN_EXPORT void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child);
92 WIN_EXPORT void YGNodeRemoveAllChildren(const YGNodeRef node);
93 WIN_EXPORT YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index);
94 WIN_EXPORT YGNodeRef YGNodeGetOwner(const YGNodeRef node);
95 WIN_EXPORT uint32_t YGNodeGetChildCount(const YGNodeRef node);
96 WIN_EXPORT void YGNodeSetChildren(
97     YGNodeRef const owner,
98     const YGNodeRef children[],
99     const uint32_t count);
100
101 WIN_EXPORT void YGNodeCalculateLayout(const YGNodeRef node,
102                                       const float availableWidth,
103                                       const float availableHeight,
104                                       const YGDirection ownerDirection);
105
106 // Mark a node as dirty. Only valid for nodes with a custom measure function
107 // set.
108 // YG knows when to mark all other nodes as dirty but because nodes with
109 // measure functions
110 // depends on information not known to YG they must perform this dirty
111 // marking manually.
112 WIN_EXPORT void YGNodeMarkDirty(const YGNodeRef node);
113
114 // This function marks the current node and all its descendants as dirty. This function is added to test yoga benchmarks.
115 // This function is not expected to be used in production as calling `YGCalculateLayout` will cause the recalculation of each and every node.
116 WIN_EXPORT void YGNodeMarkDirtyAndPropogateToDescendants(const YGNodeRef node);
117
118 WIN_EXPORT void YGNodePrint(const YGNodeRef node, const YGPrintOptions options);
119
120 WIN_EXPORT bool YGFloatIsUndefined(const float value);
121
122 WIN_EXPORT bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,
123                                               const float width,
124                                               const YGMeasureMode heightMode,
125                                               const float height,
126                                               const YGMeasureMode lastWidthMode,
127                                               const float lastWidth,
128                                               const YGMeasureMode lastHeightMode,
129                                               const float lastHeight,
130                                               const float lastComputedWidth,
131                                               const float lastComputedHeight,
132                                               const float marginRow,
133                                               const float marginColumn,
134                                               const YGConfigRef config);
135
136 WIN_EXPORT void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode);
137
138 #define YG_NODE_PROPERTY(type, name, paramName)                          \
139   WIN_EXPORT void YGNodeSet##name(const YGNodeRef node, type paramName); \
140   WIN_EXPORT type YGNodeGet##name(const YGNodeRef node);
141
142 #define YG_NODE_STYLE_PROPERTY(type, name, paramName)                               \
143   WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const type paramName); \
144   WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node);
145
146 #define YG_NODE_STYLE_PROPERTY_UNIT(type, name, paramName)                                    \
147   WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node, const float paramName);          \
148   WIN_EXPORT void YGNodeStyleSet##name##Percent(const YGNodeRef node, const float paramName); \
149   WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node);
150
151 #define YG_NODE_STYLE_PROPERTY_UNIT_AUTO(type, name, paramName) \
152   YG_NODE_STYLE_PROPERTY_UNIT(type, name, paramName)            \
153   WIN_EXPORT void YGNodeStyleSet##name##Auto(const YGNodeRef node);
154
155 #define YG_NODE_STYLE_EDGE_PROPERTY(type, name, paramName)    \
156   WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node,  \
157                                        const YGEdge edge,     \
158                                        const type paramName); \
159   WIN_EXPORT type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge);
160
161 #define YG_NODE_STYLE_EDGE_PROPERTY_UNIT(type, name, paramName)         \
162   WIN_EXPORT void YGNodeStyleSet##name(const YGNodeRef node,            \
163                                        const YGEdge edge,               \
164                                        const float paramName);          \
165   WIN_EXPORT void YGNodeStyleSet##name##Percent(const YGNodeRef node,   \
166                                                 const YGEdge edge,      \
167                                                 const float paramName); \
168   WIN_EXPORT WIN_STRUCT(type) YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge);
169
170 #define YG_NODE_STYLE_EDGE_PROPERTY_UNIT_AUTO(type, name) \
171   WIN_EXPORT void YGNodeStyleSet##name##Auto(const YGNodeRef node, const YGEdge edge);
172
173 #define YG_NODE_LAYOUT_PROPERTY(type, name) \
174   WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node);
175
176 #define YG_NODE_LAYOUT_EDGE_PROPERTY(type, name) \
177   WIN_EXPORT type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge);
178
179 void* YGNodeGetContext(YGNodeRef node);
180 void YGNodeSetContext(YGNodeRef node, void* context);
181 YGMeasureFunc YGNodeGetMeasureFunc(YGNodeRef node);
182 void YGNodeSetMeasureFunc(YGNodeRef node, YGMeasureFunc measureFunc);
183 YGBaselineFunc YGNodeGetBaselineFunc(YGNodeRef node);
184 void YGNodeSetBaselineFunc(YGNodeRef node, YGBaselineFunc baselineFunc);
185 YGDirtiedFunc YGNodeGetDirtiedFunc(YGNodeRef node);
186 void YGNodeSetDirtiedFunc(YGNodeRef node, YGDirtiedFunc dirtiedFunc);
187 YGPrintFunc YGNodeGetPrintFunc(YGNodeRef node);
188 void YGNodeSetPrintFunc(YGNodeRef node, YGPrintFunc printFunc);
189 bool YGNodeGetHasNewLayout(YGNodeRef node);
190 void YGNodeSetHasNewLayout(YGNodeRef node, bool hasNewLayout);
191 YGNodeType YGNodeGetNodeType(YGNodeRef node);
192 void YGNodeSetNodeType(YGNodeRef node, YGNodeType nodeType);
193 bool YGNodeIsDirty(YGNodeRef node);
194 bool YGNodeLayoutGetDidUseLegacyFlag(const YGNodeRef node);
195
196 YG_NODE_STYLE_PROPERTY(YGDirection, Direction, direction);
197 YG_NODE_STYLE_PROPERTY(YGFlexDirection, FlexDirection, flexDirection);
198 YG_NODE_STYLE_PROPERTY(YGJustify, JustifyContent, justifyContent);
199 YG_NODE_STYLE_PROPERTY(YGAlign, AlignContent, alignContent);
200 YG_NODE_STYLE_PROPERTY(YGAlign, AlignItems, alignItems);
201 YG_NODE_STYLE_PROPERTY(YGAlign, AlignSelf, alignSelf);
202 YG_NODE_STYLE_PROPERTY(YGPositionType, PositionType, positionType);
203 YG_NODE_STYLE_PROPERTY(YGWrap, FlexWrap, flexWrap);
204 YG_NODE_STYLE_PROPERTY(YGOverflow, Overflow, overflow);
205 YG_NODE_STYLE_PROPERTY(YGDisplay, Display, display);
206 YG_NODE_STYLE_PROPERTY(float, Flex, flex);
207 YG_NODE_STYLE_PROPERTY(float, FlexGrow, flexGrow);
208 YG_NODE_STYLE_PROPERTY(float, FlexShrink, flexShrink);
209 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, FlexBasis, flexBasis);
210
211 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Position, position);
212 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Margin, margin);
213 YG_NODE_STYLE_EDGE_PROPERTY_UNIT_AUTO(YGValue, Margin);
214 YG_NODE_STYLE_EDGE_PROPERTY_UNIT(YGValue, Padding, padding);
215 YG_NODE_STYLE_EDGE_PROPERTY(float, Border, border);
216
217 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, Width, width);
218 YG_NODE_STYLE_PROPERTY_UNIT_AUTO(YGValue, Height, height);
219 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MinWidth, minWidth);
220 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MinHeight, minHeight);
221 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MaxWidth, maxWidth);
222 YG_NODE_STYLE_PROPERTY_UNIT(YGValue, MaxHeight, maxHeight);
223
224 // Yoga specific properties, not compatible with flexbox specification
225 // Aspect ratio control the size of the undefined dimension of a node.
226 // Aspect ratio is encoded as a floating point value width/height. e.g. A value of 2 leads to a node
227 // with a width twice the size of its height while a value of 0.5 gives the opposite effect.
228 //
229 // - On a node with a set width/height aspect ratio control the size of the unset dimension
230 // - On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if
231 // unset
232 // - On a node with a measure function aspect ratio works as though the measure function measures
233 // the flex basis
234 // - On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if
235 // unset
236 // - Aspect ratio takes min/max dimensions into account
237 YG_NODE_STYLE_PROPERTY(float, AspectRatio, aspectRatio);
238
239 YG_NODE_LAYOUT_PROPERTY(float, Left);
240 YG_NODE_LAYOUT_PROPERTY(float, Top);
241 YG_NODE_LAYOUT_PROPERTY(float, Right);
242 YG_NODE_LAYOUT_PROPERTY(float, Bottom);
243 YG_NODE_LAYOUT_PROPERTY(float, Width);
244 YG_NODE_LAYOUT_PROPERTY(float, Height);
245 YG_NODE_LAYOUT_PROPERTY(YGDirection, Direction);
246 YG_NODE_LAYOUT_PROPERTY(bool, HadOverflow);
247 bool YGNodeLayoutGetDidLegacyStretchFlagAffectLayout(const YGNodeRef node);
248
249 // Get the computed values for these nodes after performing layout. If they were set using
250 // point values then the returned value will be the same as YGNodeStyleGetXXX. However if
251 // they were set using a percentage value then the returned value is the computed value used
252 // during layout.
253 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Margin);
254 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Border);
255 YG_NODE_LAYOUT_EDGE_PROPERTY(float, Padding);
256
257 WIN_EXPORT void YGConfigSetLogger(const YGConfigRef config, YGLogger logger);
258 WIN_EXPORT void YGLog(const YGNodeRef node, YGLogLevel level, const char *message, ...);
259 WIN_EXPORT void YGLogWithConfig(const YGConfigRef config, YGLogLevel level, const char *format, ...);
260 WIN_EXPORT void YGAssert(const bool condition, const char *message);
261 WIN_EXPORT void YGAssertWithNode(const YGNodeRef node, const bool condition, const char *message);
262 WIN_EXPORT void YGAssertWithConfig(const YGConfigRef config,
263                                    const bool condition,
264                                    const char *message);
265 // Set this to number of pixels in 1 point to round calculation results
266 // If you want to avoid rounding - set PointScaleFactor to 0
267 WIN_EXPORT void YGConfigSetPointScaleFactor(const YGConfigRef config, const float pixelsInPoint);
268 void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour(
269     const YGConfigRef config,
270     const bool shouldDiffLayout);
271
272 // Yoga previously had an error where containers would take the maximum space possible instead of
273 // the minimum
274 // like they are supposed to. In practice this resulted in implicit behaviour similar to align-self:
275 // stretch;
276 // Because this was such a long-standing bug we must allow legacy users to switch back to this
277 // behaviour.
278 WIN_EXPORT void YGConfigSetUseLegacyStretchBehaviour(const YGConfigRef config,
279                                                      const bool useLegacyStretchBehaviour);
280
281 // YGConfig
282 WIN_EXPORT YGConfigRef YGConfigNew(void);
283 WIN_EXPORT void YGConfigFree(const YGConfigRef config);
284 WIN_EXPORT void YGConfigCopy(const YGConfigRef dest, const YGConfigRef src);
285 WIN_EXPORT int32_t YGConfigGetInstanceCount(void);
286
287 WIN_EXPORT void YGConfigSetExperimentalFeatureEnabled(const YGConfigRef config,
288                                                       const YGExperimentalFeature feature,
289                                                       const bool enabled);
290 WIN_EXPORT bool YGConfigIsExperimentalFeatureEnabled(const YGConfigRef config,
291                                                      const YGExperimentalFeature feature);
292
293 // Using the web defaults is the prefered configuration for new projects.
294 // Usage of non web defaults should be considered as legacy.
295 WIN_EXPORT void YGConfigSetUseWebDefaults(const YGConfigRef config, const bool enabled);
296 WIN_EXPORT bool YGConfigGetUseWebDefaults(const YGConfigRef config);
297
298 WIN_EXPORT void YGConfigSetCloneNodeFunc(const YGConfigRef config,
299                                           const YGCloneNodeFunc callback);
300
301 // Export only for C#
302 WIN_EXPORT YGConfigRef YGConfigGetDefault(void);
303
304 WIN_EXPORT void YGConfigSetContext(const YGConfigRef config, void *context);
305 WIN_EXPORT void *YGConfigGetContext(const YGConfigRef config);
306
307 WIN_EXPORT float YGRoundValueToPixelGrid(
308     const float value,
309     const float pointScaleFactor,
310     const bool forceCeil,
311     const bool forceFloor);
312
313 YG_EXTERN_C_END
314
315 #ifdef __cplusplus
316
317 #include <functional>
318 #include <vector>
319
320 // Calls f on each node in the tree including the given node argument.
321 extern void YGTraversePreOrder(YGNodeRef const node, std::function<void(YGNodeRef node)>&& f);
322
323 extern void YGNodeSetChildren(
324     YGNodeRef const owner,
325     const std::vector<YGNodeRef>& children);
326
327 #endif