From b8b683dc462928a59ef3815041cf51afa071fa73 Mon Sep 17 00:00:00 2001 From: David Vacca Date: Fri, 22 May 2020 01:17:26 -0700 Subject: [PATCH] Serialize ART text components and send data to Android Summary: This diff implements the serialization of Text components to send data from C++ to java changelog: [Internal] internal changes to support ART in fabric Reviewed By: JoshuaGross Differential Revision: D21681875 fbshipit-source-id: eba31f35c95e0a2d3226ec70421832719083d7fa --- .../fabric/components/art/conversions.h | 37 +++++++++++++++++-- .../fabric/components/art/state/Text.cpp | 2 +- .../fabric/components/art/state/Text.h | 31 +++++++++++++--- .../fabric/components/art/state/primitives.h | 2 +- .../components/art/text/ARTTextProps.cpp | 7 +--- .../fabric/components/art/text/ARTTextProps.h | 18 ++++++++- .../components/art/text/ARTTextShadowNode.cpp | 15 +++++++- 7 files changed, 94 insertions(+), 18 deletions(-) diff --git a/ReactCommon/fabric/components/art/conversions.h b/ReactCommon/fabric/components/art/conversions.h index 4507da0d77..cc3e9b1ca6 100644 --- a/ReactCommon/fabric/components/art/conversions.h +++ b/ReactCommon/fabric/components/art/conversions.h @@ -65,6 +65,38 @@ inline folly::dynamic toDynamic(Shape const &shape) { return result; } +inline folly::dynamic toDynamic(ARTTextAlignment const &aligment) { + switch (aligment) { + case ARTTextAlignment::Right: + return 1; + break; + case ARTTextAlignment::Center: + return 2; + break; + case ARTTextAlignment::Default: + default: + return 0; + break; + } +} + +inline folly::dynamic toDynamic(ARTTextFrame const &frame) { + folly::dynamic result = folly::dynamic::object(); + result["fontSize"] = frame.font.fontSize; + result["fontStyle"] = frame.font.fontStyle; + result["fontFamily"] = frame.font.fontFamily; + result["fontWeight"] = frame.font.fontWeight; + auto lines = frame.lines; + if (lines.size() > 0) { + folly::dynamic serializedLines = folly::dynamic::array(); + for (int i = 0; i < lines.size(); i++) { + serializedLines.push_back(lines[i]); + } + result["lines"] = serializedLines; + } + return result; +} + inline folly::dynamic toDynamic(Text const &text) { folly::dynamic result = folly::dynamic::object(); result["type"] = 3; @@ -77,9 +109,8 @@ inline folly::dynamic toDynamic(Text const &text) { result["strokeWidth"] = text.strokeWidth; result["strokeCap"] = text.strokeCap; result["strokeJoin"] = text.strokeJoin; - result["aligment"] = text.aligment; - // TODO T64130144: add serialization of Frame - // result["aligment"] = toDynamic(text.frame); + result["alignment"] = toDynamic(text.alignment); + result["frame"] = toDynamic(text.frame); return result; } diff --git a/ReactCommon/fabric/components/art/state/Text.cpp b/ReactCommon/fabric/components/art/state/Text.cpp index 6a3f262718..d3e52e2aba 100644 --- a/ReactCommon/fabric/components/art/state/Text.cpp +++ b/ReactCommon/fabric/components/art/state/Text.cpp @@ -13,7 +13,7 @@ namespace facebook { namespace react { #ifdef ANDROID -folly::dynamic Shape::getDynamic() const { +folly::dynamic Text::getDynamic() const { return toDynamic(*this); } #endif diff --git a/ReactCommon/fabric/components/art/state/Text.h b/ReactCommon/fabric/components/art/state/Text.h index 6a65f128ce..44f61d5e5d 100644 --- a/ReactCommon/fabric/components/art/state/Text.h +++ b/ReactCommon/fabric/components/art/state/Text.h @@ -24,14 +24,35 @@ namespace react { class Text : public Shape { public: using Shared = std::shared_ptr; - Text(ARTElement elementType) : Shape(){}; + Text( + Float opacity, + std::vector transform, + std::vector d, + std::vector stroke, + std::vector strokeDash, + std::vector fill, + Float strokeWidth, + int strokeCap, + int strokeJoin, + ARTTextAlignment alignment, + ARTTextFrame frame) + : Shape( + opacity, + transform, + d, + stroke, + strokeDash, + fill, + strokeWidth, + strokeCap, + strokeJoin), + alignment(alignment), + frame(frame){}; Text() = default; virtual ~Text(){}; - int aligment{0}; - - // TODO T64130144: add frame data - // ARTTextFrame frame{} + ARTTextAlignment alignment{ARTTextAlignment::Default}; + 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 5f384da99c..6af1d70297 100644 --- a/ReactCommon/fabric/components/art/state/primitives.h +++ b/ReactCommon/fabric/components/art/state/primitives.h @@ -8,9 +8,9 @@ #pragma once #include -#include #include #include +#include namespace facebook { namespace react { diff --git a/ReactCommon/fabric/components/art/text/ARTTextProps.cpp b/ReactCommon/fabric/components/art/text/ARTTextProps.cpp index 3e0d897d7b..b7948e9a8f 100644 --- a/ReactCommon/fabric/components/art/text/ARTTextProps.cpp +++ b/ReactCommon/fabric/components/art/text/ARTTextProps.cpp @@ -33,11 +33,8 @@ ARTTextProps::ARTTextProps( convertRawProp(rawProps, "strokeCap", sourceProps.strokeCap, {1})), strokeJoin( convertRawProp(rawProps, "strokeJoin", sourceProps.strokeJoin, {1})), - aligment(convertRawProp( - rawProps, - "aligment", - sourceProps.aligment, - {ARTTextAlignment::Default})), + alignment( + convertRawProp(rawProps, "alignment", sourceProps.alignment, {})), 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 b1844b11ec..087e56669c 100644 --- a/ReactCommon/fabric/components/art/text/ARTTextProps.h +++ b/ReactCommon/fabric/components/art/text/ARTTextProps.h @@ -20,6 +20,22 @@ static inline void fromRawValue( const RawValue &value, 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 void fromRawValue( @@ -80,7 +96,7 @@ class ARTTextProps : public Props { Float strokeWidth{1.0}; int strokeCap{1}; int strokeJoin{1}; - ARTTextAlignment aligment{ARTTextAlignment::Default}; + ARTTextAlignment alignment{ARTTextAlignment::Default}; ARTTextFrame frame{}; #pragma mark - DebugStringConvertible diff --git a/ReactCommon/fabric/components/art/text/ARTTextShadowNode.cpp b/ReactCommon/fabric/components/art/text/ARTTextShadowNode.cpp index 49312c2ff2..953ba5ee20 100644 --- a/ReactCommon/fabric/components/art/text/ARTTextShadowNode.cpp +++ b/ReactCommon/fabric/components/art/text/ARTTextShadowNode.cpp @@ -15,8 +15,19 @@ namespace react { extern const char ARTTextComponentName[] = "ARTText"; Element::Shared ARTTextShadowNode::getElement() const { - // TODO add support for Text - return std::make_shared(); + auto props = getConcreteProps(); + return std::make_shared( + props.opacity, + props.transform, + props.d, + props.stroke, + props.strokeDash, + props.fill, + props.strokeWidth, + props.strokeCap, + props.strokeJoin, + props.alignment, + props.frame); } } // namespace react