Fabric/Text: attributedstring module, the second part
Reviewed By: mdvacca Differential Revision: D7748711 fbshipit-source-id: 57b37cb2eaa20b85c6f6719b38eac2a2c0479485
This commit is contained in:
parent
e2287976f3
commit
62576bcb78
|
@ -0,0 +1,68 @@
|
|||
/**
|
||||
* 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 "AttributedString.h"
|
||||
|
||||
#include <fabric/debug/DebugStringConvertibleItem.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
using Fragment = AttributedString::Fragment;
|
||||
using Fragments = AttributedString::Fragments;
|
||||
|
||||
void AttributedString::appendFragment(const Fragment &fragment) {
|
||||
ensureUnsealed();
|
||||
fragments_.push_back(fragment);
|
||||
}
|
||||
|
||||
void AttributedString::prependFragment(const Fragment &fragment) {
|
||||
ensureUnsealed();
|
||||
fragments_.insert(fragments_.begin(), fragment);
|
||||
}
|
||||
|
||||
void AttributedString::appendAttributedString(const AttributedString &attributedString) {
|
||||
ensureUnsealed();
|
||||
fragments_.insert(fragments_.end(), attributedString.fragments_.begin(), attributedString.fragments_.end());
|
||||
}
|
||||
|
||||
void AttributedString::prependAttributedString(const AttributedString &attributedString) {
|
||||
ensureUnsealed();
|
||||
fragments_.insert(fragments_.begin(), attributedString.fragments_.begin(), attributedString.fragments_.end());
|
||||
}
|
||||
|
||||
const std::vector<Fragment> &AttributedString::getFragments() const {
|
||||
return fragments_;
|
||||
}
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList AttributedString::getDebugChildren() const {
|
||||
SharedDebugStringConvertibleList list = {};
|
||||
|
||||
for (auto &&fragment : fragments_) {
|
||||
auto propsList = fragment.textAttributes.DebugStringConvertible::getDebugProps();
|
||||
|
||||
if (fragment.shadowNode) {
|
||||
propsList.push_back(std::make_shared<DebugStringConvertibleItem>("shadowNode", fragment.shadowNode->getDebugDescription()));
|
||||
}
|
||||
|
||||
list.push_back(
|
||||
std::make_shared<DebugStringConvertibleItem>(
|
||||
"Fragment",
|
||||
fragment.string,
|
||||
SharedDebugStringConvertibleList(),
|
||||
propsList
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
|
@ -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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <fabric/attributedstring/TextAttributes.h>
|
||||
#include <fabric/core/Sealable.h>
|
||||
#include <fabric/core/ShadowNode.h>
|
||||
#include <fabric/debug/DebugStringConvertible.h>
|
||||
#include <folly/Optional.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class AttributedString;
|
||||
|
||||
using SharedAttributedString = std::shared_ptr<const AttributedString>;
|
||||
|
||||
/*
|
||||
* Simple, cross-platfrom, React-specific implementation of attributed string
|
||||
* (aka spanned string).
|
||||
* `AttributedString` is basically a list of `Fragments` which have `string` and
|
||||
* `textAttributes` + `shadowNode` associated with the `string`.
|
||||
*/
|
||||
class AttributedString:
|
||||
public Sealable,
|
||||
public DebugStringConvertible {
|
||||
|
||||
public:
|
||||
|
||||
class Fragment {
|
||||
public:
|
||||
std::string string;
|
||||
TextAttributes textAttributes;
|
||||
SharedShadowNode shadowNode;
|
||||
};
|
||||
|
||||
using Fragments = std::vector<Fragment>;
|
||||
|
||||
/*
|
||||
* Appends and prepends a `fragment` to the string.
|
||||
*/
|
||||
void appendFragment(const Fragment &fragment);
|
||||
void prependFragment(const Fragment &fragment);
|
||||
|
||||
/*
|
||||
* Appends and prepends an `attributedString` (all its fragments) to
|
||||
* the string.
|
||||
*/
|
||||
void appendAttributedString(const AttributedString &attributedString);
|
||||
void prependAttributedString(const AttributedString &attributedString);
|
||||
|
||||
/*
|
||||
* Returns read-only reference to a list of fragments.
|
||||
*/
|
||||
const Fragments &getFragments() const;
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList getDebugChildren() const override;
|
||||
|
||||
private:
|
||||
|
||||
Fragments fragments_;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* 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 "TextAttributes.h"
|
||||
|
||||
#include <fabric/debug/DebugStringConvertibleItem.h>
|
||||
#include <fabric/graphics/graphicValuesConversions.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
void TextAttributes::apply(TextAttributes textAttributes) {
|
||||
// Color
|
||||
foregroundColor = textAttributes.foregroundColor ? textAttributes.foregroundColor : foregroundColor;
|
||||
backgroundColor = textAttributes.backgroundColor ? textAttributes.backgroundColor : backgroundColor;
|
||||
opacity = !isnan(textAttributes.opacity) ? textAttributes.opacity : opacity;
|
||||
|
||||
// Font
|
||||
fontFamily = !textAttributes.fontFamily.empty() ? textAttributes.fontFamily : fontFamily;
|
||||
fontSize = !isnan(textAttributes.fontSize) ? textAttributes.fontSize : fontSize;
|
||||
fontSizeMultiplier = !isnan(textAttributes.fontSizeMultiplier) ? textAttributes.fontSizeMultiplier : fontSizeMultiplier;
|
||||
fontWeight = textAttributes.fontWeight.has_value() ? textAttributes.fontWeight : fontWeight;
|
||||
fontStyle = textAttributes.fontStyle.has_value() ? textAttributes.fontStyle : fontStyle;
|
||||
fontVariant = textAttributes.fontVariant.has_value() ? textAttributes.fontVariant : fontVariant;
|
||||
allowFontScaling = textAttributes.allowFontScaling.has_value() ? textAttributes.allowFontScaling : allowFontScaling;
|
||||
letterSpacing = !isnan(textAttributes.letterSpacing) ? textAttributes.letterSpacing : letterSpacing;
|
||||
|
||||
// Paragraph Styles
|
||||
lineHeight = !isnan(textAttributes.lineHeight) ? textAttributes.lineHeight : lineHeight;
|
||||
alignment = textAttributes.alignment.has_value() ? textAttributes.alignment : alignment;
|
||||
baseWritingDirection = textAttributes.baseWritingDirection.has_value() ? textAttributes.baseWritingDirection : baseWritingDirection;
|
||||
|
||||
// Decoration
|
||||
textDecorationColor = textAttributes.textDecorationColor ? textAttributes.textDecorationColor : textDecorationColor;
|
||||
textDecorationLineType = textAttributes.textDecorationLineType.has_value() ? textAttributes.textDecorationLineType : textDecorationLineType;
|
||||
textDecorationLineStyle = textAttributes.textDecorationLineStyle.has_value() ? textAttributes.textDecorationLineStyle : textDecorationLineStyle;
|
||||
textDecorationLinePattern = textAttributes.textDecorationLinePattern.has_value() ? textAttributes.textDecorationLinePattern : textDecorationLinePattern;
|
||||
|
||||
// Shadow
|
||||
textShadowOffset = textAttributes.textShadowOffset.has_value() ? textAttributes.textShadowOffset.value() : textShadowOffset;
|
||||
textShadowRadius = !isnan(textAttributes.textShadowRadius) ? textAttributes.textShadowRadius : textShadowRadius;
|
||||
textShadowColor = textAttributes.textShadowColor ? textAttributes.textShadowColor : textShadowColor;
|
||||
|
||||
// Special
|
||||
isHighlighted = textAttributes.isHighlighted.has_value() ? textAttributes.isHighlighted : isHighlighted;
|
||||
layoutDirection = textAttributes.layoutDirection.has_value() ? textAttributes.layoutDirection : layoutDirection;
|
||||
}
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList TextAttributes::getDebugProps() const {
|
||||
TextAttributes defaultAttributes = {};
|
||||
|
||||
SharedDebugStringConvertibleList list = {};
|
||||
|
||||
#define PROPS_ADD_TO_SET(propertyName, accessor, convertor) \
|
||||
if (propertyName != defaultAttributes.propertyName) { \
|
||||
list.push_back(std::make_shared<DebugStringConvertibleItem>(#propertyName, convertor(propertyName accessor))); \
|
||||
}
|
||||
|
||||
PROPS_ADD_TO_SET(backgroundColor, , colorNameFromColor)
|
||||
PROPS_ADD_TO_SET(foregroundColor, , colorNameFromColor)
|
||||
PROPS_ADD_TO_SET(opacity, , std::to_string)
|
||||
|
||||
PROPS_ADD_TO_SET(fontFamily, , )
|
||||
PROPS_ADD_TO_SET(fontSize, , std::to_string)
|
||||
PROPS_ADD_TO_SET(fontSizeMultiplier, , std::to_string)
|
||||
|
||||
// TODO: Implement all fields.
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
|
@ -0,0 +1,79 @@
|
|||
/**
|
||||
* 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 <limits>
|
||||
|
||||
#include <fabric/attributedstring/TextPrimitives.h>
|
||||
#include <fabric/core/LayoutPrimitives.h>
|
||||
#include <fabric/core/ReactPrimitives.h>
|
||||
#include <fabric/debug/DebugStringConvertible.h>
|
||||
#include <fabric/graphics/Color.h>
|
||||
#include <fabric/graphics/Geometry.h>
|
||||
#include <folly/Optional.h>
|
||||
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
class TextAttributes;
|
||||
|
||||
using SharedTextAttributes = std::shared_ptr<const TextAttributes>;
|
||||
|
||||
class TextAttributes:
|
||||
public DebugStringConvertible {
|
||||
public:
|
||||
|
||||
#pragma mark - Fields
|
||||
|
||||
// Color
|
||||
SharedColor foregroundColor {nullptr};
|
||||
SharedColor backgroundColor {nullptr};
|
||||
Float opacity {std::numeric_limits<Float>::quiet_NaN()};
|
||||
|
||||
// Font
|
||||
std::string fontFamily {""};
|
||||
Float fontSize {std::numeric_limits<Float>::quiet_NaN()};
|
||||
Float fontSizeMultiplier {std::numeric_limits<Float>::quiet_NaN()};
|
||||
folly::Optional<FontWeight> fontWeight {};
|
||||
folly::Optional<FontStyle> fontStyle {};
|
||||
folly::Optional<FontVariant> fontVariant {};
|
||||
folly::Optional<bool> allowFontScaling {};
|
||||
Float letterSpacing {std::numeric_limits<Float>::quiet_NaN()};
|
||||
|
||||
// Paragraph Styles
|
||||
Float lineHeight {std::numeric_limits<Float>::quiet_NaN()};
|
||||
folly::Optional<TextAlignment> alignment {};
|
||||
folly::Optional<WritingDirection> baseWritingDirection {};
|
||||
|
||||
// Decoration
|
||||
SharedColor textDecorationColor {nullptr};
|
||||
folly::Optional<TextDecorationLineType> textDecorationLineType {};
|
||||
folly::Optional<TextDecorationLineStyle> textDecorationLineStyle {};
|
||||
folly::Optional<TextDecorationLinePattern> textDecorationLinePattern {};
|
||||
|
||||
// Shadow
|
||||
folly::Optional<Point> textShadowOffset {};
|
||||
Float textShadowRadius {std::numeric_limits<Float>::quiet_NaN()};
|
||||
SharedColor textShadowColor {nullptr};
|
||||
|
||||
// Special
|
||||
folly::Optional<bool> isHighlighted {};
|
||||
folly::Optional<LayoutDirection> layoutDirection {};
|
||||
|
||||
#pragma mark - Operations
|
||||
|
||||
void apply(TextAttributes textAttributes);
|
||||
|
||||
#pragma mark - DebugStringConvertible
|
||||
|
||||
SharedDebugStringConvertibleList getDebugProps() const override;
|
||||
};
|
||||
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
Loading…
Reference in New Issue