diff --git a/ReactCommon/fabric/text/propsConversions.h b/ReactCommon/fabric/text/propsConversions.h new file mode 100644 index 00000000000..1656e82af44 --- /dev/null +++ b/ReactCommon/fabric/text/propsConversions.h @@ -0,0 +1,29 @@ +/** + * 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 +#include +#include + +namespace facebook { +namespace react { + +APPLY_RAW_PROP_TEMPLATE(EllipsizeMode, ellipsizeModeFromDynamic) +APPLY_RAW_PROP_TEMPLATE(FontWeight, fontWeightFromDynamic) +APPLY_RAW_PROP_TEMPLATE(FontStyle, fontStyleFromDynamic) +APPLY_RAW_PROP_TEMPLATE(FontVariant, fontVariantFromDynamic) +APPLY_RAW_PROP_TEMPLATE(WritingDirection, writingDirectionFromDynamic) +APPLY_RAW_PROP_TEMPLATE(TextAlignment, textAlignmentFromDynamic) +APPLY_RAW_PROP_TEMPLATE(TextDecorationLineType, textDecorationLineTypeFromDynamic) +APPLY_RAW_PROP_TEMPLATE(TextDecorationLineStyle, textDecorationLineStyleFromDynamic) +APPLY_RAW_PROP_TEMPLATE(TextDecorationLinePattern, textDecorationLinePatternFromDynamic) + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/text/tests/TextTest.cpp b/ReactCommon/fabric/text/tests/TextTest.cpp index 55ae97a50fa..6b995ae90d7 100644 --- a/ReactCommon/fabric/text/tests/TextTest.cpp +++ b/ReactCommon/fabric/text/tests/TextTest.cpp @@ -9,6 +9,6 @@ #include -TEST(TextLayoutManagerTest, testSomething) { +TEST(TextTest, testSomething) { // TODO: } diff --git a/ReactCommon/fabric/text/text/TextComponentDescriptor.h b/ReactCommon/fabric/text/text/TextComponentDescriptor.h new file mode 100644 index 00000000000..043492f21e9 --- /dev/null +++ b/ReactCommon/fabric/text/text/TextComponentDescriptor.h @@ -0,0 +1,25 @@ +/** + * 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 +#include + +namespace facebook { +namespace react { + +class TextComponentDescriptor: public ConcreteComponentDescriptor { +public: + ComponentName getComponentName() const override { + return "Text"; + } +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/text/text/TextProps.cpp b/ReactCommon/fabric/text/text/TextProps.cpp new file mode 100644 index 00000000000..402d2ea6712 --- /dev/null +++ b/ReactCommon/fabric/text/text/TextProps.cpp @@ -0,0 +1,75 @@ +/** + * 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. + */ + +#include "TextProps.h" + +#include +#include +#include +#include +#include + +namespace facebook { +namespace react { + +void TextProps::apply(const RawProps &rawProps) { + Props::apply(rawProps); + + // Color + applyRawProp(rawProps, "color", textAttributes_.foregroundColor); + applyRawProp(rawProps, "backgroundColor", textAttributes_.backgroundColor); + applyRawProp(rawProps, "opacity", textAttributes_.opacity); + + // Font + applyRawProp(rawProps, "fontFamily", textAttributes_.fontFamily); + applyRawProp(rawProps, "fontSize", textAttributes_.fontSize); + applyRawProp(rawProps, "fontSizeMultiplier", textAttributes_.fontSizeMultiplier); + applyRawProp(rawProps, "fontWeight", textAttributes_.fontWeight); + applyRawProp(rawProps, "fontStyle", textAttributes_.fontStyle); + applyRawProp(rawProps, "fontVariant", textAttributes_.fontVariant); + applyRawProp(rawProps, "allowFontScaling", textAttributes_.allowFontScaling); + applyRawProp(rawProps, "letterSpacing", textAttributes_.letterSpacing); + + // Paragraph + applyRawProp(rawProps, "lineHeight", textAttributes_.lineHeight); + applyRawProp(rawProps, "alignment", textAttributes_.alignment); + applyRawProp(rawProps, "baseWritingDirection", textAttributes_.baseWritingDirection); + + // Decoration + applyRawProp(rawProps, "textDecorationColor", textAttributes_.textDecorationColor); + applyRawProp(rawProps, "textDecorationLineType", textAttributes_.textDecorationLineType); + applyRawProp(rawProps, "textDecorationLineStyle", textAttributes_.textDecorationLineStyle); + applyRawProp(rawProps, "textDecorationLinePattern", textAttributes_.textDecorationLinePattern); + + // Shadow + applyRawProp(rawProps, "textShadowOffset", textAttributes_.textShadowOffset); + applyRawProp(rawProps, "textShadowRadius", textAttributes_.textShadowRadius); + applyRawProp(rawProps, "textShadowColor", textAttributes_.textShadowColor); + + // Special + applyRawProp(rawProps, "isHighlighted", textAttributes_.isHighlighted); +} + +#pragma mark - Getters + +TextAttributes TextProps::getTextAttributes() const { + return textAttributes_; +} + +#pragma mark - DebugStringConvertible + +SharedDebugStringConvertibleList TextProps::getDebugProps() const { + SharedDebugStringConvertibleList list = {}; + + auto textAttributesPropsList = textAttributes_.getDebugProps(); + std::move(textAttributesPropsList.begin(), textAttributesPropsList.end(), std::back_inserter(list)); + + return list; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/text/text/TextProps.h b/ReactCommon/fabric/text/text/TextProps.h new file mode 100644 index 00000000000..1a96b5519fb --- /dev/null +++ b/ReactCommon/fabric/text/text/TextProps.h @@ -0,0 +1,46 @@ +/** + * 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 +#include +#include + +namespace facebook { +namespace react { + +class TextProps; + +using SharedTextProps = std::shared_ptr; + +class TextProps: + public Props { + +public: + void apply(const RawProps &rawProps) override; + +#pragma mark - Getters + + TextAttributes getTextAttributes() const; + +private: + + /* + * Not all `TextAttributes` fields make sense and is used as TextProps values. + */ + TextAttributes textAttributes_; + +#pragma mark - DebugStringConvertible + + SharedDebugStringConvertibleList getDebugProps() const override; +}; + +} // namespace react +} // namespace facebook + diff --git a/ReactCommon/fabric/text/text/TextShadowNode.cpp b/ReactCommon/fabric/text/text/TextShadowNode.cpp new file mode 100644 index 00000000000..5429c7ba606 --- /dev/null +++ b/ReactCommon/fabric/text/text/TextShadowNode.cpp @@ -0,0 +1,59 @@ +/** + * 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. + */ + +#include "TextShadowNode.h" + +#include + +#include "RawTextShadowNode.h" +#include "RawTextProps.h" + +namespace facebook { +namespace react { + +ComponentName TextShadowNode::getComponentName() const { + return ComponentName("Text"); +} + +AttributedString TextShadowNode::getAttributedString(const TextAttributes &baseTextAttributes) const { + // TODO: Implement caching. + + TextAttributes textAttributes = baseTextAttributes; + textAttributes.apply(getProps()->getTextAttributes()); + + AttributedString attributedString; + + for (auto &&childNode : *getChildren()) { + // RawShadowNode + SharedRawTextShadowNode rawTextShadowNode = std::dynamic_pointer_cast(childNode); + if (rawTextShadowNode) { + AttributedString::Fragment fragment; + fragment.string = rawTextShadowNode->getProps()->getText(); + fragment.textAttributes = textAttributes; + attributedString.appendFragment(fragment); + continue; + } + + // TextShadowNode + SharedTextShadowNode textShadowNode = std::dynamic_pointer_cast(childNode); + if (textShadowNode) { + attributedString.appendAttributedString(textShadowNode->getAttributedString(textAttributes)); + continue; + } + + // Any other kind of ShadowNode + AttributedString::Fragment fragment; + fragment.shadowNode = childNode; + fragment.textAttributes = textAttributes; + attributedString.appendFragment(fragment); + } + + return attributedString; +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/text/text/TextShadowNode.h b/ReactCommon/fabric/text/text/TextShadowNode.h new file mode 100644 index 00000000000..0eb421a9663 --- /dev/null +++ b/ReactCommon/fabric/text/text/TextShadowNode.h @@ -0,0 +1,40 @@ +/** + * 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 +#include +#include +#include +#include + +namespace facebook { +namespace react { + +class TextShadowNode; + +using SharedTextShadowNode = std::shared_ptr; + +class TextShadowNode: + public ConcreteShadowNode { + +public: + + using ConcreteShadowNode::ConcreteShadowNode; + + ComponentName getComponentName() const override; + + /* + * Returns a `AttributedString` which represents text content of the node. + */ + AttributedString getAttributedString(const TextAttributes &baseTextAttributes) const; +}; + +} // namespace react +} // namespace facebook