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 @@ -289,6 +289,15 @@ protected void onDraw(Canvas canvas) {

if (spanned != null) {
Layout layout = getLayout();
if (layout == null && getWidth() > 0 && getHeight() > 0) {
// setText drops the Layout and Fabric re-measures only on a frame change, so a text
// update that kept the frame reaches onDraw without one and the effects below would be
// skipped. Rebuild it here; TextView.onDraw would build one for the text anyway.
measure(
MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
layout = getLayout();
}
if (layout != null) {
CanvasEffectSpan[] drawSpans =
spanned.getSpans(0, spanned.length(), CanvasEffectSpan.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.Layout
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
Expand All @@ -27,6 +28,7 @@ import com.facebook.react.bridge.JavaOnlyArray
import com.facebook.react.bridge.JavaOnlyMap
import com.facebook.react.uimanager.DisplayMetricsHolder
import com.facebook.react.uimanager.ReactStylesDiffMap
import com.facebook.react.views.text.internal.span.CanvasEffectSpan
import com.facebook.react.views.text.internal.span.ReactAbsoluteSizeSpan
import org.assertj.core.api.Assertions.assertThat
import org.junit.After
Expand Down Expand Up @@ -130,6 +132,39 @@ class ReactTextViewTest {
assertThat(view.letterSpacing).isEqualTo(expectedLetterSpacing)
}

@Test
fun effectSpansPaintAfterFramePreservingUpdateWithoutRemeasure() {
val width = 200
val height = 100
val view = TestReactTextView(RuntimeEnvironment.getApplication())
view.layoutParams =
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
)
view.setTextColor(Color.BLACK)
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24f)
val plain = SpannableString("Hello")
view.setSpanned(plain)
view.text = plain
view.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY),
)
view.layout(0, 0, width, height)
assertThat(view.layout).isNotNull()

val span = RecordingCanvasEffectSpan()
val decorated = SpannableString("Hello")
decorated.setSpan(span, 0, decorated.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
view.setSpanned(decorated)
view.text = decorated
view.drawTextForTest(Canvas(createBitmap(width, height)))

assertThat(view.layout).isNotNull()
assertThat(span.drawCalls + span.preDrawCalls).isGreaterThan(0)
}

@Test
fun drawsGlyphInkOutsideLineHeightWhenOverflowIsVisible() {
val bitmap = drawReactTextViewWithOverflow(null)
Expand Down Expand Up @@ -351,6 +386,19 @@ class ReactTextViewTest {
}
}

private class RecordingCanvasEffectSpan : CanvasEffectSpan() {
var preDrawCalls = 0
var drawCalls = 0

override fun onPreDraw(start: Int, end: Int, canvas: Canvas, layout: Layout) {
preDrawCalls++
}

override fun onDraw(start: Int, end: Int, canvas: Canvas, layout: Layout) {
drawCalls++
}
}

private class OverflowingInkSpan(private val lineHeight: Int) : ReplacementSpan() {
override fun getSize(
paint: Paint,
Expand Down
Loading