Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

@interface RCTTextLayoutManager (RCTParagraphComponentViewPrivate)

- (CGRect)drawingFrameForAttributedString:(facebook::react::AttributedString)attributedString
- (CGRect)drawingFrameForAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
containerFrame:(CGRect *)containerFrame;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ AttributedStringBox::AttributedStringBox(const AttributedString& value)
value_(std::make_shared<const AttributedString>(value)),
opaquePointer_({}) {};

AttributedStringBox::AttributedStringBox(AttributedString&& value)
: mode_(Mode::Value),
value_(std::make_shared<const AttributedString>(std::move(value))),
opaquePointer_({}) {};

AttributedStringBox::AttributedStringBox(std::shared_ptr<void> opaquePointer)
: mode_(Mode::OpaquePointer),
value_({}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class AttributedStringBox final {
* Custom explicit constructors.
*/
explicit AttributedStringBox(const AttributedString &value);
explicit AttributedStringBox(AttributedString &&value);
explicit AttributedStringBox(std::shared_ptr<void> opaquePointer);

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ TEST(AttributedStringBoxTest, testValueConstructor) {
EXPECT_EQ(attributedStringBox.getValue(), attributedString);
}

TEST(AttributedStringBoxTest, testValueMoveConstructor) {
auto attributedString = AttributedString{};
auto fragment = AttributedString::Fragment{};
fragment.string = "test string";
attributedString.appendFragment(std::move(fragment));
auto expectedAttributedString = attributedString;
auto attributedStringBox = AttributedStringBox{std::move(attributedString)};

EXPECT_EQ(attributedStringBox.getMode(), AttributedStringBox::Mode::Value);
EXPECT_EQ(attributedStringBox.getValue(), expectedAttributedString);
}

TEST(AttributedStringBoxTest, testOpaquePointerConstructor) {
auto string = std::make_shared<std::string>("test string");
auto attributedStringBox = AttributedStringBox{string};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ const Content& ParagraphShadowNode::getContent(
attributedString.setBaseTextAttributes(textAttributes);

content_ = Content{
attributedString, getConcreteProps().paragraphAttributes, attachments};
std::move(attributedString),
getConcreteProps().paragraphAttributes,
std::move(attachments)};

return content_.value();
}
Expand Down Expand Up @@ -264,7 +266,7 @@ Size ParagraphShadowNode::measureContent(

auto size = textLayoutManager_
->measure(
AttributedStringBox{content.attributedString},
AttributedStringBox{std::move(content.attributedString)},
content.paragraphAttributes,
textLayoutContext,
layoutConstraints)
Expand All @@ -282,7 +284,7 @@ Float ParagraphShadowNode::baseline(
auto content =
getContentWithMeasuredAttachments(layoutContext, layoutConstraints);

AttributedStringBox attributedStringBox{content.attributedString};
AttributedStringBox attributedStringBox{std::move(content.attributedString)};

if constexpr (TextLayoutManagerExtended::supportsLineMeasurement()) {
auto lines =
Expand Down Expand Up @@ -346,7 +348,9 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) {
.pointScaleFactor = layoutContext.pointScaleFactor,
.surfaceId = getSurfaceId(),
};
AttributedStringBox attributedStringBox{content.attributedString};
// The state update above already captured the attributed string and
// nothing below reads it, so the box can take it instead of copying it.
AttributedStringBox attributedStringBox{std::move(content.attributedString)};

if (getConcreteProps().onTextLayout) {
if constexpr (TextLayoutManagerExtended::supportsLineMeasurement()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using RCTTextLayoutFragmentEnumerationBlock =
*/
@interface RCTTextLayoutManager : NSObject

- (facebook::react::TextMeasurement)measureAttributedString:(facebook::react::AttributedString)attributedString
- (facebook::react::TextMeasurement)measureAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
layoutContext:(facebook::react::TextLayoutContext)layoutContext
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;
Expand All @@ -39,23 +39,23 @@ using RCTTextLayoutFragmentEnumerationBlock =
layoutContext:(facebook::react::TextLayoutContext)layoutContext
layoutConstraints:(facebook::react::LayoutConstraints)layoutConstraints;

- (void)drawAttributedString:(facebook::react::AttributedString)attributedString
- (void)drawAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
drawHighlightPath:(void (^_Nullable)(UIBezierPath *highlightPath))block;

- (facebook::react::LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedString)attributedString
paragraphAttributes:
(facebook::react::ParagraphAttributes)paragraphAttributes
size:(CGSize)size;
- (facebook::react::LinesMeasurements)
getLinesForAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
size:(CGSize)size;

- (std::shared_ptr<const facebook::react::EventEmitter>)
getEventEmitterWithAttributeString:(facebook::react::AttributedString)attributedString
getEventEmitterWithAttributeString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
atPoint:(CGPoint)point;

- (void)getRectWithAttributedString:(facebook::react::AttributedString)attributedString
- (void)getRectWithAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
enumerateAttribute:(NSString *)enumerateAttribute
frame:(CGRect)frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr
return [self _measureTextStorage:textStorage paragraphAttributes:paragraphAttributes layoutContext:layoutContext];
}

- (TextMeasurement)measureAttributedString:(AttributedString)attributedString
- (TextMeasurement)measureAttributedString:(const AttributedString &)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
layoutContext:(TextLayoutContext)layoutContext
layoutConstraints:(LayoutConstraints)layoutConstraints
Expand All @@ -70,7 +70,7 @@ - (TextMeasurement)measureAttributedString:(AttributedString)attributedString
layoutConstraints:layoutConstraints];
}

- (CGRect)drawingFrameForAttributedString:(AttributedString)attributedString
- (CGRect)drawingFrameForAttributedString:(const AttributedString &)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
containerFrame:(CGRect *)containerFrame
Expand Down Expand Up @@ -128,7 +128,7 @@ - (CGRect)drawingFrameForAttributedString:(AttributedString)attributedString
return drawingFrame;
}

- (void)drawAttributedString:(AttributedString)attributedString
- (void)drawAttributedString:(const AttributedString &)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
drawHighlightPath:(void (^_Nullable)(UIBezierPath *highlightPath))block
Expand Down Expand Up @@ -363,7 +363,7 @@ - (void)processTruncatedAttributedText:(NSTextStorage *)textStorage
}
}

