From 888866461b9018c8316b2f2caa28bcabd28cebca Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 22 May 2020 01:17:26 -0700 Subject: [PATCH] Refactor types of ART Text class Summary: This diff refactors the types of ART Text classes, this is necessary on the next diffs of the stack closeoncommit changelog: [internal] Reviewed By: JoshuaGross Differential Revision: D21681876 fbshipit-source-id: ea438e89df6d860b3ff8bbdae657ca123b417a1b --- .../fabric/components/art/conversions.h | 1 + .../fabric/components/art/state/Text.h | 2 +- .../fabric/components/art/state/primitives.h | 16 +++++ .../components/art/text/ARTTextProps.cpp | 6 +- .../fabric/components/art/text/ARTTextProps.h | 65 ++++++++----------- 5 files changed, 49 insertions(+), 41 deletions(-) diff --git a/ReactCommon/fabric/components/art/conversions.h b/ReactCommon/fabric/components/art/conversions.h index b462eeaa964..4507da0d77e 100644 --- a/ReactCommon/fabric/components/art/conversions.h +++ b/ReactCommon/fabric/components/art/conversions.h @@ -30,6 +30,7 @@ inline folly::dynamic toDynamic(std::vector const &elements) { } return result; } + inline folly::dynamic toDynamic(Element::ListOfShared const &elements) { folly::dynamic children = folly::dynamic::array(); for (auto const &element : elements) { diff --git a/ReactCommon/fabric/components/art/state/Text.h b/ReactCommon/fabric/components/art/state/Text.h index 24a4e1e698b..6a65f128cee 100644 --- a/ReactCommon/fabric/components/art/state/Text.h +++ b/ReactCommon/fabric/components/art/state/Text.h @@ -31,7 +31,7 @@ class Text : public Shape { int aligment{0}; // TODO T64130144: add frame data - // ARTTextFrameStruct frame{} + // ARTTextFrame frame{} #ifdef ANDROID folly::dynamic getDynamic() const override; diff --git a/ReactCommon/fabric/components/art/state/primitives.h b/ReactCommon/fabric/components/art/state/primitives.h index bb305f91b64..5f384da99c5 100644 --- a/ReactCommon/fabric/components/art/state/primitives.h +++ b/ReactCommon/fabric/components/art/state/primitives.h @@ -7,6 +7,8 @@ #pragma once +#include +#include #include #include @@ -15,5 +17,19 @@ namespace react { enum class ARTElement { Shape, Text, Group }; +enum class ARTTextAlignment { Default, Right, Center }; + +struct ARTTextFrameFont { + Float fontSize; + std::string fontStyle; + std::string fontFamily; + std::string fontWeight; +}; + +struct ARTTextFrame { + std::vector lines; + ARTTextFrameFont font; +}; + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/components/art/text/ARTTextProps.cpp b/ReactCommon/fabric/components/art/text/ARTTextProps.cpp index 28164da595b..3e0d897d7b5 100644 --- a/ReactCommon/fabric/components/art/text/ARTTextProps.cpp +++ b/ReactCommon/fabric/components/art/text/ARTTextProps.cpp @@ -33,7 +33,11 @@ ARTTextProps::ARTTextProps( convertRawProp(rawProps, "strokeCap", sourceProps.strokeCap, {1})), strokeJoin( convertRawProp(rawProps, "strokeJoin", sourceProps.strokeJoin, {1})), - aligment(convertRawProp(rawProps, "aligment", sourceProps.aligment, {0})), + aligment(convertRawProp( + rawProps, + "aligment", + sourceProps.aligment, + {ARTTextAlignment::Default})), frame(convertRawProp(rawProps, "frame", sourceProps.frame, {})){}; #pragma mark - DebugStringConvertible diff --git a/ReactCommon/fabric/components/art/text/ARTTextProps.h b/ReactCommon/fabric/components/art/text/ARTTextProps.h index c00cd017de2..b1844b11ec6 100644 --- a/ReactCommon/fabric/components/art/text/ARTTextProps.h +++ b/ReactCommon/fabric/components/art/text/ARTTextProps.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -15,48 +16,34 @@ namespace facebook { namespace react { -struct ARTTextFrameFontStruct { - Float fontSize; - std::string fontStyle; - std::string fontFamily; - std::string fontWeight; -}; - static inline void fromRawValue( const RawValue &value, - ARTTextFrameFontStruct &result) { + ARTTextFrameFont &result) { auto map = (better::map)value; - - auto fontSize = map.find("fontSize"); - if (fontSize != map.end()) { - fromRawValue(fontSize->second, result.fontSize); - } - auto fontStyle = map.find("fontStyle"); - if (fontStyle != map.end()) { - fromRawValue(fontStyle->second, result.fontStyle); - } - auto fontFamily = map.find("fontFamily"); - if (fontFamily != map.end()) { - fromRawValue(fontFamily->second, result.fontFamily); - } - auto fontWeight = map.find("fontWeight"); - if (fontWeight != map.end()) { - fromRawValue(fontWeight->second, result.fontWeight); - } } -static inline std::string toString(const ARTTextFrameFontStruct &value) { - return "[Object ARTTextFrameFontStruct]"; -} - -struct ARTTextFrameStruct { - std::vector lines; - ARTTextFrameFontStruct font; -}; - static inline void fromRawValue( const RawValue &value, - ARTTextFrameStruct &result) { + ARTTextAlignment &result) { + auto alignment = (int)value; + switch (alignment) { + case 1: + result = ARTTextAlignment::Right; + break; + case 2: + result = ARTTextAlignment::Center; + break; + default: + result = ARTTextAlignment::Default; + break; + } +} + +static inline std::string toString(const ARTTextFrameFont &value) { + return "[Object ARTTextFrameFont]"; +} + +static inline void fromRawValue(const RawValue &value, ARTTextFrame &result) { auto map = (better::map)value; auto lines = map.find("lines"); @@ -69,8 +56,8 @@ static inline void fromRawValue( } } -static inline std::string toString(const ARTTextFrameStruct &value) { - return "[Object ARTTextFrameStruct]"; +static inline std::string toString(const ARTTextFrame &value) { + return "[Object ARTTextFrame]"; } class ARTTextProps; @@ -93,8 +80,8 @@ class ARTTextProps : public Props { Float strokeWidth{1.0}; int strokeCap{1}; int strokeJoin{1}; - int aligment{0}; - ARTTextFrameStruct frame{}; + ARTTextAlignment aligment{ARTTextAlignment::Default}; + ARTTextFrame frame{}; #pragma mark - DebugStringConvertible