From 582e1bded3a72f70625230afb8dfa8365e34d0e1 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Mon, 14 May 2018 15:43:25 -0700 Subject: [PATCH] Fabric: Text types conversions & better debug printing Summary: Trivial. Reviewed By: fkgozali Differential Revision: D7863489 fbshipit-source-id: c7b9852e104ca12eea144473ed8e1f2973a36df7 --- .../attributedstring/TextAttributes.cpp | 17 +++--- .../debugStringConvertibleUtils.h | 6 ++ .../attributedstring/textValuesConversions.h | 61 +++++++++++++++++++ ReactCommon/fabric/core/BUCK | 1 + .../fabric/core/debugStringConvertibleUtils.h | 20 ++++++ .../core/layout/layoutValuesConversions.h | 32 ++++++++++ 6 files changed, 128 insertions(+), 9 deletions(-) create mode 100644 ReactCommon/fabric/core/debugStringConvertibleUtils.h create mode 100644 ReactCommon/fabric/core/layout/layoutValuesConversions.h diff --git a/ReactCommon/fabric/attributedstring/TextAttributes.cpp b/ReactCommon/fabric/attributedstring/TextAttributes.cpp index 4f85297dc..0373f5f2a 100644 --- a/ReactCommon/fabric/attributedstring/TextAttributes.cpp +++ b/ReactCommon/fabric/attributedstring/TextAttributes.cpp @@ -7,6 +7,7 @@ #include "TextAttributes.h" +#include #include #include #include "debugStringConvertibleUtils.h" @@ -54,8 +55,6 @@ void TextAttributes::apply(TextAttributes textAttributes) { #pragma mark - DebugStringConvertible SharedDebugStringConvertibleList TextAttributes::getDebugProps() const { - // TODO: Implement all fields. - return { // Color debugStringConvertibleItem("backgroundColor", backgroundColor), @@ -68,20 +67,20 @@ SharedDebugStringConvertibleList TextAttributes::getDebugProps() const { debugStringConvertibleItem("fontSizeMultiplier", fontSizeMultiplier), debugStringConvertibleItem("fontWeight", fontWeight), debugStringConvertibleItem("fontStyle", fontStyle), - //debugStringConvertibleItem("fontVariant", fontVariant), + debugStringConvertibleItem("fontVariant", fontVariant), debugStringConvertibleItem("allowFontScaling", allowFontScaling), debugStringConvertibleItem("letterSpacing", letterSpacing), // Paragraph Styles debugStringConvertibleItem("lineHeight", lineHeight), - //debugStringConvertibleItem("alignment", alignment), - //debugStringConvertibleItem("baseWritingDirection", baseWritingDirection), + debugStringConvertibleItem("alignment", alignment), + debugStringConvertibleItem("baseWritingDirection", baseWritingDirection), // Decoration debugStringConvertibleItem("textDecorationColor", textDecorationColor), - //debugStringConvertibleItem("textDecorationLineType", textDecorationLineType), - //debugStringConvertibleItem("textDecorationLineStyle", textDecorationLineStyle), - //debugStringConvertibleItem("textDecorationLinePattern", textDecorationLinePattern), + debugStringConvertibleItem("textDecorationLineType", textDecorationLineType), + debugStringConvertibleItem("textDecorationLineStyle", textDecorationLineStyle), + debugStringConvertibleItem("textDecorationLinePattern", textDecorationLinePattern), // Shadow debugStringConvertibleItem("textShadowOffset", textShadowOffset), @@ -90,7 +89,7 @@ SharedDebugStringConvertibleList TextAttributes::getDebugProps() const { // Special debugStringConvertibleItem("isHighlighted", isHighlighted), - //debugStringConvertibleItem("layoutDirection", layoutDirection), + debugStringConvertibleItem("layoutDirection", layoutDirection), }; } diff --git a/ReactCommon/fabric/attributedstring/debugStringConvertibleUtils.h b/ReactCommon/fabric/attributedstring/debugStringConvertibleUtils.h index 617efb4c1..3f303cd06 100644 --- a/ReactCommon/fabric/attributedstring/debugStringConvertibleUtils.h +++ b/ReactCommon/fabric/attributedstring/debugStringConvertibleUtils.h @@ -16,6 +16,12 @@ namespace react { DEBUG_STRING_CONVERTIBLE_TEMPLATE(EllipsizeMode, stringFromEllipsizeMode) DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontWeight, stringFromFontWeight) DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontStyle, stringFromFontStyle) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(FontVariant, stringFromFontVariant) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextAlignment, stringFromTextAlignment) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(WritingDirection, stringFromWritingDirection) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLineType, stringFromTextDecorationLineType) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLineStyle, stringFromTextDecorationLineStyle) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(TextDecorationLinePattern, stringFromTextDecorationLinePattern) } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/attributedstring/textValuesConversions.h b/ReactCommon/fabric/attributedstring/textValuesConversions.h index deb5660e2..bae4eb65b 100644 --- a/ReactCommon/fabric/attributedstring/textValuesConversions.h +++ b/ReactCommon/fabric/attributedstring/textValuesConversions.h @@ -83,6 +83,22 @@ inline FontVariant fontVariantFromDynamic(const folly::dynamic &value) { return fontVariant; } +inline std::string stringFromFontVariant(const FontVariant &fontVariant) { + std::string result; + std::string separator = ", "; + 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 TextAlignment textAlignmentFromDynamic(const folly::dynamic &value) { auto string = value.asString(); if (string == "natural") { return TextAlignment::Natural; } @@ -93,6 +109,16 @@ inline TextAlignment textAlignmentFromDynamic(const folly::dynamic &value) { abort(); } +inline std::string stringFromTextAlignment(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 WritingDirection writingDirectionFromDynamic(const folly::dynamic &value) { auto string = value.asString(); if (string == "natural") { return WritingDirection::Natural; } @@ -101,6 +127,14 @@ inline WritingDirection writingDirectionFromDynamic(const folly::dynamic &value) abort(); } +inline std::string stringFromWritingDirection(const WritingDirection &writingDirection) { + switch (writingDirection) { + case WritingDirection::Natural: return "natural"; + case WritingDirection::LeftToRight: return "ltr"; + case WritingDirection::RightToLeft: return "rtl"; + } +} + inline TextDecorationLineType textDecorationLineTypeFromDynamic(const folly::dynamic &value) { auto string = value.asString(); if (string == "none") { return TextDecorationLineType::None; } @@ -110,6 +144,15 @@ inline TextDecorationLineType textDecorationLineTypeFromDynamic(const folly::dyn abort(); } +inline std::string stringFromTextDecorationLineType(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 TextDecorationLineStyle textDecorationLineStyleFromDynamic(const folly::dynamic &value) { auto string = value.asString(); if (string == "single") { return TextDecorationLineStyle::Single; } @@ -118,6 +161,14 @@ inline TextDecorationLineStyle textDecorationLineStyleFromDynamic(const folly::d abort(); } +inline std::string stringFromTextDecorationLineStyle(const TextDecorationLineStyle &textDecorationLineStyle) { + switch (textDecorationLineStyle) { + case TextDecorationLineStyle::Single: return "single"; + case TextDecorationLineStyle::Thick: return "thick"; + case TextDecorationLineStyle::Double: return "double"; + } +} + inline TextDecorationLinePattern textDecorationLinePatternFromDynamic(const folly::dynamic &value) { auto string = value.asString(); if (string == "solid") { return TextDecorationLinePattern::Solid; } @@ -128,5 +179,15 @@ inline TextDecorationLinePattern textDecorationLinePatternFromDynamic(const foll abort(); } +inline std::string stringFromTextDecorationLinePattern(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"; + } +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/core/BUCK b/ReactCommon/fabric/core/BUCK index 4c732efc4..02c39ac75 100644 --- a/ReactCommon/fabric/core/BUCK +++ b/ReactCommon/fabric/core/BUCK @@ -20,6 +20,7 @@ rn_xplat_cxx_library( header_namespace = "", exported_headers = subdir_glob( [ + ("", "*.h"), ("primitives", "*.h"), ("componentdescriptor", "*.h"), ("layout", "*.h"), diff --git a/ReactCommon/fabric/core/debugStringConvertibleUtils.h b/ReactCommon/fabric/core/debugStringConvertibleUtils.h new file mode 100644 index 000000000..0dc60ea89 --- /dev/null +++ b/ReactCommon/fabric/core/debugStringConvertibleUtils.h @@ -0,0 +1,20 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * 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 +#include + +namespace facebook { +namespace react { + +DEBUG_STRING_CONVERTIBLE_TEMPLATE(LayoutDirection, stringFromLayoutDirection) +DEBUG_STRING_CONVERTIBLE_TEMPLATE(DisplayType, stringFromDisplayType) + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/core/layout/layoutValuesConversions.h b/ReactCommon/fabric/core/layout/layoutValuesConversions.h new file mode 100644 index 000000000..dc821a35f --- /dev/null +++ b/ReactCommon/fabric/core/layout/layoutValuesConversions.h @@ -0,0 +1,32 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * + * 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 + +namespace facebook { +namespace react { + +inline std::string stringFromLayoutDirection(const LayoutDirection &layoutDirection) { + switch (layoutDirection) { + case LayoutDirection::Undefined: return "undefined"; + case LayoutDirection::LeftToRight: return "ltr"; + case LayoutDirection::RightToLeft: return "rtl"; + } +} + +inline std::string stringFromDisplayType(const DisplayType &displayType) { + switch (displayType) { + case DisplayType::None: return "none"; + case DisplayType::Flex: return "flex"; + case DisplayType::Inline: return "inline"; + } +} + +} // namespace react +} // namespace facebook