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 igordmn committed Aug 18, 2022
1 parent 85922d9 commit f00ab48
Show file tree
Hide file tree
Showing 3 changed files with 38 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 @@ -73,6 +73,7 @@ import kotlin.math.floor
import org.jetbrains.skia.Rect as SkRect
import org.jetbrains.skia.paragraph.Paragraph as SkParagraph
import org.jetbrains.skia.paragraph.TextStyle as SkTextStyle
import org.jetbrains.skia.paragraph.TextIndent as SkTextIndent
import org.jetbrains.skia.FontStyle as SkFontStyle
import org.jetbrains.skia.Font as SkFont
import org.jetbrains.skia.paragraph.DecorationLineStyle as SkDecorationLineStyle
Expand Down Expand Up @@ -826,6 +827,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 @@ -40,7 +40,7 @@ leakcanary = "2.8.1"
metalava = "1.0.0-alpha06"
mockito = "2.25.0"
protobuf = "3.19.4"
skiko = "0.7.11"
skiko = "0.7.14"
sqldelight = "1.3.0"
wire = "3.6.0"

Expand Down

0 comments on commit f00ab48

Please sign in to comment.