Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TextIndent #210

Merged
merged 2 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.platform.Font
import androidx.compose.ui.text.platform.FontLoader
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.Density
import androidx.compose.ui.unit.sp
import com.google.common.truth.Truth
Expand Down Expand Up @@ -317,6 +319,35 @@ class DesktopParagraphTest {
}
}

@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
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 @@ -770,6 +771,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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ksp = "1.6.10-1.0.2"
ktlint = "0.43.0"
leakcanary = "2.7"
mockito = "2.25.0"
skiko = "0.7.12"
skiko = "0.7.14"
sqldelight = "1.3.0"
wire = "3.6.0"

Expand Down