Fabric/Text: text module, text part

Summary: <Text> component is used to describe text attributes in React.

Reviewed By: mdvacca

Differential Revision: D7751851

fbshipit-source-id: f90a4367cad64283ee64828b0d5e24470ee3d9f7
This commit is contained in:
Valentin Shergin
2018-05-07 21:49:21 -07:00
committed by Facebook Github Bot
parent 02c3cd4c4e
commit dc7a87e737
7 changed files with 275 additions and 1 deletions

View File

@@ -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 <fabric/attributedstring/TextPrimitives.h>
#include <fabric/attributedstring/textValuesConversions.h>
#include <fabric/core/propsConversions.h>
#include <folly/dynamic.h>
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

View File

@@ -9,6 +9,6 @@
#include <gtest/gtest.h>
TEST(TextLayoutManagerTest, testSomething) {
TEST(TextTest, testSomething) {
// TODO:
}

View File

@@ -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 <fabric/core/ConcreteComponentDescriptor.h>
#include <fabric/text/TextProps.h>
#include <fabric/text/TextShadowNode.h>
namespace facebook {
namespace react {
class TextComponentDescriptor: public ConcreteComponentDescriptor<TextShadowNode> {
public:
ComponentName getComponentName() const override {
return "Text";
}
};
} // namespace react
} // namespace facebook

View File

@@ -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 <fabric/attributedstring/textValuesConversions.h>
#include <fabric/core/propsConversions.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
#include <fabric/graphics/graphicValuesConversions.h>
#include <fabric/text/propsConversions.h>
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

View File

@@ -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 <fabric/attributedstring/TextAttributes.h>
#include <fabric/core/Props.h>
#include <fabric/graphics/Color.h>
#include <fabric/graphics/Geometry.h>
namespace facebook {
namespace react {
class TextProps;
using SharedTextProps = std::shared_ptr<const TextProps>;
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

View File

@@ -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 <fabric/debug/DebugStringConvertibleItem.h>
#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<const RawTextShadowNode>(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<const TextShadowNode>(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

View File

@@ -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 <fabric/attributedstring/AttributedString.h>
#include <fabric/attributedstring/TextAttributes.h>
#include <fabric/core/ConcreteShadowNode.h>
#include <fabric/core/ShadowNode.h>
#include <fabric/text/TextProps.h>
#include <fabric/text/TextShadowNode.h>
namespace facebook {
namespace react {
class TextShadowNode;
using SharedTextShadowNode = std::shared_ptr<const TextShadowNode>;
class TextShadowNode:
public ConcreteShadowNode<TextProps> {
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