Skip to content

Commit

Permalink
Fix position of cursor on empty line (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
VeselovAlex authored and eymar committed Nov 16, 2022
1 parent 8644417 commit 02b65f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,22 @@ internal class SkiaParagraph(
// workaround for https://bugs.chromium.org/p/skia/issues/detail?id=11321 :(
private val lineMetrics: Array<LineMetrics>
get() = if (text == "") {
val height = layouter.defaultHeight.toDouble()
val metrics = layouter.defaultFont.metrics
val ascent = -metrics.ascent.toDouble()
val descent = metrics.descent.toDouble()
val baseline = para.alphabeticBaseline.toDouble()
val height = with(layouter.paragraphStyle.strutStyle) {
if (isEnabled && !isHeightForced && isHeightOverridden && fontSize > 0.0f) {
(height * fontSize).toDouble()
} else {
ascent + descent
}
}

arrayOf(
LineMetrics(
0, 0, 0, 0, true,
height, 0.0, height, height, 0.0, 0.0, height, 0
ascent, descent, ascent, height, 0.0, 0.0, baseline, 0
)
)
} else {
Expand Down Expand Up @@ -291,6 +302,7 @@ internal class SkiaParagraph(
}
}

// TODO(b/229518449): Implement an alternative to paint function that takes a brush.
override fun paint(
canvas: Canvas,
color: Color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class ParagraphLayouter(
private var width: Float = -1f

val defaultHeight get() = builder.defaultHeight
val defaultFont get() = builder.defaultFont
val paragraphStyle get() = builder.paragraphStyle

fun layoutParagraph(
width: Float = this.width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import org.jetbrains.skia.paragraph.Shadow as SkShadow
import org.jetbrains.skia.paragraph.StrutStyle
import org.jetbrains.skia.paragraph.TextBox
import org.jetbrains.skia.paragraph.TextStyle as SkTextStyle
import org.jetbrains.skia.paragraph.Rect as SkRect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shadow
import androidx.compose.ui.graphics.isSpecified
Expand Down Expand Up @@ -289,6 +290,8 @@ internal class ParagraphBuilder(
) {
private lateinit var initialStyle: SpanStyle
private lateinit var defaultStyle: ComputedStyle
internal lateinit var paragraphStyle: ParagraphStyle
private set
private lateinit var ops: List<Op>

/**
Expand All @@ -312,6 +315,7 @@ internal class ParagraphBuilder(

var pos = 0
val ps = textStyleToParagraphStyle(textStyle, defaultStyle)
paragraphStyle = ps

if (maxLines != Int.MAX_VALUE) {
ps.maxLinesCount = maxLines
Expand Down Expand Up @@ -524,6 +528,7 @@ internal class ParagraphBuilder(
else -> error("Unexpected size in textStyleToParagraphStyle")
}
strutStyle.height = lineHeight / fontSize
strutStyle.fontSize = fontSize
pStyle.strutStyle = strutStyle
}
pStyle.direction = textDirection.toSkDirection()
Expand Down

0 comments on commit 02b65f7

Please sign in to comment.