From 99119a210487af18983145fd374ff7ebc88931f3 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Thu, 8 May 2025 16:18:01 -0700 Subject: [PATCH] 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 --- .../ReactAndroid/api/ReactAndroid.api | 2 -- .../facebook/react/fabric/FabricUIManager.java | 9 +++++++-- .../react/views/text/TextLayoutManager.java | 17 ++++++++++++----- .../textlayoutmanager/TextLayoutManager.cpp | 11 +++++++++-- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 4ef5b7eb5b9..210858c3656 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -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 diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index cd26afb1907..7ed7b71223f 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -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, diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java index e42d79c248d..d367b0b66bd 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/text/TextLayoutManager.java @@ -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); } diff --git a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp index 84764829e8c..48b28866fbd 100644 --- a/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/textlayoutmanager/platform/android/react/renderer/textlayoutmanager/TextLayoutManager.cpp @@ -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(