/
Yoga.c
3165 lines (2795 loc) · 137 KB
/
Yoga.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <string.h>
#include "YGNodeList.h"
#include "Yoga.h"
#ifdef _MSC_VER
#include <float.h>
#ifndef isnan
#define isnan _isnan
#endif
#ifndef __cplusplus
#define inline __inline
#endif
/* define fmaxf if < VC12 */
#if _MSC_VER < 1800
__forceinline const float fmaxf(const float a, const float b) {
return (a > b) ? a : b;
}
#endif
#endif
typedef struct YGCachedMeasurement {
float availableWidth;
float availableHeight;
YGMeasureMode widthMeasureMode;
YGMeasureMode heightMeasureMode;
float computedWidth;
float computedHeight;
} YGCachedMeasurement;
// This value was chosen based on empiracle data. Even the most complicated
// layouts should not require more than 16 entries to fit within the cache.
#define YG_MAX_CACHED_RESULT_COUNT 16
typedef struct YGLayout {
float position[4];
float dimensions[2];
float margin[6];
float padding[6];
YGDirection direction;
uint32_t computedFlexBasisGeneration;
float computedFlexBasis;
// Instead of recomputing the entire layout every single time, we
// cache some information to break early when nothing changed
uint32_t generationCount;
YGDirection lastParentDirection;
uint32_t nextCachedMeasurementsIndex;
YGCachedMeasurement cachedMeasurements[YG_MAX_CACHED_RESULT_COUNT];
float measuredDimensions[2];
YGCachedMeasurement cachedLayout;
} YGLayout;
typedef struct YGStyle {
YGDirection direction;
YGFlexDirection flexDirection;
YGJustify justifyContent;
YGAlign alignContent;
YGAlign alignItems;
YGAlign alignSelf;
YGPositionType positionType;
YGWrap flexWrap;
YGOverflow overflow;
float flex;
float flexGrow;
float flexShrink;
YGValue flexBasis;
YGValue margin[YGEdgeCount];
YGValue position[YGEdgeCount];
YGValue padding[YGEdgeCount];
YGValue border[YGEdgeCount];
YGValue dimensions[2];
YGValue minDimensions[2];
YGValue maxDimensions[2];
// Yoga specific properties, not compatible with flexbox specification
float aspectRatio;
} YGStyle;
typedef struct YGNode {
YGStyle style;
YGLayout layout;
uint32_t lineIndex;
YGNodeRef parent;
YGNodeListRef children;
struct YGNode *nextChild;
YGMeasureFunc measure;
YGBaselineFunc baseline;
YGPrintFunc print;
void *context;
bool isDirty;
bool hasNewLayout;
} YGNode;
#define YG_UNDEFINED_VALUES \
{ .value = YGUndefined, .unit = YGUnitUndefined }
#define YG_DEFAULT_EDGE_VALUES_UNIT \
{ \
[YGEdgeLeft] = YG_UNDEFINED_VALUES, [YGEdgeTop] = YG_UNDEFINED_VALUES, \
[YGEdgeRight] = YG_UNDEFINED_VALUES, [YGEdgeBottom] = YG_UNDEFINED_VALUES, \
[YGEdgeStart] = YG_UNDEFINED_VALUES, [YGEdgeEnd] = YG_UNDEFINED_VALUES, \
[YGEdgeHorizontal] = YG_UNDEFINED_VALUES, [YGEdgeVertical] = YG_UNDEFINED_VALUES, \
[YGEdgeAll] = YG_UNDEFINED_VALUES, \
}
#define YG_DEFAULT_DIMENSION_VALUES \
{ [YGDimensionWidth] = YGUndefined, [YGDimensionHeight] = YGUndefined, }
#define YG_DEFAULT_DIMENSION_VALUES_UNIT \
{ [YGDimensionWidth] = YG_UNDEFINED_VALUES, [YGDimensionHeight] = YG_UNDEFINED_VALUES, }
static YGNode gYGNodeDefaults = {
.parent = NULL,
.children = NULL,
.hasNewLayout = true,
.isDirty = false,
.style =
{
.flex = YGUndefined,
.flexGrow = YGUndefined,
.flexShrink = YGUndefined,
.flexBasis = YG_UNDEFINED_VALUES,
.justifyContent = YGJustifyFlexStart,
.alignItems = YGAlignStretch,
.alignContent = YGAlignFlexStart,
.direction = YGDirectionInherit,
.flexDirection = YGFlexDirectionColumn,
.overflow = YGOverflowVisible,
.dimensions = YG_DEFAULT_DIMENSION_VALUES_UNIT,
.minDimensions = YG_DEFAULT_DIMENSION_VALUES_UNIT,
.maxDimensions = YG_DEFAULT_DIMENSION_VALUES_UNIT,
.position = YG_DEFAULT_EDGE_VALUES_UNIT,
.margin = YG_DEFAULT_EDGE_VALUES_UNIT,
.padding = YG_DEFAULT_EDGE_VALUES_UNIT,
.border = YG_DEFAULT_EDGE_VALUES_UNIT,
.aspectRatio = YGUndefined,
},
.layout =
{
.dimensions = YG_DEFAULT_DIMENSION_VALUES,
.lastParentDirection = (YGDirection) -1,
.nextCachedMeasurementsIndex = 0,
.computedFlexBasis = YGUndefined,
.measuredDimensions = YG_DEFAULT_DIMENSION_VALUES,
.cachedLayout =
{
.widthMeasureMode = (YGMeasureMode) -1,
.heightMeasureMode = (YGMeasureMode) -1,
.computedWidth = -1,
.computedHeight = -1,
},
},
};
static void YGNodeMarkDirtyInternal(const YGNodeRef node);
YGMalloc gYGMalloc = &malloc;
YGCalloc gYGCalloc = &calloc;
YGRealloc gYGRealloc = &realloc;
YGFree gYGFree = &free;
static YGValue YGValueZero = {.value = 0, .unit = YGUnitPixel};
#ifdef ANDROID
#include <android/log.h>
static int YGAndroidLog(YGLogLevel level, const char *format, va_list args) {
int androidLevel = YGLogLevelDebug;
switch (level) {
case YGLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
case YGLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case YGLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case YGLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case YGLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
}
const int result = __android_log_vprint(androidLevel, "YG-layout", format, args);
return result;
}
static YGLogger gLogger = &YGAndroidLog;
#else
static int YGDefaultLog(YGLogLevel level, const char *format, va_list args) {
switch (level) {
case YGLogLevelError:
return vfprintf(stderr, format, args);
case YGLogLevelWarn:
case YGLogLevelInfo:
case YGLogLevelDebug:
case YGLogLevelVerbose:
default:
return vprintf(format, args);
}
}
static YGLogger gLogger = &YGDefaultLog;
#endif
static inline const YGValue *YGComputedEdgeValue(const YGValue edges[YGEdgeCount],
const YGEdge edge,
const YGValue *const defaultValue) {
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get computed value of multi-edge shorthands");
if (edges[edge].unit != YGUnitUndefined) {
return &edges[edge];
}
if ((edge == YGEdgeTop || edge == YGEdgeBottom) &&
edges[YGEdgeVertical].unit != YGUnitUndefined) {
return &edges[YGEdgeVertical];
}
if ((edge == YGEdgeLeft || edge == YGEdgeRight || edge == YGEdgeStart || edge == YGEdgeEnd) &&
edges[YGEdgeHorizontal].unit != YGUnitUndefined) {
return &edges[YGEdgeHorizontal];
}
if (edges[YGEdgeAll].unit != YGUnitUndefined) {
return &edges[YGEdgeAll];
}
if (edge == YGEdgeStart || edge == YGEdgeEnd) {
return &YGValueUndefined;
}
return defaultValue;
}
static inline float YGValueResolve(const YGValue *const unit, const float parentSize) {
if (unit->unit == YGUnitPixel) {
return unit->value;
} else {
return unit->value * parentSize / 100.0f;
}
}
int32_t gNodeInstanceCount = 0;
YGNodeRef YGNodeNew(void) {
const YGNodeRef node = gYGMalloc(sizeof(YGNode));
YG_ASSERT(node, "Could not allocate memory for node");
gNodeInstanceCount++;
memcpy(node, &gYGNodeDefaults, sizeof(YGNode));
return node;
}
void YGNodeFree(const YGNodeRef node) {
if (node->parent) {
YGNodeListDelete(node->parent->children, node);
node->parent = NULL;
}
const uint32_t childCount = YGNodeGetChildCount(node);
for (uint32_t i = 0; i < childCount; i++) {
const YGNodeRef child = YGNodeGetChild(node, i);
child->parent = NULL;
}
YGNodeListFree(node->children);
gYGFree(node);
gNodeInstanceCount--;
}
void YGNodeFreeRecursive(const YGNodeRef root) {
while (YGNodeGetChildCount(root) > 0) {
const YGNodeRef child = YGNodeGetChild(root, 0);
YGNodeRemoveChild(root, child);
YGNodeFreeRecursive(child);
}
YGNodeFree(root);
}
void YGNodeReset(const YGNodeRef node) {
YG_ASSERT(YGNodeGetChildCount(node) == 0,
"Cannot reset a node which still has children attached");
YG_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent");
YGNodeListFree(node->children);
memcpy(node, &gYGNodeDefaults, sizeof(YGNode));
}
int32_t YGNodeGetInstanceCount(void) {
return gNodeInstanceCount;
}
static void YGNodeMarkDirtyInternal(const YGNodeRef node) {
if (!node->isDirty) {
node->isDirty = true;
node->layout.computedFlexBasis = YGUndefined;
if (node->parent) {
YGNodeMarkDirtyInternal(node->parent);
}
}
}
void YGNodeSetMeasureFunc(const YGNodeRef node, YGMeasureFunc measureFunc) {
if (measureFunc == NULL) {
node->measure = NULL;
} else {
YG_ASSERT(YGNodeGetChildCount(node) == 0,
"Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc;
}
}
YGMeasureFunc YGNodeGetMeasureFunc(const YGNodeRef node) {
return node->measure;
}
void YGNodeSetBaselineFunc(const YGNodeRef node, YGBaselineFunc baselineFunc) {
node->baseline = baselineFunc;
}
YGBaselineFunc YGNodeGetBaselineFunc(const YGNodeRef node) {
return node->baseline;
}
void YGNodeInsertChild(const YGNodeRef node, const YGNodeRef child, const uint32_t index) {
YG_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first.");
YG_ASSERT(node->measure == NULL,
"Cannot add child: Nodes with measure functions cannot have children.");
YGNodeListInsert(&node->children, child, index);
child->parent = node;
YGNodeMarkDirtyInternal(node);
}
void YGNodeRemoveChild(const YGNodeRef node, const YGNodeRef child) {
if (YGNodeListDelete(node->children, child) != NULL) {
child->parent = NULL;
YGNodeMarkDirtyInternal(node);
}
}
YGNodeRef YGNodeGetChild(const YGNodeRef node, const uint32_t index) {
return YGNodeListGet(node->children, index);
}
YGNodeRef YGNodeGetParent(const YGNodeRef node) {
return node->parent;
}
inline uint32_t YGNodeGetChildCount(const YGNodeRef node) {
return YGNodeListCount(node->children);
}
void YGNodeMarkDirty(const YGNodeRef node) {
YG_ASSERT(node->measure != NULL,
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
YGNodeMarkDirtyInternal(node);
}
bool YGNodeIsDirty(const YGNodeRef node) {
return node->isDirty;
}
void YGNodeCopyStyle(const YGNodeRef dstNode, const YGNodeRef srcNode) {
if (memcmp(&dstNode->style, &srcNode->style, sizeof(YGStyle)) != 0) {
memcpy(&dstNode->style, &srcNode->style, sizeof(YGStyle));
YGNodeMarkDirtyInternal(dstNode);
}
}
inline float YGNodeStyleGetFlexGrow(const YGNodeRef node) {
if (!YGFloatIsUndefined(node->style.flexGrow)) {
return node->style.flexGrow;
}
if (!YGFloatIsUndefined(node->style.flex) && node->style.flex > 0.0f) {
return node->style.flex;
}
return 0.0f;
}
inline float YGNodeStyleGetFlexShrink(const YGNodeRef node) {
if (!YGFloatIsUndefined(node->style.flexShrink)) {
return node->style.flexShrink;
}
if (!YGFloatIsUndefined(node->style.flex) && node->style.flex < 0.0f) {
return -node->style.flex;
}
return 0.0f;
}
static inline const YGValue *YGNodeStyleGetFlexBasisPtr(const YGNodeRef node) {
if (node->style.flexBasis.unit != YGUnitUndefined) {
return &node->style.flexBasis;
}
if (!YGFloatIsUndefined(node->style.flex) && node->style.flex > 0.0f) {
return &YGValueZero;
}
return &YGValueUndefined;
}
inline YGValue YGNodeStyleGetFlexBasis(const YGNodeRef node) {
return *YGNodeStyleGetFlexBasisPtr(node);
}
void YGNodeStyleSetFlex(const YGNodeRef node, const float flex) {
if (node->style.flex != flex) {
node->style.flex = flex;
YGNodeMarkDirtyInternal(node);
}
}
#define YG_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \
void YGNodeSet##name(const YGNodeRef node, type paramName) { \
node->instanceName = paramName; \
} \
\
type YGNodeGet##name(const YGNodeRef node) { \
return node->instanceName; \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName != paramName) { \
node->style.instanceName = paramName; \
YGNodeMarkDirtyInternal(node); \
} \
}
#define YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL(type, name, paramName, instanceName) \
void YGNodeStyleSet##name(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPixel) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPixel; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
void YGNodeStyleSet##name##Percent(const YGNodeRef node, const type paramName) { \
if (node->style.instanceName.value != paramName || \
node->style.instanceName.unit != YGUnitPercent) { \
node->style.instanceName.value = paramName; \
node->style.instanceName.unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent; \
YGNodeMarkDirtyInternal(node); \
} \
}
#define YG_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \
YG_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \
\
type YGNodeStyleGet##name(const YGNodeRef node) { \
return node->style.instanceName; \
}
#define YG_NODE_STYLE_PROPERTY_UNIT_IMPL(type, name, paramName, instanceName) \
YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL(float, name, paramName, instanceName) \
\
type YGNodeStyleGet##name(const YGNodeRef node) { \
return node->style.instanceName; \
}
#define YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(type, name, paramName, instanceName, defaultValue) \
void YGNodeStyleSet##name(const YGNodeRef node, const YGEdge edge, const float paramName) { \
if (node->style.instanceName[edge].value != paramName || \
node->style.instanceName[edge].unit != YGUnitPixel) { \
node->style.instanceName[edge].value = paramName; \
node->style.instanceName[edge].unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPixel; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
void YGNodeStyleSet##name##Percent(const YGNodeRef node, \
const YGEdge edge, \
const float paramName) { \
if (node->style.instanceName[edge].value != paramName || \
node->style.instanceName[edge].unit != YGUnitPercent) { \
node->style.instanceName[edge].value = paramName; \
node->style.instanceName[edge].unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPercent; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
type YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \
return *YGComputedEdgeValue(node->style.instanceName, edge, &defaultValue); \
}
#define YG_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \
void YGNodeStyleSet##name(const YGNodeRef node, const YGEdge edge, const float paramName) { \
if (node->style.instanceName[edge].value != paramName || \
node->style.instanceName[edge].unit != YGUnitPixel) { \
node->style.instanceName[edge].value = paramName; \
node->style.instanceName[edge].unit = \
YGFloatIsUndefined(paramName) ? YGUnitUndefined : YGUnitPixel; \
YGNodeMarkDirtyInternal(node); \
} \
} \
\
float YGNodeStyleGet##name(const YGNodeRef node, const YGEdge edge) { \
return YGComputedEdgeValue(node->style.instanceName, edge, &defaultValue)->value; \
}
#define YG_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeRef node) { \
return node->layout.instanceName; \
}
#define YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(type, name, instanceName) \
type YGNodeLayoutGet##name(const YGNodeRef node, const YGEdge edge) { \
YG_ASSERT(edge <= YGEdgeEnd, "Cannot get layout properties of multi-edge shorthands"); \
\
if (edge == YGEdgeLeft) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeEnd]; \
} else { \
return node->layout.instanceName[YGEdgeStart]; \
} \
} \
\
if (edge == YGEdgeRight) { \
if (node->layout.direction == YGDirectionRTL) { \
return node->layout.instanceName[YGEdgeStart]; \
} else { \
return node->layout.instanceName[YGEdgeEnd]; \
} \
} \
\
return node->layout.instanceName[edge]; \
}
YG_NODE_PROPERTY_IMPL(void *, Context, context, context);
YG_NODE_PROPERTY_IMPL(YGPrintFunc, PrintFunc, printFunc, print);
YG_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout);
YG_NODE_STYLE_PROPERTY_IMPL(YGDirection, Direction, direction, direction);
YG_NODE_STYLE_PROPERTY_IMPL(YGFlexDirection, FlexDirection, flexDirection, flexDirection);
YG_NODE_STYLE_PROPERTY_IMPL(YGJustify, JustifyContent, justifyContent, justifyContent);
YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignContent, alignContent, alignContent);
YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignItems, alignItems, alignItems);
YG_NODE_STYLE_PROPERTY_IMPL(YGAlign, AlignSelf, alignSelf, alignSelf);
YG_NODE_STYLE_PROPERTY_IMPL(YGPositionType, PositionType, positionType, positionType);
YG_NODE_STYLE_PROPERTY_IMPL(YGWrap, FlexWrap, flexWrap, flexWrap);
YG_NODE_STYLE_PROPERTY_IMPL(YGOverflow, Overflow, overflow, overflow);
YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow);
YG_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink);
YG_NODE_STYLE_PROPERTY_SETTER_UNIT_IMPL(float, FlexBasis, flexBasis, flexBasis);
YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Position, position, position, YGValueUndefined);
YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Margin, margin, margin, YGValueZero);
YG_NODE_STYLE_EDGE_PROPERTY_UNIT_IMPL(YGValue, Padding, padding, padding, YGValueZero);
YG_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, YGValueZero);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, Width, width, dimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, Height, height, dimensions[YGDimensionHeight]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MinWidth, minWidth, minDimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MinHeight, minHeight, minDimensions[YGDimensionHeight]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MaxWidth, maxWidth, maxDimensions[YGDimensionWidth]);
YG_NODE_STYLE_PROPERTY_UNIT_IMPL(YGValue, MaxHeight, maxHeight, maxDimensions[YGDimensionHeight]);
// Yoga specific properties, not compatible with flexbox specification
YG_NODE_STYLE_PROPERTY_IMPL(float, AspectRatio, aspectRatio, aspectRatio);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[YGEdgeLeft]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[YGEdgeTop]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[YGEdgeRight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[YGEdgeBottom]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[YGDimensionWidth]);
YG_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[YGDimensionHeight]);
YG_NODE_LAYOUT_PROPERTY_IMPL(YGDirection, Direction, direction);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Margin, margin);
YG_NODE_LAYOUT_RESOLVED_PROPERTY_IMPL(float, Padding, padding);
uint32_t gCurrentGenerationCount = 0;
bool YGLayoutNodeInternal(const YGNodeRef node,
const float availableWidth,
const float availableHeight,
const YGDirection parentDirection,
const YGMeasureMode widthMeasureMode,
const YGMeasureMode heightMeasureMode,
const float parentWidth,
const float parentHeight,
const bool performLayout,
const char *reason);
inline bool YGFloatIsUndefined(const float value) {
return isnan(value);
}
static inline bool YGValueEqual(const YGValue a, const YGValue b) {
if (a.unit != b.unit) {
return false;
}
if (a.unit == YGUnitUndefined) {
return true;
}
return fabs(a.value - b.value) < 0.0001f;
}
static inline bool YGFloatsEqual(const float a, const float b) {
if (YGFloatIsUndefined(a)) {
return YGFloatIsUndefined(b);
}
return fabs(a - b) < 0.0001f;
}
static void YGIndent(const uint32_t n) {
for (uint32_t i = 0; i < n; i++) {
YGLog(YGLogLevelDebug, " ");
}
}
static void YGPrintNumberIfNotZero(const char *str, const YGValue *const number) {
if (!YGFloatsEqual(number->value, 0)) {
YGLog(YGLogLevelDebug,
"%s: %g%s, ",
str,
number->value,
number->unit == YGUnitPixel ? "px" : "%");
}
}
static void YGPrintNumberIfNotUndefinedf(const char *str, const float number) {
if (!YGFloatIsUndefined(number)) {
YGLog(YGLogLevelDebug, "%s: %g, ", str, number);
}
}
static void YGPrintNumberIfNotUndefined(const char *str, const YGValue *const number) {
if (number->unit != YGUnitUndefined) {
YGLog(YGLogLevelDebug,
"%s: %g%s, ",
str,
number->value,
number->unit == YGUnitPixel ? "px" : "%");
}
}
static bool YGFourValuesEqual(const YGValue four[4]) {
return YGValueEqual(four[0], four[1]) && YGValueEqual(four[0], four[2]) &&
YGValueEqual(four[0], four[3]);
}
static void YGNodePrintInternal(const YGNodeRef node,
const YGPrintOptions options,
const uint32_t level) {
YGIndent(level);
YGLog(YGLogLevelDebug, "{");
if (node->print) {
node->print(node);
}
if (options & YGPrintOptionsLayout) {
YGLog(YGLogLevelDebug, "layout: {");
YGLog(YGLogLevelDebug, "width: %g, ", node->layout.dimensions[YGDimensionWidth]);
YGLog(YGLogLevelDebug, "height: %g, ", node->layout.dimensions[YGDimensionHeight]);
YGLog(YGLogLevelDebug, "top: %g, ", node->layout.position[YGEdgeTop]);
YGLog(YGLogLevelDebug, "left: %g", node->layout.position[YGEdgeLeft]);
YGLog(YGLogLevelDebug, "}, ");
}
if (options & YGPrintOptionsStyle) {
if (node->style.flexDirection == YGFlexDirectionColumn) {
YGLog(YGLogLevelDebug, "flexDirection: 'column', ");
} else if (node->style.flexDirection == YGFlexDirectionColumnReverse) {
YGLog(YGLogLevelDebug, "flexDirection: 'column-reverse', ");
} else if (node->style.flexDirection == YGFlexDirectionRow) {
YGLog(YGLogLevelDebug, "flexDirection: 'row', ");
} else if (node->style.flexDirection == YGFlexDirectionRowReverse) {
YGLog(YGLogLevelDebug, "flexDirection: 'row-reverse', ");
}
if (node->style.justifyContent == YGJustifyCenter) {
YGLog(YGLogLevelDebug, "justifyContent: 'center', ");
} else if (node->style.justifyContent == YGJustifyFlexEnd) {
YGLog(YGLogLevelDebug, "justifyContent: 'flex-end', ");
} else if (node->style.justifyContent == YGJustifySpaceAround) {
YGLog(YGLogLevelDebug, "justifyContent: 'space-around', ");
} else if (node->style.justifyContent == YGJustifySpaceBetween) {
YGLog(YGLogLevelDebug, "justifyContent: 'space-between', ");
}
if (node->style.alignItems == YGAlignCenter) {
YGLog(YGLogLevelDebug, "alignItems: 'center', ");
} else if (node->style.alignItems == YGAlignFlexEnd) {
YGLog(YGLogLevelDebug, "alignItems: 'flex-end', ");
} else if (node->style.alignItems == YGAlignStretch) {
YGLog(YGLogLevelDebug, "alignItems: 'stretch', ");
}
if (node->style.alignContent == YGAlignCenter) {
YGLog(YGLogLevelDebug, "alignContent: 'center', ");
} else if (node->style.alignContent == YGAlignFlexEnd) {
YGLog(YGLogLevelDebug, "alignContent: 'flex-end', ");
} else if (node->style.alignContent == YGAlignStretch) {
YGLog(YGLogLevelDebug, "alignContent: 'stretch', ");
}
if (node->style.alignSelf == YGAlignFlexStart) {
YGLog(YGLogLevelDebug, "alignSelf: 'flex-start', ");
} else if (node->style.alignSelf == YGAlignCenter) {
YGLog(YGLogLevelDebug, "alignSelf: 'center', ");
} else if (node->style.alignSelf == YGAlignFlexEnd) {
YGLog(YGLogLevelDebug, "alignSelf: 'flex-end', ");
} else if (node->style.alignSelf == YGAlignStretch) {
YGLog(YGLogLevelDebug, "alignSelf: 'stretch', ");
}
YGPrintNumberIfNotUndefinedf("flexGrow", YGNodeStyleGetFlexGrow(node));
YGPrintNumberIfNotUndefinedf("flexShrink", YGNodeStyleGetFlexShrink(node));
YGPrintNumberIfNotUndefined("flexBasis", YGNodeStyleGetFlexBasisPtr(node));
if (node->style.overflow == YGOverflowHidden) {
YGLog(YGLogLevelDebug, "overflow: 'hidden', ");
} else if (node->style.overflow == YGOverflowVisible) {
YGLog(YGLogLevelDebug, "overflow: 'visible', ");
} else if (node->style.overflow == YGOverflowScroll) {
YGLog(YGLogLevelDebug, "overflow: 'scroll', ");
}
if (YGFourValuesEqual(node->style.margin)) {
YGPrintNumberIfNotZero("margin",
YGComputedEdgeValue(node->style.margin, YGEdgeLeft, &YGValueZero));
} else {
YGPrintNumberIfNotZero("marginLeft",
YGComputedEdgeValue(node->style.margin, YGEdgeLeft, &YGValueZero));
YGPrintNumberIfNotZero("marginRight",
YGComputedEdgeValue(node->style.margin, YGEdgeRight, &YGValueZero));
YGPrintNumberIfNotZero("marginTop",
YGComputedEdgeValue(node->style.margin, YGEdgeTop, &YGValueZero));
YGPrintNumberIfNotZero("marginBottom",
YGComputedEdgeValue(node->style.margin, YGEdgeBottom, &YGValueZero));
YGPrintNumberIfNotZero("marginStart",
YGComputedEdgeValue(node->style.margin, YGEdgeStart, &YGValueZero));
YGPrintNumberIfNotZero("marginEnd",
YGComputedEdgeValue(node->style.margin, YGEdgeEnd, &YGValueZero));
}
if (YGFourValuesEqual(node->style.padding)) {
YGPrintNumberIfNotZero("padding",
YGComputedEdgeValue(node->style.padding, YGEdgeLeft, &YGValueZero));
} else {
YGPrintNumberIfNotZero("paddingLeft",
YGComputedEdgeValue(node->style.padding, YGEdgeLeft, &YGValueZero));
YGPrintNumberIfNotZero("paddingRight",
YGComputedEdgeValue(node->style.padding, YGEdgeRight, &YGValueZero));
YGPrintNumberIfNotZero("paddingTop",
YGComputedEdgeValue(node->style.padding, YGEdgeTop, &YGValueZero));
YGPrintNumberIfNotZero("paddingBottom",
YGComputedEdgeValue(node->style.padding, YGEdgeBottom, &YGValueZero));
YGPrintNumberIfNotZero("paddingStart",
YGComputedEdgeValue(node->style.padding, YGEdgeStart, &YGValueZero));
YGPrintNumberIfNotZero("paddingEnd",
YGComputedEdgeValue(node->style.padding, YGEdgeEnd, &YGValueZero));
}
if (YGFourValuesEqual(node->style.border)) {
YGPrintNumberIfNotZero("borderWidth",
YGComputedEdgeValue(node->style.border, YGEdgeLeft, &YGValueZero));
} else {
YGPrintNumberIfNotZero("borderLeftWidth",
YGComputedEdgeValue(node->style.border, YGEdgeLeft, &YGValueZero));
YGPrintNumberIfNotZero("borderRightWidth",
YGComputedEdgeValue(node->style.border, YGEdgeRight, &YGValueZero));
YGPrintNumberIfNotZero("borderTopWidth",
YGComputedEdgeValue(node->style.border, YGEdgeTop, &YGValueZero));
YGPrintNumberIfNotZero("borderBottomWidth",
YGComputedEdgeValue(node->style.border, YGEdgeBottom, &YGValueZero));
YGPrintNumberIfNotZero("borderStartWidth",
YGComputedEdgeValue(node->style.border, YGEdgeStart, &YGValueZero));
YGPrintNumberIfNotZero("borderEndWidth",
YGComputedEdgeValue(node->style.border, YGEdgeEnd, &YGValueZero));
}
YGPrintNumberIfNotUndefined("width", &node->style.dimensions[YGDimensionWidth]);
YGPrintNumberIfNotUndefined("height", &node->style.dimensions[YGDimensionHeight]);
YGPrintNumberIfNotUndefined("maxWidth", &node->style.maxDimensions[YGDimensionWidth]);
YGPrintNumberIfNotUndefined("maxHeight", &node->style.maxDimensions[YGDimensionHeight]);
YGPrintNumberIfNotUndefined("minWidth", &node->style.minDimensions[YGDimensionWidth]);
YGPrintNumberIfNotUndefined("minHeight", &node->style.minDimensions[YGDimensionHeight]);
if (node->style.positionType == YGPositionTypeAbsolute) {
YGLog(YGLogLevelDebug, "position: 'absolute', ");
}
YGPrintNumberIfNotUndefined(
"left", YGComputedEdgeValue(node->style.position, YGEdgeLeft, &YGValueUndefined));
YGPrintNumberIfNotUndefined(
"right", YGComputedEdgeValue(node->style.position, YGEdgeRight, &YGValueUndefined));
YGPrintNumberIfNotUndefined(
"top", YGComputedEdgeValue(node->style.position, YGEdgeTop, &YGValueUndefined));
YGPrintNumberIfNotUndefined(
"bottom", YGComputedEdgeValue(node->style.position, YGEdgeBottom, &YGValueUndefined));
}
const uint32_t childCount = YGNodeListCount(node->children);
if (options & YGPrintOptionsChildren && childCount > 0) {
YGLog(YGLogLevelDebug, "children: [\n");
for (uint32_t i = 0; i < childCount; i++) {
YGNodePrintInternal(YGNodeGetChild(node, i), options, level + 1);
}
YGIndent(level);
YGLog(YGLogLevelDebug, "]},\n");
} else {
YGLog(YGLogLevelDebug, "},\n");
}
}
void YGNodePrint(const YGNodeRef node, const YGPrintOptions options) {
YGNodePrintInternal(node, options, 0);
}
static const YGEdge leading[4] = {
[YGFlexDirectionColumn] = YGEdgeTop,
[YGFlexDirectionColumnReverse] = YGEdgeBottom,
[YGFlexDirectionRow] = YGEdgeLeft,
[YGFlexDirectionRowReverse] = YGEdgeRight,
};
static const YGEdge trailing[4] = {
[YGFlexDirectionColumn] = YGEdgeBottom,
[YGFlexDirectionColumnReverse] = YGEdgeTop,
[YGFlexDirectionRow] = YGEdgeRight,
[YGFlexDirectionRowReverse] = YGEdgeLeft,
};
static const YGEdge pos[4] = {
[YGFlexDirectionColumn] = YGEdgeTop,
[YGFlexDirectionColumnReverse] = YGEdgeBottom,
[YGFlexDirectionRow] = YGEdgeLeft,
[YGFlexDirectionRowReverse] = YGEdgeRight,
};
static const YGDimension dim[4] = {
[YGFlexDirectionColumn] = YGDimensionHeight,
[YGFlexDirectionColumnReverse] = YGDimensionHeight,
[YGFlexDirectionRow] = YGDimensionWidth,
[YGFlexDirectionRowReverse] = YGDimensionWidth,
};
static inline bool YGFlexDirectionIsRow(const YGFlexDirection flexDirection) {
return flexDirection == YGFlexDirectionRow || flexDirection == YGFlexDirectionRowReverse;
}
static inline bool YGFlexDirectionIsColumn(const YGFlexDirection flexDirection) {
return flexDirection == YGFlexDirectionColumn || flexDirection == YGFlexDirectionColumnReverse;
}
static inline float YGNodeLeadingMargin(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
if (YGFlexDirectionIsRow(axis) && node->style.margin[YGEdgeStart].unit != YGUnitUndefined) {
return YGValueResolve(&node->style.margin[YGEdgeStart], widthSize);
}
return YGValueResolve(YGComputedEdgeValue(node->style.margin, leading[axis], &YGValueZero),
widthSize);
}
static float YGNodeTrailingMargin(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
if (YGFlexDirectionIsRow(axis) && node->style.margin[YGEdgeEnd].unit != YGUnitUndefined) {
return YGValueResolve(&node->style.margin[YGEdgeEnd], widthSize);
}
return YGValueResolve(YGComputedEdgeValue(node->style.margin, trailing[axis], &YGValueZero),
widthSize);
}
static float YGNodeLeadingPadding(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
if (YGFlexDirectionIsRow(axis) && node->style.padding[YGEdgeStart].unit != YGUnitUndefined &&
YGValueResolve(&node->style.padding[YGEdgeStart], widthSize) >= 0.0f) {
return YGValueResolve(&node->style.padding[YGEdgeStart], widthSize);
}
return fmaxf(YGValueResolve(YGComputedEdgeValue(node->style.padding, leading[axis], &YGValueZero),
widthSize),
0.0f);
}
static float YGNodeTrailingPadding(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
if (YGFlexDirectionIsRow(axis) && node->style.padding[YGEdgeEnd].unit != YGUnitUndefined &&
YGValueResolve(&node->style.padding[YGEdgeEnd], widthSize) >= 0.0f) {
return YGValueResolve(&node->style.padding[YGEdgeEnd], widthSize);
}
return fmaxf(YGValueResolve(YGComputedEdgeValue(node->style.padding, trailing[axis], &YGValueZero),
widthSize),
0.0f);
}
static float YGNodeLeadingBorder(const YGNodeRef node, const YGFlexDirection axis) {
if (YGFlexDirectionIsRow(axis) && node->style.border[YGEdgeStart].unit != YGUnitUndefined &&
node->style.border[YGEdgeStart].value >= 0.0f) {
return node->style.border[YGEdgeStart].value;
}
return fmaxf(YGComputedEdgeValue(node->style.border, leading[axis], &YGValueZero)->value, 0.0f);
}
static float YGNodeTrailingBorder(const YGNodeRef node, const YGFlexDirection axis) {
if (YGFlexDirectionIsRow(axis) && node->style.border[YGEdgeEnd].unit != YGUnitUndefined &&
node->style.border[YGEdgeEnd].value >= 0.0f) {
return node->style.border[YGEdgeEnd].value;
}
return fmaxf(YGComputedEdgeValue(node->style.border, trailing[axis], &YGValueZero)->value, 0.0f);
}
static inline float YGNodeLeadingPaddingAndBorder(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
return YGNodeLeadingPadding(node, axis, widthSize) + YGNodeLeadingBorder(node, axis);
}
static inline float YGNodeTrailingPaddingAndBorder(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
return YGNodeTrailingPadding(node, axis, widthSize) + YGNodeTrailingBorder(node, axis);
}
static inline float YGNodeMarginForAxis(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
return YGNodeLeadingMargin(node, axis, widthSize) + YGNodeTrailingMargin(node, axis, widthSize);
}
static inline float YGNodePaddingAndBorderForAxis(const YGNodeRef node,
const YGFlexDirection axis,
const float widthSize) {
return YGNodeLeadingPaddingAndBorder(node, axis, widthSize) +
YGNodeTrailingPaddingAndBorder(node, axis, widthSize);
}
static inline YGAlign YGNodeAlignItem(const YGNodeRef node, const YGNodeRef child) {
const YGAlign align =
child->style.alignSelf == YGAlignAuto ? node->style.alignItems : child->style.alignSelf;
if (align == YGAlignBaseline && YGFlexDirectionIsColumn(node->style.flexDirection)) {
return YGAlignFlexStart;
}
return align;
}
static inline YGDirection YGNodeResolveDirection(const YGNodeRef node,
const YGDirection parentDirection) {
if (node->style.direction == YGDirectionInherit) {
return parentDirection > YGDirectionInherit ? parentDirection : YGDirectionLTR;
} else {
return node->style.direction;
}
}
static float YGBaseline(const YGNodeRef node) {
if (node->baseline != NULL) {
const float baseline = node->baseline(node,
node->layout.measuredDimensions[YGDimensionWidth],
node->layout.measuredDimensions[YGDimensionHeight]);
YG_ASSERT(!YGFloatIsUndefined(baseline), "Expect custom baseline function to not return NaN")
return baseline;
}
YGNodeRef baselineChild = NULL;
for (uint32_t i = 0; i < YGNodeGetChildCount(node); i++) {
const YGNodeRef child = YGNodeGetChild(node, i);
if (child->lineIndex > 0) {
break;
}