Fix TextLayoutManager MeasureMode Regression (#51183)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51183

D58818560 tried to deduplicate some code, but introduced an error, where we no longer correctly incorporate the width MeasureMode into the text layout that we create, instead, passing `YogaMeasureMode.EXACTLY`.

In effect, this means the Android layout created always takes up the maximum allowable space, even if content is smaller. This is later masked, because our returned measure when `AT_MOST` is based on maximum line length, and the layout is then recreated when drawing a TextView, but means:

1. Attachments may not be positioned correctly, when using a non-left-aligned paragraph alignment
2. Directly drawing the layout shows the wrong thing

Changelog:
[Android][Fixed] - Fix TextLayoutManager MeasureMode Regression

Reviewed By: rshest

Differential Revision: D74366936

fbshipit-source-id: 3eda8c716ba9790a61c2da19023e140afbb6971d
This commit is contained in:
Nick Gerleman authored and Facebook GitHub Bot committed 2025-05-08 16:18:01 -07:00
1 parent 1c52eec2c6
commit 99119a2104
4 files changed
+28 -11

No files matched your search

@@ -2357,14 +2357,12 @@ public class com/facebook/react/fabric/FabricUIManager : com/facebook/react/brid
public fun initialize ()V
public fun invalidate ()V
public fun markActiveTouchForTag (II)V
public fun measurePreparedLayout (Lcom/facebook/react/views/text/PreparedLayout;FFFF)[F
public fun onAllAnimationsComplete ()V
public fun onAnimationStarted ()V
public fun onHostDestroy ()V
public fun onHostPause ()V
public fun onHostResume ()V
public fun onRequestEventBeat ()V
public fun prepareLayout (ILcom/facebook/react/common/mapbuffer/ReadableMapBuffer;Lcom/facebook/react/common/mapbuffer/ReadableMapBuffer;FF)Lcom/facebook/react/views/text/PreparedLayout;
public fun prependUIBlock (Lcom/facebook/react/fabric/interop/UIBlock;)V
public fun profileNextBatch ()V
public fun receiveEvent (IILjava/lang/String;Lcom/facebook/react/bridge/WritableMap;)V
@@ -654,11 +654,14 @@ public class FabricUIManager
@AnyThread
@ThreadConfined(ANY)
@UnstableReactNativeAPI
public PreparedLayout prepareLayout(
int surfaceId,
ReadableMapBuffer attributedString,
ReadableMapBuffer paragraphAttributes,
float minWidth,
float maxWidth,
float minHeight,
float maxHeight) {
SurfaceMountingManager surfaceMountingManager =
mMountingManager.getSurfaceManagerEnforced(surfaceId, "prepareLayout");
@@ -667,8 +670,9 @@ public class FabricUIManager
Preconditions.checkNotNull(surfaceMountingManager.getContext()),
attributedString,
paragraphAttributes,
PixelUtil.toPixelFromDIP(maxWidth),
PixelUtil.toPixelFromDIP(maxHeight),
getYogaSize(minWidth, maxWidth),
getYogaMeasureMode(minWidth, maxWidth),
getYogaSize(minHeight, maxHeight),
null /* T219881133: Migrate away from ReactTextViewManagerCallback */);
int maximumNumberOfLines =
@@ -681,6 +685,7 @@ public class FabricUIManager
@AnyThread
@ThreadConfined(ANY)
@UnstableReactNativeAPI
public float[] measurePreparedLayout(
PreparedLayout preparedLayout,
float minWidth,
@@ -52,7 +52,6 @@ import com.facebook.react.views.text.internal.span.ReactUnderlineSpan;
import com.facebook.react.views.text.internal.span.SetSpanOperation;
import com.facebook.react.views.text.internal.span.ShadowStyleSpan;
import com.facebook.react.views.text.internal.span.TextInlineViewPlaceholderSpan;
import com.facebook.yoga.YogaConstants;
import com.facebook.yoga.YogaMeasureMode;
import com.facebook.yoga.YogaMeasureOutput;
import java.util.ArrayList;
@@ -390,8 +389,7 @@ public class TextLayoutManager {
boolean isScriptRTL = TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(text, 0, spanLength);
if (boring == null
&& (unconstrainedWidth
|| (!YogaConstants.isUndefined(desiredWidth) && desiredWidth <= width))) {
&& (unconstrainedWidth || (!Float.isNaN(desiredWidth) && desiredWidth <= width))) {
// Is used when the width is not known and the text is not boring, ie. if it contains
// unicode characters.
@@ -510,6 +508,7 @@ public class TextLayoutManager {
MapBuffer attributedString,
MapBuffer paragraphAttributes,
float width,
YogaMeasureMode widthYogaMeasureMode,
float height,
@Nullable ReactTextViewManagerCallback reactTextViewManagerCallback) {
Spannable text =
@@ -582,7 +581,7 @@ public class TextLayoutManager {
text,
boring,
width,
YogaMeasureMode.EXACTLY,
widthYogaMeasureMode,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
@@ -696,6 +695,7 @@ public class TextLayoutManager {
attributedString,
paragraphAttributes,
width,
widthYogaMeasureMode,
height,
reactTextViewManagerCallback);
@@ -961,7 +961,14 @@ public class TextLayoutManager {
float width,
float height) {
Layout layout =
createLayout(context, attributedString, paragraphAttributes, width, height, null);
createLayout(
context,
attributedString,
paragraphAttributes,
width,
YogaMeasureMode.EXACTLY,
height,
null);
return FontMetricsUtil.getFontMetrics(
layout.getText(), layout, Preconditions.checkNotNull(sTextPaintInstance.get()), context);
}
@@ -308,6 +308,8 @@ TextLayoutManager::PreparedLayout TextLayoutManager::prepareLayout(
JReadableMapBuffer::javaobject,
JReadableMapBuffer::javaobject,
jfloat,
jfloat,
jfloat,
jfloat)>("prepareLayout");
auto attributedStringMB =
@@ -315,6 +317,9 @@ TextLayoutManager::PreparedLayout TextLayoutManager::prepareLayout(
auto paragraphAttributesMB =
JReadableMapBuffer::createWithContents(toMapBuffer(paragraphAttributes));
auto minimumSize = layoutConstraints.minimumSize;
auto maximumSize = layoutConstraints.maximumSize;
// T222682416: We don't have any global cache here. We should investigate
// whether that is desirable
return {jni::make_global(prepareLayout(
@@ -322,8 +327,10 @@ TextLayoutManager::PreparedLayout TextLayoutManager::prepareLayout(
layoutContext.surfaceId,
attributedStringMB.get(),
paragraphAttributesMB.get(),
layoutConstraints.maximumSize.width,
layoutConstraints.maximumSize.height))};
minimumSize.width,
maximumSize.width,
minimumSize.height,
maximumSize.height))};
}
TextMeasurement TextLayoutManager::measurePreparedLayout(