-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Fix Android text descender clipping with tight lineHeight #57115
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
TorinAsakura
wants to merge
5
commits into
react:main
Choose a base branch
from
torin-asakura:fix/android-text-descender-clipping
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
357f29d
fix(android): prevent text descender clipping
TorinAsakura 8b1f51f
Merge remote-tracking branch 'origin/main' into fix/android-text-desc…
TorinAsakura 459c205
fix(android): draw visible text overflow without layout growth
TorinAsakura 89ff6ff
fix(android): align visible text gravity padding
TorinAsakura 2c04e30
fix(android): clamp visible text gravity offset
TorinAsakura 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
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
148 changes: 148 additions & 0 deletions
148
...eact-native/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextViewTest.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,148 @@ | ||
| /* | ||
| * 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.content.Context | ||
| import android.graphics.Bitmap | ||
| import android.graphics.Canvas | ||
| import android.graphics.Color | ||
| import android.graphics.Paint | ||
| import android.text.SpannableString | ||
| import android.text.Spanned | ||
| import android.text.style.ReplacementSpan | ||
| import android.util.TypedValue | ||
| import android.view.Gravity | ||
| import android.view.View | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RuntimeEnvironment | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class ReactTextViewTest { | ||
|
|
||
| @Test | ||
| fun drawsGlyphInkOutsideLineHeightWhenOverflowIsVisible() { | ||
| val bitmap = drawReactTextViewWithOverflow(null) | ||
|
|
||
| assertThat(hasVisiblePixelBelowViewBounds(bitmap)).isTrue() | ||
| } | ||
|
|
||
| @Test | ||
| fun bottomGravityDoesNotShiftLayoutUpWhenTextIsTallerThanView() { | ||
| val lineHeight = 48 | ||
| val viewHeight = 24 | ||
| val bitmap = drawReactTextViewWithOverflow(null, lineHeight, viewHeight, Gravity.BOTTOM) | ||
|
|
||
| assertThat(firstVisiblePixelY(bitmap)).isGreaterThanOrEqualTo(lineHeight) | ||
| } | ||
|
|
||
| private fun drawReactTextViewWithOverflow(overflow: String?): Bitmap { | ||
| return drawReactTextViewWithOverflow(overflow, lineHeight = 24, viewHeight = 24, gravity = null) | ||
| } | ||
|
|
||
| private fun drawReactTextViewWithOverflow( | ||
| overflow: String?, | ||
| lineHeight: Int, | ||
| viewHeight: Int, | ||
| gravity: Int?, | ||
| ): Bitmap { | ||
| val width = 200 | ||
| val bitmapHeight = 80 | ||
| val text = SpannableString("x") | ||
| text.setSpan( | ||
| OverflowingInkSpan(lineHeight), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
|
|
||
| val view = TestReactTextView(RuntimeEnvironment.getApplication()) | ||
| view.setTextColor(Color.BLACK) | ||
| view.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24f) | ||
| view.includeFontPadding = true | ||
| view.setSpanned(text) | ||
| view.text = text | ||
| view.setOverflow(overflow) | ||
| if (gravity != null) { | ||
| view.setGravityVertical(gravity) | ||
| } | ||
| view.measure( | ||
| View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY), | ||
| View.MeasureSpec.makeMeasureSpec(viewHeight, View.MeasureSpec.EXACTLY), | ||
| ) | ||
| view.layout(0, 0, width, viewHeight) | ||
|
|
||
| return Bitmap.createBitmap(width, bitmapHeight, Bitmap.Config.ARGB_8888).also { | ||
| view.drawTextForTest(Canvas(it)) | ||
| } | ||
| } | ||
|
|
||
| private fun hasVisiblePixelBelowViewBounds(bitmap: Bitmap): Boolean { | ||
| for (y in 24 until bitmap.height) { | ||
| for (x in 0 until bitmap.width) { | ||
| if (Color.alpha(bitmap.getPixel(x, y)) != 0) { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false | ||
| } | ||
|
|
||
| private fun firstVisiblePixelY(bitmap: Bitmap): Int { | ||
| for (y in 0 until bitmap.height) { | ||
| for (x in 0 until bitmap.width) { | ||
| if (Color.alpha(bitmap.getPixel(x, y)) != 0) { | ||
| return y | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return bitmap.height | ||
| } | ||
|
|
||
| private class TestReactTextView(context: Context) : ReactTextView(context) { | ||
| fun drawTextForTest(canvas: Canvas) { | ||
| super.onDraw(canvas) | ||
| } | ||
| } | ||
|
|
||
| private class OverflowingInkSpan(private val lineHeight: Int) : ReplacementSpan() { | ||
| override fun getSize( | ||
| paint: Paint, | ||
| text: CharSequence, | ||
| start: Int, | ||
| end: Int, | ||
| fm: Paint.FontMetricsInt?, | ||
| ): Int { | ||
| fm?.ascent = -lineHeight | ||
| fm?.descent = 0 | ||
| fm?.top = -lineHeight | ||
| fm?.bottom = 0 | ||
| return lineHeight | ||
| } | ||
|
|
||
| override fun draw( | ||
| canvas: Canvas, | ||
| text: CharSequence, | ||
| start: Int, | ||
| end: Int, | ||
| x: Float, | ||
| top: Int, | ||
| y: Int, | ||
| bottom: Int, | ||
| paint: Paint, | ||
| ) { | ||
| canvas.drawRect( | ||
| x, | ||
| y + (lineHeight / 4f), | ||
| x + lineHeight, | ||
| y + (lineHeight / 2f), | ||
| paint, | ||
| ) | ||
| } | ||
| } | ||
| } |
90 changes: 90 additions & 0 deletions
90
...oid/src/test/java/com/facebook/react/views/text/internal/span/CustomLineHeightSpanTest.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,90 @@ | ||
| /* | ||
| * 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.internal.span | ||
|
|
||
| import android.graphics.Paint | ||
| import android.text.Layout | ||
| import android.text.SpannableString | ||
| import android.text.Spanned | ||
| import android.text.StaticLayout | ||
| import android.text.TextPaint | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class CustomLineHeightSpanTest { | ||
|
|
||
| @Test | ||
| fun tightLineHeightDoesNotClipFirstOrLastLineFontBounds() { | ||
| val span = CustomLineHeightSpan(16f) | ||
| val fm = | ||
| Paint.FontMetricsInt().apply { | ||
| top = -18 | ||
| ascent = -14 | ||
| descent = 6 | ||
| bottom = 8 | ||
| } | ||
|
|
||
| span.chooseHeight("gjpqy", 0, 5, 0, 0, fm) | ||
|
|
||
| assertThat(fm.ascent).isEqualTo(-12) | ||
| assertThat(fm.descent).isEqualTo(4) | ||
| assertThat(fm.top).isEqualTo(-12) | ||
| assertThat(fm.bottom).isEqualTo(4) | ||
| } | ||
|
|
||
| @Test | ||
| fun looseLineHeightStillExpandsFirstAndLastLineBounds() { | ||
| val span = CustomLineHeightSpan(24f) | ||
| val fm = | ||
| Paint.FontMetricsInt().apply { | ||
| top = -18 | ||
| ascent = -14 | ||
| descent = 6 | ||
| bottom = 8 | ||
| } | ||
|
|
||
| span.chooseHeight("gjpqy", 0, 5, 0, 0, fm) | ||
|
|
||
| assertThat(fm.ascent).isEqualTo(-16) | ||
| assertThat(fm.descent).isEqualTo(8) | ||
| assertThat(fm.top).isEqualTo(-16) | ||
| assertThat(fm.bottom).isEqualTo(8) | ||
| } | ||
|
|
||
| @Test | ||
| fun tightLineHeightDoesNotExpandStaticLayoutHeightWithFontPadding() { | ||
| val layout = buildStaticLayout("gjpqy\ngjpqy\ngjpqy", lineHeight = 24) | ||
|
|
||
| assertThat(layout.lineCount).isEqualTo(3) | ||
| assertThat(layout.height).isEqualTo(72) | ||
| } | ||
|
|
||
| @Test | ||
| fun tightLineHeightDoesNotExpandSingleLineStaticLayoutHeightWithFontPadding() { | ||
| val layout = buildStaticLayout("gjpqy", lineHeight = 24) | ||
|
|
||
| assertThat(layout.lineCount).isEqualTo(1) | ||
| assertThat(layout.height).isEqualTo(24) | ||
| } | ||
|
|
||
| private fun buildStaticLayout(text: String, lineHeight: Int): StaticLayout { | ||
| val spannable = SpannableString(text) | ||
| spannable.setSpan( | ||
| CustomLineHeightSpan(lineHeight.toFloat()), 0, text.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE) | ||
|
|
||
| return StaticLayout.Builder.obtain( | ||
| spannable, 0, spannable.length, TextPaint().apply { textSize = 24f }, 400) | ||
| .setAlignment(Layout.Alignment.ALIGN_NORMAL) | ||
| .setIncludePad(true) | ||
| .setLineSpacing(0f, 1f) | ||
| .build() | ||
| } | ||
| } |
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.
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.
getAvailableVerticalSpace() uses getCompoundPaddingTop/Bottom() while the canvas translation uses getExtendedPaddingTop() — Android's TextView.getVerticalOffset() uses extended paddings for both.
Suggestions: Calculate available vertical space using getExtendedPaddingTop() and getExtendedPaddingBottom() to match TextView's gravity logic.
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.
89ff6ff
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.
Found one more narrow spot in the same vertical offset path and fixed it here: 2c04e30