diff --git a/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm b/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm new file mode 100644 index 000000000000..7cfde60bd51f --- /dev/null +++ b/packages/react-native/React/Tests/Text/RCTTextMeasureCacheTests.mm @@ -0,0 +1,119 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +#import + +#include +#include +#include +#include +#include +#include +#include + +#include + +using namespace facebook::react; + +@interface RCTTextMeasureCacheTests : XCTestCase + +@end + +@implementation RCTTextMeasureCacheTests { + std::unique_ptr _textLayoutManager; + std::unique_ptr _telemetry; +} + +- (void)setUp +{ + [super setUp]; + _textLayoutManager = std::make_unique(std::make_shared()); + _telemetry = std::make_unique(); + _telemetry->setAsThreadLocal(); +} + +- (void)tearDown +{ + _telemetry->unsetAsThreadLocal(); + _telemetry.reset(); + _textLayoutManager.reset(); + [super tearDown]; +} + +static AttributedStringBox AttributedStringBoxWithText(std::string text) +{ + auto attributedString = AttributedString{}; + attributedString.appendFragment( + {.string = std::move(text), .textAttributes = TextAttributes::defaultTextAttributes()}); + return AttributedStringBox{attributedString}; +} + +static LayoutConstraints ConstraintsWithMaximumWidth(Float maximumWidth) +{ + auto constraints = LayoutConstraints{}; + constraints.maximumSize.width = maximumWidth; + return constraints; +} + +- (TextMeasurement)measure:(const AttributedStringBox &)attributedStringBox + constraints:(const LayoutConstraints &)constraints +{ + return _textLayoutManager->measure(attributedStringBox, ParagraphAttributes{}, TextLayoutContext{}, constraints); +} + +- (void)testConstraintsThatDifferOnlyInMinimumSizeShareAMeasurement +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + auto constraints = ConstraintsWithMaximumWidth(200); + auto measurement = [self measure:text constraints:constraints]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + + auto withMinimumSize = constraints; + withMinimumSize.minimumSize = facebook::react::Size{.width = 150, .height = 60}; + auto measurementWithMinimumSize = [self measure:text constraints:withMinimumSize]; + + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + // The shared measurement is still clamped to the caller's constraints. + XCTAssertLessThan(measurement.size.width, 150); + XCTAssertEqual(measurementWithMinimumSize.size.width, 150); + XCTAssertEqual(measurementWithMinimumSize.size.height, 60); +} + +- (void)testConstraintsThatDifferOnlyInMaximumHeightShareAMeasurement +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + auto constraints = ConstraintsWithMaximumWidth(200); + auto measurement = [self measure:text constraints:constraints]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + XCTAssertGreaterThan(measurement.size.height, 5); + + auto withMaximumHeight = constraints; + withMaximumHeight.maximumSize.height = 5; + auto measurementWithMaximumHeight = [self measure:text constraints:withMaximumHeight]; + + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + // The shared measurement is still clamped to the caller's constraints. + XCTAssertEqual(measurementWithMaximumHeight.size.height, 5); + XCTAssertEqual(measurementWithMaximumHeight.size.width, measurement.size.width); +} + +- (void)testConstraintsWithADifferentMaximumWidthMeasureAgain +{ + auto text = AttributedStringBoxWithText("Hello, world"); + + [self measure:text constraints:ConstraintsWithMaximumWidth(200)]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 1); + + [self measure:text constraints:ConstraintsWithMaximumWidth(30)]; + XCTAssertEqual(_telemetry->getNumberOfTextMeasurements(), 2); +} + +@end diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm index 83e63854cd14..56990e739dcd 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTTextLayoutManager.mm @@ -66,6 +66,8 @@ - (TextMeasurement)measureNSAttributedString:(NSAttributedString *)attributedStr return {}; } + // Only the maximum width is read here. TextLayoutManager::measure relies on that: its cache key leaves out the + // minimum size and the maximum height. CGSize maximumSize = CGSize{layoutConstraints.maximumSize.width, CGFLOAT_MAX}; NSTextStorage *textStorage = [self _textStorageAndLayoutManagerWithAttributesString:attributedString paragraphAttributes:paragraphAttributes diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm index 0f2fbe82b8f5..5a326011bd31 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/TextLayoutManager.mm @@ -8,6 +8,7 @@ #import "TextLayoutManager.h" #import "RCTTextLayoutManager.h" +#import #import #import @@ -53,10 +54,17 @@ const auto &attributedString = placeholderAttributedString ? *placeholderAttributedString : originalAttributedString; + // The native measurement depends only on the maximum width, and the result is clamped to the + // full constraints below, outside the cache. So the minimum size and the maximum height are left + // out of the key, and the measurement gets the key's constraints, so that every caller sharing + // an entry gets the same result. + LayoutConstraints keyConstraints = layoutConstraints; + keyConstraints.minimumSize = Size{}; + keyConstraints.maximumSize.height = std::numeric_limits::infinity(); measurement = textMeasureCache_.get( {.attributedString = attributedString, .paragraphAttributes = paragraphAttributes, - .layoutConstraints = layoutConstraints, + .layoutConstraints = keyConstraints, .pointScaleFactor = layoutContext.pointScaleFactor}, [&]() { auto telemetry = TransactionTelemetry::threadLocalTelemetry(); @@ -67,7 +75,7 @@ auto measurement = [textLayoutManager measureAttributedString:attributedString paragraphAttributes:paragraphAttributes layoutContext:layoutContext - layoutConstraints:layoutConstraints]; + layoutConstraints:keyConstraints]; if (telemetry) { telemetry->didMeasureText();