-
Notifications
You must be signed in to change notification settings - Fork 24k
/
CSSLayout.c
2566 lines (2243 loc) · 104 KB
/
CSSLayout.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 "CSSLayout.h"
#include "CSSNodeList.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 CSSCachedMeasurement {
float availableWidth;
float availableHeight;
CSSMeasureMode widthMeasureMode;
CSSMeasureMode heightMeasureMode;
float computedWidth;
float computedHeight;
} CSSCachedMeasurement;
// 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.
enum { CSS_MAX_CACHED_RESULT_COUNT = 16 };
typedef struct CSSLayout {
float position[4];
float dimensions[2];
CSSDirection 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;
CSSDirection lastParentDirection;
uint32_t nextCachedMeasurementsIndex;
CSSCachedMeasurement cachedMeasurements[CSS_MAX_CACHED_RESULT_COUNT];
float measuredDimensions[2];
CSSCachedMeasurement cachedLayout;
} CSSLayout;
typedef struct CSSStyle {
CSSDirection direction;
CSSFlexDirection flexDirection;
CSSJustify justifyContent;
CSSAlign alignContent;
CSSAlign alignItems;
CSSAlign alignSelf;
CSSPositionType positionType;
CSSWrap flexWrap;
CSSOverflow overflow;
float flex;
float flexGrow;
float flexShrink;
float flexBasis;
float margin[CSSEdgeCount];
float position[CSSEdgeCount];
float padding[CSSEdgeCount];
float border[CSSEdgeCount];
float dimensions[2];
float minDimensions[2];
float maxDimensions[2];
} CSSStyle;
typedef struct CSSNode {
CSSStyle style;
CSSLayout layout;
uint32_t lineIndex;
bool hasNewLayout;
CSSNodeRef parent;
CSSNodeListRef children;
bool isDirty;
struct CSSNode *nextChild;
CSSMeasureFunc measure;
CSSPrintFunc print;
void *context;
} CSSNode;
static void _CSSNodeMarkDirty(const CSSNodeRef node);
CSSMalloc gCSSMalloc = &malloc;
CSSCalloc gCSSCalloc = &calloc;
CSSRealloc gCSSRealloc = &realloc;
CSSFree gCSSFree = &free;
#ifdef ANDROID
#include <android/log.h>
static int _csslayoutAndroidLog(CSSLogLevel level, const char *format, va_list args) {
int androidLevel = CSSLogLevelDebug;
switch (level) {
case CSSLogLevelError:
androidLevel = ANDROID_LOG_ERROR;
break;
case CSSLogLevelWarn:
androidLevel = ANDROID_LOG_WARN;
break;
case CSSLogLevelInfo:
androidLevel = ANDROID_LOG_INFO;
break;
case CSSLogLevelDebug:
androidLevel = ANDROID_LOG_DEBUG;
break;
case CSSLogLevelVerbose:
androidLevel = ANDROID_LOG_VERBOSE;
break;
case CSSLogLevelCount:
break;
}
const int result = __android_log_vprint(androidLevel, "css-layout", format, args);
return result;
}
static CSSLogger gLogger = &_csslayoutAndroidLog;
#else
static int _csslayoutDefaultLog(CSSLogLevel level, const char *format, va_list args) {
switch (level) {
case CSSLogLevelError:
return vfprintf(stderr, format, args);
case CSSLogLevelWarn:
case CSSLogLevelInfo:
case CSSLogLevelDebug:
case CSSLogLevelVerbose:
default:
return vprintf(format, args);
}
}
static CSSLogger gLogger = &_csslayoutDefaultLog;
#endif
static inline float computedEdgeValue(const float edges[CSSEdgeCount],
const CSSEdge edge,
const float defaultValue) {
CSS_ASSERT(edge <= CSSEdgeEnd, "Cannot get computed value of multi-edge shorthands");
if (!CSSValueIsUndefined(edges[edge])) {
return edges[edge];
}
if ((edge == CSSEdgeTop || edge == CSSEdgeBottom) &&
!CSSValueIsUndefined(edges[CSSEdgeVertical])) {
return edges[CSSEdgeVertical];
}
if ((edge == CSSEdgeLeft || edge == CSSEdgeRight || edge == CSSEdgeStart || edge == CSSEdgeEnd) &&
!CSSValueIsUndefined(edges[CSSEdgeHorizontal])) {
return edges[CSSEdgeHorizontal];
}
if (!CSSValueIsUndefined(edges[CSSEdgeAll])) {
return edges[CSSEdgeAll];
}
if (edge == CSSEdgeStart || edge == CSSEdgeEnd) {
return CSSUndefined;
}
return defaultValue;
}
int32_t gNodeInstanceCount = 0;
CSSNodeRef CSSNodeNew(void) {
const CSSNodeRef node = gCSSCalloc(1, sizeof(CSSNode));
CSS_ASSERT(node, "Could not allocate memory for node");
gNodeInstanceCount++;
CSSNodeInit(node);
return node;
}
void CSSNodeFree(const CSSNodeRef node) {
if (node->parent) {
CSSNodeListDelete(node->parent->children, node);
node->parent = NULL;
}
const uint32_t childCount = CSSNodeChildCount(node);
for (uint32_t i = 0; i < childCount; i++) {
const CSSNodeRef child = CSSNodeGetChild(node, i);
child->parent = NULL;
}
CSSNodeListFree(node->children);
gCSSFree(node);
gNodeInstanceCount--;
}
void CSSNodeFreeRecursive(const CSSNodeRef root) {
while (CSSNodeChildCount(root) > 0) {
const CSSNodeRef child = CSSNodeGetChild(root, 0);
CSSNodeRemoveChild(root, child);
CSSNodeFreeRecursive(child);
}
CSSNodeFree(root);
}
void CSSNodeReset(const CSSNodeRef node) {
CSS_ASSERT(CSSNodeChildCount(node) == 0, "Cannot reset a node which still has children attached");
CSS_ASSERT(node->parent == NULL, "Cannot reset a node still attached to a parent");
CSSNodeListFree(node->children);
memset(node, 0, sizeof(CSSNode));
CSSNodeInit(node);
}
int32_t CSSNodeGetInstanceCount(void) {
return gNodeInstanceCount;
}
void CSSNodeInit(const CSSNodeRef node) {
node->parent = NULL;
node->children = NULL;
node->hasNewLayout = true;
node->isDirty = false;
node->style.flex = CSSUndefined;
node->style.flexGrow = CSSUndefined;
node->style.flexShrink = CSSUndefined;
node->style.flexBasis = CSSUndefined;
node->style.alignItems = CSSAlignStretch;
node->style.alignContent = CSSAlignFlexStart;
node->style.direction = CSSDirectionInherit;
node->style.flexDirection = CSSFlexDirectionColumn;
node->style.overflow = CSSOverflowVisible;
// Some of the fields default to undefined and not 0
node->style.dimensions[CSSDimensionWidth] = CSSUndefined;
node->style.dimensions[CSSDimensionHeight] = CSSUndefined;
node->style.minDimensions[CSSDimensionWidth] = CSSUndefined;
node->style.minDimensions[CSSDimensionHeight] = CSSUndefined;
node->style.maxDimensions[CSSDimensionWidth] = CSSUndefined;
node->style.maxDimensions[CSSDimensionHeight] = CSSUndefined;
for (CSSEdge edge = CSSEdgeLeft; edge < CSSEdgeCount; edge++) {
node->style.position[edge] = CSSUndefined;
node->style.margin[edge] = CSSUndefined;
node->style.padding[edge] = CSSUndefined;
node->style.border[edge] = CSSUndefined;
}
node->layout.dimensions[CSSDimensionWidth] = CSSUndefined;
node->layout.dimensions[CSSDimensionHeight] = CSSUndefined;
// Such that the comparison is always going to be false
node->layout.lastParentDirection = (CSSDirection) -1;
node->layout.nextCachedMeasurementsIndex = 0;
node->layout.computedFlexBasis = CSSUndefined;
node->layout.measuredDimensions[CSSDimensionWidth] = CSSUndefined;
node->layout.measuredDimensions[CSSDimensionHeight] = CSSUndefined;
node->layout.cachedLayout.widthMeasureMode = (CSSMeasureMode) -1;
node->layout.cachedLayout.heightMeasureMode = (CSSMeasureMode) -1;
node->layout.cachedLayout.computedWidth = -1;
node->layout.cachedLayout.computedHeight = -1;
}
static void _CSSNodeMarkDirty(const CSSNodeRef node) {
if (!node->isDirty) {
node->isDirty = true;
node->layout.computedFlexBasis = CSSUndefined;
if (node->parent) {
_CSSNodeMarkDirty(node->parent);
}
}
}
void CSSNodeSetMeasureFunc(const CSSNodeRef node, CSSMeasureFunc measureFunc) {
if (measureFunc == NULL) {
node->measure = NULL;
} else {
CSS_ASSERT(CSSNodeChildCount(node) == 0,
"Cannot set measure function: Nodes with measure functions cannot have children.");
node->measure = measureFunc;
}
}
CSSMeasureFunc CSSNodeGetMeasureFunc(const CSSNodeRef node) {
return node->measure;
}
void CSSNodeInsertChild(const CSSNodeRef node, const CSSNodeRef child, const uint32_t index) {
CSS_ASSERT(child->parent == NULL, "Child already has a parent, it must be removed first.");
CSS_ASSERT(node->measure == NULL,
"Cannot add child: Nodes with measure functions cannot have children.");
CSSNodeListInsert(&node->children, child, index);
child->parent = node;
_CSSNodeMarkDirty(node);
}
void CSSNodeRemoveChild(const CSSNodeRef node, const CSSNodeRef child) {
if (CSSNodeListDelete(node->children, child) != NULL) {
child->parent = NULL;
_CSSNodeMarkDirty(node);
}
}
CSSNodeRef CSSNodeGetChild(const CSSNodeRef node, const uint32_t index) {
return CSSNodeListGet(node->children, index);
}
inline uint32_t CSSNodeChildCount(const CSSNodeRef node) {
return CSSNodeListCount(node->children);
}
void CSSNodeMarkDirty(const CSSNodeRef node) {
CSS_ASSERT(node->measure != NULL,
"Only leaf nodes with custom measure functions"
"should manually mark themselves as dirty");
_CSSNodeMarkDirty(node);
}
bool CSSNodeIsDirty(const CSSNodeRef node) {
return node->isDirty;
}
void CSSNodeCopyStyle(const CSSNodeRef dstNode, const CSSNodeRef srcNode) {
if (memcmp(&dstNode->style, &srcNode->style, sizeof(CSSStyle)) != 0) {
memcpy(&dstNode->style, &srcNode->style, sizeof(CSSStyle));
_CSSNodeMarkDirty(dstNode);
}
}
inline float CSSNodeStyleGetFlexGrow(const CSSNodeRef node) {
if (!CSSValueIsUndefined(node->style.flexGrow)) {
return node->style.flexGrow;
}
if (!CSSValueIsUndefined(node->style.flex) && node->style.flex > 0) {
return node->style.flex;
}
return 0;
}
inline float CSSNodeStyleGetFlexShrink(const CSSNodeRef node) {
if (!CSSValueIsUndefined(node->style.flexShrink)) {
return node->style.flexShrink;
}
if (!CSSValueIsUndefined(node->style.flex) && node->style.flex < 0) {
return -node->style.flex;
}
return 0;
}
inline float CSSNodeStyleGetFlexBasis(const CSSNodeRef node) {
if (!CSSValueIsUndefined(node->style.flexBasis)) {
return node->style.flexBasis;
}
if (!CSSValueIsUndefined(node->style.flex)) {
return node->style.flex > 0 ? 0 : CSSUndefined;
}
return CSSUndefined;
}
void CSSNodeStyleSetFlex(const CSSNodeRef node, const float flex) {
if (node->style.flex != flex) {
node->style.flex = flex;
_CSSNodeMarkDirty(node);
}
}
#define CSS_NODE_PROPERTY_IMPL(type, name, paramName, instanceName) \
void CSSNodeSet##name(const CSSNodeRef node, type paramName) { \
node->instanceName = paramName; \
} \
\
type CSSNodeGet##name(const CSSNodeRef node) { \
return node->instanceName; \
}
#define CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \
void CSSNodeStyleSet##name(const CSSNodeRef node, const type paramName) { \
if (node->style.instanceName != paramName) { \
node->style.instanceName = paramName; \
_CSSNodeMarkDirty(node); \
} \
}
#define CSS_NODE_STYLE_PROPERTY_IMPL(type, name, paramName, instanceName) \
CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(type, name, paramName, instanceName) \
\
type CSSNodeStyleGet##name(const CSSNodeRef node) { \
return node->style.instanceName; \
}
#define CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(type, name, paramName, instanceName, defaultValue) \
void CSSNodeStyleSet##name(const CSSNodeRef node, const CSSEdge edge, const type paramName) { \
if (node->style.instanceName[edge] != paramName) { \
node->style.instanceName[edge] = paramName; \
_CSSNodeMarkDirty(node); \
} \
} \
\
type CSSNodeStyleGet##name(const CSSNodeRef node, const CSSEdge edge) { \
return computedEdgeValue(node->style.instanceName, edge, defaultValue); \
}
#define CSS_NODE_LAYOUT_PROPERTY_IMPL(type, name, instanceName) \
type CSSNodeLayoutGet##name(const CSSNodeRef node) { \
return node->layout.instanceName; \
}
CSS_NODE_PROPERTY_IMPL(void *, Context, context, context);
CSS_NODE_PROPERTY_IMPL(CSSPrintFunc, PrintFunc, printFunc, print);
CSS_NODE_PROPERTY_IMPL(bool, HasNewLayout, hasNewLayout, hasNewLayout);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSDirection, Direction, direction, direction);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSFlexDirection, FlexDirection, flexDirection, flexDirection);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSJustify, JustifyContent, justifyContent, justifyContent);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSAlign, AlignContent, alignContent, alignContent);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSAlign, AlignItems, alignItems, alignItems);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSAlign, AlignSelf, alignSelf, alignSelf);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSPositionType, PositionType, positionType, positionType);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSWrap, FlexWrap, flexWrap, flexWrap);
CSS_NODE_STYLE_PROPERTY_IMPL(CSSOverflow, Overflow, overflow, overflow);
CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexGrow, flexGrow, flexGrow);
CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexShrink, flexShrink, flexShrink);
CSS_NODE_STYLE_PROPERTY_SETTER_IMPL(float, FlexBasis, flexBasis, flexBasis);
CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Position, position, position, CSSUndefined);
CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Margin, margin, margin, 0);
CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Padding, padding, padding, 0);
CSS_NODE_STYLE_EDGE_PROPERTY_IMPL(float, Border, border, border, 0);
CSS_NODE_STYLE_PROPERTY_IMPL(float, Width, width, dimensions[CSSDimensionWidth]);
CSS_NODE_STYLE_PROPERTY_IMPL(float, Height, height, dimensions[CSSDimensionHeight]);
CSS_NODE_STYLE_PROPERTY_IMPL(float, MinWidth, minWidth, minDimensions[CSSDimensionWidth]);
CSS_NODE_STYLE_PROPERTY_IMPL(float, MinHeight, minHeight, minDimensions[CSSDimensionHeight]);
CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxWidth, maxWidth, maxDimensions[CSSDimensionWidth]);
CSS_NODE_STYLE_PROPERTY_IMPL(float, MaxHeight, maxHeight, maxDimensions[CSSDimensionHeight]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Left, position[CSSEdgeLeft]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Top, position[CSSEdgeTop]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Right, position[CSSEdgeRight]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Bottom, position[CSSEdgeBottom]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Width, dimensions[CSSDimensionWidth]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(float, Height, dimensions[CSSDimensionHeight]);
CSS_NODE_LAYOUT_PROPERTY_IMPL(CSSDirection, Direction, direction);
uint32_t gCurrentGenerationCount = 0;
bool layoutNodeInternal(const CSSNodeRef node,
const float availableWidth,
const float availableHeight,
const CSSDirection parentDirection,
const CSSMeasureMode widthMeasureMode,
const CSSMeasureMode heightMeasureMode,
const bool performLayout,
const char *reason);
inline bool CSSValueIsUndefined(const float value) {
return isnan(value);
}
static inline bool eq(const float a, const float b) {
if (CSSValueIsUndefined(a)) {
return CSSValueIsUndefined(b);
}
return fabs(a - b) < 0.0001;
}
static void indent(const uint32_t n) {
for (uint32_t i = 0; i < n; i++) {
CSSLog(CSSLogLevelDebug, " ");
}
}
static void printNumberIfNotZero(const char *str, const float number) {
if (!eq(number, 0)) {
CSSLog(CSSLogLevelDebug, "%s: %g, ", str, number);
}
}
static void printNumberIfNotUndefined(const char *str, const float number) {
if (!CSSValueIsUndefined(number)) {
CSSLog(CSSLogLevelDebug, "%s: %g, ", str, number);
}
}
static bool eqFour(const float four[4]) {
return eq(four[0], four[1]) && eq(four[0], four[2]) && eq(four[0], four[3]);
}
static void _CSSNodePrint(const CSSNodeRef node,
const CSSPrintOptions options,
const uint32_t level) {
indent(level);
CSSLog(CSSLogLevelDebug, "{");
if (node->print) {
node->print(node);
}
if (options & CSSPrintOptionsLayout) {
CSSLog(CSSLogLevelDebug, "layout: {");
CSSLog(CSSLogLevelDebug, "width: %g, ", node->layout.dimensions[CSSDimensionWidth]);
CSSLog(CSSLogLevelDebug, "height: %g, ", node->layout.dimensions[CSSDimensionHeight]);
CSSLog(CSSLogLevelDebug, "top: %g, ", node->layout.position[CSSEdgeTop]);
CSSLog(CSSLogLevelDebug, "left: %g", node->layout.position[CSSEdgeLeft]);
CSSLog(CSSLogLevelDebug, "}, ");
}
if (options & CSSPrintOptionsStyle) {
if (node->style.flexDirection == CSSFlexDirectionColumn) {
CSSLog(CSSLogLevelDebug, "flexDirection: 'column', ");
} else if (node->style.flexDirection == CSSFlexDirectionColumnReverse) {
CSSLog(CSSLogLevelDebug, "flexDirection: 'column-reverse', ");
} else if (node->style.flexDirection == CSSFlexDirectionRow) {
CSSLog(CSSLogLevelDebug, "flexDirection: 'row', ");
} else if (node->style.flexDirection == CSSFlexDirectionRowReverse) {
CSSLog(CSSLogLevelDebug, "flexDirection: 'row-reverse', ");
}
if (node->style.justifyContent == CSSJustifyCenter) {
CSSLog(CSSLogLevelDebug, "justifyContent: 'center', ");
} else if (node->style.justifyContent == CSSJustifyFlexEnd) {
CSSLog(CSSLogLevelDebug, "justifyContent: 'flex-end', ");
} else if (node->style.justifyContent == CSSJustifySpaceAround) {
CSSLog(CSSLogLevelDebug, "justifyContent: 'space-around', ");
} else if (node->style.justifyContent == CSSJustifySpaceBetween) {
CSSLog(CSSLogLevelDebug, "justifyContent: 'space-between', ");
}
if (node->style.alignItems == CSSAlignCenter) {
CSSLog(CSSLogLevelDebug, "alignItems: 'center', ");
} else if (node->style.alignItems == CSSAlignFlexEnd) {
CSSLog(CSSLogLevelDebug, "alignItems: 'flex-end', ");
} else if (node->style.alignItems == CSSAlignStretch) {
CSSLog(CSSLogLevelDebug, "alignItems: 'stretch', ");
}
if (node->style.alignContent == CSSAlignCenter) {
CSSLog(CSSLogLevelDebug, "alignContent: 'center', ");
} else if (node->style.alignContent == CSSAlignFlexEnd) {
CSSLog(CSSLogLevelDebug, "alignContent: 'flex-end', ");
} else if (node->style.alignContent == CSSAlignStretch) {
CSSLog(CSSLogLevelDebug, "alignContent: 'stretch', ");
}
if (node->style.alignSelf == CSSAlignFlexStart) {
CSSLog(CSSLogLevelDebug, "alignSelf: 'flex-start', ");
} else if (node->style.alignSelf == CSSAlignCenter) {
CSSLog(CSSLogLevelDebug, "alignSelf: 'center', ");
} else if (node->style.alignSelf == CSSAlignFlexEnd) {
CSSLog(CSSLogLevelDebug, "alignSelf: 'flex-end', ");
} else if (node->style.alignSelf == CSSAlignStretch) {
CSSLog(CSSLogLevelDebug, "alignSelf: 'stretch', ");
}
printNumberIfNotUndefined("flexGrow", CSSNodeStyleGetFlexGrow(node));
printNumberIfNotUndefined("flexShrink", CSSNodeStyleGetFlexShrink(node));
printNumberIfNotUndefined("flexBasis", CSSNodeStyleGetFlexBasis(node));
if (node->style.overflow == CSSOverflowHidden) {
CSSLog(CSSLogLevelDebug, "overflow: 'hidden', ");
} else if (node->style.overflow == CSSOverflowVisible) {
CSSLog(CSSLogLevelDebug, "overflow: 'visible', ");
} else if (node->style.overflow == CSSOverflowScroll) {
CSSLog(CSSLogLevelDebug, "overflow: 'scroll', ");
}
if (eqFour(node->style.margin)) {
printNumberIfNotZero("margin", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0));
} else {
printNumberIfNotZero("marginLeft", computedEdgeValue(node->style.margin, CSSEdgeLeft, 0));
printNumberIfNotZero("marginRight", computedEdgeValue(node->style.margin, CSSEdgeRight, 0));
printNumberIfNotZero("marginTop", computedEdgeValue(node->style.margin, CSSEdgeTop, 0));
printNumberIfNotZero("marginBottom", computedEdgeValue(node->style.margin, CSSEdgeBottom, 0));
printNumberIfNotZero("marginStart", computedEdgeValue(node->style.margin, CSSEdgeStart, 0));
printNumberIfNotZero("marginEnd", computedEdgeValue(node->style.margin, CSSEdgeEnd, 0));
}
if (eqFour(node->style.padding)) {
printNumberIfNotZero("padding", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0));
} else {
printNumberIfNotZero("paddingLeft", computedEdgeValue(node->style.padding, CSSEdgeLeft, 0));
printNumberIfNotZero("paddingRight", computedEdgeValue(node->style.padding, CSSEdgeRight, 0));
printNumberIfNotZero("paddingTop", computedEdgeValue(node->style.padding, CSSEdgeTop, 0));
printNumberIfNotZero("paddingBottom",
computedEdgeValue(node->style.padding, CSSEdgeBottom, 0));
printNumberIfNotZero("paddingStart", computedEdgeValue(node->style.padding, CSSEdgeStart, 0));
printNumberIfNotZero("paddingEnd", computedEdgeValue(node->style.padding, CSSEdgeEnd, 0));
}
if (eqFour(node->style.border)) {
printNumberIfNotZero("borderWidth", computedEdgeValue(node->style.border, CSSEdgeLeft, 0));
} else {
printNumberIfNotZero("borderLeftWidth",
computedEdgeValue(node->style.border, CSSEdgeLeft, 0));
printNumberIfNotZero("borderRightWidth",
computedEdgeValue(node->style.border, CSSEdgeRight, 0));
printNumberIfNotZero("borderTopWidth", computedEdgeValue(node->style.border, CSSEdgeTop, 0));
printNumberIfNotZero("borderBottomWidth",
computedEdgeValue(node->style.border, CSSEdgeBottom, 0));
printNumberIfNotZero("borderStartWidth",
computedEdgeValue(node->style.border, CSSEdgeStart, 0));
printNumberIfNotZero("borderEndWidth", computedEdgeValue(node->style.border, CSSEdgeEnd, 0));
}
printNumberIfNotUndefined("width", node->style.dimensions[CSSDimensionWidth]);
printNumberIfNotUndefined("height", node->style.dimensions[CSSDimensionHeight]);
printNumberIfNotUndefined("maxWidth", node->style.maxDimensions[CSSDimensionWidth]);
printNumberIfNotUndefined("maxHeight", node->style.maxDimensions[CSSDimensionHeight]);
printNumberIfNotUndefined("minWidth", node->style.minDimensions[CSSDimensionWidth]);
printNumberIfNotUndefined("minHeight", node->style.minDimensions[CSSDimensionHeight]);
if (node->style.positionType == CSSPositionTypeAbsolute) {
CSSLog(CSSLogLevelDebug, "position: 'absolute', ");
}
printNumberIfNotUndefined("left",
computedEdgeValue(node->style.position, CSSEdgeLeft, CSSUndefined));
printNumberIfNotUndefined("right",
computedEdgeValue(node->style.position, CSSEdgeRight, CSSUndefined));
printNumberIfNotUndefined("top",
computedEdgeValue(node->style.position, CSSEdgeTop, CSSUndefined));
printNumberIfNotUndefined("bottom",
computedEdgeValue(node->style.position, CSSEdgeBottom, CSSUndefined));
}
const uint32_t childCount = CSSNodeListCount(node->children);
if (options & CSSPrintOptionsChildren && childCount > 0) {
CSSLog(CSSLogLevelDebug, "children: [\n");
for (uint32_t i = 0; i < childCount; i++) {
_CSSNodePrint(CSSNodeGetChild(node, i), options, level + 1);
}
indent(level);
CSSLog(CSSLogLevelDebug, "]},\n");
} else {
CSSLog(CSSLogLevelDebug, "},\n");
}
}
void CSSNodePrint(const CSSNodeRef node, const CSSPrintOptions options) {
_CSSNodePrint(node, options, 0);
}
static const CSSEdge leading[4] = {
[CSSFlexDirectionColumn] = CSSEdgeTop,
[CSSFlexDirectionColumnReverse] = CSSEdgeBottom,
[CSSFlexDirectionRow] = CSSEdgeLeft,
[CSSFlexDirectionRowReverse] = CSSEdgeRight,
};
static const CSSEdge trailing[4] = {
[CSSFlexDirectionColumn] = CSSEdgeBottom,
[CSSFlexDirectionColumnReverse] = CSSEdgeTop,
[CSSFlexDirectionRow] = CSSEdgeRight,
[CSSFlexDirectionRowReverse] = CSSEdgeLeft,
};
static const CSSEdge pos[4] = {
[CSSFlexDirectionColumn] = CSSEdgeTop,
[CSSFlexDirectionColumnReverse] = CSSEdgeBottom,
[CSSFlexDirectionRow] = CSSEdgeLeft,
[CSSFlexDirectionRowReverse] = CSSEdgeRight,
};
static const CSSDimension dim[4] = {
[CSSFlexDirectionColumn] = CSSDimensionHeight,
[CSSFlexDirectionColumnReverse] = CSSDimensionHeight,
[CSSFlexDirectionRow] = CSSDimensionWidth,
[CSSFlexDirectionRowReverse] = CSSDimensionWidth,
};
static inline bool isRowDirection(const CSSFlexDirection flexDirection) {
return flexDirection == CSSFlexDirectionRow || flexDirection == CSSFlexDirectionRowReverse;
}
static inline bool isColumnDirection(const CSSFlexDirection flexDirection) {
return flexDirection == CSSFlexDirectionColumn || flexDirection == CSSFlexDirectionColumnReverse;
}
static inline float getLeadingMargin(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[CSSEdgeStart])) {
return node->style.margin[CSSEdgeStart];
}
return computedEdgeValue(node->style.margin, leading[axis], 0);
}
static float getTrailingMargin(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.margin[CSSEdgeEnd])) {
return node->style.margin[CSSEdgeEnd];
}
return computedEdgeValue(node->style.margin, trailing[axis], 0);
}
static float getLeadingPadding(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[CSSEdgeStart]) &&
node->style.padding[CSSEdgeStart] >= 0) {
return node->style.padding[CSSEdgeStart];
}
return fmaxf(computedEdgeValue(node->style.padding, leading[axis], 0), 0);
}
static float getTrailingPadding(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.padding[CSSEdgeEnd]) &&
node->style.padding[CSSEdgeEnd] >= 0) {
return node->style.padding[CSSEdgeEnd];
}
return fmaxf(computedEdgeValue(node->style.padding, trailing[axis], 0), 0);
}
static float getLeadingBorder(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[CSSEdgeStart]) &&
node->style.border[CSSEdgeStart] >= 0) {
return node->style.border[CSSEdgeStart];
}
return fmaxf(computedEdgeValue(node->style.border, leading[axis], 0), 0);
}
static float getTrailingBorder(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis) && !CSSValueIsUndefined(node->style.border[CSSEdgeEnd]) &&
node->style.border[CSSEdgeEnd] >= 0) {
return node->style.border[CSSEdgeEnd];
}
return fmaxf(computedEdgeValue(node->style.border, trailing[axis], 0), 0);
}
static inline float getLeadingPaddingAndBorder(const CSSNodeRef node, const CSSFlexDirection axis) {
return getLeadingPadding(node, axis) + getLeadingBorder(node, axis);
}
static inline float getTrailingPaddingAndBorder(const CSSNodeRef node,
const CSSFlexDirection axis) {
return getTrailingPadding(node, axis) + getTrailingBorder(node, axis);
}
static inline float getMarginAxis(const CSSNodeRef node, const CSSFlexDirection axis) {
return getLeadingMargin(node, axis) + getTrailingMargin(node, axis);
}
static inline float getPaddingAndBorderAxis(const CSSNodeRef node, const CSSFlexDirection axis) {
return getLeadingPaddingAndBorder(node, axis) + getTrailingPaddingAndBorder(node, axis);
}
static inline CSSAlign getAlignItem(const CSSNodeRef node, const CSSNodeRef child) {
return child->style.alignSelf == CSSAlignAuto ? node->style.alignItems : child->style.alignSelf;
}
static inline CSSDirection resolveDirection(const CSSNodeRef node,
const CSSDirection parentDirection) {
if (node->style.direction == CSSDirectionInherit) {
return parentDirection > CSSDirectionInherit ? parentDirection : CSSDirectionLTR;
} else {
return node->style.direction;
}
}
static inline CSSFlexDirection resolveAxis(const CSSFlexDirection flexDirection,
const CSSDirection direction) {
if (direction == CSSDirectionRTL) {
if (flexDirection == CSSFlexDirectionRow) {
return CSSFlexDirectionRowReverse;
} else if (flexDirection == CSSFlexDirectionRowReverse) {
return CSSFlexDirectionRow;
}
}
return flexDirection;
}
static CSSFlexDirection getCrossFlexDirection(const CSSFlexDirection flexDirection,
const CSSDirection direction) {
return isColumnDirection(flexDirection) ? resolveAxis(CSSFlexDirectionRow, direction)
: CSSFlexDirectionColumn;
}
static inline bool isFlex(const CSSNodeRef node) {
return (node->style.positionType == CSSPositionTypeRelative &&
(node->style.flexGrow != 0 || node->style.flexShrink != 0 || node->style.flex != 0));
}
static inline float getDimWithMargin(const CSSNodeRef node, const CSSFlexDirection axis) {
return node->layout.measuredDimensions[dim[axis]] + getLeadingMargin(node, axis) +
getTrailingMargin(node, axis);
}
static inline bool isStyleDimDefined(const CSSNodeRef node, const CSSFlexDirection axis) {
const float value = node->style.dimensions[dim[axis]];
return !CSSValueIsUndefined(value) && value >= 0.0;
}
static inline bool isLayoutDimDefined(const CSSNodeRef node, const CSSFlexDirection axis) {
const float value = node->layout.measuredDimensions[dim[axis]];
return !CSSValueIsUndefined(value) && value >= 0.0;
}
static inline bool isLeadingPosDefined(const CSSNodeRef node, const CSSFlexDirection axis) {
return (isRowDirection(axis) &&
!CSSValueIsUndefined(
computedEdgeValue(node->style.position, CSSEdgeStart, CSSUndefined))) ||
!CSSValueIsUndefined(computedEdgeValue(node->style.position, leading[axis], CSSUndefined));
}
static inline bool isTrailingPosDefined(const CSSNodeRef node, const CSSFlexDirection axis) {
return (isRowDirection(axis) &&
!CSSValueIsUndefined(
computedEdgeValue(node->style.position, CSSEdgeEnd, CSSUndefined))) ||
!CSSValueIsUndefined(
computedEdgeValue(node->style.position, trailing[axis], CSSUndefined));
}
static float getLeadingPosition(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis)) {
const float leadingPosition =
computedEdgeValue(node->style.position, CSSEdgeStart, CSSUndefined);
if (!CSSValueIsUndefined(leadingPosition)) {
return leadingPosition;
}
}
const float leadingPosition =
computedEdgeValue(node->style.position, leading[axis], CSSUndefined);
return CSSValueIsUndefined(leadingPosition) ? 0 : leadingPosition;
}
static float getTrailingPosition(const CSSNodeRef node, const CSSFlexDirection axis) {
if (isRowDirection(axis)) {
const float trailingPosition =
computedEdgeValue(node->style.position, CSSEdgeEnd, CSSUndefined);
if (!CSSValueIsUndefined(trailingPosition)) {
return trailingPosition;
}
}
const float trailingPosition =
computedEdgeValue(node->style.position, trailing[axis], CSSUndefined);
return CSSValueIsUndefined(trailingPosition) ? 0 : trailingPosition;
}
static float boundAxisWithinMinAndMax(const CSSNodeRef node,
const CSSFlexDirection axis,
const float value) {
float min = CSSUndefined;
float max = CSSUndefined;
if (isColumnDirection(axis)) {
min = node->style.minDimensions[CSSDimensionHeight];
max = node->style.maxDimensions[CSSDimensionHeight];
} else if (isRowDirection(axis)) {
min = node->style.minDimensions[CSSDimensionWidth];
max = node->style.maxDimensions[CSSDimensionWidth];
}
float boundValue = value;
if (!CSSValueIsUndefined(max) && max >= 0.0 && boundValue > max) {
boundValue = max;
}
if (!CSSValueIsUndefined(min) && min >= 0.0 && boundValue < min) {
boundValue = min;
}
return boundValue;
}
// Like boundAxisWithinMinAndMax but also ensures that the value doesn't go
// below the
// padding and border amount.
static inline float boundAxis(const CSSNodeRef node,
const CSSFlexDirection axis,
const float value) {
return fmaxf(boundAxisWithinMinAndMax(node, axis, value), getPaddingAndBorderAxis(node, axis));
}
static void setTrailingPosition(const CSSNodeRef node,
const CSSNodeRef child,
const CSSFlexDirection axis) {
const float size = child->layout.measuredDimensions[dim[axis]];
child->layout.position[trailing[axis]] =
node->layout.measuredDimensions[dim[axis]] - size - child->layout.position[pos[axis]];
}
// If both left and right are defined, then use left. Otherwise return
// +left or -right depending on which is defined.
static float getRelativePosition(const CSSNodeRef node, const CSSFlexDirection axis) {
return isLeadingPosDefined(node, axis) ? getLeadingPosition(node, axis)
: -getTrailingPosition(node, axis);
}
static void constrainMaxSizeForMode(const float maxSize, CSSMeasureMode *mode, float *size) {
switch (*mode) {
case CSSMeasureModeExactly:
case CSSMeasureModeAtMost:
*size = (CSSValueIsUndefined(maxSize) || *size < maxSize) ? *size : maxSize;
break;
case CSSMeasureModeUndefined:
if (!CSSValueIsUndefined(maxSize)) {
*mode = CSSMeasureModeAtMost;
*size = maxSize;
}
break;
case CSSMeasureModeCount:
break;
}
}
static void setPosition(const CSSNodeRef node, const CSSDirection direction) {
const CSSFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction);
const CSSFlexDirection crossAxis = getCrossFlexDirection(mainAxis, direction);
const float relativePositionMain = getRelativePosition(node, mainAxis);
const float relativePositionCross = getRelativePosition(node, crossAxis);
node->layout.position[leading[mainAxis]] =
getLeadingMargin(node, mainAxis) + relativePositionMain;
node->layout.position[trailing[mainAxis]] =
getTrailingMargin(node, mainAxis) + relativePositionMain;
node->layout.position[leading[crossAxis]] =
getLeadingMargin(node, crossAxis) + relativePositionCross;
node->layout.position[trailing[crossAxis]] =
getTrailingMargin(node, crossAxis) + relativePositionCross;
}
static void computeChildFlexBasis(const CSSNodeRef node,
const CSSNodeRef child,
const float width,
const CSSMeasureMode widthMode,
const float height,
const CSSMeasureMode heightMode,
const CSSDirection direction) {
const CSSFlexDirection mainAxis = resolveAxis(node->style.flexDirection, direction);
const bool isMainAxisRow = isRowDirection(mainAxis);
float childWidth;
float childHeight;
CSSMeasureMode childWidthMeasureMode;
CSSMeasureMode childHeightMeasureMode;
const bool isRowStyleDimDefined = isStyleDimDefined(child, CSSFlexDirectionRow);
const bool isColumnStyleDimDefined = isStyleDimDefined(child, CSSFlexDirectionColumn);
if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) &&
!CSSValueIsUndefined(isMainAxisRow ? width : height)) {
if (CSSValueIsUndefined(child->layout.computedFlexBasis) ||
child->layout.computedFlexBasisGeneration != gCurrentGenerationCount) {
child->layout.computedFlexBasis =
fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis));
}
} else if (isMainAxisRow && isRowStyleDimDefined) {
// The width is definite, so use that as the flex basis.
child->layout.computedFlexBasis = fmaxf(child->style.dimensions[CSSDimensionWidth],
getPaddingAndBorderAxis(child, CSSFlexDirectionRow));
} else if (!isMainAxisRow && isColumnStyleDimDefined) {
// The height is definite, so use that as the flex basis.
child->layout.computedFlexBasis = fmaxf(child->style.dimensions[CSSDimensionHeight],
getPaddingAndBorderAxis(child, CSSFlexDirectionColumn));
} else {
// Compute the flex basis and hypothetical main size (i.e. the clamped
// flex basis).
childWidth = CSSUndefined;
childHeight = CSSUndefined;
childWidthMeasureMode = CSSMeasureModeUndefined;
childHeightMeasureMode = CSSMeasureModeUndefined;
if (isRowStyleDimDefined) {
childWidth =
child->style.dimensions[CSSDimensionWidth] + getMarginAxis(child, CSSFlexDirectionRow);
childWidthMeasureMode = CSSMeasureModeExactly;
}
if (isColumnStyleDimDefined) {
childHeight = child->style.dimensions[CSSDimensionHeight] +
getMarginAxis(child, CSSFlexDirectionColumn);
childHeightMeasureMode = CSSMeasureModeExactly;