forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversions.h
More file actions
631 lines (588 loc) · 17.7 KB
/
conversions.h
File metadata and controls
631 lines (588 loc) · 17.7 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
/*
* Copyright (c) Facebook, Inc. and its 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 <folly/Conv.h>
#include <folly/dynamic.h>
#include <react/attributedstring/AttributedString.h>
#include <react/attributedstring/ParagraphAttributes.h>
#include <react/attributedstring/TextAttributes.h>
#include <react/attributedstring/conversions.h>
#include <react/attributedstring/primitives.h>
#include <react/core/LayoutableShadowNode.h>
#include <react/core/ShadowNode.h>
#include <react/core/conversions.h>
#include <react/core/propsConversions.h>
#include <react/graphics/Geometry.h>
#include <react/graphics/conversions.h>
#include <cmath>
#include <glog/logging.h>
namespace facebook {
namespace react {
inline std::string toString(const EllipsizeMode &ellipsisMode) {
switch (ellipsisMode) {
case EllipsizeMode::Clip:
return "clip";
case EllipsizeMode::Head:
return "head";
case EllipsizeMode::Tail:
return "tail";
case EllipsizeMode::Middle:
return "middle";
}
}
inline void fromRawValue(const RawValue &value, EllipsizeMode &result) {
auto string = (std::string)value;
if (string == "clip") {
result = EllipsizeMode::Clip;
return;
}
if (string == "head") {
result = EllipsizeMode::Head;
return;
}
if (string == "tail") {
result = EllipsizeMode::Tail;
return;
}
if (string == "middle") {
result = EllipsizeMode::Middle;
return;
}
abort();
}
inline std::string toString(const TextBreakStrategy &textBreakStrategy) {
switch (textBreakStrategy) {
case TextBreakStrategy::Simple:
return "simple";
case TextBreakStrategy::HighQuality:
return "highQuality";
case TextBreakStrategy::Balanced:
return "balanced";
}
}
inline void fromRawValue(const RawValue &value, TextBreakStrategy &result) {
auto string = (std::string)value;
if (string == "simple") {
result = TextBreakStrategy::Simple;
return;
}
if (string == "highQuality") {
result = TextBreakStrategy::HighQuality;
return;
}
if (string == "balanced") {
result = TextBreakStrategy::Balanced;
return;
}
abort();
}
inline void fromRawValue(const RawValue &value, FontWeight &result) {
auto string = (std::string)value;
if (string == "normal") {
result = FontWeight::Regular;
return;
}
if (string == "regular") {
result = FontWeight::Regular;
return;
}
if (string == "bold") {
result = FontWeight::Bold;
return;
}
if (string == "100") {
result = FontWeight::Weight100;
return;
}
if (string == "200") {
result = FontWeight::Weight200;
return;
}
if (string == "300") {
result = FontWeight::Weight300;
return;
}
if (string == "400") {
result = FontWeight::Weight400;
return;
}
if (string == "500") {
result = FontWeight::Weight500;
return;
}
if (string == "600") {
result = FontWeight::Weight600;
return;
}
if (string == "700") {
result = FontWeight::Weight700;
return;
}
if (string == "800") {
result = FontWeight::Weight800;
return;
}
if (string == "900") {
result = FontWeight::Weight900;
return;
}
abort();
}
inline std::string toString(const FontWeight &fontWeight) {
return folly::to<std::string>((int)fontWeight);
}
inline void fromRawValue(const RawValue &value, FontStyle &result) {
auto string = (std::string)value;
if (string == "normal") {
result = FontStyle::Normal;
return;
}
if (string == "italic") {
result = FontStyle::Italic;
return;
}
if (string == "oblique") {
result = FontStyle::Oblique;
return;
}
abort();
}
inline std::string toString(const FontStyle &fontStyle) {
switch (fontStyle) {
case FontStyle::Normal:
return "normal";
case FontStyle::Italic:
return "italic";
case FontStyle::Oblique:
return "oblique";
}
}
inline void fromRawValue(const RawValue &value, FontVariant &result) {
assert(value.hasType<std::vector<std::string>>());
result = FontVariant::Default;
auto items = std::vector<std::string>{value};
for (const auto &item : items) {
if (item == "small-caps") {
result = (FontVariant)((int)result | (int)FontVariant::SmallCaps);
continue;
}
if (item == "oldstyle-nums") {
result = (FontVariant)((int)result | (int)FontVariant::OldstyleNums);
continue;
}
if (item == "lining-nums") {
result = (FontVariant)((int)result | (int)FontVariant::LiningNums);
continue;
}
if (item == "tabular-nums") {
result = (FontVariant)((int)result | (int)FontVariant::TabularNums);
continue;
}
if (item == "proportional-nums") {
result = (FontVariant)((int)result | (int)FontVariant::ProportionalNums);
continue;
}
}
}
inline std::string toString(const FontVariant &fontVariant) {
auto result = std::string{};
auto separator = std::string{", "};
if ((int)fontVariant & (int)FontVariant::SmallCaps) {
result += "small-caps" + separator;
}
if ((int)fontVariant & (int)FontVariant::OldstyleNums) {
result += "oldstyle-nums" + separator;
}
if ((int)fontVariant & (int)FontVariant::LiningNums) {
result += "lining-nums" + separator;
}
if ((int)fontVariant & (int)FontVariant::TabularNums) {
result += "tabular-nums" + separator;
}
if ((int)fontVariant & (int)FontVariant::ProportionalNums) {
result += "proportional-nums" + separator;
}
if (!result.empty()) {
result.erase(result.length() - separator.length());
}
return result;
}
inline void fromRawValue(const RawValue &value, TextAlignment &result) {
auto string = (std::string)value;
if (string == "auto") {
result = TextAlignment::Natural;
return;
}
if (string == "left") {
result = TextAlignment::Left;
return;
}
if (string == "center") {
result = TextAlignment::Center;
return;
}
if (string == "right") {
result = TextAlignment::Right;
return;
}
if (string == "justify") {
result = TextAlignment::Justified;
return;
}
abort();
}
inline std::string toString(const TextAlignment &textAlignment) {
switch (textAlignment) {
case TextAlignment::Natural:
return "natural";
case TextAlignment::Left:
return "left";
case TextAlignment::Center:
return "center";
case TextAlignment::Right:
return "right";
case TextAlignment::Justified:
return "justified";
}
}
inline void fromRawValue(const RawValue &value, WritingDirection &result) {
auto string = (std::string)value;
if (string == "natural") {
result = WritingDirection::Natural;
return;
}
if (string == "ltr") {
result = WritingDirection::LeftToRight;
return;
}
if (string == "rtl") {
result = WritingDirection::RightToLeft;
return;
}
abort();
}
inline std::string toString(const WritingDirection &writingDirection) {
switch (writingDirection) {
case WritingDirection::Natural:
return "natural";
case WritingDirection::LeftToRight:
return "ltr";
case WritingDirection::RightToLeft:
return "rtl";
}
}
inline void fromRawValue(
const RawValue &value,
TextDecorationLineType &result) {
auto string = (std::string)value;
if (string == "none") {
result = TextDecorationLineType::None;
return;
}
if (string == "underline") {
result = TextDecorationLineType::Underline;
return;
}
// TODO: remove "line-through" after deprecation
if (string == "strikethrough" || string == "line-through") {
result = TextDecorationLineType::Strikethrough;
return;
}
// TODO: remove "underline line-through" after "line-through" deprecation
if (string == "underline-strikethrough" ||
string == "underline line-through") {
result = TextDecorationLineType::UnderlineStrikethrough;
return;
}
abort();
}
inline std::string toString(
const TextDecorationLineType &textDecorationLineType) {
switch (textDecorationLineType) {
case TextDecorationLineType::None:
return "none";
case TextDecorationLineType::Underline:
return "underline";
case TextDecorationLineType::Strikethrough:
return "strikethrough";
case TextDecorationLineType::UnderlineStrikethrough:
return "underline-strikethrough";
}
}
inline void fromRawValue(
const RawValue &value,
TextDecorationLineStyle &result) {
auto string = (std::string)value;
if (string == "single") {
result = TextDecorationLineStyle::Single;
return;
}
if (string == "thick") {
result = TextDecorationLineStyle::Thick;
return;
}
if (string == "double") {
result = TextDecorationLineStyle::Double;
return;
}
abort();
}
inline std::string toString(
const TextDecorationLineStyle &textDecorationLineStyle) {
switch (textDecorationLineStyle) {
case TextDecorationLineStyle::Single:
return "single";
case TextDecorationLineStyle::Thick:
return "thick";
case TextDecorationLineStyle::Double:
return "double";
}
}
inline void fromRawValue(
const RawValue &value,
TextDecorationLinePattern &result) {
auto string = (std::string)value;
if (string == "solid") {
result = TextDecorationLinePattern::Solid;
return;
}
if (string == "dot") {
result = TextDecorationLinePattern::Dot;
return;
}
if (string == "dash") {
result = TextDecorationLinePattern::Dash;
return;
}
if (string == "dash-dot") {
result = TextDecorationLinePattern::DashDot;
return;
}
if (string == "dash-dot-dot") {
result = TextDecorationLinePattern::DashDotDot;
return;
}
abort();
}
inline std::string toString(
const TextDecorationLinePattern &textDecorationLinePattern) {
switch (textDecorationLinePattern) {
case TextDecorationLinePattern::Solid:
return "solid";
case TextDecorationLinePattern::Dot:
return "dot";
case TextDecorationLinePattern::Dash:
return "dash";
case TextDecorationLinePattern::DashDot:
return "dash-dot";
case TextDecorationLinePattern::DashDotDot:
return "dash-dot-dot";
}
}
inline ParagraphAttributes convertRawProp(
RawProps const &rawProps,
ParagraphAttributes const &sourceParagraphAttributes,
ParagraphAttributes const &defaultParagraphAttributes) {
auto paragraphAttributes = ParagraphAttributes{};
paragraphAttributes.maximumNumberOfLines = convertRawProp(
rawProps,
"numberOfLines",
sourceParagraphAttributes.maximumNumberOfLines,
defaultParagraphAttributes.maximumNumberOfLines);
paragraphAttributes.ellipsizeMode = convertRawProp(
rawProps,
"ellipsizeMode",
sourceParagraphAttributes.ellipsizeMode,
defaultParagraphAttributes.ellipsizeMode);
paragraphAttributes.textBreakStrategy = convertRawProp(
rawProps,
"textBreakStrategy",
sourceParagraphAttributes.textBreakStrategy,
defaultParagraphAttributes.textBreakStrategy);
paragraphAttributes.adjustsFontSizeToFit = convertRawProp(
rawProps,
"adjustsFontSizeToFit",
sourceParagraphAttributes.adjustsFontSizeToFit,
defaultParagraphAttributes.adjustsFontSizeToFit);
paragraphAttributes.minimumFontSize = convertRawProp(
rawProps,
"minimumFontSize",
sourceParagraphAttributes.minimumFontSize,
defaultParagraphAttributes.minimumFontSize);
paragraphAttributes.maximumFontSize = convertRawProp(
rawProps,
"maximumFontSize",
sourceParagraphAttributes.maximumFontSize,
defaultParagraphAttributes.maximumFontSize);
return paragraphAttributes;
}
inline void fromRawValue(
RawValue const &value,
AttributedString::Range &result) {
auto map = (better::map<std::string, int>)value;
auto start = map.find("start");
if (start != map.end()) {
result.location = start->second;
}
auto end = map.find("end");
if (end != map.end()) {
result.length = start->second - result.location;
}
}
inline std::string toString(AttributedString::Range const &range) {
return "{location: " + folly::to<std::string>(range.location) +
", length: " + folly::to<std::string>(range.length) + "}";
}
#ifdef ANDROID
inline folly::dynamic toDynamic(
const ParagraphAttributes ¶graphAttributes) {
auto values = folly::dynamic::object();
values("maximumNumberOfLines", paragraphAttributes.maximumNumberOfLines);
values("ellipsizeMode", toString(paragraphAttributes.ellipsizeMode));
values("textBreakStrategy", toString(paragraphAttributes.textBreakStrategy));
values("adjustsFontSizeToFit", paragraphAttributes.adjustsFontSizeToFit);
return values;
}
inline folly::dynamic toDynamic(const FontVariant &fontVariant) {
auto result = folly::dynamic::array();
if ((int)fontVariant & (int)FontVariant::SmallCaps) {
result.push_back("small-caps");
}
if ((int)fontVariant & (int)FontVariant::OldstyleNums) {
result.push_back("oldstyle-nums");
}
if ((int)fontVariant & (int)FontVariant::LiningNums) {
result.push_back("lining-nums");
}
if ((int)fontVariant & (int)FontVariant::TabularNums) {
result.push_back("tabular-nums");
}
if ((int)fontVariant & (int)FontVariant::ProportionalNums) {
result.push_back("proportional-nums");
}
return result;
}
inline folly::dynamic toDynamic(const TextAttributes &textAttributes) {
auto _textAttributes = folly::dynamic::object();
if (textAttributes.foregroundColor) {
_textAttributes(
"foregroundColor", toDynamic(textAttributes.foregroundColor));
}
if (textAttributes.backgroundColor) {
_textAttributes(
"backgroundColor", toDynamic(textAttributes.backgroundColor));
}
if (!std::isnan(textAttributes.opacity)) {
_textAttributes("opacity", textAttributes.opacity);
}
if (!textAttributes.fontFamily.empty()) {
_textAttributes("fontFamily", textAttributes.fontFamily);
}
if (!std::isnan(textAttributes.fontSize)) {
_textAttributes("fontSize", textAttributes.fontSize);
}
if (!std::isnan(textAttributes.fontSizeMultiplier)) {
_textAttributes("fontSizeMultiplier", textAttributes.fontSizeMultiplier);
}
if (textAttributes.fontWeight.has_value()) {
_textAttributes("fontWeight", toString(*textAttributes.fontWeight));
}
if (textAttributes.fontStyle.has_value()) {
_textAttributes("fontStyle", toString(*textAttributes.fontStyle));
}
if (textAttributes.fontVariant.has_value()) {
_textAttributes("fontVariant", toDynamic(*textAttributes.fontVariant));
}
if (textAttributes.allowFontScaling.has_value()) {
_textAttributes("allowFontScaling", *textAttributes.allowFontScaling);
}
if (!std::isnan(textAttributes.letterSpacing)) {
_textAttributes("letterSpacing", textAttributes.letterSpacing);
}
if (!std::isnan(textAttributes.lineHeight)) {
_textAttributes("lineHeight", textAttributes.lineHeight);
}
if (textAttributes.alignment.has_value()) {
_textAttributes("alignment", toString(*textAttributes.alignment));
}
if (textAttributes.baseWritingDirection.has_value()) {
_textAttributes(
"baseWritingDirection", toString(*textAttributes.baseWritingDirection));
}
// Decoration
if (textAttributes.textDecorationColor) {
_textAttributes(
"textDecorationColor", toDynamic(textAttributes.textDecorationColor));
}
if (textAttributes.textDecorationLineType.has_value()) {
_textAttributes(
"textDecorationLine", toString(*textAttributes.textDecorationLineType));
}
if (textAttributes.textDecorationLineStyle.has_value()) {
_textAttributes(
"textDecorationLineStyle",
toString(*textAttributes.textDecorationLineStyle));
}
if (textAttributes.textDecorationLinePattern.has_value()) {
_textAttributes(
"textDecorationLinePattern",
toString(*textAttributes.textDecorationLinePattern));
}
// Shadow
// textShadowOffset = textAttributes.textShadowOffset.has_value() ?
// textAttributes.textShadowOffset.value() : textShadowOffset;
if (!std::isnan(textAttributes.textShadowRadius)) {
_textAttributes("textShadowRadius", textAttributes.textShadowRadius);
}
if (textAttributes.textShadowColor) {
_textAttributes(
"textShadowColor", toDynamic(textAttributes.textShadowColor));
}
// Special
if (textAttributes.isHighlighted.has_value()) {
_textAttributes("isHighlighted", *textAttributes.isHighlighted);
}
if (textAttributes.layoutDirection.has_value()) {
_textAttributes(
"layoutDirection", toString(*textAttributes.layoutDirection));
}
return _textAttributes;
}
inline folly::dynamic toDynamic(const AttributedString &attributedString) {
auto value = folly::dynamic::object();
auto fragments = folly::dynamic::array();
for (auto fragment : attributedString.getFragments()) {
folly::dynamic dynamicFragment = folly::dynamic::object();
dynamicFragment["string"] = fragment.string;
if (fragment.parentShadowView.componentHandle) {
dynamicFragment["reactTag"] = fragment.parentShadowView.tag;
}
if (fragment.isAttachment()) {
dynamicFragment["isAttachment"] = true;
dynamicFragment["width"] =
(int)fragment.parentShadowView.layoutMetrics.frame.size.width;
dynamicFragment["height"] =
(int)fragment.parentShadowView.layoutMetrics.frame.size.height;
}
dynamicFragment["textAttributes"] = toDynamic(fragment.textAttributes);
fragments.push_back(dynamicFragment);
}
value("fragments", fragments);
value(
"hash", std::hash<facebook::react::AttributedString>{}(attributedString));
value("string", attributedString.getString());
return value;
}
inline folly::dynamic toDynamic(AttributedString::Range const &range) {
folly::dynamic dynamicValue = folly::dynamic::object();
dynamicValue["location"] = range.location;
dynamicValue["length"] = range.length;
return dynamicValue;
}
#endif
} // namespace react
} // namespace facebook