Skip to content
Merged
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 @@ -1217,7 +1217,8 @@ internal object TextLayoutManager {
.layout
}

private fun createLayout(
@VisibleForTesting
internal fun createLayout(
text: Spannable,
paint: TextPaint,
attributedString: MapBuffer,
Expand All @@ -1227,7 +1228,7 @@ internal object TextLayoutManager {
height: Float,
heightYogaMeasureMode: YogaMeasureMode,
): CreateLayoutResult {
val boring = isBoring(text, paint)
var boring = isBoring(text, paint)

val textBreakStrategy =
TextAttributeProps.getTextBreakStrategy(
Expand Down Expand Up @@ -1301,6 +1302,11 @@ internal object TextLayoutManager {
justificationMode,
paint,
)
// The spans and paint now hold the fitted font size. Re-measure so the final layout does not
// keep the line metrics of the original, larger font.
if (boring != null) {
boring = isBoring(text, paint)
}
}

var layout =
Expand Down Expand Up @@ -1957,7 +1963,7 @@ internal object TextLayoutManager {
return metrics
}

private class CreateLayoutResult(
internal class CreateLayoutResult(
val layout: Layout,
val textBreakStrategy: Int,
val justificationMode: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.views.text

import android.text.Layout
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import com.facebook.react.common.mapbuffer.WritableMapBuffer
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan
import com.facebook.yoga.YogaMeasureMode
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.GraphicsMode

/**
* A single-line label that fits its width but not its height is shrunk by adjustsFontSizeToFit. The
* layout createLayout returns must use the line metrics of the fitted font size, not those of the
* original, larger font: otherwise the line is taller than the height it was shrunk to fit, and its
* baseline sits low. PreparedLayoutTextView draws this layout directly, so the label is drawn
* off-center.
*/
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
class TextLayoutManagerAdjustFontSizeToFitMetricsTest {

@Before
fun setUp() {
DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(RuntimeEnvironment.getApplication())
}

@After
fun tearDown() {
DisplayMetricsHolder.setScreenDisplayMetrics(null)
}

@Test
fun `text shrunk to fit a height is laid out with the fitted font metrics`() {
val fullSizeLineHeight = layoutWithoutAdjusting().height
val availableHeight = fullSizeLineHeight * 3f / 4f

val text = spannableWithFontSize(FONT_SIZE)
val paint = newPaint()
val layout = createAdjustedLayout(text, paint, availableHeight)

assertThat(largestFontSize(text)).isLessThan(FONT_SIZE)
assertThat(layout.height.toFloat()).isLessThanOrEqualTo(availableHeight)
assertThat(layout.getLineBaseline(0))
.isEqualTo(layoutWithoutAdjusting(text, paint).getLineBaseline(0))
}

private fun createAdjustedLayout(text: SpannableString, paint: TextPaint, height: Float): Layout {
return TextLayoutManager.createLayout(
text,
paint,
WritableMapBuffer(),
singleLineParagraph().put(TextLayoutManager.PA_KEY_ADJUST_FONT_SIZE_TO_FIT, true),
WIDTH,
YogaMeasureMode.EXACTLY,
height,
YogaMeasureMode.AT_MOST,
)
.layout
}

// The same text at whatever size its spans and paint hold, without adjustsFontSizeToFit.
private fun layoutWithoutAdjusting(
text: SpannableString = spannableWithFontSize(FONT_SIZE),
paint: TextPaint = newPaint(),
): Layout =
TextLayoutManager.createLayout(
SpannableString(text),
TextPaint(paint),
WritableMapBuffer(),
singleLineParagraph(),
WIDTH,
YogaMeasureMode.EXACTLY,
Float.NaN,
YogaMeasureMode.UNDEFINED,
)
.layout

private fun singleLineParagraph(): WritableMapBuffer =
WritableMapBuffer()
.put(TextLayoutManager.PA_KEY_MAX_NUMBER_OF_LINES, 1)
.put(TextLayoutManager.PA_KEY_TEXT_BREAK_STRATEGY, "simple")
.put(TextLayoutManager.PA_KEY_HYPHENATION_FREQUENCY, "none")

private fun spannableWithFontSize(fontSize: Int): SpannableString {
val text = SpannableString("Done")
text.setSpan(ReactAbsoluteSizeSpan(fontSize), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
return text
}

private fun newPaint(): TextPaint =
TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = FONT_SIZE.toFloat() }

private fun largestFontSize(text: Spanned): Int =
text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java).maxOfOrNull { it.size } ?: 0

private companion object {
const val FONT_SIZE = 40
const val WIDTH = 10_000f
}
}
31 changes: 31 additions & 0 deletions packages/rn-tester/js/examples/Text/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,26 @@ class AdjustingFontSize extends React.Component<
}
}

function AdjustsFontSizeToFitHeightExample(props: {}): React.Node {
// Each label fits its container's width at its full font size but is too
// tall for it, so adjustsFontSizeToFit shrinks it to fit the height. Every
// label should be fully visible and vertically centered in its box.
return (
<View testID="text-adjusts-font-size-to-fit-height">
{[40, 48, 64].map(fontSize => (
<View key={fontSize} style={styles.adjustsFontSizeToFitHeightBox}>
<Text
adjustsFontSizeToFit={true}
numberOfLines={1}
style={{fontSize}}>
Done
</Text>
</View>
))}
</View>
);
}

function TextLinkifyExample(props: {}): React.Node {
return (
<>
Expand Down Expand Up @@ -1472,6 +1492,11 @@ const examples = [
return <TextAdjustsDynamicLayoutExample />;
},
},
{
title: 'adjustsFontSizeToFit shrinking to fit a height',
name: 'adjustsFontSizeToFitHeight',
render: AdjustsFontSizeToFitHeightExample,
},
{
title: 'Wrap',
name: 'wrap',
Expand Down Expand Up @@ -1987,6 +2012,12 @@ const styles = StyleSheet.create({
fontSize: 16,
width: 150,
},
adjustsFontSizeToFitHeightBox: {
backgroundColor: '#E4E6EB',
height: 40,
justifyContent: 'center',
marginBottom: 2,
},
});

module.exports = {
Expand Down
Loading