-
Notifications
You must be signed in to change notification settings - Fork 25.3k
Fix minimumFontScale with adjustsFontSizeToFit in the New Architecture
#58492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
conner1reimers
wants to merge
5
commits into
react:main
Choose a base branch
from
conner-reimers:minimum-font-scale-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+271
−10
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5417632
Fix minimumFontScale with adjustsFontSizeToFit on iOS in the New Arch…
conner1reimers 54e914e
Honor minimumFontScale with adjustsFontSizeToFit on Android in the Ne…
conner1reimers d089183
Keep minimumFontSize and serialize minimumFontScale under a new MapBu…
conner1reimers a86976e
Restore ReactTextView.setMinimumFontSize() as deprecated and add setM…
conner1reimers cfa5c05
Update C++ API snapshots for PA_KEY_MINIMUM_FONT_SCALE
conner1reimers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,6 +98,7 @@ internal object TextLayoutManager { | |
| const val PA_KEY_MINIMUM_FONT_SIZE: Int = 6 | ||
| const val PA_KEY_TEXT_ALIGN_VERTICAL: Int = 8 | ||
| const val PA_KEY_TEXT_WIDTH_MODE: Int = 9 | ||
|
Comment on lines
99
to
100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: re-order
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been fixed. |
||
| const val PA_KEY_MINIMUM_FONT_SCALE: Int = 10 | ||
|
|
||
| private val TAG: String = TextLayoutManager::class.java.simpleName | ||
|
|
||
|
|
@@ -1171,6 +1172,10 @@ internal object TextLayoutManager { | |
| if (paragraphAttributes.contains(PA_KEY_MINIMUM_FONT_SIZE)) | ||
| paragraphAttributes.getDouble(PA_KEY_MINIMUM_FONT_SIZE).toFloat() | ||
| else Float.NaN | ||
| val minimumFontScale = | ||
| if (paragraphAttributes.contains(PA_KEY_MINIMUM_FONT_SCALE)) | ||
| paragraphAttributes.getDouble(PA_KEY_MINIMUM_FONT_SCALE).toFloat() | ||
| else Float.NaN | ||
|
|
||
| adjustSpannableFontToFit( | ||
| text, | ||
|
|
@@ -1179,6 +1184,7 @@ internal object TextLayoutManager { | |
| height, | ||
| heightYogaMeasureMode, | ||
| minimumFontSize, | ||
| minimumFontScale, | ||
| maximumNumberOfLines, | ||
| includeFontPadding, | ||
| textBreakStrategy, | ||
|
|
@@ -1338,6 +1344,7 @@ internal object TextLayoutManager { | |
| height: Float, | ||
| heightYogaMeasureMode: YogaMeasureMode, | ||
| minimumFontSizeAttr: Float, | ||
| minimumFontScale: Float, | ||
| maximumNumberOfLines: Int, | ||
| includeFontPadding: Boolean, | ||
| textBreakStrategy: Int, | ||
|
|
@@ -1349,17 +1356,25 @@ internal object TextLayoutManager { | |
| var boring = isBoring(text, paint) | ||
| var layout: Layout | ||
|
|
||
| // Minimum font size is 4pts to match the iOS implementation. | ||
| val minimumFontSize = | ||
| (if (minimumFontSizeAttr.isNaN()) 4.dpToPx() else minimumFontSizeAttr).toInt() | ||
|
|
||
| // Find the largest font size used in the spannable to use as a starting point. | ||
| var currentFontSize = minimumFontSize | ||
| var currentFontSize = 0 | ||
| val spans = text.getSpans(0, text.length, ReactAbsoluteSizeSpan::class.java) | ||
| for (span in spans) { | ||
| currentFontSize = max(currentFontSize, span.size) | ||
| } | ||
|
|
||
| // An explicit minimum font size wins over minimumFontScale, which is applied to the largest | ||
| // font size in the spannable. The 4dp floor matches the iOS implementation. | ||
| val absoluteMinimumFontSize = 4.dpToPx().toInt() | ||
| val minimumFontSize = | ||
| when { | ||
| !minimumFontSizeAttr.isNaN() -> minimumFontSizeAttr.toInt() | ||
| !minimumFontScale.isNaN() && minimumFontScale > 0f -> | ||
| max((minimumFontScale * currentFontSize).toInt(), absoluteMinimumFontSize) | ||
| else -> absoluteMinimumFontSize | ||
| } | ||
| currentFontSize = max(currentFontSize, minimumFontSize) | ||
|
|
||
| var intervalStart = minimumFontSize | ||
| var intervalEnd = currentFontSize | ||
| var previousFontSize = currentFontSize | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...roid/src/test/java/com/facebook/react/views/text/TextLayoutManagerMinimumFontScaleTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * 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.ReactConstants | ||
| import com.facebook.react.uimanager.DisplayMetricsHolder | ||
| import com.facebook.react.uimanager.PixelUtil.dpToPx | ||
| 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 | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class TextLayoutManagerMinimumFontScaleTest { | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(RuntimeEnvironment.getApplication()) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| DisplayMetricsHolder.setScreenDisplayMetrics(null) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minimumFontScale limits how far the font shrinks relative to the largest font size`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontScale = 0.5f) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo((LARGE_FONT_SIZE * 0.5f).toInt()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minimumFontScale is applied to the largest font size in the spannable`() { | ||
| val text = SpannableString("Small text and LARGE TEXT") | ||
| text.setSpan(ReactAbsoluteSizeSpan(SMALL_FONT_SIZE), 0, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
| text.setSpan( | ||
| ReactAbsoluteSizeSpan(LARGE_FONT_SIZE), | ||
| 15, | ||
| text.length, | ||
| Spanned.SPAN_EXCLUSIVE_EXCLUSIVE, | ||
| ) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontScale = 0.5f) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo((LARGE_FONT_SIZE * 0.5f).toInt()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `missing minimumFontScale shrinks down to the 4dp floor`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontScale = Float.NaN) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(4.dpToPx().toInt()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `zero minimumFontScale shrinks down to the 4dp floor`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontScale = 0f) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(4.dpToPx().toInt()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `minimumFontScale never shrinks below the 4dp floor`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontScale = 0.01f) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(4.dpToPx().toInt()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `explicit minimumFontSize is used as the floor`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontSize = 12f, minimumFontScale = Float.NaN) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(12) | ||
| } | ||
|
|
||
| @Test | ||
| fun `explicit minimumFontSize takes precedence over minimumFontScale`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| adjustToUnsatisfiableHeight(text, minimumFontSize = 12f, minimumFontScale = 0.5f) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(12) | ||
| } | ||
|
|
||
| @Test | ||
| fun `text that already fits is not shrunk`() { | ||
| val text = spannableWithFontSize(LARGE_FONT_SIZE) | ||
|
|
||
| TextLayoutManager.adjustSpannableFontToFit( | ||
| text, | ||
| 10_000f, | ||
| YogaMeasureMode.EXACTLY, | ||
| 10_000f, | ||
| YogaMeasureMode.EXACTLY, | ||
| Float.NaN, | ||
| 0.5f, | ||
| ReactConstants.UNSET, | ||
| true, | ||
| Layout.BREAK_STRATEGY_SIMPLE, | ||
| Layout.HYPHENATION_FREQUENCY_NONE, | ||
| Layout.Alignment.ALIGN_NORMAL, | ||
| 0, | ||
| newPaint(), | ||
| ) | ||
|
|
||
| assertThat(largestFontSize(text)).isEqualTo(LARGE_FONT_SIZE) | ||
| } | ||
|
|
||
| // Uses a height no font size can satisfy so the text is shrunk all the way to the minimum. | ||
| private fun adjustToUnsatisfiableHeight( | ||
| text: SpannableString, | ||
| minimumFontScale: Float, | ||
| minimumFontSize: Float = Float.NaN, | ||
| ) { | ||
| TextLayoutManager.adjustSpannableFontToFit( | ||
| text, | ||
| 10_000f, | ||
| YogaMeasureMode.EXACTLY, | ||
| 1f, | ||
| YogaMeasureMode.EXACTLY, | ||
| minimumFontSize, | ||
| minimumFontScale, | ||
| ReactConstants.UNSET, | ||
| true, | ||
| Layout.BREAK_STRATEGY_SIMPLE, | ||
| Layout.HYPHENATION_FREQUENCY_NONE, | ||
| Layout.Alignment.ALIGN_NORMAL, | ||
| 0, | ||
| newPaint(), | ||
| ) | ||
| } | ||
|
|
||
| private fun spannableWithFontSize(fontSize: Int): SpannableString { | ||
| val text = SpannableString("Hello") | ||
| text.setSpan(ReactAbsoluteSizeSpan(fontSize), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
| return text | ||
| } | ||
|
|
||
| private fun newPaint(): TextPaint = | ||
| TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = LARGE_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 SMALL_FONT_SIZE = 10 | ||
| const val LARGE_FONT_SIZE = 40 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a public API change - please avoid. Can you add back the old API as deprecated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setMinimumFontSize()was added back as deprecated, andsetMinimumFontScale()was added alongside it.minimumFontSizestill takes precedence when explicitly set, so existing callers keep the old behavior.