- (LinesMeasurements)getLinesForAttributedString:(facebook::react::AttributedString)attributedString
- (LinesMeasurements)getLinesForAttributedString:(const facebook::react::AttributedString &)attributedString
paragraphAttributes:(facebook::react::ParagraphAttributes)paragraphAttributes
size:(CGSize)size
{
Expand Down Expand Up @@ -467,7 +467,7 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
return textStorage;
}

- (std::shared_ptr<const EventEmitter>)getEventEmitterWithAttributeString:(AttributedString)attributedString
- (std::shared_ptr<const EventEmitter>)getEventEmitterWithAttributeString:(const AttributedString &)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
frame:(CGRect)frame
atPoint:(CGPoint)point
Expand Down Expand Up @@ -497,7 +497,7 @@ - (NSTextStorage *)_textStorageAndLayoutManagerWithAttributesString:(NSAttribute
return nil;
}

- (void)getRectWithAttributedString:(AttributedString)attributedString
- (void)getRectWithAttributedString:(const AttributedString &)attributedString
paragraphAttributes:(ParagraphAttributes)paragraphAttributes
enumerateAttribute:(NSString *)enumerateAttribute
frame:(CGRect)frame
Expand Down Expand Up @@ -539,7 +539,7 @@ - (void)getRectWithAttributedString:(AttributedString)attributedString

#pragma mark - Private

