Skip to content

Commit

Permalink
Pass LayoutContext to TextLayoutManager (#40873)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rozele authored and facebook-github-bot committed Oct 16, 2023
1 parent 6bf7a01 commit ab72950
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -23,6 +24,7 @@ TextMeasurement ParagraphLayoutManager::measure(
cachedTextMeasurement_ = textLayoutManager_->measure(
AttributedStringBox(attributedString),
paragraphAttributes,
layoutContext,
layoutConstraints,
hostTextStorage_);
lastAvailableWidth_ = layoutConstraints.maximumSize.width;
Expand All @@ -33,6 +35,7 @@ TextMeasurement ParagraphLayoutManager::measure(
return textLayoutManager_->measure(
AttributedStringBox(attributedString),
paragraphAttributes,
layoutContext,
layoutConstraints,
nullptr);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -26,6 +27,7 @@ class ParagraphLayoutManager {
TextMeasurement measure(
const AttributedString& attributedString,
const ParagraphAttributes& paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints) const;

LinesMeasurements measureLines(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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_
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -37,6 +38,7 @@ class TextLayoutManager {
virtual TextMeasurement measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void>) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -32,6 +33,7 @@ class TextLayoutManager {
TextMeasurement measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext& layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> hostTextStorage) const;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
TextMeasurement TextLayoutManager::measure(
AttributedStringBox attributedStringBox,
ParagraphAttributes paragraphAttributes,
const TextLayoutContext &layoutContext,
LayoutConstraints layoutConstraints,
std::shared_ptr<void> hostTextStorage) const
{
Expand Down

0 comments on commit ab72950

Please sign in to comment.