Add Unit Em
Bug: 139312484
Test: ./gradlew test
Change-Id: Ie3c229a699422048418f4addb3a7218473a96c95
diff --git a/ui/ui-text/src/androidTest/java/androidx/ui/text/ParagraphIntegrationTest.kt b/ui/ui-text/src/androidTest/java/androidx/ui/text/ParagraphIntegrationTest.kt
index 51a0e6a..50c660c 100644
--- a/ui/ui-text/src/androidTest/java/androidx/ui/text/ParagraphIntegrationTest.kt
+++ b/ui/ui-text/src/androidTest/java/androidx/ui/text/ParagraphIntegrationTest.kt
@@ -22,6 +22,7 @@
import androidx.ui.core.Density
import androidx.ui.core.PxPosition
import androidx.ui.core.Sp
+import androidx.ui.core.em
import androidx.ui.core.px
import androidx.ui.core.sp
import androidx.ui.core.withDensity
@@ -2348,7 +2349,7 @@
val fontSize = 20.sp
val fontSizeInPx = fontSize.toPx().value
val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val textStyle = TextStyle(letterSpacing = letterSpacing.em)
val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
val paragraph = simpleParagraph(
@@ -2373,7 +2374,7 @@
val fontSize = 20.sp
val fontSizeInPx = fontSize.toPx().value
val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val textStyle = TextStyle(letterSpacing = letterSpacing.em)
val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
val paragraph = simpleParagraph(
@@ -2398,11 +2399,11 @@
val fontSize = 20.sp
val fontSizeInPx = fontSize.toPx().value
val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val textStyle = TextStyle(letterSpacing = letterSpacing.em)
val letterSpacingOverwrite = 10.0f
val textStyleOverwrite =
- TextStyle(letterSpacing = letterSpacingOverwrite)
+ TextStyle(letterSpacing = letterSpacingOverwrite.em)
val paragraphWidth = fontSizeInPx * (1 + letterSpacingOverwrite) * text.length
val paragraph = simpleParagraph(
@@ -2625,7 +2626,7 @@
val fontSize = 100.sp
val fontSizeInPx = fontSize.toPx().value
val letterSpacing = 1f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val textStyle = TextStyle(letterSpacing = letterSpacing.em)
val paragraph = simpleParagraph(
text = text,
diff --git a/ui/ui-text/src/androidTest/java/androidx/ui/text/platform/AndroidParagraphTest.kt b/ui/ui-text/src/androidTest/java/androidx/ui/text/platform/AndroidParagraphTest.kt
index 7afacb2..e28d153 100644
--- a/ui/ui-text/src/androidTest/java/androidx/ui/text/platform/AndroidParagraphTest.kt
+++ b/ui/ui-text/src/androidTest/java/androidx/ui/text/platform/AndroidParagraphTest.kt
@@ -21,6 +21,7 @@
import androidx.text.style.SkewXSpan
import androidx.text.style.TypefaceSpan
import androidx.ui.core.Density
+import androidx.ui.core.em
import androidx.ui.core.px
import androidx.ui.core.sp
import androidx.ui.core.withDensity
@@ -330,7 +331,7 @@
fun testAnnotatedString_setLetterSpacingOnWholeText() {
val text = "abcde"
val letterSpacing = 2.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val textStyle = TextStyle(letterSpacing = letterSpacing.em)
val paragraph = simpleParagraph(
text = text,
@@ -345,7 +346,7 @@
@Test
fun testAnnotatedString_setLetterSpacingOnPartText() {
val text = "abcde"
- val textStyle = TextStyle(letterSpacing = 2.0f)
+ val textStyle = TextStyle(letterSpacing = 2.em)
val paragraph = simpleParagraph(
text = text,
@@ -360,8 +361,8 @@
@Test
fun testAnnotatedString_setLetterSpacingTwice_lastOneOverwrite() {
val text = "abcde"
- val textStyle = TextStyle(letterSpacing = 2.0f)
- val textStyleOverwrite = TextStyle(letterSpacing = 3.0f)
+ val textStyle = TextStyle(letterSpacing = 2.em)
+ val textStyleOverwrite = TextStyle(letterSpacing = 3.em)
val paragraph = simpleParagraph(
text = text,
@@ -1035,10 +1036,10 @@
@Test
fun testTextStyle_letterSpacing_appliedOnTextPaint() {
- val letterSpacing = 2.0f
+ val letterSpacing = 2
val paragraph = simpleParagraph(
text = "",
- textStyle = TextStyle(letterSpacing = letterSpacing),
+ textStyle = TextStyle(letterSpacing = letterSpacing.em),
constraints = ParagraphConstraints(width = 0.0f)
)
diff --git a/ui/ui-text/src/main/java/androidx/ui/text/TextStyle.kt b/ui/ui-text/src/main/java/androidx/ui/text/TextStyle.kt
index df82ada..60859fc 100644
--- a/ui/ui-text/src/main/java/androidx/ui/text/TextStyle.kt
+++ b/ui/ui-text/src/main/java/androidx/ui/text/TextStyle.kt
@@ -16,7 +16,9 @@
package androidx.ui.text
+import androidx.ui.core.Em
import androidx.ui.core.Sp
+import androidx.ui.core.em
import androidx.ui.core.sp
import androidx.ui.graphics.Color
import androidx.ui.graphics.Shadow
@@ -68,7 +70,7 @@
val fontSynthesis: FontSynthesis? = null,
var fontFamily: FontFamily? = null,
val fontFeatureSettings: String? = null,
- val letterSpacing: Float? = null,
+ val letterSpacing: Em? = null,
val baselineShift: BaselineShift? = null,
val textGeometricTransform: TextGeometricTransform? = null,
val localeList: LocaleList? = null,
@@ -131,6 +133,13 @@
return androidx.ui.core.lerp(start, end, t)
}
+ private fun lerpEm(a: Em?, b: Em?, t: Float, default: Em = 0f.em): Em? {
+ if (a == null && b == null) return null
+ val start = a ?: default
+ val end = b ?: default
+ return androidx.ui.core.lerp(start, end, t)
+ }
+
private fun <T> lerpDiscrete(a: T?, b: T?, t: Float): T? = if (t < 0.5) a else b
/**
@@ -213,7 +222,7 @@
stop.fontFeatureSettings,
fraction
),
- letterSpacing = lerpFloat(
+ letterSpacing = lerpEm(
start.letterSpacing,
stop.letterSpacing,
fraction
diff --git a/ui/ui-text/src/main/java/androidx/ui/text/platform/AndroidParagraphHelper.kt b/ui/ui-text/src/main/java/androidx/ui/text/platform/AndroidParagraphHelper.kt
index 2b5dcbf..2bb4282 100644
--- a/ui/ui-text/src/main/java/androidx/ui/text/platform/AndroidParagraphHelper.kt
+++ b/ui/ui-text/src/main/java/androidx/ui/text/platform/AndroidParagraphHelper.kt
@@ -98,7 +98,7 @@
}
style.letterSpacing?.let {
- letterSpacing = it
+ letterSpacing = it.value
}
style.fontFeatureSettings?.let {
@@ -281,7 +281,7 @@
// TODO(haoyuchang): support letter spacing with pixel.
style.letterSpacing?.let {
spannableString.setSpan(
- LetterSpacingSpan(it),
+ LetterSpacingSpan(it.value),
start,
end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
diff --git a/ui/ui-text/src/test/java/androidx/ui/text/AnnotatedStringBuilderTest.kt b/ui/ui-text/src/test/java/androidx/ui/text/AnnotatedStringBuilderTest.kt
index 40d2daa..e66e09c 100644
--- a/ui/ui-text/src/test/java/androidx/ui/text/AnnotatedStringBuilderTest.kt
+++ b/ui/ui-text/src/test/java/androidx/ui/text/AnnotatedStringBuilderTest.kt
@@ -17,6 +17,7 @@
package androidx.ui.text
import androidx.ui.core.Sp
+import androidx.ui.core.em
import androidx.ui.core.sp
import androidx.ui.graphics.Color
import androidx.ui.text.font.FontStyle
@@ -281,7 +282,7 @@
TextStyle(color = Color.Red),
TextStyle(fontStyle = FontStyle.Italic),
TextStyle(fontWeight = FontWeight.Bold),
- TextStyle(letterSpacing = 1.2f)
+ TextStyle(letterSpacing = 1.2.em)
)
val buildResult = with(AnnotatedString.Builder()) {
@@ -313,7 +314,7 @@
TextStyle(color = Color.Red),
TextStyle(fontStyle = FontStyle.Italic),
TextStyle(fontWeight = FontWeight.Bold),
- TextStyle(letterSpacing = 1.2f)
+ TextStyle(letterSpacing = 1.2.em)
)
val buildResult = with(AnnotatedString.Builder()) {
diff --git a/ui/ui-text/src/test/java/androidx/ui/text/TextSpanTest.kt b/ui/ui-text/src/test/java/androidx/ui/text/TextSpanTest.kt
index 1502bc9..598adfb 100644
--- a/ui/ui-text/src/test/java/androidx/ui/text/TextSpanTest.kt
+++ b/ui/ui-text/src/test/java/androidx/ui/text/TextSpanTest.kt
@@ -16,6 +16,7 @@
package androidx.ui.text
+import androidx.ui.core.em
import androidx.ui.core.sp
import androidx.ui.graphics.Color
import com.google.common.truth.Truth.assertThat
@@ -39,7 +40,7 @@
@Test
fun `constructor with customized style`() {
- val textStyle = TextStyle(fontSize = 10.sp, letterSpacing = 1.5f)
+ val textStyle = TextStyle(fontSize = 10.sp, letterSpacing = 1.5.em)
val textSpan = TextSpan(style = textStyle)
assertThat(textSpan.style).isEqualTo(textStyle)
@@ -207,7 +208,7 @@
fun `compareTo with one null style should return LAYOUT`() {
val textSpan1 = TextSpan()
val textSpan2 =
- TextSpan(style = TextStyle(letterSpacing = 1.5f))
+ TextSpan(style = TextStyle(letterSpacing = 1.5.em))
assertThat(textSpan1.compareTo(textSpan2)).isEqualTo(RenderComparison.LAYOUT)
}
diff --git a/ui/ui-text/src/test/java/androidx/ui/text/TextStyleTest.kt b/ui/ui-text/src/test/java/androidx/ui/text/TextStyleTest.kt
index fe6831e..ad56088 100644
--- a/ui/ui-text/src/test/java/androidx/ui/text/TextStyleTest.kt
+++ b/ui/ui-text/src/test/java/androidx/ui/text/TextStyleTest.kt
@@ -16,6 +16,7 @@
package androidx.ui.text
+import androidx.ui.core.em
import androidx.ui.core.px
import androidx.ui.core.sp
import androidx.ui.engine.geometry.Offset
@@ -90,7 +91,7 @@
@Test
fun `constructor with customized letterSpacing`() {
- val letterSpacing = 1.0f
+ val letterSpacing = 1.em
val textStyle = TextStyle(letterSpacing = letterSpacing)
@@ -317,7 +318,7 @@
@Test
fun `merge with other's letterSpacing is null should use this' letterSpacing`() {
- val letterSpacing = 1.2f
+ val letterSpacing = 1.2.em
val textStyle = TextStyle(letterSpacing = letterSpacing)
val otherTextStyle = TextStyle()
@@ -328,8 +329,8 @@
@Test
fun `merge with other's letterSpacing is set should use other's letterSpacing`() {
- val letterSpacing = 1.2f
- val otherLetterSpacing = 1.5f
+ val letterSpacing = 1.2.em
+ val otherLetterSpacing = 1.5.em
val textStyle = TextStyle(letterSpacing = letterSpacing)
val otherTextStyle = TextStyle(letterSpacing = otherLetterSpacing)
@@ -978,7 +979,7 @@
@Test
fun `lerp letterSpacing with a is Null and t is smaller than half`() {
- val letterSpacing = 2.0f
+ val letterSpacing = 2.em
val t = 0.3f
val textStyle = TextStyle(letterSpacing = letterSpacing)
@@ -989,7 +990,7 @@
@Test
fun `lerp letterSpacing with a is Null and t is larger than half`() {
- val letterSpacing = 2.0f
+ val letterSpacing = 2.em
val t = 0.8f
val textStyle = TextStyle(letterSpacing = letterSpacing)
@@ -1000,7 +1001,7 @@
@Test
fun `lerp letterSpacing with b is Null and t is smaller than half`() {
- val letterSpacing = 2.0f
+ val letterSpacing = 2.em
val t = 0.3f
val textStyle = TextStyle(letterSpacing = letterSpacing)
@@ -1011,7 +1012,7 @@
@Test
fun `lerp letterSpacing with b is Null and t is larger than half`() {
- val letterSpacing = 2.0f
+ val letterSpacing = 2.em
val t = 0.8f
val textStyle = TextStyle(letterSpacing = letterSpacing)
@@ -1374,7 +1375,7 @@
fontWeight = FontWeight.W800,
fontStyle = FontStyle.Italic,
fontFeatureSettings = fontFeatureSettings,
- letterSpacing = 1.0f,
+ letterSpacing = 1.em,
baselineShift = BaselineShift.Subscript,
textGeometricTransform = TextGeometricTransform(scaleX = 1.0f),
localeList = LocaleList("en-US"),
@@ -1405,7 +1406,7 @@
assertThat(textStyle.compareTo(textStyle.copy(fontFeatureSettings = null)))
.isEqualTo(RenderComparison.LAYOUT)
- assertThat(textStyle.compareTo(textStyle.copy(letterSpacing = 2.0f)))
+ assertThat(textStyle.compareTo(textStyle.copy(letterSpacing = 2.em)))
.isEqualTo(RenderComparison.LAYOUT)
assertThat(textStyle.compareTo(textStyle.copy(baselineShift = BaselineShift.Superscript)))
@@ -1433,7 +1434,7 @@
fontSize = fontSize,
fontWeight = FontWeight.W800,
fontStyle = FontStyle.Italic,
- letterSpacing = 1.0f,
+ letterSpacing = 1.em,
baselineShift = BaselineShift.Superscript,
textGeometricTransform = TextGeometricTransform(null, null),
localeList = LocaleList("en-US"),