- (NSAttributedString *)_nsAttributedStringFromAttributedString:(AttributedString)attributedString
- (NSAttributedString *)_nsAttributedStringFromAttributedString:(const AttributedString &)attributedString
{
auto sharedNSAttributedString = _cache.get(attributedString, [&]() {
return wrapManagedObject(RCTNSAttributedStringFromAttributedString(attributedString));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@
#import "TextLayoutManager.h"
#import "RCTTextLayoutManager.h"

#import <optional>

#import <react/renderer/attributedstring/PlaceholderAttributedString.h>
#import <react/renderer/telemetry/TransactionTelemetry.h>
#import <react/utils/ManagedObjectWrapper.h>

namespace facebook::react {

// `ensurePlaceholderIfEmpty_DO_NOT_USE` returns a copy, so only call it for an empty string that needs one.
static std::optional<AttributedString> placeholderIfEmpty(const AttributedString &attributedString)
{
if (attributedString.isEmpty()) {
return ensurePlaceholderIfEmpty_DO_NOT_USE(attributedString);
}
return std::nullopt;
}

TextLayoutManager::TextLayoutManager(const std::shared_ptr<const ContextContainer> & /*contextContainer*/)
{
nativeTextLayoutManager_ = wrapManagedObject([RCTTextLayoutManager new]);
Expand All @@ -37,8 +48,10 @@

switch (attributedStringBox.getMode()) {
case AttributedStringBox::Mode::Value: {
auto originalAtributedString = attributedStringBox.getValue();
auto attributedString = ensurePlaceholderIfEmpty_DO_NOT_USE(originalAtributedString);
const auto &originalAttributedString = attributedStringBox.getValue();
auto placeholderAttributedString = placeholderIfEmpty(originalAttributedString);
const auto &attributedString =
placeholderAttributedString ? *placeholderAttributedString : originalAttributedString;

measurement = textMeasureCache_.get(
{.attributedString = attributedString,
Expand All @@ -60,7 +73,7 @@
// being used to represent empty string. iOS TextLayoutManager
// should instead measure using `baseTextAttributes` of the
// `AttributedString`.
if (originalAtributedString.isEmpty()) {
if (originalAttributedString.isEmpty()) {
measurement.size.width = 0;
}

Expand Down Expand Up @@ -106,7 +119,9 @@
const Size &size) const
{
react_native_assert(attributedStringBox.getMode() == AttributedStringBox::Mode::Value);
auto attributedString = ensurePlaceholderIfEmpty_DO_NOT_USE(attributedStringBox.getValue());
const auto &originalAttributedString = attributedStringBox.getValue();
auto placeholderAttributedString = placeholderIfEmpty(originalAttributedString);
const auto &attributedString = placeholderAttributedString ? *placeholderAttributedString : originalAttributedString;

RCTTextLayoutManager *textLayoutManager = (RCTTextLayoutManager *)unwrapManagedObject(nativeTextLayoutManager_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ class facebook::react::AttributedStringBox {
public AttributedStringBox();
public AttributedStringBox(const facebook::react::AttributedString& value);
public AttributedStringBox(const facebook::react::AttributedStringBox& other) = default;
public AttributedStringBox(facebook::react::AttributedString&& value);
public AttributedStringBox(facebook::react::AttributedStringBox&& other) noexcept;
public AttributedStringBox(std::shared_ptr<void> opaquePointer);
public const facebook::react::AttributedString& getValue() const;
Expand Down
11 changes: 6 additions & 5 deletions scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -1939,12 +1939,12 @@ interface RCTTextInputComponentView : public RCTViewComponentView {
}

interface RCTTextLayoutManager : public NSObject {
public virtual facebook::react::LinesMeasurements getLinesForAttributedString:paragraphAttributes:size:(facebook::react::AttributedString attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGSize size);
public virtual facebook::react::TextMeasurement measureAttributedString:paragraphAttributes:layoutContext:layoutConstraints:(facebook::react::AttributedString attributedString, facebook::react::ParagraphAttributes paragraphAttributes, facebook::react::TextLayoutContext layoutContext, facebook::react::LayoutConstraints layoutConstraints);
public virtual facebook::react::LinesMeasurements getLinesForAttributedString:paragraphAttributes:size:(const facebook::react::AttributedString& attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGSize size);
public virtual facebook::react::TextMeasurement measureAttributedString:paragraphAttributes:layoutContext:layoutConstraints:(const facebook::react::AttributedString& attributedString, facebook::react::ParagraphAttributes paragraphAttributes, facebook::react::TextLayoutContext layoutContext, facebook::react::LayoutConstraints layoutConstraints);
public virtual facebook::react::TextMeasurement measureNSAttributedString:paragraphAttributes:layoutContext:layoutConstraints:(NSAttributedString* attributedString, facebook::react::ParagraphAttributes paragraphAttributes, facebook::react::TextLayoutContext layoutContext, facebook::react::LayoutConstraints layoutConstraints);
public virtual std::shared_ptr<const facebook::react::EventEmitter> getEventEmitterWithAttributeString:paragraphAttributes:frame:atPoint:(facebook::react::AttributedString attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGRect frame, CGPoint point);
public virtual void drawAttributedString:paragraphAttributes:frame:drawHighlightPath:(facebook::react::AttributedString attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGRect frame, void(^)(UIBezierPath* highlightPath) block);
public virtual void getRectWithAttributedString:paragraphAttributes:enumerateAttribute:frame:usingBlock:(facebook::react::AttributedString attributedString, facebook::react::ParagraphAttributes paragraphAttributes, NSString* enumerateAttribute, CGRect frame, RCTTextLayoutFragmentEnumerationBlock block);
public virtual std::shared_ptr<const facebook::react::EventEmitter> getEventEmitterWithAttributeString:paragraphAttributes:frame:atPoint:(const facebook::react::AttributedString& attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGRect frame, CGPoint point);
public virtual void drawAttributedString:paragraphAttributes:frame:drawHighlightPath:(const facebook::react::AttributedString& attributedString, facebook::react::ParagraphAttributes paragraphAttributes, CGRect frame, void(^)(UIBezierPath* highlightPath) block);
public virtual void getRectWithAttributedString:paragraphAttributes:enumerateAttribute:frame:usingBlock:(const facebook::react::AttributedString& attributedString, facebook::react::ParagraphAttributes paragraphAttributes, NSString* enumerateAttribute, CGRect frame, RCTTextLayoutFragmentEnumerationBlock block);
}

interface RCTTextSelection : public NSObject {
Expand Down Expand Up @@ -3963,6 +3963,7 @@ class facebook::react::AttributedStringBox {
public AttributedStringBox();
public AttributedStringBox(const facebook::react::AttributedString& value);
public AttributedStringBox(const facebook::react::AttributedStringBox& other) = default;
public AttributedStringBox(facebook::react::AttributedString&& value);
public AttributedStringBox(facebook::react::AttributedStringBox&& other) noexcept;
public AttributedStringBox(std::shared_ptr<void> opaquePointer);
public const facebook::react::AttributedString& getValue() const;
Expand Down
1 change: 1 addition & 0 deletions scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,7 @@ class facebook::react::AttributedStringBox {
public AttributedStringBox();
public AttributedStringBox(const facebook::react::AttributedString& value);
public AttributedStringBox(const facebook::react::AttributedStringBox& other) = default;
public AttributedStringBox(facebook::react::AttributedString&& value);
public AttributedStringBox(facebook::react::AttributedStringBox&& other) noexcept;
public AttributedStringBox(std::shared_ptr<void> opaquePointer);
public const facebook::react::AttributedString& getValue() const;
Expand Down
Loading