Fabric: Text types conversions & better debug printing

Summary: Trivial.

Reviewed By: fkgozali

Differential Revision: D7863489

fbshipit-source-id: c7b9852e104ca12eea144473ed8e1f2973a36df7
This commit is contained in:
Valentin Shergin 2018-05-14 15:43:25 -07:00 committed by Facebook Github Bot
parent 37652f26b4
commit 582e1bded3
6 changed files with 128 additions and 9 deletions

View File

@ -7,6 +7,7 @@
#include "TextAttributes.h"
#include <fabric/core/debugStringConvertibleUtils.h>
#include <fabric/graphics/debugStringConvertibleUtils.h>
#include <fabric/graphics/graphicValuesConversions.h>
#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),
};
}

View File

@ -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

View File

@ -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

View File

@ -20,6 +20,7 @@ rn_xplat_cxx_library(
header_namespace = "",
exported_headers = subdir_glob(
[
("", "*.h"),
("primitives", "*.h"),
("componentdescriptor", "*.h"),
("layout", "*.h"),

View File

@ -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 <fabric/debug/debugStringConvertibleUtils.h>
#include <fabric/core/layoutValuesConversions.h>
namespace facebook {
namespace react {
DEBUG_STRING_CONVERTIBLE_TEMPLATE(LayoutDirection, stringFromLayoutDirection)
DEBUG_STRING_CONVERTIBLE_TEMPLATE(DisplayType, stringFromDisplayType)
} // namespace react
} // namespace facebook

View File

@ -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 <fabric/core/LayoutPrimitives.h>
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