Pass LayoutContext to TextLayoutManager (#40873)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/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
This commit is contained in:
Eric Rozell
2023-10-16 09:42:33 -07:00
committed by Facebook GitHub Bot
parent 6bf7a01a4d
commit ab72950715
12 changed files with 77 additions and 3 deletions
@@ -14,6 +14,7 @@ namespace facebook::react {
TextMeasurement ParagraphLayoutManager::measure(
const AttributedString& attributedString,
const ParagraphAttributes& paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints) const {
if (CoreFeatures::cacheLastTextMeasurement) {
bool shouldMeasure = shouldMeasureString(
@@ -23,6 +24,7 @@ TextMeasurement ParagraphLayoutManager::measure(
cachedTextMeasurement_ = textLayoutManager_->measure(
AttributedStringBox(attributedString),
paragraphAttributes,
layoutContext,
layoutConstraints,
hostTextStorage_);
lastAvailableWidth_ = layoutConstraints.maximumSize.width;
@@ -33,6 +35,7 @@ TextMeasurement ParagraphLayoutManager::measure(
return textLayoutManager_->measure(
AttributedStringBox(attributedString),
paragraphAttributes,
layoutContext,
layoutConstraints,
nullptr);
}
@@ -10,6 +10,7 @@
#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextLayoutManager.h>
namespace facebook::react {
@@ -26,6 +27,7 @@ class ParagraphLayoutManager {
TextMeasurement measure(
const AttributedString& attributedString,
const ParagraphAttributes& paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints) const;
LinesMeasurements measureLines(
@@ -16,6 +16,7 @@
#include <react/renderer/core/TraitCast.h>
#include <react/renderer/graphics/rounding.h>
#include <react/renderer/telemetry/TransactionTelemetry.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/utils/CoreFeatures.h>
#include "ParagraphState.h"
@@ -152,9 +153,15 @@ Size ParagraphShadowNode::measureContent(
attributedString.appendFragment({string, textAttributes, {}});
}
TextLayoutContext textLayoutContext{};
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
return getStateData()
.paragraphLayoutManager
.measure(attributedString, content.paragraphAttributes, layoutConstraints)
.measure(
attributedString,
content.paragraphAttributes,
textLayoutContext,
layoutConstraints)
.size;
}
@@ -171,8 +178,13 @@ void ParagraphShadowNode::layout(LayoutContext layoutContext) {
updateStateIfNeeded(content);
TextLayoutContext textLayoutContext{};
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
auto measurement = getStateData().paragraphLayoutManager.measure(
content.attributedString, content.paragraphAttributes, layoutConstraints);
content.attributedString,
content.paragraphAttributes,
textLayoutContext,
layoutConstraints);
if (getConcreteProps().onTextLayout) {
auto linesMeasurements = getStateData().paragraphLayoutManager.measureLines(
@@ -16,6 +16,7 @@
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/core/LayoutContext.h>
#include <react/renderer/core/conversions.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <utility>
@@ -157,7 +158,7 @@ void AndroidTextInputShadowNode::updateStateIfNeeded() {
#pragma mark - LayoutableShadowNode
Size AndroidTextInputShadowNode::measureContent(
const LayoutContext& /*layoutContext*/,
const LayoutContext& layoutContext,
const LayoutConstraints& layoutConstraints) const {
if (getStateData().cachedAttributedStringId != 0) {
return textLayoutManager_
@@ -183,10 +184,13 @@ Size AndroidTextInputShadowNode::measureContent(
return {0, 0};
}
TextLayoutContext textLayoutContext;
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
return textLayoutManager_
->measure(
AttributedStringBox{attributedString},
getConcreteProps().paragraphAttributes,
textLayoutContext,
layoutConstraints,
nullptr)
.size;
@@ -13,6 +13,7 @@
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/core/LayoutContext.h>
#include <react/renderer/core/conversions.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
namespace facebook::react {
@@ -106,10 +107,13 @@ void TextInputShadowNode::updateStateIfNeeded(
Size TextInputShadowNode::measureContent(
const LayoutContext& layoutContext,
const LayoutConstraints& layoutConstraints) const {
TextLayoutContext textLayoutContext{};
textLayoutContext.pointScaleFactor = layoutContext.pointScaleFactor;
return textLayoutManager_
->measure(
attributedStringBoxToMeasure(layoutContext),
getConcreteProps().getEffectiveParagraphAttributes(),
textLayoutContext,
layoutConstraints,
nullptr)
.size;
@@ -0,0 +1,40 @@
/*
* 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.
*/
#pragma once
#include <react/renderer/graphics/Float.h>
namespace facebook::react {
/*
* TextLayoutContext: Additional contextual information useful for text
* measurement.
*/
struct TextLayoutContext {
/*
* Reflects the scale factor needed to convert from the logical coordinate
* space into the device coordinate space of the physical screen.
* Some layout systems *might* use this to round layout metric values
* to `pixel value`.
*/
Float pointScaleFactor{1.0};
};
inline bool operator==(
TextLayoutContext const& lhs,
TextLayoutContext const& rhs) {
return std::tie(lhs.pointScaleFactor) == std::tie(rhs.pointScaleFactor);
}
inline bool operator!=(
TextLayoutContext const& lhs,
TextLayoutContext const& rhs) {
return !(lhs == rhs);
}
} // namespace facebook::react
@@ -159,6 +159,7 @@ void* TextLayoutManager::getNativeTextLayoutManager() const {
TextMeasurement TextLayoutManager::measure(
const AttributedStringBox& attributedStringBox,
const ParagraphAttributes& paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> /* hostTextStorage */) const {
auto& attributedString = attributedStringBox.getValue();
@@ -11,6 +11,7 @@
#include <react/renderer/attributedstring/AttributedString.h>
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
@@ -45,6 +46,7 @@ class TextLayoutManager {
TextMeasurement measure(
const AttributedStringBox& attributedStringBox,
const ParagraphAttributes& paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> /* hostTextStorage */) const;
@@ -16,6 +16,7 @@ void* TextLayoutManager::getNativeTextLayoutManager() const {
TextMeasurement TextLayoutManager::measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext& /*layoutContext*/,
LayoutConstraints layoutConstraints,
std::shared_ptr<void>) const {
TextMeasurement::Attachments attachments;
@@ -13,6 +13,7 @@
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
@@ -37,6 +38,7 @@ class TextLayoutManager {
virtual TextMeasurement measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void>) const;
@@ -12,6 +12,7 @@
#include <react/renderer/attributedstring/AttributedStringBox.h>
#include <react/renderer/attributedstring/ParagraphAttributes.h>
#include <react/renderer/core/LayoutConstraints.h>
#include <react/renderer/textlayoutmanager/TextLayoutContext.h>
#include <react/renderer/textlayoutmanager/TextMeasureCache.h>
#include <react/utils/ContextContainer.h>
@@ -32,6 +33,7 @@ class TextLayoutManager {
TextMeasurement measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> hostTextStorage) const;
@@ -41,6 +41,7 @@ std::shared_ptr<void> TextLayoutManager::getHostTextStorage(
TextMeasurement TextLayoutManager::measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext &layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> hostTextStorage) const
{