Skip to content

Commit

Permalink
Fix missing size for applied ShaderBrush to span (#1389)
Browse files Browse the repository at this point in the history
Correctly initialize `brushSize` even without calling explicit text
style setter.

Fixes JetBrains/compose-multiplatform#4897

## Testing

Use reproduction from the issue

## Release Notes

### Fixes - Multiple Platforms

- Fix applying `ShaderBrush` to part of `AnnotatedString`
  • Loading branch information
MatkovIvan committed Jun 7, 2024
1 parent e0fe94b commit 7a13310
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ internal class SkiaParagraph(
}

init {
// Size is not known until layout is complete but to apply it, we need to re-create
// skia's paragraph :'(
// layouter might use cached instance if no [ShaderBrush] was applied.
layouter.setBrushSize(Size(width, height))
paragraph = layouter.layoutParagraph(width)

paragraph.layout(width)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ package androidx.compose.ui.text.platform

import androidx.compose.ui.geometry.Size
import androidx.compose.ui.geometry.isUnspecified
import androidx.compose.ui.graphics.*
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ShaderBrush
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.drawscope.DrawStyle
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.ExperimentalTextApi
import androidx.compose.ui.text.Placeholder
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.style.ResolvedTextDirection
import androidx.compose.ui.text.style.TextDecoration
import androidx.compose.ui.unit.Density
import kotlin.math.abs
import org.jetbrains.skia.Paint
import org.jetbrains.skia.paragraph.LineMetrics
import org.jetbrains.skia.paragraph.Paragraph

Expand Down Expand Up @@ -94,6 +97,22 @@ internal class ParagraphLayouter(
}
}

fun setBrushSize(
brushSize: Size,
) {
if (builder.brushSize != brushSize) {
builder.brushSize = brushSize

// [brushSize] requires only for shader recreation and does not require re-layout,
// but we have to invalidate it because it's backed into skia's paragraph.
// Since it affects only [ShaderBrush] we can keep the cache if it's not used.
if (builder.textStyle.brush is ShaderBrush ||
builder.spanStyles.any { it.item.brush is ShaderBrush }) {
paragraphCache = null
}
}
}

fun setTextStyle(
color: Color,
shadow: Shadow?,
Expand Down

0 comments on commit 7a13310

Please sign in to comment.