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 @@ -69,6 +69,7 @@ import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.max
import kotlin.math.min
import kotlin.math.round
import org.json.JSONArray
import org.json.JSONObject

Expand Down Expand Up @@ -1538,9 +1539,15 @@ internal object TextLayoutManager {
}
currentFontSize = max(currentFontSize, minimumFontSize)

// Every font size is scaled from its original size, so that rounding does not compound across
// iterations. Text that fits keeps its original sizes. Shrunk text scales every size by one
// ratio and clamps it to the minimum, matching scaleFontSizeWithRatio on iOS.
val originalFontSize = currentFontSize
val originalSpanSizes = IntArray(spans.size) { spans[it].size }
val originalPaintTextSize = paint.textSize

var intervalStart = minimumFontSize
var intervalEnd = currentFontSize
var previousFontSize = currentFontSize

// `true` instead of `intervalStart != intervalEnd` so that the last iteration where both are at
// the same size goes through and updates all relevant objects with the final font size
Expand All @@ -1551,18 +1558,27 @@ internal object TextLayoutManager {
// we always end up doing two measurements to check whether intervalEnd would fit.
val currentFontSize = (intervalStart + intervalEnd + 1) / 2

val ratio = currentFontSize.toFloat() / previousFontSize.toFloat()
paint.textSize = max((paint.textSize * ratio).toInt(), minimumFontSize).toFloat()

val sizeSpans = text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java)
for (span in sizeSpans) {
val isShrunk = currentFontSize < originalFontSize
val ratio = currentFontSize.toFloat() / originalFontSize.toFloat()
paint.textSize =
if (isShrunk) max(round(originalPaintTextSize * ratio), minimumFontSize.toFloat())
else originalPaintTextSize

for (i in spans.indices) {
val span = spans[i]
val resizedSpan =
ReactAbsoluteSizeSpan(
if (isShrunk) max(round(originalSpanSizes[i] * ratio).toInt(), minimumFontSize)
else originalSpanSizes[i]
)
text.setSpan(
ReactAbsoluteSizeSpan(max((span.size * ratio).toInt(), minimumFontSize)),
resizedSpan,
text.getSpanStart(span),
text.getSpanEnd(span),
text.getSpanFlags(span),
)
text.removeSpan(span)
spans[i] = resizedSpan
}
if (boring != null) {
boring = isBoring(text, paint)
Expand Down Expand Up @@ -1611,8 +1627,6 @@ internal object TextLayoutManager {
// Text fits the constraints
intervalStart = currentFontSize
}

previousFontSize = currentFontSize
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.graphics.text.LineBreakConfig
import android.text.Layout
import android.text.SpannableString
import android.text.Spanned
import android.text.StaticLayout
import android.text.TextPaint
import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.DisplayMetricsHolder
Expand All @@ -25,6 +26,7 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.GraphicsMode

@RunWith(RobolectricTestRunner::class)
@SuppressLint("InlinedApi")
Expand Down Expand Up @@ -136,6 +138,60 @@ class TextLayoutManagerMinimumFontScaleTest {
assertThat(largestFontSize(text)).isEqualTo(LARGE_FONT_SIZE)
}

@Test
fun `text that already fits keeps its font size at every size`() {
val shrunkFontSizes =
(8..120).filter { fontSize ->
val text = spannableWithFontSize(fontSize)
adjustToFit(text, height = 10_000f, minimumFontScale = Float.NaN)
largestFontSize(text) != fontSize
}

assertThat(shrunkFontSizes).isEmpty()
}

@Test
fun `smaller font sizes are kept when the text already fits`() {
val text = spannableWithSmallAndLargeFontSizes()

adjustToFit(text, height = 10_000f, minimumFontScale = Float.NaN)

assertThat(fontSizes(text)).containsExactly(LARGE_FONT_SIZE, SMALL_FONT_SIZE)
}

@Test
fun `minimumFontScale does not grow smaller font sizes`() {
val text = spannableWithSmallAndLargeFontSizes()

adjustToFit(text, height = 10_000f, minimumFontScale = 0.5f)

assertThat(fontSizes(text)).containsExactly(LARGE_FONT_SIZE, SMALL_FONT_SIZE)
}

@Test
@GraphicsMode(GraphicsMode.Mode.NATIVE) // Real font metrics, so the height depends on font size.
fun `smaller font sizes shrink in proportion to the largest one`() {
val text = spannableWithSmallAndLargeFontSizes(smallFontSize = 20)
// The height of the text at half its font sizes, with a minimum well below that.
val height =
layoutHeight(spannableWithSmallAndLargeFontSizes(largeFontSize = 20, smallFontSize = 10))

adjustToFit(text, height = height, minimumFontScale = 0.1f)

assertThat(fontSizes(text)).containsExactly(20, 10)
}

@Test
fun `shrunk font sizes are clamped to the minimum font size`() {
val text = spannableWithSmallAndLargeFontSizes()

adjustToUnsatisfiableHeight(text, minimumFontScale = 0.5f)

// Like iOS, every font size is clamped to the minimum derived from the largest one.
val minimumFontSize = (LARGE_FONT_SIZE * 0.5f).toInt()
assertThat(fontSizes(text)).containsExactly(minimumFontSize, minimumFontSize)
}

// Uses a height no font size can satisfy so the text is shrunk all the way to the minimum.
private fun adjustToUnsatisfiableHeight(
text: SpannableString,
Expand All @@ -162,6 +218,59 @@ class TextLayoutManagerMinimumFontScaleTest {
)
}

private fun adjustToFit(text: SpannableString, height: Float, minimumFontScale: Float) {
TextLayoutManager.adjustSpannableFontToFit(
text,
10_000f,
YogaMeasureMode.EXACTLY,
height,
YogaMeasureMode.EXACTLY,
Float.NaN,
minimumFontScale,
ReactConstants.UNSET,
true,
Layout.BREAK_STRATEGY_SIMPLE,
Layout.HYPHENATION_FREQUENCY_NONE,
/* lineBreakStyle = */ LineBreakConfig.LINE_BREAK_STYLE_NONE,
/* lineBreakWordStyle = */ LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE,
Layout.Alignment.ALIGN_NORMAL,
0,
newPaint(),
)
}

// "Total " at the large font size followed by "USD" at the small one.
private fun spannableWithSmallAndLargeFontSizes(
largeFontSize: Int = LARGE_FONT_SIZE,
smallFontSize: Int = SMALL_FONT_SIZE,
): SpannableString {
val text = SpannableString("Total USD")
text.setSpan(ReactAbsoluteSizeSpan(largeFontSize), 0, 6, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
text.setSpan(
ReactAbsoluteSizeSpan(smallFontSize),
6,
text.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,
)
return text
}

private fun layoutHeight(text: SpannableString): Float {
val paint = newPaint().apply { textSize = largestFontSize(text).toFloat() }
return StaticLayout.Builder.obtain(text, 0, text.length, paint, 10_000)
.setIncludePad(true)
.build()
.height
.toFloat()
}

// Font sizes in text order.
private fun fontSizes(text: Spanned): List<Int> =
text
.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java)
.sortedBy { text.getSpanStart(it) }
.map { it.size }

private fun spannableWithFontSize(fontSize: Int): SpannableString {
val text = SpannableString("Hello")
text.setSpan(ReactAbsoluteSizeSpan(fontSize), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
Expand Down