Skip to content

Commit ab72950

Browse files
rozelefacebook-github-bot
authored andcommitted
Pass LayoutContext to TextLayoutManager (#40873)
Summary: Pull Request resolved: #40873 Some host platforms may require the LayoutContext for computing the size of text, e.g. to read the pointScaleFactor value. This change passes the LayoutContext to TextLayoutManager so it can be used during text measurement. Please note, for now, this does not wire any fields from LayoutContext through to the TextMeasureCache, TextLayoutManager::getHostTextStorage, or TextLayoutManager::measureCachedSpannableById (on Android). ## Changelog: [General] [Internal] Reviewed By: rshest Differential Revision: D50227592 fbshipit-source-id: 37ec16a4828c6cef4a1c1f01d144a86dd29dec29
1 parent 6bf7a01 commit ab72950

File tree

12 files changed

+77
-3
lines changed

12 files changed

+77
-3
lines changed

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace facebook::react {
1414
TextMeasurement ParagraphLayoutManager::measure(
1515
const AttributedString& attributedString,
1616
const ParagraphAttributes& paragraphAttributes,
17+
const TextLayoutContext& layoutContext,
1718
LayoutConstraints layoutConstraints) const {
1819
if (CoreFeatures::cacheLastTextMeasurement) {
1920
bool shouldMeasure = shouldMeasureString(
@@ -23,6 +24,7 @@ TextMeasurement ParagraphLayoutManager::measure(
2324
cachedTextMeasurement_ = textLayoutManager_->measure(
2425
AttributedStringBox(attributedString),
2526
paragraphAttributes,
27+
layoutContext,
2628
layoutConstraints,
2729
hostTextStorage_);
2830
lastAvailableWidth_ = layoutConstraints.maximumSize.width;
@@ -33,6 +35,7 @@ TextMeasurement ParagraphLayoutManager::measure(
3335
return textLayoutManager_->measure(
3436
AttributedStringBox(attributedString),
3537
paragraphAttributes,
38+
layoutContext,
3639
layoutConstraints,
3740
nullptr);
3841
}

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphLayoutManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <react/renderer/attributedstring/AttributedString.h>
1111
#include <react/renderer/attributedstring/ParagraphAttributes.h>
1212
#include <react/renderer/core/LayoutConstraints.h>
13+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1314
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
1415

1516
namespace facebook::react {
@@ -26,6 +27,7 @@ class ParagraphLayoutManager {
2627
TextMeasurement measure(
2728
const AttributedString& attributedString,
2829
const ParagraphAttributes& paragraphAttributes,
30+
const TextLayoutContext& layoutContext,
2931
LayoutConstraints layoutConstraints) const;
3032

3133
LinesMeasurements measureLines(

packages/react-native/ReactCommon/react/renderer/components/text/ParagraphShadowNode.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <react/renderer/core/TraitCast.h>
1717
#include <react/renderer/graphics/rounding.h>
1818
#include <react/renderer/telemetry/TransactionTelemetry.h>
19+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1920
#include <react/utils/CoreFeatures.h>
2021

2122
#include "ParagraphState.h"
@@ -152,9 +153,15 @@ Size ParagraphShadowNode::measureContent(
152153
attributedString.appendFragment({string, textAttributes, {}});
153154
}
154155

156+
TextLayoutContext textLayoutContext{};
157+
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
155158
return getStateData()
156159
.paragraphLayoutManager
157-
.measure(attributedString, content.paragraphAttributes, layoutConstraints)
160+
.measure(
161+
attributedString,
162+
content.paragraphAttributes,
163+
textLayoutContext,
164+
layoutConstraints)
158165
.size;
159166
}
160167

@@ -171,8 +178,13 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) {
171178

172179
updateStateIfNeeded(content);
173180

181+
TextLayoutContext textLayoutContext{};
182+
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
174183
auto measurement = getStateData().paragraphLayoutManager.measure(
175-
content.attributedString, content.paragraphAttributes, layoutConstraints);
184+
content.attributedString,
185+
content.paragraphAttributes,
186+
textLayoutContext,
187+
layoutConstraints);
176188

177189
if (getConcreteProps().onTextLayout) {
178190
auto linesMeasurements = getStateData().paragraphLayoutManager.measureLines(

packages/react-native/ReactCommon/react/renderer/components/textinput/androidtextinput/react/renderer/components/androidtextinput/AndroidTextInputShadowNode.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <react/renderer/core/LayoutConstraints.h>
1717
#include <react/renderer/core/LayoutContext.h>
1818
#include <react/renderer/core/conversions.h>
19+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1920

2021
#include <utility>
2122

@@ -157,7 +158,7 @@ void AndroidTextInputShadowNode::updateStateIfNeeded() {
157158
#pragma mark - LayoutableShadowNode
158159

159160
Size AndroidTextInputShadowNode::measureContent(
160-
const LayoutContext& /*layoutContext*/,
161+
const LayoutContext& layoutContext,
161162
const LayoutConstraints& layoutConstraints) const {
162163
if (getStateData().cachedAttributedStringId != 0) {
163164
return textLayoutManager_
@@ -183,10 +184,13 @@ Size AndroidTextInputShadowNode::measureContent(
183184
return {0, 0};
184185
}
185186

187+
TextLayoutContext textLayoutContext;
188+
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
186189
return textLayoutManager_
187190
->measure(
188191
AttributedStringBox{attributedString},
189192
getConcreteProps().paragraphAttributes,
193+
textLayoutContext,
190194
layoutConstraints,
191195
nullptr)
192196
.size;

packages/react-native/ReactCommon/react/renderer/components/textinput/iostextinput/react/renderer/components/iostextinput/TextInputShadowNode.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <react/renderer/core/LayoutConstraints.h>
1414
#include <react/renderer/core/LayoutContext.h>
1515
#include <react/renderer/core/conversions.h>
16+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1617

1718
namespace facebook::react {
1819

@@ -106,10 +107,13 @@ void TextInputShadowNode::updateStateIfNeeded(
106107
Size TextInputShadowNode::measureContent(
107108
const LayoutContext& layoutContext,
108109
const LayoutConstraints& layoutConstraints) const {
110+
TextLayoutContext textLayoutContext{};
111+
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
109112
return textLayoutManager_
110113
->measure(
111114
attributedStringBoxToMeasure(layoutContext),
112115
getConcreteProps().getEffectiveParagraphAttributes(),
116+
textLayoutContext,
113117
layoutConstraints,
114118
nullptr)
115119
.size;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <react/renderer/graphics/Float.h>
11+
12+
namespace facebook::react {
13+
14+
/*
15+
* TextLayoutContext: Additional contextual information useful for text
16+
* measurement.
17+
*/
18+
struct TextLayoutContext {
19+
/*
20+
* Reflects the scale factor needed to convert from the logical coordinate
21+
* space into the device coordinate space of the physical screen.
22+
* Some layout systems *might* use this to round layout metric values
23+
* to `pixel value`.
24+
*/
25+
Float pointScaleFactor{1.0};
26+
};
27+
28+
inline bool operator==(
29+
TextLayoutContext const& lhs,
30+
TextLayoutContext const& rhs) {
31+
return std::tie(lhs.pointScaleFactor) == std::tie(rhs.pointScaleFactor);
32+
}
33+
34+
inline bool operator!=(
35+
TextLayoutContext const& lhs,
36+
TextLayoutContext const& rhs) {
37+
return !(lhs == rhs);
38+
}
39+
40+
} // namespace facebook::react

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ void* TextLayoutManager::getNativeTextLayoutManager() const {
159159
TextMeasurement TextLayoutManager::measure(
160160
const AttributedStringBox& attributedStringBox,
161161
const ParagraphAttributes& paragraphAttributes,
162+
const TextLayoutContext& layoutContext,
162163
LayoutConstraints layoutConstraints,
163164
std::shared_ptr<void> /* hostTextStorage */) const {
164165
auto& attributedString = attributedStringBox.getValue();

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <react/renderer/attributedstring/AttributedString.h>
1212
#include <react/renderer/attributedstring/AttributedStringBox.h>
1313
#include <react/renderer/core/LayoutConstraints.h>
14+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1415
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
1516
#include <react/utils/ContextContainer.h>
1617

@@ -45,6 +46,7 @@ class TextLayoutManager {
4546
TextMeasurement measure(
4647
const AttributedStringBox& attributedStringBox,
4748
const ParagraphAttributes& paragraphAttributes,
49+
const TextLayoutContext& layoutContext,
4850
LayoutConstraints layoutConstraints,
4951
std::shared_ptr<void> /* hostTextStorage */) const;
5052

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void* TextLayoutManager::getNativeTextLayoutManager() const {
1616
TextMeasurement TextLayoutManager::measure(
1717
AttributedStringBox attributedStringBox,
1818
ParagraphAttributes paragraphAttributes,
19+
const TextLayoutContext& /*layoutContext*/,
1920
LayoutConstraints layoutConstraints,
2021
std::shared_ptr<void>) const {
2122
TextMeasurement::Attachments attachments;

packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/cxx/TextLayoutManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <react/renderer/attributedstring/AttributedStringBox.h>
1414
#include <react/renderer/attributedstring/ParagraphAttributes.h>
1515
#include <react/renderer/core/LayoutConstraints.h>
16+
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
1617
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
1718
#include <react/utils/ContextContainer.h>
1819

@@ -37,6 +38,7 @@ class TextLayoutManager {
3738
virtual TextMeasurement measure(
3839
AttributedStringBox attributedStringBox,
3940
ParagraphAttributes paragraphAttributes,
41+
const TextLayoutContext& layoutContext,
4042
LayoutConstraints layoutConstraints,
4143
std::shared_ptr<void>) const;
4244

0 commit comments

Comments
 (0)