Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<dffc5db0f80501f6246f9d6087c6fb4a>>
* @generated SignedSource<<8531ce29d0e5362517d35559ebda623b>>
*/

/**
Expand All @@ -27,5 +27,7 @@ public open class ReactNativeFeatureFlagsOverrides_RNOSS_Experimental_Android :

override fun enableSwiftUIBasedFilters(): Boolean = true

override fun fixTextClippingAndroid15useBoundsForWidth(): Boolean = true

override fun preventShadowTreeCommitExhaustion(): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -620,15 +620,89 @@ internal object TextLayoutManager {
)
}

val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()
// Pre-Android 15: Use existing advance-based logic
if (
Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM ||
!ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
) {
val desiredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt()

val layoutWidth =
when (widthYogaMeasureMode) {
YogaMeasureMode.EXACTLY -> floor(width).toInt()
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
else -> desiredWidth
}
return buildLayout(
text,
layoutWidth,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
ellipsizeMode,
maxNumberOfLines,
paint,
)
}

// Android 15+: Need to account for visual bounds
// Step 1: Create unconstrained layout to get visual bounds width
val unconstrainedLayout =
buildLayout(
text,
Int.MAX_VALUE / 2,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
null,
ReactConstants.UNSET,
paint,
)

// Calculate visual bounds width from unconstrained layout
var desiredVisualWidth = 0f
for (i in 0 until unconstrainedLayout.lineCount) {
val lineWidth = unconstrainedLayout.getLineRight(i) - unconstrainedLayout.getLineLeft(i)
desiredVisualWidth = max(desiredVisualWidth, lineWidth)
}

val layoutWidth =
when (widthYogaMeasureMode) {
YogaMeasureMode.EXACTLY -> floor(width).toInt()
YogaMeasureMode.AT_MOST -> min(desiredWidth, floor(width).toInt())
else -> desiredWidth
YogaMeasureMode.AT_MOST -> min(ceil(desiredVisualWidth).toInt(), floor(width).toInt())
else -> ceil(desiredVisualWidth).toInt()
}

// Step 2: Create final layout with correct width
return buildLayout(
text,
layoutWidth,
includeFontPadding,
textBreakStrategy,
hyphenationFrequency,
alignment,
justificationMode,
ellipsizeMode,
maxNumberOfLines,
paint,
)
}

private fun buildLayout(
text: Spannable,
layoutWidth: Int,
includeFontPadding: Boolean,
textBreakStrategy: Int,
hyphenationFrequency: Int,
alignment: Layout.Alignment,
justificationMode: Int,
ellipsizeMode: TextUtils.TruncateAt?,
maxNumberOfLines: Int,
paint: TextPaint,
): Layout {
val builder =
StaticLayout.Builder.obtain(text, 0, text.length, paint, layoutWidth)
.setAlignment(alignment)
Expand All @@ -649,6 +723,13 @@ internal object TextLayoutManager {
builder.setUseLineSpacingFromFallbacks(true)
}

if (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM &&
ReactNativeFeatureFlags.fixTextClippingAndroid15useBoundsForWidth()
) {
builder.setUseBoundsForWidth(true)
}

return builder.build()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<c5b1dc86ec11d40e5d26ae8a6f0ee6ff>>
* @generated SignedSource<<6a047fa1d33ea17ebd7ba8d0680ee1cc>>
*/

/**
Expand Down Expand Up @@ -35,6 +35,10 @@ class ReactNativeFeatureFlagsOverridesOSSExperimental : public ReactNativeFeatur
return true;
}

bool fixTextClippingAndroid15useBoundsForWidth() override {
return true;
}

bool preventShadowTreeCommitExhaustion() override {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ const definitions: FeatureFlagDefinitions = {
expectedReleaseValue: true,
purpose: 'experimentation',
},
ossReleaseStage: 'none',
ossReleaseStage: 'experimental',
},
fuseboxAssertSingleHostState: {
defaultValue: true,
Expand Down
4 changes: 4 additions & 0 deletions packages/rn-tester/js/examples/Text/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,10 @@ const examples = [
<RNTesterText style={{fontStyle: 'normal'}}>
Move fast and be normal
</RNTesterText>
<RNTesterText style={{fontStyle: 'italic'}}>
Move fast and be italic, but just be longer so that you don't fit on
a single line and make sure text is not truncated.
</RNTesterText>
</>
);
},
Expand Down
Loading