forked from microsoft/react-native-macos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversions.h
More file actions
1784 lines (1608 loc) · 53.4 KB
/
conversions.h
File metadata and controls
1784 lines (1608 loc) · 53.4 KB
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) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <glog/logging.h>
#include <react/debug/react_native_expect.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/components/view/primitives.h>
#include <react/renderer/core/LayoutMetrics.h>
#include <react/renderer/core/PropsParserContext.h>
#include <react/renderer/core/RawProps.h>
#include <react/renderer/core/graphicsConversions.h>
#include <react/renderer/css/CSSAngle.h>
#include <react/renderer/css/CSSNumber.h>
#include <react/renderer/css/CSSPercentage.h>
#include <react/renderer/css/CSSRatio.h>
#include <react/renderer/css/CSSTransform.h>
#include <react/renderer/css/CSSTransformOrigin.h>
#include <react/renderer/css/CSSValueParser.h>
#include <react/renderer/debug/flags.h>
#include <react/renderer/graphics/BackgroundPosition.h>
#include <react/renderer/graphics/BackgroundRepeat.h>
#include <react/renderer/graphics/BackgroundSize.h>
#include <react/renderer/graphics/BlendMode.h>
#include <react/renderer/graphics/Isolation.h>
#include <react/renderer/graphics/LinearGradient.h>
#include <react/renderer/graphics/PlatformColorParser.h>
#include <react/renderer/graphics/Transform.h>
#include <react/renderer/graphics/ValueUnit.h>
#include <yoga/YGEnums.h>
#include <yoga/node/Node.h>
#include <cmath>
#include <optional>
#include <string>
#include <unordered_map>
namespace facebook::react {
/*
* Yoga's `float` <-> React Native's `Float` (can be `double` or `float`)
*
* Regular Yoga `float` values represent some onscreen-position-related values.
* They can be real numbers or special value `YGUndefined` (which actually is
* `NaN`). Conceptually, layout computation process inside Yoga should never
* produce `NaN` values from non-`NaN` values. At the same time, ` YGUndefined`
* values have special "no limit" meaning in Yoga, therefore ` YGUndefined`
* usually corresponds to `Infinity` value.
*/
inline Float floatFromYogaFloat(float value)
{
static_assert(YGUndefined != YGUndefined, "The code of this function assumes that YGUndefined is NaN.");
if (std::isnan(value) /* means: `value == YGUndefined` */) {
return std::numeric_limits<Float>::infinity();
}
return (Float)value;
}
inline float yogaFloatFromFloat(Float value)
{
if (!std::isfinite(value)) {
return YGUndefined;
}
return (float)value;
}
/*
* `yoga::FloatOptional` <-> React Native's `Float`
*
* `yoga::FloatOptional` represents optional dimensionless float values in Yoga
* Style object (e.g. `flex`). The most suitable analogy to empty
* `yoga::FloatOptional` is `NaN` value.
* `yoga::FloatOptional` values are usually parsed from some outside data source
* which usually has some special corresponding representation for an empty
* value.
*/
inline Float floatFromYogaOptionalFloat(yoga::FloatOptional value)
{
if (value.isUndefined()) {
return std::numeric_limits<Float>::quiet_NaN();
}
return floatFromYogaFloat(value.unwrap());
}
inline yoga::FloatOptional yogaOptionalFloatFromFloat(Float value)
{
if (std::isnan(value)) {
return yoga::FloatOptional();
}
return yoga::FloatOptional((float)value);
}
inline std::optional<Float> optionalFloatFromYogaValue(
const yoga::Style::Length &length,
std::optional<Float> base = {})
{
if (length.isPoints()) {
return floatFromYogaOptionalFloat(length.value());
} else if (length.isPercent()) {
return base.has_value() ? std::optional<Float>(base.value() * floatFromYogaOptionalFloat(length.value()))
: std::optional<Float>();
} else {
return {};
}
}
static inline PositionType positionTypeFromYogaPositionType(yoga::PositionType positionType)
{
switch (positionType) {
case yoga::PositionType::Static:
return PositionType::Static;
case yoga::PositionType::Relative:
return PositionType::Relative;
case yoga::PositionType::Absolute:
return PositionType::Absolute;
}
}
inline DisplayType displayTypeFromYGDisplay(YGDisplay display)
{
switch (display) {
case YGDisplayNone:
return DisplayType::None;
case YGDisplayContents:
return DisplayType::Contents;
case YGDisplayFlex:
return DisplayType::Flex;
}
}
inline LayoutMetrics layoutMetricsFromYogaNode(yoga::Node &yogaNode)
{
auto layoutMetrics = LayoutMetrics{};
layoutMetrics.frame = Rect{
.origin =
Point{
.x = floatFromYogaFloat(YGNodeLayoutGetLeft(&yogaNode)),
.y = floatFromYogaFloat(YGNodeLayoutGetTop(&yogaNode))},
.size = Size{
.width = floatFromYogaFloat(YGNodeLayoutGetWidth(&yogaNode)),
.height = floatFromYogaFloat(YGNodeLayoutGetHeight(&yogaNode))}};
layoutMetrics.borderWidth = EdgeInsets{
floatFromYogaFloat(YGNodeLayoutGetBorder(&yogaNode, YGEdgeLeft)),
floatFromYogaFloat(YGNodeLayoutGetBorder(&yogaNode, YGEdgeTop)),
floatFromYogaFloat(YGNodeLayoutGetBorder(&yogaNode, YGEdgeRight)),
floatFromYogaFloat(YGNodeLayoutGetBorder(&yogaNode, YGEdgeBottom))};
layoutMetrics.contentInsets = EdgeInsets{
layoutMetrics.borderWidth.left + floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeLeft)),
layoutMetrics.borderWidth.top + floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeTop)),
layoutMetrics.borderWidth.right + floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeRight)),
layoutMetrics.borderWidth.bottom + floatFromYogaFloat(YGNodeLayoutGetPadding(&yogaNode, YGEdgeBottom))};
layoutMetrics.displayType = displayTypeFromYGDisplay(YGNodeStyleGetDisplay(&yogaNode));
layoutMetrics.positionType = positionTypeFromYogaPositionType(yogaNode.style().positionType());
layoutMetrics.layoutDirection = YGNodeLayoutGetDirection(&yogaNode) == YGDirectionRTL ? LayoutDirection::RightToLeft
: LayoutDirection::LeftToRight;
return layoutMetrics;
}
inline YGDirection yogaDirectionFromLayoutDirection(LayoutDirection direction)
{
switch (direction) {
case LayoutDirection::Undefined:
return YGDirectionInherit;
case LayoutDirection::LeftToRight:
return YGDirectionLTR;
case LayoutDirection::RightToLeft:
return YGDirectionRTL;
}
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Direction &result)
{
result = yoga::Direction::Inherit;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "inherit") {
result = yoga::Direction::Inherit;
return;
}
if (stringValue == "ltr") {
result = yoga::Direction::LTR;
return;
}
if (stringValue == "rtl") {
result = yoga::Direction::RTL;
return;
}
LOG(ERROR) << "Could not parse yoga::Direction: " << stringValue;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::FlexDirection &result)
{
result = yoga::FlexDirection::Column;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "row") {
result = yoga::FlexDirection::Row;
return;
}
if (stringValue == "column") {
result = yoga::FlexDirection::Column;
return;
}
if (stringValue == "column-reverse") {
result = yoga::FlexDirection::ColumnReverse;
return;
}
if (stringValue == "row-reverse") {
result = yoga::FlexDirection::RowReverse;
return;
}
LOG(ERROR) << "Could not parse yoga::FlexDirection: " << stringValue;
}
inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue &value, yoga::BoxSizing &result)
{
result = yoga::BoxSizing::BorderBox;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "border-box") {
result = yoga::BoxSizing::BorderBox;
return;
}
if (stringValue == "content-box") {
result = yoga::BoxSizing::ContentBox;
return;
}
LOG(ERROR) << "Could not parse yoga::BoxSizing: " << stringValue;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Justify &result)
{
result = yoga::Justify::FlexStart;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "flex-start") {
result = yoga::Justify::FlexStart;
return;
}
if (stringValue == "center") {
result = yoga::Justify::Center;
return;
}
if (stringValue == "flex-end") {
result = yoga::Justify::FlexEnd;
return;
}
if (stringValue == "space-between") {
result = yoga::Justify::SpaceBetween;
return;
}
if (stringValue == "space-around") {
result = yoga::Justify::SpaceAround;
return;
}
if (stringValue == "space-evenly") {
result = yoga::Justify::SpaceEvenly;
return;
}
LOG(ERROR) << "Could not parse yoga::Justify: " << stringValue;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Align &result)
{
result = yoga::Align::Stretch;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "auto") {
result = yoga::Align::Auto;
return;
}
if (stringValue == "flex-start") {
result = yoga::Align::FlexStart;
return;
}
if (stringValue == "center") {
result = yoga::Align::Center;
return;
}
if (stringValue == "flex-end") {
result = yoga::Align::FlexEnd;
return;
}
if (stringValue == "stretch") {
result = yoga::Align::Stretch;
return;
}
if (stringValue == "baseline") {
result = yoga::Align::Baseline;
return;
}
if (stringValue == "space-between") {
result = yoga::Align::SpaceBetween;
return;
}
if (stringValue == "space-around") {
result = yoga::Align::SpaceAround;
return;
}
if (stringValue == "space-evenly") {
result = yoga::Align::SpaceEvenly;
return;
}
LOG(ERROR) << "Could not parse yoga::Align: " << stringValue;
react_native_expect(false);
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::PositionType &result)
{
result = yoga::PositionType::Relative;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "static") {
result = yoga::PositionType::Static;
return;
}
if (stringValue == "relative") {
result = yoga::PositionType::Relative;
return;
}
if (stringValue == "absolute") {
result = yoga::PositionType::Absolute;
return;
}
LOG(ERROR) << "Could not parse yoga::PositionType: " << stringValue;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Wrap &result)
{
result = yoga::Wrap::NoWrap;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "nowrap") {
result = yoga::Wrap::NoWrap;
return;
}
if (stringValue == "wrap") {
result = yoga::Wrap::Wrap;
return;
}
if (stringValue == "wrap-reverse") {
result = yoga::Wrap::WrapReverse;
return;
}
LOG(ERROR) << "Could not parse yoga::Wrap: " << stringValue;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Overflow &result)
{
result = yoga::Overflow::Visible;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "visible") {
result = yoga::Overflow::Visible;
return;
}
if (stringValue == "hidden") {
result = yoga::Overflow::Hidden;
return;
}
if (stringValue == "scroll") {
result = yoga::Overflow::Scroll;
return;
}
LOG(ERROR) << "Could not parse yoga::Overflow:" << stringValue;
react_native_expect(false);
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Display &result)
{
result = yoga::Display::Flex;
react_native_expect(value.hasType<std::string>());
if (!value.hasType<std::string>()) {
return;
}
auto stringValue = (std::string)value;
if (stringValue == "flex") {
result = yoga::Display::Flex;
return;
}
if (stringValue == "none") {
result = yoga::Display::None;
return;
}
if (stringValue == "contents") {
result = yoga::Display::Contents;
return;
}
LOG(ERROR) << "Could not parse yoga::Display: " << stringValue;
}
inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue &value, yoga::Style::SizeLength &result)
{
if (value.hasType<Float>()) {
result = yoga::StyleSizeLength::points((float)value);
return;
} else if (value.hasType<std::string>()) {
const auto stringValue = (std::string)value;
if (stringValue == "auto") {
result = yoga::StyleSizeLength::ofAuto();
return;
} else if (stringValue == "max-content") {
result = yoga::StyleSizeLength::ofMaxContent();
return;
} else if (stringValue == "stretch") {
result = yoga::StyleSizeLength::ofStretch();
return;
} else if (stringValue == "fit-content") {
result = yoga::StyleSizeLength::ofFitContent();
return;
} else {
auto parsed = parseCSSProperty<CSSNumber, CSSPercentage>(stringValue);
if (std::holds_alternative<CSSPercentage>(parsed)) {
result = yoga::StyleSizeLength::percent(std::get<CSSPercentage>(parsed).value);
return;
} else if (std::holds_alternative<CSSNumber>(parsed)) {
result = yoga::StyleSizeLength::points(std::get<CSSNumber>(parsed).value);
return;
}
}
}
result = yoga::StyleSizeLength::undefined();
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::Style::Length &result)
{
if (value.hasType<Float>()) {
result = yoga::StyleLength::points((float)value);
return;
} else if (value.hasType<std::string>()) {
const auto stringValue = (std::string)value;
if (stringValue == "auto") {
result = yoga::StyleLength::ofAuto();
return;
} else {
auto parsed = parseCSSProperty<CSSNumber, CSSPercentage>(stringValue);
if (std::holds_alternative<CSSPercentage>(parsed)) {
result = yoga::StyleLength::percent(std::get<CSSPercentage>(parsed).value);
return;
} else if (std::holds_alternative<CSSNumber>(parsed)) {
result = yoga::StyleLength::points(std::get<CSSNumber>(parsed).value);
return;
}
}
}
result = yoga::StyleLength::undefined();
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, YGValue &result)
{
yoga::Style::Length length{};
fromRawValue(context, value, length);
result = (YGValue)length;
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, yoga::FloatOptional &result)
{
result = value.hasType<float>() ? yoga::FloatOptional((float)value) : yoga::FloatOptional();
}
inline yoga::FloatOptional convertAspectRatio(const PropsParserContext & /*context*/, const RawValue &value)
{
if (value.hasType<float>()) {
return yoga::FloatOptional((float)value);
}
if (ReactNativeFeatureFlags::enableNativeCSSParsing() && value.hasType<std::string>()) {
auto ratio = parseCSSProperty<CSSRatio>((std::string)value);
if (std::holds_alternative<CSSRatio>(ratio)) {
auto r = std::get<CSSRatio>(ratio);
if (!r.isDegenerate()) {
return yoga::FloatOptional(r.numerator / r.denominator);
}
}
}
return {};
}
inline std::optional<Float> toRadians(const RawValue &value)
{
if (value.hasType<Float>()) {
return (Float)value;
}
if (!value.hasType<std::string>()) {
return {};
}
auto angle = parseCSSProperty<CSSAngle>((std::string)value);
if (std::holds_alternative<CSSAngle>(angle)) {
return static_cast<float>(std::get<CSSAngle>(angle).degrees * M_PI / 180.0f);
}
return {};
}
inline ValueUnit toValueUnit(const RawValue &value)
{
if (value.hasType<Float>()) {
return ValueUnit((Float)value, UnitType::Point);
}
if (!value.hasType<std::string>()) {
return {};
}
auto pct = parseCSSProperty<CSSPercentage>((std::string)value);
if (std::holds_alternative<CSSPercentage>(pct)) {
return ValueUnit(std::get<CSSPercentage>(pct).value, UnitType::Percent);
}
return {};
}
inline void fromRawValue(const PropsParserContext & /*context*/, const RawValue &value, ValueUnit &result)
{
result = toValueUnit(value);
}
inline ValueUnit cssLengthPercentageToValueUnit(const std::variant<CSSLength, CSSPercentage> &value)
{
if (std::holds_alternative<CSSLength>(value)) {
auto len = std::get<CSSLength>(value);
if (len.unit != CSSLengthUnit::Px) {
return {};
}
return {len.value, UnitType::Point};
} else {
return {std::get<CSSPercentage>(value).value, UnitType::Percent};
}
}
inline std::optional<TransformOperation> fromCSSTransformFunction(const CSSTransformFunction &cssTransform)
{
constexpr auto Zero = ValueUnit(0, UnitType::Point);
constexpr auto One = ValueUnit(1, UnitType::Point);
return std::visit(
[&](auto &&func) -> std::optional<TransformOperation> {
using T = std::decay_t<decltype(func)>;
if constexpr (std::is_same_v<T, CSSRotate>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = Zero, .z = ValueUnit(radians, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSRotateX>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = ValueUnit(radians, UnitType::Point), .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSRotateY>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = ValueUnit(radians, UnitType::Point), .z = Zero};
}
if constexpr (std::is_same_v<T, CSSRotateZ>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = Zero, .z = ValueUnit(radians, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSTranslate>) {
auto x = cssLengthPercentageToValueUnit(func.x);
auto y = cssLengthPercentageToValueUnit(func.y);
if (!x || !y) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = x, .y = y, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslateX>) {
auto x = cssLengthPercentageToValueUnit(func.value);
if (!x) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = x, .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslateY>) {
auto y = cssLengthPercentageToValueUnit(func.value);
if (!y) {
return std::nullopt;
}
return TransformOperation{.type = TransformOperationType::Translate, .x = Zero, .y = y, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSTranslate3D>) {
auto x = cssLengthPercentageToValueUnit(func.x);
auto y = cssLengthPercentageToValueUnit(func.y);
if (!x || !y || func.z.unit != CSSLengthUnit::Px) {
return std::nullopt;
}
return TransformOperation{
.type = TransformOperationType::Translate, .x = x, .y = y, .z = ValueUnit(func.z.value, UnitType::Point)};
}
if constexpr (std::is_same_v<T, CSSScale>) {
return TransformOperation{
.type = TransformOperationType::Scale,
.x = ValueUnit(func.x, UnitType::Point),
.y = ValueUnit(func.y, UnitType::Point),
.z = One};
}
if constexpr (std::is_same_v<T, CSSScaleX>) {
return TransformOperation{
.type = TransformOperationType::Scale, .x = ValueUnit(func.value, UnitType::Point), .y = One, .z = One};
}
if constexpr (std::is_same_v<T, CSSScaleY>) {
return TransformOperation{
.type = TransformOperationType::Scale, .x = One, .y = ValueUnit(func.value, UnitType::Point), .z = One};
}
if constexpr (std::is_same_v<T, CSSSkewX>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Skew, .x = ValueUnit(radians, UnitType::Point), .y = Zero, .z = Zero};
}
if constexpr (std::is_same_v<T, CSSSkewY>) {
auto radians = static_cast<float>(func.degrees * M_PI / 180.0f);
return TransformOperation{
.type = TransformOperationType::Skew, .x = Zero, .y = ValueUnit(radians, UnitType::Point), .z = Zero};
}
if constexpr (std::is_same_v<T, CSSPerspective>) {
if (func.length.unit != CSSLengthUnit::Px) {
return std::nullopt;
}
return TransformOperation{
.type = TransformOperationType::Perspective,
.x = ValueUnit(func.length.value, UnitType::Point),
.y = Zero,
.z = Zero};
}
if constexpr (std::is_same_v<T, CSSMatrix>) {
return TransformOperation{.type = TransformOperationType::Arbitrary, .x = Zero, .y = Zero, .z = Zero};
}
},
cssTransform);
}
inline void parseProcessedTransform(const PropsParserContext & /*context*/, const RawValue &value, Transform &result)
{
auto transformMatrix = Transform{};
react_native_expect(value.hasType<std::vector<RawValue>>());
if (!value.hasType<std::vector<RawValue>>()) {
result = transformMatrix;
return;
}
auto configurations = static_cast<std::vector<RawValue>>(value);
for (const auto &configuration : configurations) {
if (!configuration.hasType<std::unordered_map<std::string, RawValue>>()) {
result = {};
return;
}
auto configurationPair = static_cast<std::unordered_map<std::string, RawValue>>(configuration);
if (configurationPair.size() != 1) {
result = {};
return;
}
auto pair = configurationPair.begin();
auto operation = pair->first;
auto ¶meters = pair->second;
auto Zero = ValueUnit(0, UnitType::Point);
auto One = ValueUnit(1, UnitType::Point);
if (operation == "matrix") {
// T215634510: We should support matrix transforms as part of a list of
// transforms
if (configurations.size() > 1) {
result = {};
return;
}
if (!parameters.hasType<std::vector<Float>>()) {
result = {};
return;
}
auto numbers = (std::vector<Float>)parameters;
if (numbers.size() != 9 && numbers.size() != 16) {
result = {};
return;
}
if (numbers.size() == 16) {
size_t i = 0;
for (auto number : numbers) {
transformMatrix.matrix[i++] = number;
}
} else if (numbers.size() == 9) {
// We need to convert the 2d transform matrix into a 3d one as such:
// [
// x00, x01, 0, x02
// x10, x11, 0, x12
// 0, 0, 1, 0
// x20, x21, 0, x22
// ]
transformMatrix.matrix[0] = numbers[0];
transformMatrix.matrix[1] = numbers[1];
transformMatrix.matrix[2] = 0;
transformMatrix.matrix[3] = numbers[2];
transformMatrix.matrix[4] = numbers[3];
transformMatrix.matrix[5] = numbers[4];
transformMatrix.matrix[6] = 0;
transformMatrix.matrix[7] = numbers[5];
transformMatrix.matrix[8] = 0;
transformMatrix.matrix[9] = 0;
transformMatrix.matrix[10] = 1;
transformMatrix.matrix[11] = 0;
transformMatrix.matrix[12] = numbers[6];
transformMatrix.matrix[13] = numbers[7];
transformMatrix.matrix[14] = 0;
transformMatrix.matrix[15] = numbers[8];
}
transformMatrix.operations.push_back(
TransformOperation{.type = TransformOperationType::Arbitrary, .x = Zero, .y = Zero, .z = Zero});
} else if (operation == "perspective") {
if (!parameters.hasType<Float>()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Perspective,
.x = ValueUnit((Float)parameters, UnitType::Point),
.y = Zero,
.z = Zero});
} else if (operation == "rotateX") {
auto radians = toRadians(parameters);
if (!radians.has_value()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Rotate, .x = ValueUnit(*radians, UnitType::Point), .y = Zero, .z = Zero});
} else if (operation == "rotateY") {
auto radians = toRadians(parameters);
if (!radians.has_value()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = ValueUnit(*radians, UnitType::Point), .z = Zero});
} else if (operation == "rotateZ" || operation == "rotate") {
auto radians = toRadians(parameters);
if (!radians.has_value()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Rotate, .x = Zero, .y = Zero, .z = ValueUnit(*radians, UnitType::Point)});
} else if (operation == "scale") {
if (!parameters.hasType<Float>()) {
result = {};
return;
}
auto number = ValueUnit((Float)parameters, UnitType::Point);
transformMatrix.operations.push_back(
TransformOperation{.type = TransformOperationType::Scale, .x = number, .y = number, .z = number});
} else if (operation == "scaleX") {
if (!parameters.hasType<Float>()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Scale,
.x = ValueUnit((Float)parameters, UnitType::Point),
.y = One,
.z = One});
} else if (operation == "scaleY") {
if (!parameters.hasType<Float>()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Scale,
.x = One,
.y = ValueUnit((Float)parameters, UnitType::Point),
.z = One});
} else if (operation == "scaleZ") {
if (!parameters.hasType<Float>()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Scale,
.x = One,
.y = One,
.z = ValueUnit((Float)parameters, UnitType::Point)});
} else if (operation == "translate") {
if (!parameters.hasType<std::vector<RawValue>>()) {
result = {};
return;
}
auto numbers = (std::vector<RawValue>)parameters;
if (numbers.size() != 2) {
result = {};
return;
}
auto valueX = toValueUnit(numbers[0]);
if (!valueX) {
result = {};
return;
}
auto valueY = toValueUnit(numbers[1]);
if (!valueY) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{.type = TransformOperationType::Translate, .x = valueX, .y = valueY, .z = Zero});
} else if (operation == "translateX") {
auto valueX = toValueUnit(parameters);
if (!valueX) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{.type = TransformOperationType::Translate, .x = valueX, .y = Zero, .z = Zero});
} else if (operation == "translateY") {
auto valueY = toValueUnit(parameters);
if (!valueY) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{.type = TransformOperationType::Translate, .x = Zero, .y = valueY, .z = Zero});
} else if (operation == "skewX") {
auto radians = toRadians(parameters);
if (!radians.has_value()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Skew, .x = ValueUnit(*radians, UnitType::Point), .y = Zero, .z = Zero});
} else if (operation == "skewY") {
auto radians = toRadians(parameters);
if (!radians.has_value()) {
result = {};
return;
}
transformMatrix.operations.push_back(
TransformOperation{
.type = TransformOperationType::Skew, .x = Zero, .y = ValueUnit(*radians, UnitType::Point), .z = Zero});
}
}
result = transformMatrix;
}
inline void parseUnprocessedTransformString(const std::string &value, Transform &result)
{
auto transformList = parseCSSProperty<CSSTransformList>(value);
if (!std::holds_alternative<CSSTransformList>(transformList)) {
result = {};
return;
}
auto transformMatrix = Transform{};
const auto &cssFuncs = std::get<CSSTransformList>(transformList);
transformMatrix.operations.reserve(cssFuncs.size());
for (const auto &cssFunc : cssFuncs) {
auto op = fromCSSTransformFunction(cssFunc);
if (!op.has_value()) {
result = {};
return;
}
if (op->type == TransformOperationType::Arbitrary) {
// CSSMatrix: expand 6-value 2D matrix to 4x4 matrix
if (std::holds_alternative<CSSMatrix>(cssFunc)) {
const auto &m = std::get<CSSMatrix>(cssFunc);
transformMatrix.matrix[0] = m.values[0];
transformMatrix.matrix[1] = m.values[1];
transformMatrix.matrix[2] = 0;
transformMatrix.matrix[3] = 0;
transformMatrix.matrix[4] = m.values[2];
transformMatrix.matrix[5] = m.values[3];
transformMatrix.matrix[6] = 0;
transformMatrix.matrix[7] = 0;
transformMatrix.matrix[8] = 0;
transformMatrix.matrix[9] = 0;
transformMatrix.matrix[10] = 1;
transformMatrix.matrix[11] = 0;
transformMatrix.matrix[12] = m.values[4];
transformMatrix.matrix[13] = m.values[5];
transformMatrix.matrix[14] = 0;
transformMatrix.matrix[15] = 1;
}
}
transformMatrix.operations.push_back(*op);
}
result = transformMatrix;
}
inline void parseUnprocessedTransform(const PropsParserContext &context, const RawValue &value, Transform &result)
{
if (value.hasType<std::string>()) {
parseUnprocessedTransformString((std::string)value, result);
} else {
parseProcessedTransform(context, value, result);
}
}
inline void fromRawValue(const PropsParserContext &context, const RawValue &value, Transform &result)
{
if (ReactNativeFeatureFlags::enableNativeCSSParsing()) {
parseUnprocessedTransform(context, value, result);
} else {
parseProcessedTransform(context, value, result);
}
}
inline void
parseProcessedTransformOrigin(const PropsParserContext & /*context*/, const RawValue &value, TransformOrigin &result)
{
if (!value.hasType<std::vector<RawValue>>()) {
result = {};
return;
}
auto origins = (std::vector<RawValue>)value;
if (origins.size() != 3) {
result = {};
return;
}