Status Update
Comments
gr...@google.com <gr...@google.com>
gr...@google.com <gr...@google.com> #2
Some notes from digging into this a bit:
- unexpected wrapping doesn't start until a TextView that line wraps runs it's line breaking logic.
- style doesn't seem to matter, so long as the line breaker is ran.
- Moving the
TextView
below the compose view so that it runs first makes the first draw not wrap in the compose text. Subsequent re-measures will start wrapping. - This may require a
StaticLayout
to run inTextView
beforeStaticLayout
runs inText
(BoringLayout
doesn't seem to cause this, but that doesn't have line breaking by definition), but I'm not certain.
- Inputs to
LineBreaker.computeLineBreaks
seem to have consistent arguments for repro and non-repro use cases. - Couldn't run in demo app, so the layouts/views may also be necessary to repro.
- Used a API 35 Pixel 9 Pro XL emulator to repro. Verified that using API 34 does not repro.
- Moving the
TextView
from the layout to anAndroidView
in ourComposeView
still repros.
gr...@google.com <gr...@google.com> #3
Able to repro from a blank project with only activity-compose, compose foundation, and the font files.
- Create new blank compose project. (should be target api 35 already)
- Replace
dependencies
inapp/build.gradle.kts
with the below and sync the dependencies. - Delete the
ui
source dirs (all the material related stuff). - Copy the font files from the
reprod.zip
in the description of this bug into the new project. - Replace the
MainActivity
file with the below code. - Run the app on a Pixel 9 Pro XL - API 35 emulator.
dependencies {
implementation("androidx.activity:activity-compose:1.10.0")
implementation("androidx.compose.foundation:foundation:1.7.6")
}
import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeContentPadding
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.Font
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.viewinterop.AndroidView
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent { Content() }
}
}
private val ReproFontFamily =
FontFamily(
Font(R.font.noto_ikea_latin_regular, FontWeight.Normal),
Font(R.font.noto_ikea_latin_bold, FontWeight.Bold)
)
@Composable
private fun Content() {
Column(
modifier = Modifier
.safeContentPadding()
.fillMaxWidth()
.padding(32.dp)
) {
AndroidView(factory = { ctx -> TextView(ctx).apply { text = "Line1\nLine2" } })
BasicText(
text = "ALEX",
style = TextStyle(
fontWeight = FontWeight.Bold,
color = Color.Black,
fontFamily = ReproFontFamily,
fontSize = 14.sp,
lineHeight = 22.sp,
),
modifier = Modifier.background(Color.Magenta),
)
}
}
gr...@google.com <gr...@google.com> #4
This actually does work in the demo app, I just forgot to change the target api. See
gr...@google.com <gr...@google.com> #5
Maybe related, but not convinced:
gr...@google.com <gr...@google.com> #6
Nona, can you take a look?
pa...@partners.mbank.pl <pa...@partners.mbank.pl> #7
fontSize = 12.sp
fontWeight = SemiBold
letterSpacing = 0
lineHeights = 16.sp
pa...@partners.mbank.pl <pa...@partners.mbank.pl> #8
gr...@google.com <gr...@google.com> #9
I don't recommend this, but if you really need a workaround right now, here it is. This is probably quite brittle, slow, and I have not tested it other than checking that it does fix the bug. Use at your own risk. The actual fix for this is expected in 1.8.0-beta02
.
A very hacky workaround would be adding this modifier to the end of your Text
's modifier chain:
/** Intercept pre-layout to manually recycle `StaticLayout.Builder.useBoundsForWidth`. */
private fun Modifier.unexpectedTextWrappingWorkaround(): Modifier =
layout { measurable, constraints ->
if (Build.VERSION.SDK_INT >= 35) {
Api35Helper.resetStaticLayoutBuilderUseBoundsForWidth()
}
val placeable = measurable.measure(constraints)
layout(placeable.width, placeable.height) { placeable.place(0, 0) }
}
@RequiresApi(35)
private object Api35Helper {
@JvmStatic
fun resetStaticLayoutBuilderUseBoundsForWidth() {
StaticLayout.Builder.obtain("a", 0, 1, TextPaint(), 1024)
.setUseBoundsForWidth(false)
.build() // recycles the StaticLayout.
}
}
ap...@google.com <ap...@google.com> #10
Project: platform/frameworks/support
Branch: androidx-main
Author: Seigo Nonaka <
Link:
Fix unexpected enabling of useBoundsForWidth
Expand for full commit details
Fix unexpected enabling of useBoundsForWidth
This fixes a bug where a text may wrap to a second line where it is unnecessary.
Bug: 391378120
Test: Manual tests to ensure the unexpected wrapping stops after the fix is applied
Test: StaticLayoutFactoryTest#create_useBoundsForWidth_disabled
Test: BasicTextUnexpectedWrappingRegressionTest
Change-Id: I1b40c5816f2b4c1e787de05d5332db6fc0efad14
Files:
- A
compose/foundation/foundation/src/androidInstrumentedTest/assets/font/overshoot_test.ttx
- A
compose/foundation/foundation/src/androidInstrumentedTest/kotlin/androidx/compose/foundation/text/BasicTextUnexpectedWrappingRegressionTest.kt
- A
compose/foundation/foundation/src/androidInstrumentedTest/res/font/overshoot_test.ttf
- M
compose/ui/ui-text/src/androidInstrumentedTest/kotlin/androidx/compose/ui/text/android/StaticLayoutFactoryTest.kt
- M
compose/ui/ui-text/src/androidMain/kotlin/androidx/compose/ui/text/android/StaticLayoutFactory.android.kt
Hash: 7d19be04e9c2af237f0605a148605bf52f607497
Date: Thu Jan 23 17:40:54 2025
pa...@partners.mbank.pl <pa...@partners.mbank.pl> #11
gr...@google.com <gr...@google.com> #12
This will be targeting 1.8
, likely landing in beta02
.
1.7.*
targets android API 34, and this fix requires API 35. Upgrading 1.7
to android api 35 is too large of a change for a 1.7.x
version.
gr...@google.com <gr...@google.com> #13
Separate note, an upstream fix in the android platform is expected in api level 36, so this issue should only occur when API level is 35 and compose version is below 1.8.
er...@gmail.com <er...@gmail.com> #14
Saw that beta02
is out but there is no mention of this issue being fixed in the release notes. Were you able to land the fix in beta02 or it got pushed to a future version?
Description
Jetpack Compose version: BOM 2024.12.01
Jetpack Compose component(s) used: Text in Material3
Android Studio Build: Meerkat Canary 9
Kotlin version: 2.1.0
Problem:
Since bumping target SDK to 35 we notice that some strings on some devices receive unintended line breaks even when the Text Composable is allowed to be as wide as it wants. Trying to force single line does not work either as setting
maxLines=1
clip the string rather than join to one line.Examples of problematic strings:
Steps to Reproduce or Code Sample to Reproduce:
Code: Attached minimal reproduction repo including our custom font
Device: Reproduced on Pixel 9 Pro XL real device and emulator using the font/display settings in attached screenshot. Also reported on but not verified: Pixel 8 Pro, Pixel Tablet
Summary of the reprod sample: Activity with the following XML:
...and the following sample code:
This will produce the result in attached screenshots.
Some findings from debugging the issue: