From 22540899d4113aeb23f2dc62fa4fa914f0f68096 Mon Sep 17 00:00:00 2001 From: Aleksandr Veselov Date: Wed, 9 Mar 2022 09:49:17 +0100 Subject: [PATCH] Implement TextIndent (#210) Change-Id: I2ef34a8fe0a41bda94bf62a2d850ae612f1e9220 --- .../compose/ui/text/DesktopParagraphTest.kt | 31 +++++++++++++++++++ .../ui/text/platform/SkiaParagraph.skiko.kt | 5 +++ gradle/libs.versions.toml | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/compose/ui/ui-text/src/desktopTest/kotlin/androidx/compose/ui/text/DesktopParagraphTest.kt b/compose/ui/ui-text/src/desktopTest/kotlin/androidx/compose/ui/text/DesktopParagraphTest.kt index 20e9b7a8e5339..3e2b1e23d6006 100644 --- a/compose/ui/ui-text/src/desktopTest/kotlin/androidx/compose/ui/text/DesktopParagraphTest.kt +++ b/compose/ui/ui-text/src/desktopTest/kotlin/androidx/compose/ui/text/DesktopParagraphTest.kt @@ -27,7 +27,9 @@ import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.platform.Font import androidx.compose.ui.text.style.BaselineShift +import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextDirection +import androidx.compose.ui.text.style.TextIndent import androidx.compose.ui.unit.Constraints import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.sp @@ -318,6 +320,35 @@ class DesktopParagraphTest { Truth.assertThat(paragraphWithoutStyles.getLineBottom(1)).isNotEqualTo(secondLineBottom) } + @Test + fun `applies text indent for paragraph`() { + fun measureLines(alignment: TextAlign, direction: TextDirection): List { + val paragraph = simpleParagraph( + text = "sample\ntext", + style = TextStyle( + fontSize = 20.sp, + textIndent = TextIndent(50.sp, 20.sp), + textAlign = alignment, + textDirection = direction, + ) + ) + return listOf( + paragraph.getLineLeft(0), + paragraph.getLineRight(0), + paragraph.getLineLeft(1), + paragraph.getLineRight(1) + ) + } + Truth.assertThat(measureLines(TextAlign.Left, TextDirection.Ltr)).isEqualTo(listOf(50f, 170f, 20f, 100f)) + Truth.assertThat(measureLines(TextAlign.Center, TextDirection.Ltr)).isEqualTo(listOf(965f, 1085f, 970f, 1050f)) + Truth.assertThat(measureLines(TextAlign.Right, TextDirection.Ltr)).isEqualTo(listOf(1830f, 1950f, 1900f, 1980f)) + Truth.assertThat(measureLines(TextAlign.Justify, TextDirection.Ltr)).isEqualTo(listOf(50f, 170f, 20f, 100f)) + Truth.assertThat(measureLines(TextAlign.Left, TextDirection.Rtl)).isEqualTo(listOf(50f, 170f, 20f, 100f)) + Truth.assertThat(measureLines(TextAlign.Center, TextDirection.Rtl)).isEqualTo(listOf(915f, 1035f, 950f, 1030f)) + Truth.assertThat(measureLines(TextAlign.Right, TextDirection.Rtl)).isEqualTo(listOf(1830f, 1950f, 1900f, 1980f)) + Truth.assertThat(measureLines(TextAlign.Justify, TextDirection.Rtl)).isEqualTo(listOf(1830f, 1950f, 1900f, 1980f)) + } + private fun simpleParagraph( text: String = "", style: TextStyle? = null, diff --git a/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/SkiaParagraph.skiko.kt b/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/SkiaParagraph.skiko.kt index 3408be477c48d..907062148f084 100644 --- a/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/SkiaParagraph.skiko.kt +++ b/compose/ui/ui-text/src/skikoMain/kotlin/androidx/compose/ui/text/platform/SkiaParagraph.skiko.kt @@ -532,6 +532,11 @@ internal class ParagraphBuilder( pStyle.strutStyle = strutStyle } pStyle.direction = textDirection.toSkDirection() + textStyle.textIndent?.run { + with(density) { + pStyle.textIndent = SkTextIndent(firstLine.toPx(), restLine.toPx()) + } + } return pStyle } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index babead7c1d764..6ff80722903ed 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -48,7 +48,7 @@ moshi = "1.13.0" protobuf = "3.19.4" paparazzi = "1.0.0" paparazziNative = "2022.1.1-canary-f5f9f71" -skiko = "0.7.11" +skiko = "0.7.14" sqldelight = "1.3.0" retrofit = "2.7.2" wire = "4.4.1"