Skip to content

Commit

Permalink
Implement TextIndent (#210)
Browse files Browse the repository at this point in the history
Change-Id: I2ef34a8fe0a41bda94bf62a2d850ae612f1e9220
  • Loading branch information
VeselovAlex authored and eymar committed Nov 16, 2022
1 parent 02b65f7 commit 2254089
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Expand Up @@ -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
Expand Down Expand Up @@ -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<Float> {
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,
Expand Down
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Expand Up @@ -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"
Expand Down

0 comments on commit 2254089

Please sign in to comment.