|
9 | 9 |
|
10 | 10 | #include <folly/dynamic.h> |
11 | 11 | #include <react/debug/react_native_expect.h> |
| 12 | +#include <react/featureflags/ReactNativeFeatureFlags.h> |
12 | 13 | #include <react/renderer/attributedstring/AttributedString.h> |
13 | 14 | #include <react/renderer/attributedstring/ParagraphAttributes.h> |
14 | 15 | #include <react/renderer/attributedstring/TextAttributes.h> |
|
21 | 22 | #include <react/renderer/core/conversions.h> |
22 | 23 | #include <react/renderer/core/graphicsConversions.h> |
23 | 24 | #include <react/renderer/core/propsConversions.h> |
| 25 | +#include <react/renderer/css/CSSFontVariant.h> |
| 26 | +#include <react/renderer/css/CSSValueParser.h> |
24 | 27 | #include <unordered_map> |
25 | 28 |
|
26 | 29 | #ifdef RN_SERIALIZABLE_STATE |
@@ -317,7 +320,66 @@ inline std::string toString(const FontStyle &fontStyle) |
317 | 320 | return "normal"; |
318 | 321 | } |
319 | 322 |
|
320 | | -inline void fromRawValue(const PropsParserContext &context, const RawValue &value, FontVariant &result) |
| 323 | +inline std::optional<FontVariant> fontVariantFromCSSFontVariant(CSSFontVariant cssVariant) |
| 324 | +{ |
| 325 | + switch (cssVariant) { |
| 326 | + case CSSFontVariant::SmallCaps: |
| 327 | + return FontVariant::SmallCaps; |
| 328 | + case CSSFontVariant::OldstyleNums: |
| 329 | + return FontVariant::OldstyleNums; |
| 330 | + case CSSFontVariant::LiningNums: |
| 331 | + return FontVariant::LiningNums; |
| 332 | + case CSSFontVariant::TabularNums: |
| 333 | + return FontVariant::TabularNums; |
| 334 | + case CSSFontVariant::ProportionalNums: |
| 335 | + return FontVariant::ProportionalNums; |
| 336 | + case CSSFontVariant::StylisticOne: |
| 337 | + return FontVariant::StylisticOne; |
| 338 | + case CSSFontVariant::StylisticTwo: |
| 339 | + return FontVariant::StylisticTwo; |
| 340 | + case CSSFontVariant::StylisticThree: |
| 341 | + return FontVariant::StylisticThree; |
| 342 | + case CSSFontVariant::StylisticFour: |
| 343 | + return FontVariant::StylisticFour; |
| 344 | + case CSSFontVariant::StylisticFive: |
| 345 | + return FontVariant::StylisticFive; |
| 346 | + case CSSFontVariant::StylisticSix: |
| 347 | + return FontVariant::StylisticSix; |
| 348 | + case CSSFontVariant::StylisticSeven: |
| 349 | + return FontVariant::StylisticSeven; |
| 350 | + case CSSFontVariant::StylisticEight: |
| 351 | + return FontVariant::StylisticEight; |
| 352 | + case CSSFontVariant::StylisticNine: |
| 353 | + return FontVariant::StylisticNine; |
| 354 | + case CSSFontVariant::StylisticTen: |
| 355 | + return FontVariant::StylisticTen; |
| 356 | + case CSSFontVariant::StylisticEleven: |
| 357 | + return FontVariant::StylisticEleven; |
| 358 | + case CSSFontVariant::StylisticTwelve: |
| 359 | + return FontVariant::StylisticTwelve; |
| 360 | + case CSSFontVariant::StylisticThirteen: |
| 361 | + return FontVariant::StylisticThirteen; |
| 362 | + case CSSFontVariant::StylisticFourteen: |
| 363 | + return FontVariant::StylisticFourteen; |
| 364 | + case CSSFontVariant::StylisticFifteen: |
| 365 | + return FontVariant::StylisticFifteen; |
| 366 | + case CSSFontVariant::StylisticSixteen: |
| 367 | + return FontVariant::StylisticSixteen; |
| 368 | + case CSSFontVariant::StylisticSeventeen: |
| 369 | + return FontVariant::StylisticSeventeen; |
| 370 | + case CSSFontVariant::StylisticEighteen: |
| 371 | + return FontVariant::StylisticEighteen; |
| 372 | + case CSSFontVariant::StylisticNineteen: |
| 373 | + return FontVariant::StylisticNineteen; |
| 374 | + case CSSFontVariant::StylisticTwenty: |
| 375 | + return FontVariant::StylisticTwenty; |
| 376 | + default: |
| 377 | + // Ligature variants (CommonLigatures, etc.) have no FontVariant equivalent |
| 378 | + return std::nullopt; |
| 379 | + } |
| 380 | +} |
| 381 | + |
| 382 | +inline void parseProcessedFontVariant(const PropsParserContext &context, const RawValue &value, FontVariant &result) |
321 | 383 | { |
322 | 384 | result = FontVariant::Default; |
323 | 385 | react_native_expect(value.hasType<std::vector<std::string>>()); |
@@ -376,14 +438,47 @@ inline void fromRawValue(const PropsParserContext &context, const RawValue &valu |
376 | 438 | result = (FontVariant)((int)result | (int)FontVariant::StylisticTwenty); |
377 | 439 | } else { |
378 | 440 | LOG(ERROR) << "Unsupported FontVariant value: " << item; |
379 | | - react_native_expect(false); |
380 | 441 | } |
381 | 442 | } |
382 | 443 | } else { |
383 | 444 | LOG(ERROR) << "Unsupported FontVariant type"; |
384 | 445 | } |
385 | 446 | } |
386 | 447 |
|
| 448 | +inline void parseUnprocessedFontVariantString(const std::string &value, FontVariant &result) |
| 449 | +{ |
| 450 | + auto fontVariantList = parseCSSProperty<CSSFontVariantList>(value); |
| 451 | + if (!std::holds_alternative<CSSFontVariantList>(fontVariantList)) { |
| 452 | + result = FontVariant::Default; |
| 453 | + return; |
| 454 | + } |
| 455 | + |
| 456 | + result = FontVariant::Default; |
| 457 | + for (const auto &cssVariant : std::get<CSSFontVariantList>(fontVariantList)) { |
| 458 | + if (auto fv = fontVariantFromCSSFontVariant(cssVariant)) { |
| 459 | + result = (FontVariant)((int)result | (int)*fv); |
| 460 | + } |
| 461 | + } |
| 462 | +} |
| 463 | + |
| 464 | +inline void parseUnprocessedFontVariant(const PropsParserContext &context, const RawValue &value, FontVariant &result) |
| 465 | +{ |
| 466 | + if (value.hasType<std::string>()) { |
| 467 | + parseUnprocessedFontVariantString((std::string)value, result); |
| 468 | + } else { |
| 469 | + parseProcessedFontVariant(context, value, result); |
| 470 | + } |
| 471 | +} |
| 472 | + |
| 473 | +inline void fromRawValue(const PropsParserContext &context, const RawValue &value, FontVariant &result) |
| 474 | +{ |
| 475 | + if (ReactNativeFeatureFlags::enableNativeCSSParsing()) { |
| 476 | + parseUnprocessedFontVariant(context, value, result); |
| 477 | + } else { |
| 478 | + parseProcessedFontVariant(context, value, result); |
| 479 | + } |
| 480 | +} |
| 481 | + |
387 | 482 | inline std::string toString(const FontVariant &fontVariant) |
388 | 483 | { |
389 | 484 | auto result = std::string{}; |
|
0 commit comments