Use SP for fontSize
Test: ./gradlew ui-text:text
Test: ./gradlew ui-text:connectedAndroidTest
Test: ./gradlew ui-framework:test
Test: ./gradlew ui-framework:connectedAndroidTest
Test: ./gradlew ui-material:test
Test: ./gradlew ui-material:connectedAndroidTest
Test: ./gradlew ui-animation:test
Test: ./gradlew ui-animation:connectedAndroidTest
Test: ./gradlew ui-layout:test
Test: ./gradlew ui-layout:connectedAndroidTest
Test: Manual on demo app
Bug: 127345041
Change-Id: Iac6c9f5915fd557f1e80c7a3577b42db42539baa
diff --git a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/AnimatableSeekBar.kt b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/AnimatableSeekBar.kt
index 657ce32..4f66e63 100644
--- a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/AnimatableSeekBar.kt
+++ b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/AnimatableSeekBar.kt
@@ -33,6 +33,7 @@
import androidx.ui.core.gesture.DragGestureDetector
import androidx.ui.core.gesture.DragObserver
import androidx.ui.core.gesture.PressGestureDetector
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.graphics.Color
@@ -50,7 +51,7 @@
CraneWrapper {
Column {
Padding(40.dp) {
- Text("Drag or tap on the seek bar", style = TextStyle(fontSize = 80f))
+ Text("Drag or tap on the seek bar", style = TextStyle(fontSize = 8.sp))
}
Padding(left = 10.dp, right = 10.dp, bottom = 30.dp) {
diff --git a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/FancyScrolling.kt b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/FancyScrolling.kt
index f8753bc..5cf9393 100644
--- a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/FancyScrolling.kt
+++ b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/FancyScrolling.kt
@@ -39,6 +39,7 @@
import androidx.ui.core.dp
import androidx.ui.core.gesture.DragGestureDetector
import androidx.ui.core.gesture.DragObserver
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Rect
import androidx.ui.graphics.Color
import androidx.ui.layout.Column
@@ -63,7 +64,7 @@
fun FancyScrollingExample() {
Column {
Padding(40.dp) {
- Text("<== Scroll horizontally ==>", style = TextStyle(fontSize = 80f))
+ Text("<== Scroll horizontally ==>", style = TextStyle(fontSize = 80.sp))
}
val animScroll = +animatedFloat(0f)
val itemWidth = +state { 0f }
diff --git a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/RepeatedRotationActivity.kt b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/RepeatedRotationActivity.kt
index 951c110..65af089 100644
--- a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/RepeatedRotationActivity.kt
+++ b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/RepeatedRotationActivity.kt
@@ -28,7 +28,6 @@
import androidx.ui.core.dp
import androidx.ui.core.gesture.PressReleasedGestureDetector
import androidx.ui.core.toRect
-import androidx.ui.core.withDensity
import androidx.ui.layout.Center
import androidx.ui.layout.Column
import androidx.ui.layout.Container
@@ -42,6 +41,7 @@
import androidx.compose.setContent
import androidx.compose.state
import androidx.compose.unaryPlus
+import androidx.ui.core.sp
class RepeatedRotationActivity : Activity() {
@@ -57,7 +57,7 @@
Center {
val state = +state { RotationStates.Original }
Column(mainAxisAlignment = MainAxisAlignment.SpaceEvenly) {
- val textStyle = TextStyle(fontSize = +withDensity { 18.dp.toPx().value })
+ val textStyle = TextStyle(fontSize = 18.sp)
PressReleasedGestureDetector(>
state.value = RotationStates.Rotated
}) {
diff --git a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SpringBackScrolling.kt b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SpringBackScrolling.kt
index 5610919..a25ac53 100644
--- a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SpringBackScrolling.kt
+++ b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SpringBackScrolling.kt
@@ -39,6 +39,7 @@
import androidx.ui.core.dp
import androidx.ui.core.gesture.DragGestureDetector
import androidx.ui.core.gesture.DragObserver
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Rect
import androidx.ui.graphics.Color
import androidx.ui.layout.Column
@@ -63,7 +64,7 @@
fun SpringBackExample() {
Column {
Padding(40.dp) {
- Text("<== Scroll horizontally ==>", style = TextStyle(fontSize = 80f))
+ Text("<== Scroll horizontally ==>", style = TextStyle(fontSize = 80.sp))
}
val animScroll = +animatedFloat(0f)
val itemWidth = +state { 0f }
diff --git a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SwipeToDismiss.kt b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SwipeToDismiss.kt
index ac52145..1327e21 100644
--- a/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SwipeToDismiss.kt
+++ b/ui/animation/integration-tests/animation-demos/src/main/java/androidx/ui/animation/demos/SwipeToDismiss.kt
@@ -40,6 +40,7 @@
import androidx.ui.core.dp
import androidx.ui.core.gesture.DragGestureDetector
import androidx.ui.core.gesture.DragObserver
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Rect
import androidx.ui.graphics.Color
import androidx.ui.layout.Column
@@ -58,7 +59,7 @@
SwipeToDismiss()
Padding(40.dp) {
- Text("Swipe up to dismiss", style = TextStyle(fontSize = 80f))
+ Text("Swipe up to dismiss", style = TextStyle(fontSize = 80.sp))
}
}
}
diff --git a/ui/framework/src/main/java/androidx/ui/core/InputField.kt b/ui/framework/src/main/java/androidx/ui/core/InputField.kt
index 50950a0..03077b0 100644
--- a/ui/framework/src/main/java/androidx/ui/core/InputField.kt
+++ b/ui/framework/src/main/java/androidx/ui/core/InputField.kt
@@ -91,11 +91,14 @@
val processor = +memo { EditProcessor() }
processor.onNewState(value)
+ val density = +ambient(DensityAmbient)
+
// TODO(nona): Add parameter for text direction, softwrap, etc.
val delegate = InputFieldDelegate(
TextPainter(
text = AnnotatedString(text = value.text),
- style = mergedStyle
+ style = mergedStyle,
+ density = density
),
processor,
onValueChange
diff --git a/ui/framework/src/main/java/androidx/ui/core/Text.kt b/ui/framework/src/main/java/androidx/ui/core/Text.kt
index 72f3fc3..3a950d3 100644
--- a/ui/framework/src/main/java/androidx/ui/core/Text.kt
+++ b/ui/framework/src/main/java/androidx/ui/core/Text.kt
@@ -191,8 +191,12 @@
// TODO(Migration/siyamed): This is temporary and should be removed when resource
// system is resolved.
val context = composer.composer.context
+ val density = +ambientDensity()
+
mergedStyle.fontFamily?.context = context
- text.textStyles.forEach { it.style.fontFamily?.context = context }
+ text.textStyles.forEach {
+ it.style.fontFamily?.context = context
+ }
Semantics(label = text.text) {
val textPainter = +memo(
@@ -202,7 +206,8 @@
softWrap,
overflow,
textScaleFactor,
- maxLines
+ maxLines,
+ density
) {
TextPainter(
text = text,
@@ -211,7 +216,8 @@
softWrap = softWrap,
overflow = overflow,
textScaleFactor = textScaleFactor,
- maxLines = maxLines
+ maxLines = maxLines,
+ density = density
)
}
@@ -239,7 +245,8 @@
softWrap,
overflow,
textScaleFactor,
- maxLines
+ maxLines,
+ density
) {
val id = registrar.subscribe(
object : TextSelectionHandler {
diff --git a/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/LayoutActivity.kt b/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/LayoutActivity.kt
index 5fa9b0b..10d1a12 100644
--- a/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/LayoutActivity.kt
+++ b/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/LayoutActivity.kt
@@ -36,7 +36,7 @@
import androidx.compose.Composable
import androidx.compose.composer
import androidx.compose.setContent
-import androidx.ui.core.Span
+import androidx.ui.core.sp
class LayoutActivity : Activity() {
@@ -72,7 +72,7 @@
mainAxisAlignment = MainAxisAlignment.Start,
crossAxisAlignment = CrossAxisAlignment.Start
) {
- Text(text = "Row", style = TextStyle(fontSize = 48f))
+ Text(text = "Row", style = TextStyle(fontSize = 48.sp))
ContainerWithBackground(width = ExampleSize, color = lightGrey) {
Row {
PurpleSquare()
@@ -108,7 +108,7 @@
}
}
HeightSpacer(height = 24.dp)
- Text(text = "Column", style = TextStyle(fontSize = 48f))
+ Text(text = "Column", style = TextStyle(fontSize = 48.sp))
Row {
ContainerWithBackground(height = ExampleSize, color = lightGrey) {
Column {
diff --git a/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/ScrollerActivity.kt b/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/ScrollerActivity.kt
index ed8f777..6f9607d 100644
--- a/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/ScrollerActivity.kt
+++ b/ui/layout/integration-tests/layout-demos/src/main/java/androidx/ui/layout/demos/ScrollerActivity.kt
@@ -28,6 +28,7 @@
import androidx.ui.painting.TextStyle
import androidx.compose.composer
import androidx.compose.setContent
+import androidx.ui.core.sp
class ScrollerActivity : Activity() {
val phrases = listOf(
@@ -67,7 +68,7 @@
super.onCreate(savedInstanceState)
val density = Density(this)
withDensity(density) {
- val style = TextStyle(fontSize = 30.dp.toPx().value)
+ val style = TextStyle(fontSize = 30.sp)
setContent {
CraneWrapper {
Padding(padding = 10.dp) {
diff --git a/ui/material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt b/ui/material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
index b6ec960..4f76be4 100644
--- a/ui/material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
+++ b/ui/material/integration-tests/material-studies/src/main/java/androidx/ui/material/studies/rally/RallyTheme.kt
@@ -27,6 +27,7 @@
import androidx.compose.Children
import androidx.compose.Composable
import androidx.compose.composer
+import androidx.ui.core.sp
val rallyGreen = Color(0xFF1EB980.toInt())
val rallyDarkGreen = Color(0xFF045D56.toInt())
@@ -45,43 +46,43 @@
val typography = MaterialTypography(
h1 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w100,
- fontSize = 96f),
+ fontSize = 96.sp),
h2 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w100,
- fontSize = 60f),
+ fontSize = 60.sp),
h3 = TextStyle(fontFamily = FontFamily("Eczar"),
fontWeight = FontWeight.w500,
- fontSize = 48f),
+ fontSize = 48.sp),
h4 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w700,
- fontSize = 34f),
+ fontSize = 34.sp),
h5 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w700,
- fontSize = 24f),
+ fontSize = 24.sp),
h6 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w700,
- fontSize = 20f),
+ fontSize = 20.sp),
subtitle1 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w700,
- fontSize = 16f),
+ fontSize = 16.sp),
subtitle2 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w500,
- fontSize = 14f),
+ fontSize = 14.sp),
body1 = TextStyle(fontFamily = FontFamily("Eczar"),
fontWeight = FontWeight.w700,
- fontSize = 16f),
+ fontSize = 16.sp),
body2 = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w200,
- fontSize = 14f),
+ fontSize = 14.sp),
button = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w800,
- fontSize = 14f),
+ fontSize = 14.sp),
caption = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w500,
- fontSize = 12f),
+ fontSize = 12.sp),
overline = TextStyle(fontFamily = FontFamily("RobotoCondensed"),
fontWeight = FontWeight.w500,
- fontSize = 10f)
+ fontSize = 10.sp)
)
MaterialTheme(colors = colors, typography = typography) {
diff --git a/ui/material/src/main/java/androidx/ui/material/MaterialTheme.kt b/ui/material/src/main/java/androidx/ui/material/MaterialTheme.kt
index 6247af1..340c9c3 100644
--- a/ui/material/src/main/java/androidx/ui/material/MaterialTheme.kt
+++ b/ui/material/src/main/java/androidx/ui/material/MaterialTheme.kt
@@ -27,6 +27,7 @@
import androidx.compose.unaryPlus
import androidx.ui.core.CurrentTextStyleProvider
import androidx.ui.core.dp
+import androidx.ui.core.sp
import androidx.ui.core.withDensity
import androidx.ui.engine.text.FontWeight
import androidx.ui.engine.text.font.FontFamily
@@ -165,55 +166,55 @@
val h1: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.w100,
- fontSize = 96f),
+ fontSize = 96.sp),
val h2: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.w100,
- fontSize = 60f),
+ fontSize = 60.sp),
val h3: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 48f),
+ fontSize = 48.sp),
val h4: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 34f),
+ fontSize = 34.sp),
val h5: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 24f),
+ fontSize = 24.sp),
val h6: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.w500,
- fontSize = 20f),
+ fontSize = 20.sp),
val subtitle1: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 16f),
+ fontSize = 16.sp),
val subtitle2: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.w500,
- fontSize = 14f),
+ fontSize = 14.sp),
val body1: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 16f),
+ fontSize = 16.sp),
val body2: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 14f),
+ fontSize = 14.sp),
val button: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.w500,
- fontSize = 14f),
+ fontSize = 14.sp),
val caption: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 12f),
+ fontSize = 12.sp),
val overline: TextStyle = TextStyle(
fontFamily = FontFamily("Roboto"),
fontWeight = FontWeight.normal,
- fontSize = 10f)
+ fontSize = 10.sp)
)
/**
@@ -255,15 +256,7 @@
fun themeTextStyle(
choosingBlock: MaterialTypography.() -> TextStyle
) = effectOf<TextStyle> {
- var style = (+ambient(Typography)).choosingBlock()
-
- // TODO Text is working with pixels, but we define our theme in dps, let's convert here for now.
- // b/127345041
- if (style.fontSize != null) {
- style = style.copy(fontSize = +withDensity { style.fontSize!!.dp.toPx().value })
- }
-
- style
+ (+ambient(Typography)).choosingBlock()
}
// Shapes
diff --git a/ui/text/api/1.0.0-alpha01.txt b/ui/text/api/1.0.0-alpha01.txt
index 318878f..341b1a3 100644
--- a/ui/text/api/1.0.0-alpha01.txt
+++ b/ui/text/api/1.0.0-alpha01.txt
@@ -344,8 +344,8 @@
}
public final class TextPainter {
- ctor public TextPainter(androidx.ui.painting.AnnotatedString? text, androidx.ui.painting.TextStyle? style, androidx.ui.painting.ParagraphStyle? paragraphStyle, float textScaleFactor, Integer? maxLines, boolean softWrap, androidx.ui.rendering.paragraph.TextOverflow overflow, androidx.ui.engine.window.Locale? locale);
- ctor public TextPainter();
+ ctor public TextPainter(androidx.ui.painting.AnnotatedString? text, androidx.ui.painting.TextStyle? style, androidx.ui.painting.ParagraphStyle? paragraphStyle, float textScaleFactor, Integer? maxLines, boolean softWrap, androidx.ui.rendering.paragraph.TextOverflow overflow, androidx.ui.engine.window.Locale? locale, androidx.ui.core.Density density);
+ method public androidx.ui.core.Density getDensity();
method public boolean getDidExceedMaxLines();
method public float getHeight();
method public float getMaxIntrinsicWidth();
@@ -382,7 +382,7 @@
}
public final class TextStyle {
- ctor public TextStyle(androidx.ui.graphics.Color? color, Float? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
+ ctor public TextStyle(androidx.ui.graphics.Color? color, androidx.ui.core.Sp? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
ctor public TextStyle();
method public androidx.ui.graphics.Color? component1();
method public Float? component10();
@@ -392,7 +392,7 @@
method public androidx.ui.graphics.Color? component14();
method public androidx.ui.engine.text.TextDecoration? component15();
method public androidx.ui.painting.Shadow? component16();
- method public Float? component2();
+ method public androidx.ui.core.Sp? component2();
method public Float? component3();
method public androidx.ui.engine.text.FontWeight? component4();
method public androidx.ui.engine.text.FontStyle? component5();
@@ -400,14 +400,14 @@
method public androidx.ui.engine.text.font.FontFamily? component7();
method public String? component8();
method public Float? component9();
- method public androidx.ui.painting.TextStyle copy(androidx.ui.graphics.Color? color, Float? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
+ method public androidx.ui.painting.TextStyle copy(androidx.ui.graphics.Color? color, androidx.ui.core.Sp? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
method public androidx.ui.graphics.Color? getBackground();
method public androidx.ui.engine.text.BaselineShift? getBaselineShift();
method public androidx.ui.graphics.Color? getColor();
method public androidx.ui.engine.text.TextDecoration? getDecoration();
method public androidx.ui.engine.text.font.FontFamily? getFontFamily();
method public String? getFontFeatureSettings();
- method public Float? getFontSize();
+ method public androidx.ui.core.Sp? getFontSize();
method public Float? getFontSizeScale();
method public androidx.ui.engine.text.FontStyle? getFontStyle();
method public androidx.ui.engine.text.FontSynthesis? getFontSynthesis();
diff --git a/ui/text/api/api_lint.ignore b/ui/text/api/api_lint.ignore
index 9b9327e..ac5af9e 100644
--- a/ui/text/api/api_lint.ignore
+++ b/ui/text/api/api_lint.ignore
@@ -45,33 +45,25 @@
Must avoid boxed primitives (`java.lang.Float`)
AutoBoxing: androidx.ui.painting.ParagraphStyle#getLineHeight():
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextPainter#TextPainter(androidx.ui.painting.AnnotatedString, androidx.ui.painting.TextStyle, androidx.ui.painting.ParagraphStyle, float, Integer, boolean, androidx.ui.rendering.paragraph.TextOverflow, androidx.ui.engine.window.Locale) parameter #4:
+AutoBoxing: androidx.ui.painting.TextPainter#TextPainter(androidx.ui.painting.AnnotatedString, androidx.ui.painting.TextStyle, androidx.ui.painting.ParagraphStyle, float, Integer, boolean, androidx.ui.rendering.paragraph.TextOverflow, androidx.ui.engine.window.Locale, androidx.ui.core.Density) parameter #4:
Must avoid boxed primitives (`java.lang.Integer`)
-AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #1:
+AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #2:
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #2:
+AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #8:
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #8:
- Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #9:
+AutoBoxing: androidx.ui.painting.TextStyle#TextStyle(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #9:
Must avoid boxed primitives (`java.lang.Float`)
AutoBoxing: androidx.ui.painting.TextStyle#component10():
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#component2():
- Must avoid boxed primitives (`java.lang.Float`)
AutoBoxing: androidx.ui.painting.TextStyle#component3():
Must avoid boxed primitives (`java.lang.Float`)
AutoBoxing: androidx.ui.painting.TextStyle#component9():
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #1:
+AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #2:
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #2:
+AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #8:
Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #8:
- Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, Float, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #9:
- Must avoid boxed primitives (`java.lang.Float`)
-AutoBoxing: androidx.ui.painting.TextStyle#getFontSize():
+AutoBoxing: androidx.ui.painting.TextStyle#copy(androidx.ui.graphics.Color, androidx.ui.core.Sp, Float, androidx.ui.engine.text.FontWeight, androidx.ui.engine.text.FontStyle, androidx.ui.engine.text.FontSynthesis, androidx.ui.engine.text.font.FontFamily, String, Float, Float, androidx.ui.engine.text.BaselineShift, androidx.ui.engine.text.TextGeometricTransform, androidx.ui.engine.window.Locale, androidx.ui.graphics.Color, androidx.ui.engine.text.TextDecoration, androidx.ui.painting.Shadow) parameter #9:
Must avoid boxed primitives (`java.lang.Float`)
AutoBoxing: androidx.ui.painting.TextStyle#getFontSizeScale():
Must avoid boxed primitives (`java.lang.Float`)
diff --git a/ui/text/api/current.txt b/ui/text/api/current.txt
index 318878f..341b1a3 100644
--- a/ui/text/api/current.txt
+++ b/ui/text/api/current.txt
@@ -344,8 +344,8 @@
}
public final class TextPainter {
- ctor public TextPainter(androidx.ui.painting.AnnotatedString? text, androidx.ui.painting.TextStyle? style, androidx.ui.painting.ParagraphStyle? paragraphStyle, float textScaleFactor, Integer? maxLines, boolean softWrap, androidx.ui.rendering.paragraph.TextOverflow overflow, androidx.ui.engine.window.Locale? locale);
- ctor public TextPainter();
+ ctor public TextPainter(androidx.ui.painting.AnnotatedString? text, androidx.ui.painting.TextStyle? style, androidx.ui.painting.ParagraphStyle? paragraphStyle, float textScaleFactor, Integer? maxLines, boolean softWrap, androidx.ui.rendering.paragraph.TextOverflow overflow, androidx.ui.engine.window.Locale? locale, androidx.ui.core.Density density);
+ method public androidx.ui.core.Density getDensity();
method public boolean getDidExceedMaxLines();
method public float getHeight();
method public float getMaxIntrinsicWidth();
@@ -382,7 +382,7 @@
}
public final class TextStyle {
- ctor public TextStyle(androidx.ui.graphics.Color? color, Float? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
+ ctor public TextStyle(androidx.ui.graphics.Color? color, androidx.ui.core.Sp? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
ctor public TextStyle();
method public androidx.ui.graphics.Color? component1();
method public Float? component10();
@@ -392,7 +392,7 @@
method public androidx.ui.graphics.Color? component14();
method public androidx.ui.engine.text.TextDecoration? component15();
method public androidx.ui.painting.Shadow? component16();
- method public Float? component2();
+ method public androidx.ui.core.Sp? component2();
method public Float? component3();
method public androidx.ui.engine.text.FontWeight? component4();
method public androidx.ui.engine.text.FontStyle? component5();
@@ -400,14 +400,14 @@
method public androidx.ui.engine.text.font.FontFamily? component7();
method public String? component8();
method public Float? component9();
- method public androidx.ui.painting.TextStyle copy(androidx.ui.graphics.Color? color, Float? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
+ method public androidx.ui.painting.TextStyle copy(androidx.ui.graphics.Color? color, androidx.ui.core.Sp? fontSize, Float? fontSizeScale, androidx.ui.engine.text.FontWeight? fontWeight, androidx.ui.engine.text.FontStyle? fontStyle, androidx.ui.engine.text.FontSynthesis? fontSynthesis, androidx.ui.engine.text.font.FontFamily? fontFamily, String? fontFeatureSettings, Float? letterSpacing, Float? wordSpacing, androidx.ui.engine.text.BaselineShift? baselineShift, androidx.ui.engine.text.TextGeometricTransform? textGeometricTransform, androidx.ui.engine.window.Locale? locale, androidx.ui.graphics.Color? background, androidx.ui.engine.text.TextDecoration? decoration, androidx.ui.painting.Shadow? shadow);
method public androidx.ui.graphics.Color? getBackground();
method public androidx.ui.engine.text.BaselineShift? getBaselineShift();
method public androidx.ui.graphics.Color? getColor();
method public androidx.ui.engine.text.TextDecoration? getDecoration();
method public androidx.ui.engine.text.font.FontFamily? getFontFamily();
method public String? getFontFeatureSettings();
- method public Float? getFontSize();
+ method public androidx.ui.core.Sp? getFontSize();
method public Float? getFontSizeScale();
method public androidx.ui.engine.text.FontStyle? getFontStyle();
method public androidx.ui.engine.text.FontSynthesis? getFontSynthesis();
diff --git a/ui/text/integration-tests/text-demos/src/main/java/androidx/ui/text/demos/CraneText.kt b/ui/text/integration-tests/text-demos/src/main/java/androidx/ui/text/demos/CraneText.kt
index 232c441..c07ff24 100644
--- a/ui/text/integration-tests/text-demos/src/main/java/androidx/ui/text/demos/CraneText.kt
+++ b/ui/text/integration-tests/text-demos/src/main/java/androidx/ui/text/demos/CraneText.kt
@@ -46,16 +46,17 @@
import androidx.ui.painting.TextStyle
import androidx.ui.rendering.paragraph.TextOverflow
import androidx.compose.composer
+import androidx.ui.core.Sp
+import androidx.ui.core.sp
val displayText = "Text Demo"
val displayTextChinese = "文本演示"
val displayTextArabic = "عرض النص"
val displayTextHindi = "पाठ डेमो"
-val fontSize4: Float = 40.0.toFloat()
-val fontSize6: Float = 60.0.toFloat()
-val fontSize7: Float = 70.0.toFloat()
-val fontSize8: Float = 80.0.toFloat()
-val fontSize10: Float = 100.0.toFloat()
+val fontSize4: Sp = 16.sp
+val fontSize6: Sp = 20.sp
+val fontSize8: Sp = 25.sp
+val fontSize10: Sp = 30.sp
@Composable
fun TextDemo() {
diff --git a/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt b/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
index 66d5701..7bdb728 100644
--- a/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/engine/text/ParagraphIntegrationTest.kt
@@ -19,7 +19,11 @@
import androidx.test.filters.SdkSuppress
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
+import androidx.ui.core.Density
+import androidx.ui.core.Sp
import androidx.ui.core.px
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.text.FontTestData.Companion.BASIC_KERN_FONT
@@ -53,6 +57,7 @@
private lateinit var fontFamilyKernFont: FontFamily
private lateinit var fontFamilyCustom100: FontFamily
private lateinit var fontFamilyCustom200: FontFamily
+ private val defaultDensity = Density(density = 1f)
@Before
fun setup() {
@@ -76,324 +81,408 @@
@Test
fun empty_string() {
- val fontSize = 50.0f
- val text = ""
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = ""
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = 100.0f))
+ paragraph.layout(ParagraphConstraints(width = 100.0f))
- assertThat(paragraph.width, equalTo(100.0f))
- assertThat(paragraph.height, equalTo(fontSize))
- // defined in sample_font
- assertThat(paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(paragraph.maxIntrinsicWidth, equalTo(0.0f))
- assertThat(paragraph.minIntrinsicWidth, equalTo(0.0f))
- // TODO(Migration/siyamed): no baseline query per line?
- // TODO(Migration/siyamed): no line count?
+ assertThat(paragraph.width, equalTo(100.0f))
+
+ assertThat(paragraph.height, equalTo(fontSizeInPx))
+ // defined in sample_font
+ assertThat(paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(paragraph.maxIntrinsicWidth, equalTo(0.0f))
+ assertThat(paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // TODO(Migration/siyamed): no baseline query per line?
+ // TODO(Migration/siyamed): no line count?
+ }
}
@Test
fun single_line_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("xyz", "\u05D0\u05D1\u05D2")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInpx = fontSize.toPx().value
- // width greater than text width - 150
- paragraph.layout(ParagraphConstraints(width = 200.0f))
+ for (text in arrayOf("xyz", "\u05D0\u05D1\u05D2")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- assertThat(text, paragraph.width, equalTo(200.0f))
- assertThat(text, paragraph.height, equalTo(fontSize))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.length))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // width greater than text width - 150
+ paragraph.layout(ParagraphConstraints(width = 200.0f))
+
+ assertThat(text, paragraph.width, equalTo(200.0f))
+ assertThat(text, paragraph.height, equalTo(fontSizeInpx))
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInpx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInpx * text.length)
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abcdef", "\u05D0\u05D1\u05D2\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
- // 3 chars width
- paragraph.layout(ParagraphConstraints(width = 3 * fontSize))
+ for (text in arrayOf("abcdef", "\u05D0\u05D1\u05D2\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 3 chars
- assertThat(text, paragraph.width, equalTo(3 * fontSize))
- // 2 lines, 1 line gap
- assertThat(text, paragraph.height, equalTo(2 * fontSize + fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.length))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 3 chars width
+ paragraph.layout(ParagraphConstraints(width = 3 * fontSizeInPx))
+
+ // 3 chars
+ assertThat(text, paragraph.width, equalTo(3 * fontSizeInPx))
+ // 2 lines, 1 line gap
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(2 * fontSizeInPx + fontSizeInPx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInPx * text.length)
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun newline_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInpx = fontSize.toPx().value
- // 3 chars width
- paragraph.layout(ParagraphConstraints(width = 3 * fontSize))
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 3 chars
- assertThat(text, paragraph.width, equalTo(3 * fontSize))
- // 2 lines, 1 line gap
- assertThat(text, paragraph.height, equalTo(2 * fontSize + fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.indexOf("\n")))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 3 chars width
+ paragraph.layout(ParagraphConstraints(width = 3 * fontSizeInpx))
+
+ // 3 chars
+
+ assertThat(text, paragraph.width, equalTo(3 * fontSizeInpx))
+ // 2 lines, 1 line gap
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(2 * fontSizeInpx + fontSizeInpx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInpx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInpx * text.indexOf("\n"))
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun newline_and_line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
- // 2 chars width
- paragraph.layout(ParagraphConstraints(width = 2 * fontSize))
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- // 2 chars
- assertThat(text, paragraph.width, equalTo(2 * fontSize))
- // 4 lines, 3 line gaps
- assertThat(text, paragraph.height, equalTo(4 * fontSize + 3 * fontSize / 5.0f))
- // defined in sample_font
- assertThat(text, paragraph.baseline, equalTo(fontSize * 0.8f))
- assertThat(text, paragraph.maxIntrinsicWidth, equalTo(fontSize * text.indexOf("\n")))
- assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ // 2 chars width
+
+ paragraph.layout(ParagraphConstraints(width = 2 * fontSizeInPx))
+
+ // 2 chars
+ assertThat(text, paragraph.width, equalTo(2 * fontSizeInPx))
+ // 4 lines, 3 line gaps
+ assertThat(
+ text,
+ paragraph.height,
+ equalTo(4 * fontSizeInPx + 3 * fontSizeInPx / 5.0f)
+ )
+ // defined in sample_font
+ assertThat(text, paragraph.baseline, equalTo(fontSizeInPx * 0.8f))
+ assertThat(
+ text,
+ paragraph.maxIntrinsicWidth,
+ equalTo(fontSizeInPx * text.indexOf("\n"))
+ )
+ assertThat(text, paragraph.minIntrinsicWidth, equalTo(0.0f))
+ }
}
}
@Test
fun getPositionForOffset_ltr() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
- // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars 0, 1, 2 ...
- for (i in 0..text.length) {
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset does not match",
- position,
- equalTo(i)
- )
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
+ // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars 0, 1, 2 ...
+ for (i in 0..text.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset does not match",
+ position,
+ equalTo(i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_rtl() {
- val text = "\u05D0\u05D1\u05D2"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D1\u05D2"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars .., 2, 1, 0
- for (i in 0..text.length) {
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset does not match",
- position,
- equalTo(text.length - i)
- )
+ // test positions that are 1, fontSize+1, 2fontSize+1 which maps to chars .., 2, 1, 0
+ for (i in 0..text.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset does not match",
+ position,
+ equalTo(text.length - i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_ltr_multiline() {
- val firstLine = "abc"
- val secondLine = "def"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "abc"
+ val secondLine = "def"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
- // which maps to chars 3, 4, 5
- for (i in 0..secondLine.length) {
- val offset = Offset(i * fontSize + 1, fontSize * 1.5f)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset, second line does not match",
- position,
- equalTo(i + firstLine.length)
- )
+ // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
+ // which maps to chars 3, 4, 5
+ for (i in 0..secondLine.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx * 1.5f)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset, second line does not match",
+ position,
+ equalTo(i + firstLine.length)
+ )
+ }
}
}
@Test
fun getPositionForOffset_rtl_multiline() {
- val firstLine = "\u05D0\u05D1\u05D2"
- val secondLine = "\u05D3\u05D4\u05D5"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "\u05D0\u05D1\u05D2"
+ val secondLine = "\u05D3\u05D4\u05D5"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
- // which maps to chars 5, 4, 3
- for (i in 0..secondLine.length) {
- val offset = Offset(i * fontSize + 1, fontSize * 1.5f)
- val position = paragraph.getPositionForOffset(offset)
- assertThat(
- "position at index $i, offset $offset, second line does not match",
- position,
- equalTo(text.length - i)
- )
+ // test positions are 1, fontSize+1, 2fontSize+1 and always on the second line
+ // which maps to chars 5, 4, 3
+ for (i in 0..secondLine.length) {
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx * 1.5f)
+ val position = paragraph.getPositionForOffset(offset)
+ assertThat(
+ "position at index $i, offset $offset, second line does not match",
+ position,
+ equalTo(text.length - i)
+ )
+ }
}
}
@Test
fun getPositionForOffset_ltr_width_outOfBounds() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // greater than width
- var offset = Offset(fontSize * text.length * 2, fontSize / 2)
- var position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(text.length))
+ // greater than width
+ var offset = Offset(fontSizeInPx * text.length * 2, fontSizeInPx / 2)
+ var position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(text.length))
- // negative
- offset = Offset(-1 * fontSize, fontSize / 2)
- position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // negative
+ offset = Offset(-1 * fontSizeInPx, fontSizeInPx / 2)
+ position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
+ }
}
@Test
fun getPositionForOffset_ltr_height_outOfBounds() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- // greater than height
- var offset = Offset(fontSize / 2, fontSize * text.length * 2)
- var position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // greater than height
+ var offset = Offset(fontSizeInPx / 2, fontSizeInPx * text.length * 2)
+ var position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
- // negative
- offset = Offset(fontSize / 2, -1 * fontSize)
- position = paragraph.getPositionForOffset(offset)
- assertThat(position, equalTo(0))
+ // negative
+ offset = Offset(fontSizeInPx / 2, -1 * fontSizeInPx)
+ position = paragraph.getPositionForOffset(offset)
+ assertThat(position, equalTo(0))
+ }
}
@Test
fun getBoundingBoxForTextPosition_ltr_singleLine() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
- // test positions that are 0, 1, 2 ... which maps to chars 0, 1, 2 ...
- for (i in 0..text.length - 1) {
- val box = paragraph.getBoundingBoxForTextPosition(i)
- assertThat(box.left, equalTo(i * fontSize))
- assertThat(box.right, equalTo((i + 1) * fontSize))
- assertThat(box.top, equalTo(0f))
- assertThat(box.bottom, equalTo(fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
+ // test positions that are 0, 1, 2 ... which maps to chars 0, 1, 2 ...
+ for (i in 0..text.length - 1) {
+ val box = paragraph.getBoundingBoxForTextPosition(i)
+ assertThat(box.left, equalTo(i * fontSizeInPx))
+ assertThat(box.right, equalTo((i + 1) * fontSizeInPx))
+ assertThat(box.top, equalTo(0f))
+ assertThat(box.bottom, equalTo(fontSizeInPx))
+ }
}
}
@Test
fun getBoundingBoxForTextPosition_ltr_multiLines() {
- val firstLine = "abc"
- val secondLine = "def"
- val text = firstLine + secondLine
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val firstLine = "abc"
+ val secondLine = "def"
+ val text = firstLine + secondLine
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = firstLine.length * fontSizeInPx))
- // test positions are 3, 4, 5 and always on the second line
- // which maps to chars 3, 4, 5
- for (i in 0..secondLine.length - 1) {
- val textPosition = i + firstLine.length
- val box = paragraph.getBoundingBoxForTextPosition(textPosition)
- assertThat(box.left, equalTo(i * fontSize))
- assertThat(box.right, equalTo((i + 1) * fontSize))
- assertThat(box.top, equalTo(fontSize))
- assertThat(box.bottom, equalTo((2f + 1 / 5f) * fontSize))
+ // test positions are 3, 4, 5 and always on the second line
+ // which maps to chars 3, 4, 5
+ for (i in 0..secondLine.length - 1) {
+ val textPosition = i + firstLine.length
+ val box = paragraph.getBoundingBoxForTextPosition(textPosition)
+ assertThat(box.left, equalTo(i * fontSizeInPx))
+ assertThat(box.right, equalTo((i + 1) * fontSizeInPx))
+ assertThat(box.top, equalTo(fontSizeInPx))
+ assertThat(box.bottom, equalTo((2f + 1 / 5f) * fontSizeInPx))
+ }
}
}
@Test
fun getBoundingBoxForTextPosition_ltr_textPosition_negative() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- val textPosition = -1
- val box = paragraph.getBoundingBoxForTextPosition(textPosition)
- assertThat(box.left, equalTo(0f))
- assertThat(box.right, equalTo(0f))
- assertThat(box.top, equalTo(0f))
- assertThat(box.bottom, equalTo(fontSize))
+ val textPosition = -1
+ val box = paragraph.getBoundingBoxForTextPosition(textPosition)
+ assertThat(box.left, equalTo(0f))
+ assertThat(box.right, equalTo(0f))
+ assertThat(box.top, equalTo(0f))
+ assertThat(box.bottom, equalTo(fontSizeInPx))
+ }
}
@Test(expected = java.lang.IndexOutOfBoundsException::class)
fun getBoundingBoxForTextPosition_ltr_textPosition_larger_than_length_throw_exception() {
- val text = "abc"
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize)
- paragraph.layout(ParagraphConstraints(width = text.length * fontSize))
+ paragraph.layout(ParagraphConstraints(width = text.length * fontSizeInPx))
- val textPosition = text.length + 1
- paragraph.getBoundingBoxForTextPosition(textPosition)
+ val textPosition = text.length + 1
+ paragraph.getBoundingBoxForTextPosition(textPosition)
+ }
}
@Test
fun locale_withCJK_shouldNotDrawSame() {
- val text = "\u82B1"
- val fontSize = 10.0f
- val locales = arrayOf(
- // duplicate ja is on purpose
- Locale(_languageCode = "ja"),
- Locale(_languageCode = "ja"),
- Locale(_languageCode = "zh", _countryCode = "CN"),
- Locale(_languageCode = "zh", _countryCode = "TW")
- )
-
- val bitmaps = locales.map { locale ->
- val paragraph = Paragraph(
- text = text,
- textStyles = listOf(),
- style = TextStyle(
- fontSize = fontSize,
- locale = locale
- ),
- paragraphStyle = ParagraphStyle()
+ withDensity(defaultDensity) {
+ val text = "\u82B1"
+ val fontSize = 10.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val locales = arrayOf(
+ // duplicate ja is on purpose
+ Locale(_languageCode = "ja"),
+ Locale(_languageCode = "ja"),
+ Locale(_languageCode = "zh", _countryCode = "CN"),
+ Locale(_languageCode = "zh", _countryCode = "TW")
)
- // just have 10x font size to have a bitmap
- paragraph.layout(ParagraphConstraints(width = fontSize * 10))
+ val bitmaps = locales.map { locale ->
+ val paragraph = Paragraph(
+ text = text,
+ textStyles = listOf(),
+ style = TextStyle(
+ fontSize = fontSize,
+ locale = locale
+ ),
+ paragraphStyle = ParagraphStyle(),
+ density = defaultDensity
+ )
- paragraph.bitmap()
+ // just have 10x font size to have a bitmap
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx * 10))
+
+ paragraph.bitmap()
+ }
+
+ assertThat(bitmaps[0], equalToBitmap(bitmaps[1]))
+ assertThat(bitmaps[1], not(equalToBitmap(bitmaps[2])))
+ assertThat(bitmaps[1], not(equalToBitmap(bitmaps[3])))
+ // this does not work on API 21
+ // assertThat(bitmaps[2], not(equalToBitmap(bitmaps[3])))
}
-
- assertThat(bitmaps[0], equalToBitmap(bitmaps[1]))
- assertThat(bitmaps[1], not(equalToBitmap(bitmaps[2])))
- assertThat(bitmaps[1], not(equalToBitmap(bitmaps[3])))
- // this does not work on API 21
- // assertThat(bitmaps[2], not(equalToBitmap(bitmaps[3])))
}
@Test
@@ -445,11 +534,10 @@
@Test
fun maxLines_withMaxLineEqualsZero() {
val text = "a\na\na"
- val fontSize = 100.0f
val maxLines = 0
val paragraph = simpleParagraph(
text = text,
- fontSize = fontSize,
+ fontSize = 100.sp,
maxLines = maxLines
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -459,11 +547,10 @@
@Test(expected = java.lang.IllegalArgumentException::class)
fun maxLines_withMaxLineNegative_throwsException() {
val text = "a\na\na"
- val fontSize = 100.0f
val maxLines = -1
val paragraph = simpleParagraph(
text = text,
- fontSize = fontSize,
+ fontSize = 100.sp,
maxLines = maxLines
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -471,49 +558,58 @@
@Test
fun maxLines_withMaxLineSmallerThanTextLines_clipHeight() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val lineCount = text.lines().size
- val maxLines = lineCount
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val lineCount = text.lines().size
+ val maxLines = lineCount
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
fun maxLines_withMaxLineEqualsTextLine() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val maxLines = text.lines().size
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val expectHeight = (maxLines + (maxLines - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val maxLines = text.lines().size
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val expectHeight = (maxLines + (maxLines - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
fun maxLines_withMaxLineGreaterThanTextLines() {
- val text = "a\na\na"
- val fontSize = 100.0f
- val lineCount = text.lines().size
- val maxLines = lineCount + 1
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- maxLines = maxLines
- )
- paragraph.layout(ParagraphConstraints(width = 200f))
- val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSize
- assertThat(paragraph.height, equalTo(expectHeight))
+ withDensity(defaultDensity) {
+ val text = "a\na\na"
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val lineCount = text.lines().size
+ val maxLines = lineCount + 1
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ maxLines = maxLines
+ )
+ paragraph.layout(ParagraphConstraints(width = 200f))
+ val expectHeight = (lineCount + (lineCount - 1) * 0.2f) * fontSizeInPx
+ assertThat(paragraph.height, equalTo(expectHeight))
+ }
}
@Test
@@ -548,22 +644,24 @@
@Test
fun didExceedMaxLines_withMaxLinesSmallerThanTextLines_withLineWrap_returnsTrue() {
- val text = "aa"
- val fontSize = 50.0f
- val maxLines = 1
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val maxLines = 1
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
- // One line can only contain 1 character
- paragraph.layout(ParagraphConstraints(width = fontSize))
- assertThat(paragraph.didExceedMaxLines, equalTo(true))
+ // One line can only contain 1 character
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx))
+ assertThat(paragraph.didExceedMaxLines, equalTo(true))
+ }
}
@Test
fun didExceedMaxLines_withMaxLinesEqualToTextLines_withLineWrap_returnsFalse() {
val text = "a"
- val fontSize = 50.0f
val maxLines = text.lines().size
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ val paragraph = simpleParagraph(text = text, fontSize = 50.sp, maxLines = maxLines)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
assertThat(paragraph.didExceedMaxLines, equalTo(false))
@@ -571,53 +669,131 @@
@Test
fun didExceedMaxLines_withMaxLinesGreaterThanTextLines_withLineWrap_returnsFalse() {
- val text = "aa"
- val maxLines = 3
- val fontSize = 50.0f
- val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val maxLines = 3
+ val fontSize = 50.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(text = text, fontSize = fontSize, maxLines = maxLines)
- // One line can only contain 1 character
- paragraph.layout(ParagraphConstraints(width = fontSize))
- assertThat(paragraph.didExceedMaxLines, equalTo(false))
+ // One line can only contain 1 character
+ paragraph.layout(ParagraphConstraints(width = fontSizeInPx))
+ assertThat(paragraph.didExceedMaxLines, equalTo(false))
+ }
}
@Test
fun textAlign_defaultValue_alignsStart() {
- val textLTR = "aa"
- val textRTL = "\u05D0\u05D0"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val textLTR = "aa"
+ val textRTL = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
- val paragraphLTR = simpleParagraph(
- text = textLTR,
- fontSize = fontSize
- )
- val layoutLTRWidth = (textLTR.length + 2) * fontSize
- paragraphLTR.layout(ParagraphConstraints(width = layoutLTRWidth))
+ val paragraphLTR = simpleParagraph(
+ text = textLTR,
+ fontSize = fontSize
+ )
+ val layoutLTRWidth = (textLTR.length + 2) * fontSizeInPx
+ paragraphLTR.layout(ParagraphConstraints(width = layoutLTRWidth))
- val paragraphRTL = simpleParagraph(
- text = textRTL,
- fontSize = fontSize
- )
- val layoutRTLWidth = (textRTL.length + 2) * fontSize
- paragraphRTL.layout(ParagraphConstraints(width = layoutRTLWidth))
+ val paragraphRTL = simpleParagraph(
+ text = textRTL,
+ fontSize = fontSize
+ )
+ val layoutRTLWidth = (textRTL.length + 2) * fontSizeInPx
+ paragraphRTL.layout(ParagraphConstraints(width = layoutRTLWidth))
- // When textAlign is TextAlign.start, LTR aligns to left, RTL aligns to right.
- assertThat(paragraphLTR.paragraphImpl.getLineLeft(0), equalTo(0.0f))
- assertThat(paragraphRTL.paragraphImpl.getLineRight(0), equalTo(layoutRTLWidth))
+ // When textAlign is TextAlign.start, LTR aligns to left, RTL aligns to right.
+ assertThat(paragraphLTR.paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ assertThat(paragraphRTL.paragraphImpl.getLineRight(0), equalTo(layoutRTLWidth))
+ }
}
@Test
fun textAlign_whenAlignLeft_returnsZeroForGetLineLeft() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
- texts.map { text ->
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Left,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignRight_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Right,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignCenter_textIsCentered() {
+ withDensity(defaultDensity) {
+ val texts = listOf("aa", "\u05D0\u05D0")
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+
+ texts.map { text ->
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Center,
+ fontSize = fontSize
+ )
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val textWidth = text.length * fontSizeInPx
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(
+ paragraphImpl.getLineLeft(0),
+ equalTo(layoutWidth / 2 - textWidth / 2)
+ )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(layoutWidth / 2 + textWidth / 2)
+ )
+ }
+ }
+ }
+
+ @Test
+ fun textAlign_whenAlignStart_withLTR_returnsZeroForGetLineLeft() {
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
+
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Left,
+ textAlign = TextAlign.Start,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
val paragraphImpl = paragraph.paragraphImpl
assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
@@ -625,17 +801,18 @@
}
@Test
- fun textAlign_whenAlignRight_returnsLayoutWidthForGetLineRight() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ fun textAlign_whenAlignEnd_withLTR_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val text = "aa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- texts.map { text ->
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Right,
+ textAlign = TextAlign.End,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
val paragraphImpl = paragraph.paragraphImpl
assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
@@ -643,89 +820,41 @@
}
@Test
- fun textAlign_whenAlignCenter_textIsCentered() {
- val texts = listOf("aa", "\u05D0\u05D0")
- val fontSize = 20.0f
+ fun textAlign_whenAlignStart_withRTL_returnsLayoutWidthForGetLineRight() {
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- texts.map { text ->
val paragraph = simpleParagraph(
text = text,
- textAlign = TextAlign.Center,
+ textAlign = TextAlign.Start,
fontSize = fontSize
)
- val layoutWidth = (text.length + 2) * fontSize
paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val textWidth = text.length * fontSize
val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0),
- equalTo(layoutWidth / 2 - textWidth / 2))
- assertThat(paragraphImpl.getLineRight(0),
- equalTo(layoutWidth / 2 + textWidth / 2))
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
}
}
@Test
- fun textAlign_whenAlignStart_withLTR_returnsZeroForGetLineLeft() {
- val text = "aa"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Start,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
- }
-
- @Test
- fun textAlign_whenAlignEnd_withLTR_returnsLayoutWidthForGetLineRight() {
- val text = "aa"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.End,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- }
-
- @Test
- fun textAlign_whenAlignStart_withRTL_returnsLayoutWidthForGetLineRight() {
- val text = "\u05D0\u05D0"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
-
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Start,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- }
-
- @Test
fun textAlign_whenAlignEnd_withRTL_returnsZeroForGetLineLeft() {
- val text = "\u05D0\u05D0"
- val fontSize = 20.0f
- val layoutWidth = (text.length + 2) * fontSize
+ withDensity(defaultDensity) {
+ val text = "\u05D0\u05D0"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length + 2) * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.End,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.End,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ }
}
@Test
@@ -733,160 +862,188 @@
// We have to test justification above API 28 because of this bug b/68009059, where devices
// before API 28 may have an extra space at the end of line.
fun textAlign_whenAlignJustify_justifies() {
- val text = "a a a"
- val fontSize = 20.0f
- val layoutWidth = ("a a".length + 1) * fontSize
+ withDensity(defaultDensity) {
+ val text = "a a a"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = ("a a".length + 1) * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textAlign = TextAlign.Justify,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
- assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
- // Last line should align start
- assertThat(paragraphImpl.getLineLeft(1), equalTo(0.0f))
+ val paragraph = simpleParagraph(
+ text = text,
+ textAlign = TextAlign.Justify,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ assertThat(paragraphImpl.getLineLeft(0), equalTo(0.0f))
+ assertThat(paragraphImpl.getLineRight(0), equalTo(layoutWidth))
+ // Last line should align start
+ assertThat(paragraphImpl.getLineLeft(1), equalTo(0.0f))
+ }
}
@Test
fun textDirection_whenLTR_dotIsOnRight() {
- val text = "a.."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a.."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textDirection = TextDirection.Ltr,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The offset of the last character in display order.
- val offset = Offset("a.".length * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ textDirection = TextDirection.Ltr,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The offset of the last character in display order.
+ val offset = Offset("a.".length * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(2))
+ }
}
@Test
fun textDirection_whenRTL_dotIsOnLeft() {
- val text = "a.."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a.."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textDirection = TextDirection.Rtl,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The offset of the first character in display order.
- val offset = Offset(fontSize / 2 + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ textDirection = TextDirection.Rtl,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The offset of the first character in display order.
+ val offset = Offset(fontSizeInPx / 2 + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(2))
+ }
}
@Test
fun textDirection_whenDefault_withoutStrongChar_directionIsLTR() {
- val text = "..."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "..."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- for (i in 0..text.length) {
- // The offset of the i-th character in display order.
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(i))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ for (i in 0..text.length) {
+ // The offset of the i-th character in display order.
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(i))
+ }
}
}
@Test
fun textDirection_whenDefault_withFirstStrongCharLTR_directionIsLTR() {
- val text = "a\u05D0."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "a\u05D0."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- for (i in 0 until text.length) {
- // The offset of the i-th character in display order.
- val offset = Offset(i * fontSize + 1, fontSize / 2)
- val charIndex = paragraph.getPositionForOffset(offset = offset)
- assertThat(charIndex, equalTo(i))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ for (i in 0 until text.length) {
+ // The offset of the i-th character in display order.
+ val offset = Offset(i * fontSizeInPx + 1, fontSizeInPx / 2)
+ val charIndex = paragraph.getPositionForOffset(offset = offset)
+ assertThat(charIndex, equalTo(i))
+ }
}
}
@Test
fun textDirection_whenDefault_withFirstStrongCharRTL_directionIsRTL() {
- val text = "\u05D0a."
- val fontSize = 20.0f
- val layoutWidth = text.length * fontSize
+ withDensity(defaultDensity) {
+ val text = "\u05D0a."
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = text.length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- // The first character in display order should be '.'
- val offset = Offset(fontSize / 2 + 1, fontSize / 2)
- val index = paragraph.getPositionForOffset(offset = offset)
- assertThat(index, equalTo(2))
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ // The first character in display order should be '.'
+ val offset = Offset(fontSizeInPx / 2 + 1, fontSizeInPx / 2)
+ val index = paragraph.getPositionForOffset(offset = offset)
+ assertThat(index, equalTo(2))
+ }
}
@Test
fun lineHeight_returnsSameAsGiven() {
- val text = "abcdefgh"
- val fontSize = 20.0f
- // Make the layout 4 lines
- val layoutWidth = text.length * fontSize / 4
- val lineHeight = 1.5f
+ withDensity(defaultDensity) {
+ val text = "abcdefgh"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // Make the layout 4 lines
+ val layoutWidth = text.length * fontSizeInPx / 4
+ val lineHeight = 1.5f
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- lineHeight = lineHeight
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ lineHeight = lineHeight
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(4))
- // TODO(Migration/haoyuchang): Due to bug b/120530738, the height of the first line is
- // wrong in the framework. Will fix it when the lineHeight in TextSpan is implemented.
- for (i in 1 until paragraphImpl.lineCount - 1) {
- val actualHeight = paragraphImpl.getLineHeight(i)
- // In the sample_font.ttf, the height of the line should be
- // fontSize + 0.2f * fontSize(line gap)
- assertThat("line number $i", actualHeight, equalTo(1.2f * fontSize * lineHeight))
+ assertThat(paragraphImpl.lineCount, equalTo(4))
+ // TODO(Migration/haoyuchang): Due to bug b/120530738, the height of the first line is
+ // wrong in the framework. Will fix it when the lineHeight in TextSpan is implemented.
+ for (i in 1 until paragraphImpl.lineCount - 1) {
+ val actualHeight = paragraphImpl.getLineHeight(i)
+ // In the sample_font.ttf, the height of the line should be
+ // fontSize + 0.2f * fontSize(line gap)
+ assertThat(
+ "line number $i",
+ actualHeight,
+ equalTo(1.2f * fontSizeInPx * lineHeight)
+ )
+ }
}
}
@Test
fun lineHeight_hasNoEffectOnLastLine() {
- val text = "abc"
- val fontSize = 20.0f
- val layoutWidth = (text.length - 1) * fontSize
- val lineHeight = 1.5f
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val layoutWidth = (text.length - 1) * fontSizeInPx
+ val lineHeight = 1.5f
- val paragraph = simpleParagraph(
- text = text,
- fontSize = fontSize,
- lineHeight = lineHeight
- )
- paragraph.layout(ParagraphConstraints(width = layoutWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ fontSize = fontSize,
+ lineHeight = lineHeight
+ )
+ paragraph.layout(ParagraphConstraints(width = layoutWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val lastLine = paragraphImpl.lineCount - 1
- // In the sample_font.ttf, the height of the line should be
- // fontSize + 0.2 * fontSize(line gap)
- assertThat(paragraphImpl.getLineHeight(lastLine), equalTo(1.2f * fontSize))
+ val lastLine = paragraphImpl.lineCount - 1
+ // In the sample_font.ttf, the height of the line should be
+ // fontSize + 0.2 * fontSize(line gap)
+ assertThat(paragraphImpl.getLineHeight(lastLine), equalTo(1.2f * fontSizeInPx))
+ }
}
@Test(expected = IllegalArgumentException::class)
@@ -897,680 +1054,774 @@
style = TextStyle(),
paragraphStyle = ParagraphStyle(
lineHeight = -1.0f
- )
+ ),
+ density = defaultDensity
)
}
@Test
fun testAnnotatedString_setFontSizeOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize)
- val paragraphWidth = fontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
+ val paragraphWidth = fontSizeInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(paragraphImpl.getLineWidth(0), equalTo(fontSize * text.length))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(fontSizeInPx * text.length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeOnPartOfText() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyleFontSize = 30.0f
- val textStyle = TextStyle(fontSize = textStyleFontSize)
- val paragraphWidth = textStyleFontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyleFontSize = 30.sp
+ val textStyleFontSizeInPx = textStyleFontSize.toPx().value
+ val textStyle = TextStyle(fontSize = textStyleFontSize)
+ val paragraphWidth = textStyleFontSizeInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedLineRight = "abc".length * textStyleFontSize + "de".length * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedLineRight))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedLineRight =
+ "abc".length * textStyleFontSizeInPx + "de".length * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedLineRight))
+ }
}
@Test
fun testAnnotatedString_seFontSizeTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val fontSizeOverwrite = 30.0f
- val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
- val paragraphWidth = fontSizeOverwrite * text.length
+ val fontSizeOverwrite = 30.sp
+ val fontSizeOverwriteInPx = fontSizeOverwrite.toPx().value
+ val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
+ val paragraphWidth = fontSizeOverwriteInPx * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ )
)
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = "abc".length * fontSizeOverwrite + "de".length * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = "abc".length * fontSizeOverwriteInPx + "de".length * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_fontSizeScale() {
- val text = "abcde"
- val fontSize = 20f
- val fontSizeScale = 0.5f
- val textStyle = TextStyle(fontSizeScale = fontSizeScale)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeScale = 0.5f
+ val textStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleNested() {
- val text = "abcde"
- val fontSize = 20f
- val fontSizeScale = 0.5f
- val textStyle = TextStyle(fontSizeScale = fontSizeScale)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeScale = 0.5f
+ val textStyle = TextStyle(fontSizeScale = fontSizeScale)
- val fontSizeScaleNested = 2f
- val textStyleNested = TextStyle(fontSizeScale = fontSizeScaleNested)
+ val fontSizeScaleNested = 2f
+ val textStyleNested = TextStyle(fontSizeScale = fontSizeScaleNested)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleNested, 0, text.length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleNested, 0, text.length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale * fontSizeScaleNested)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale * fontSizeScaleNested)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeFirst() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale = 0.5f
- val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
+ val fontSizeScale = 0.5f
+ val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeStyle, 0, text.length),
- AnnotatedString.Item(fontSizeScaleStyle, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeScaleStyle, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeSecond() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale = 0.5f
- val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
+ val fontSizeScale = 0.5f
+ val fontSizeScaleStyle = TextStyle(fontSizeScale = fontSizeScale)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeScaleStyle, 0, text.length),
- AnnotatedString.Item(fontSizeStyle, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeScaleStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeStyle, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx)
+ )
+ }
}
@Test
fun testAnnotatedString_fontSizeScaleWithFontSizeNested() {
- val text = "abcde"
- val paragraphFontSize = 20f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val paragraphFontSize = 20.sp
- val fontSize = 30f
- val fontSizeStyle = TextStyle(fontSize = fontSize)
+ val fontSize = 30.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val fontSizeStyle = TextStyle(fontSize = fontSize)
- val fontSizeScale1 = 0.5f
- val fontSizeScaleStyle1 = TextStyle(fontSizeScale = fontSizeScale1)
+ val fontSizeScale1 = 0.5f
+ val fontSizeScaleStyle1 = TextStyle(fontSizeScale = fontSizeScale1)
- val fontSizeScale2 = 2f
- val fontSizeScaleStyle2 = TextStyle(fontSizeScale = fontSizeScale2)
+ val fontSizeScale2 = 2f
+ val fontSizeScaleStyle2 = TextStyle(fontSizeScale = fontSizeScale2)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(fontSizeScaleStyle1, 0, text.length),
- AnnotatedString.Item(fontSizeStyle, 0, text.length),
- AnnotatedString.Item(fontSizeScaleStyle2, 0, text.length)
- ),
- fontSize = paragraphFontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(fontSizeScaleStyle1, 0, text.length),
+ AnnotatedString.Item(fontSizeStyle, 0, text.length),
+ AnnotatedString.Item(fontSizeScaleStyle2, 0, text.length)
+ ),
+ fontSize = paragraphFontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(
- paragraphImpl.getLineRight(0),
- equalTo(text.length * fontSize * fontSizeScale2)
- )
+ assertThat(
+ paragraphImpl.getLineRight(0),
+ equalTo(text.length * fontSizeInPx * fontSizeScale2)
+ )
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraphWidth = fontSize * (1 + letterSpacing) * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length * (1 + letterSpacing))
- )
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length * (1 + letterSpacing))
+ )
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingOnPartText() {
- val text = "abcde"
- val fontSize = 20.0f
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraphWidth = fontSize * (1 + letterSpacing) * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacing) * text.length
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = ("abc".length * letterSpacing + text.length) * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = ("abc".length * letterSpacing + text.length) * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_setLetterSpacingTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 5.0f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
- val letterSpacing = 5.0f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ val letterSpacingOverwrite = 10.0f
+ val textStyleOverwrite = TextStyle(letterSpacing = letterSpacingOverwrite)
+ val paragraphWidth = fontSizeInPx * (1 + letterSpacingOverwrite) * text.length
- val letterSpacingOverwrite = 10.0f
- val textStyleOverwrite = TextStyle(letterSpacing = letterSpacingOverwrite)
- val paragraphWidth = fontSize * (1 + letterSpacingOverwrite) * text.length
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
-
- // Make sure there is only one line, so that we can use getLineRight to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- val expectedWidth = "abc".length * (1 + letterSpacingOverwrite) * fontSize +
- "de".length * (1 + letterSpacing) * fontSize
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Make sure there is only one line, so that we can use getLineRight to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ val expectedWidth = "abc".length * (1 + letterSpacingOverwrite) * fontSizeInPx +
+ "de".length * (1 + letterSpacing) * fontSizeInPx
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingOnWholeText() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "ab cd"
- val fontSize = 20.0f
- val wordSpacing = 5.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
- val paragraphWidth = fontSize * (1 + text.length)
+ withDensity(defaultDensity) {
+ val text = "ab cd"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 5.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing)
+ )
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingOnPartText() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "a b c"
- val fontSize = 20.0f
- val wordSpacing = 5.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
- val paragraphWidth = fontSize * (1 + text.length)
+ withDensity(defaultDensity) {
+ val text = "a b c"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 5.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "a b".length)),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "a b".length)),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing)
+ )
+ }
}
@SdkSuppress(minSdkVersion = 29)
@Test
fun testAnnotatedString_setWordSpacingTwice_lastOneOverwrite() {
if (!BuildCompat.isAtLeastQ()) return
- val text = "a b c"
- val fontSize = 20.0f
+ withDensity(defaultDensity) {
+ val text = "a b c"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val wordSpacing = 2.0f
+ val textStyle = TextStyle(wordSpacing = wordSpacing)
- val wordSpacing = 2.0f
- val textStyle = TextStyle(wordSpacing = wordSpacing)
+ val wordSpacingOverwrite = 5.0f
+ val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite)
+ val paragraphWidth = fontSizeInPx * (1 + text.length)
- val wordSpacingOverwrite = 5.0f
- val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite)
- val paragraphWidth = fontSize * (1 + text.length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "a b".length)
+ ),
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "a b".length)
- ),
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
-
- // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
- assertThat(paragraphImpl.lineCount, equalTo(1))
- // Notice that in this test font, the width of character equals to fontSize.
- assertThat(
- paragraphImpl.getLineWidth(0),
- equalTo(fontSize * text.length + wordSpacing + wordSpacingOverwrite)
- )
+ // Make sure there is only one line, so that we can use getLineWidth to test fontSize.
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ // Notice that in this test font, the width of character equals to fontSize.
+ assertThat(
+ paragraphImpl.getLineWidth(0),
+ equalTo(fontSizeInPx * text.length + wordSpacing + wordSpacingOverwrite)
+ )
+ }
}
@Test
fun textIndent_onSingleLine() {
- val text = "abc"
- val fontSize = 20.0f
- val indent = 20.0f
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- // This offset should point to the first character 'a' if indent is applied.
- // Otherwise this offset will point to the second character 'b'.
- val offset = Offset(indent + 1, fontSize / 2)
- // The position corresponding to the offset should be the first char 'a'.
- assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ // This offset should point to the first character 'a' if indent is applied.
+ // Otherwise this offset will point to the second character 'b'.
+ val offset = Offset(indent + 1, fontSizeInPx / 2)
+ // The position corresponding to the offset should be the first char 'a'.
+ assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ }
}
@Test
fun textIndent_onFirstLine() {
- val text = "abcdef"
- val fontSize = 20.0f
- val indent = 20.0f
- val paragraphWidth = "abcd".length * fontSize
+ withDensity(defaultDensity) {
+ val text = "abcdef"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
+ val paragraphWidth = "abcd".length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(2))
- // This offset should point to the first character of the first line if indent is applied.
- // Otherwise this offset will point to the second character of the second line.
- val offset = Offset(indent + 1, fontSize / 2)
- // The position corresponding to the offset should be the first char 'a'.
- assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ assertThat(paragraphImpl.lineCount, equalTo(2))
+ // This offset should point to the first character of the first line if indent is
+ // applied. Otherwise this offset will point to the second character of the second line.
+ val offset = Offset(indent + 1, fontSizeInPx / 2)
+ // The position corresponding to the offset should be the first char 'a'.
+ assertThat(paragraphImpl.getPositionForOffset(offset), equalTo(0))
+ }
}
@Test
fun textIndent_onRestLine() {
- val text = "abcde"
- val fontSize = 20.0f
- val indent = 20.0f
- val paragraphWidth = "abc".length * fontSize
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val indent = 20.0f
+ val paragraphWidth = "abc".length * fontSizeInPx
- val paragraph = simpleParagraph(
- text = text,
- textIndent = TextIndent(firstLine = 0.px, restLine = indent.px),
- fontSize = fontSize,
- fontFamily = fontFamilyMeasureFont
- )
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraphImpl = paragraph.paragraphImpl
- // This offset should point to the first character of the second line if indent is applied.
- // Otherwise this offset will point to the second character of the second line.
- val offset = Offset(indent + 1, fontSize / 2 + fontSize)
- // The position corresponding to the offset should be the 'd' in the second line.
- assertThat(
- paragraphImpl.getPositionForOffset(offset),
- equalTo("abcd".length - 1)
- )
+ val paragraph = simpleParagraph(
+ text = text,
+ textIndent = TextIndent(firstLine = 0.px, restLine = indent.px),
+ fontSize = fontSize,
+ fontFamily = fontFamilyMeasureFont
+ )
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraphImpl = paragraph.paragraphImpl
+ // This offset should point to the first character of the second line if indent is
+ // applied. Otherwise this offset will point to the second character of the second line.
+ val offset = Offset(indent + 1, fontSizeInPx / 2 + fontSizeInPx)
+ // The position corresponding to the offset should be the 'd' in the second line.
+ assertThat(
+ paragraphImpl.getPositionForOffset(offset),
+ equalTo("abcd".length - 1)
+ )
+ }
}
@Test
fun testAnnotatedString_fontFamily_changesMeasurement() {
- val text = "ad"
- val fontSize = 20.0f
- // custom 100 regular font has b as the wide glyph
- // custom 200 regular font has d as the wide glyph
- val textStyle = TextStyle(fontFamily = fontFamilyCustom200)
- // a is rendered in paragraphStyle font (custom 100), it will not have wide glyph
- // d is rendered in defaultTextStyle font (custom 200), and it will be wide glyph
- val expectedWidth = fontSize + fontSize * 3
+ withDensity(defaultDensity) {
+ val text = "ad"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // custom 100 regular font has b as the wide glyph
+ // custom 200 regular font has d as the wide glyph
+ val textStyle = TextStyle(fontFamily = fontFamilyCustom200)
+ // a is rendered in paragraphStyle font (custom 100), it will not have wide glyph
+ // d is rendered in defaultTextStyle font (custom 200), and it will be wide glyph
+ val expectedWidth = fontSizeInPx + fontSizeInPx * 3
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, "a".length, text.length)
- ),
- fontSize = fontSize,
- fontFamily = fontFamilyCustom100
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, "a".length, text.length)
+ ),
+ fontSize = fontSize,
+ fontFamily = fontFamilyCustom100
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- assertThat(paragraphImpl.lineCount, equalTo(1))
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_fontFeature_turnOffKern() {
- val text = "AaAa"
- val fontSize = 20.0f
- // This fontFeatureSetting turns off the kerning
- val textStyle = TextStyle(fontFeatureSettings = "\"kern\" 0")
+ withDensity(defaultDensity) {
+ val text = "AaAa"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ // This fontFeatureSetting turns off the kerning
+ val textStyle = TextStyle(fontFeatureSettings = "\"kern\" 0")
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, "aA".length)
- ),
- fontSize = fontSize,
- fontFamily = fontFamilyKernFont
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, "aA".length)
+ ),
+ fontSize = fontSize,
+ fontFamily = fontFamilyKernFont
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ val paragraphImpl = paragraph.paragraphImpl
- // Two characters are kerning, so minus 0.4 * fontSize
- val expectedWidth = text.length * fontSize - 0.4f * fontSize
- assertThat(paragraphImpl.lineCount, equalTo(1))
- assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ // Two characters are kerning, so minus 0.4 * fontSize
+ val expectedWidth = text.length * fontSizeInPx - 0.4f * fontSizeInPx
+ assertThat(paragraphImpl.lineCount, equalTo(1))
+ assertThat(paragraphImpl.getLineWidth(0), equalTo(expectedWidth))
+ }
}
@Test
fun testAnnotatedString_shadow() {
- val text = "abcde"
- val fontSize = 20f
- val paragraphWidth = fontSize * text.length
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraphWidth = fontSizeInPx * text.length
- val textStyle = TextStyle(shadow = Shadow(Color(0xFF00FF00.toInt()), Offset(1f, 2f), 3.px))
- val paragraphShadow = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length)
+ val textStyle =
+ TextStyle(shadow = Shadow(Color(0xFF00FF00.toInt()), Offset(1f, 2f), 3.px))
+ val paragraphShadow = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length)
+ )
)
- )
- paragraphShadow.layout(ParagraphConstraints(width = paragraphWidth))
+ paragraphShadow.layout(ParagraphConstraints(width = paragraphWidth))
- val paragraph = simpleParagraph(text = text)
- paragraph.layout(ParagraphConstraints(width = paragraphWidth))
+ val paragraph = simpleParagraph(text = text)
+ paragraph.layout(ParagraphConstraints(width = paragraphWidth))
- assertThat(paragraphShadow.bitmap(), not(equalToBitmap(paragraph.bitmap())))
+ assertThat(paragraphShadow.bitmap(), not(equalToBitmap(paragraph.bitmap())))
+ }
}
@Test
fun testDefaultTextStyle_setColor() {
- val text = "abc"
- // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
- val fontSize = 100f
- val paragraphWidth = fontSize * text.length
- val textStyle = TextStyle(color = Color.Red)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraphWidth = fontSizeInPx * text.length
+ val textStyle = TextStyle(color = Color.Red)
- val paragraphWithoutColor = simpleParagraph(
- text = text,
- fontSize = fontSize
- )
- paragraphWithoutColor.layout(ParagraphConstraints(paragraphWidth))
+ val paragraphWithoutColor = simpleParagraph(
+ text = text,
+ fontSize = fontSize
+ )
+ paragraphWithoutColor.layout(ParagraphConstraints(paragraphWidth))
- val paragraphWithColor = simpleParagraph(
- text = text,
- textStyle = textStyle,
- fontSize = fontSize
- )
- paragraphWithColor.layout(ParagraphConstraints(paragraphWidth))
+ val paragraphWithColor = simpleParagraph(
+ text = text,
+ textStyle = textStyle,
+ fontSize = fontSize
+ )
+ paragraphWithColor.layout(ParagraphConstraints(paragraphWidth))
- assertThat(
- paragraphWithColor.bitmap(),
- not(equalToBitmap(paragraphWithoutColor.bitmap()))
- )
+ assertThat(
+ paragraphWithColor.bitmap(),
+ not(equalToBitmap(paragraphWithoutColor.bitmap()))
+ )
+ }
}
@Test
fun testDefaultTextStyle_setLetterSpacing() {
- val text = "abc"
- // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
- val fontSize = 100f
- val letterSpacing = 1f
- val textStyle = TextStyle(letterSpacing = letterSpacing)
+ withDensity(defaultDensity) {
+ val text = "abc"
+ // FontSize doesn't matter here, but it should be big enough for bitmap comparison.
+ val fontSize = 100.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val letterSpacing = 1f
+ val textStyle = TextStyle(letterSpacing = letterSpacing)
- val paragraph = simpleParagraph(
- text = text,
- textStyle = textStyle,
- fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(Float.MAX_VALUE))
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = textStyle,
+ fontSize = fontSize
+ )
+ paragraph.layout(ParagraphConstraints(Float.MAX_VALUE))
- assertThat(
- paragraph.getLineRight(0),
- equalTo(fontSize * (1 + letterSpacing) * text.length)
- )
+ assertThat(
+ paragraph.getLineRight(0),
+ equalTo(fontSizeInPx * (1 + letterSpacing) * text.length)
+ )
+ }
}
@Test
fun testGetPathForRange_singleLine() {
- // Setup test.
- val text = "abc"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight - fontSize, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
+ Rect(
+ lineLeft,
+ 0f,
+ lineRight - fontSizeInPx,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "ab"
- val actualPath = paragraph.getPathForRange(0, 2)
+ // Select "ab"
+ val actualPath = paragraph.getPathForRange(0, 2)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_multiLines() {
- // Setup test.
- val text = "abc\nabc"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "abc\nabc"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val firstLineLeft = paragraphImpl.getLineLeft(0)
- val secondLineLeft = paragraphImpl.getLineLeft(1)
- val firstLineRight = paragraphImpl.getLineRight(0)
- val secondLineRight = paragraphImpl.getLineRight(1)
- expectedPath.addRect(Rect(firstLineLeft + fontSize, 0f, firstLineRight, fontSize))
- expectedPath.addRect(Rect(
- secondLineLeft,
- fontSize,
- secondLineRight - fontSize,
- paragraph.height))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val firstLineLeft = paragraphImpl.getLineLeft(0)
+ val secondLineLeft = paragraphImpl.getLineLeft(1)
+ val firstLineRight = paragraphImpl.getLineRight(0)
+ val secondLineRight = paragraphImpl.getLineRight(1)
+ expectedPath.addRect(
+ Rect(
+ firstLineLeft + fontSizeInPx,
+ 0f,
+ firstLineRight,
+ fontSizeInPx
+ )
+ )
+ expectedPath.addRect(
+ Rect(
+ secondLineLeft,
+ fontSizeInPx,
+ secondLineRight - fontSizeInPx,
+ paragraph.height
+ )
+ )
- // Run.
- // Select "bc\nab"
- val actualPath = paragraph.getPathForRange(1, 6)
+ // Select "bc\nab"
+ val actualPath = paragraph.getPathForRange(1, 6)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Bidi() {
- // Setup test.
- val textLTR = "Hello"
- val textRTL = "שלום"
- val text = textLTR + textRTL
- val selectionLTRStart = 2
- val selectionRTLEnd = 2
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val textLTR = "Hello"
+ val textRTL = "שלום"
+ val text = textLTR + textRTL
+ val selectionLTRStart = 2
+ val selectionRTLEnd = 2
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
Rect(
- lineLeft + selectionLTRStart * fontSize,
+ lineLeft + selectionLTRStart * fontSizeInPx,
0f,
- lineLeft + textLTR.length * fontSize,
- fontSize))
- expectedPath.addRect(Rect(lineRight - selectionRTLEnd * fontSize, 0f, lineRight, fontSize))
+ lineLeft + textLTR.length * fontSizeInPx,
+ fontSizeInPx
+ )
+ )
+ expectedPath.addRect(
+ Rect(
+ lineRight - selectionRTLEnd * fontSizeInPx,
+ 0f,
+ lineRight,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "llo..של"
- val actualPath =
+ // Select "llo..של"
+ val actualPath =
paragraph.getPathForRange(selectionLTRStart, textLTR.length + selectionRTLEnd)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Start_Equals_End_Returns_Empty_Path() {
val text = "abc"
- val fontSize = 20f
val paragraph = simpleParagraph(
- text = text,
- fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ text = text,
+ fontFamily = fontFamilyMeasureFont,
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1582,11 +1833,10 @@
@Test
fun testGetPathForRange_Empty_Text() {
val text = ""
- val fontSize = 20f
val paragraph = simpleParagraph(
- text = text,
- fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ text = text,
+ fontFamily = fontFamilyMeasureFont,
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1597,171 +1847,177 @@
@Test
fun testGetPathForRange_Surrogate_Pair_Start_Middle_Second_Character_Selected() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E\uD834\uDD1F", only "\uD834\uDD1F" is selected.
- val actualPath = paragraph.getPathForRange(1, text.length)
+ // Try to select "\uDD1E\uD834\uDD1F", only "\uD834\uDD1F" is selected.
+ val actualPath = paragraph.getPathForRange(1, text.length)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Surrogate_Pair_End_Middle_Second_Character_Selected() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E\uD834", actually "\uD834\uDD1F" is selected.
- val actualPath = paragraph.getPathForRange(1, text.length - 1)
+ // Try to select "\uDD1E\uD834", actually "\uD834\uDD1F" is selected.
+ val actualPath = paragraph.getPathForRange(1, text.length - 1)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Surrogate_Pair_Start_Middle_End_Same_Character_Returns_Line_Segment() {
- // Setup test.
- val text = "\uD834\uDD1E\uD834\uDD1F"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\uD834\uDD1E\uD834\uDD1F"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight / 2, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineRight / 2, 0f, lineRight / 2, fontSizeInPx))
- // Run.
- // Try to select "\uDD1E", get vertical line segment after this character.
- val actualPath = paragraph.getPathForRange(1, 2)
+ // Try to select "\uDD1E", get vertical line segment after this character.
+ val actualPath = paragraph.getPathForRange(1, 2)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Emoji_Sequence() {
- // Setup test.
- val text = "\u1F600\u1F603\u1F604\u1F606"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u1F600\u1F603\u1F604\u1F606"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft + fontSize, 0f, lineRight - fontSize, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(
+ Rect(
+ lineLeft + fontSizeInPx,
+ 0f,
+ lineRight - fontSizeInPx,
+ fontSizeInPx
+ )
+ )
- // Run.
- // Select "\u1F603\u1F604"
- val actualPath = paragraph.getPathForRange(1, text.length - 1)
+ // Select "\u1F603\u1F604"
+ val actualPath = paragraph.getPathForRange(1, text.length - 1)
- // Assert.
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Unicode_200D_Return_Line_Segment() {
- // Setup test.
- val text = "\u200D"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u200D"
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSizeInPx))
- // Run.
- val actualPath = paragraph.getPathForRange(0, 1)
+ val actualPath = paragraph.getPathForRange(0, 1)
- // Assert.
- assertThat(lineLeft, equalTo(lineRight))
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ assertThat(lineLeft, equalTo(lineRight))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetPathForRange_Unicode_2066_Return_Line_Segment() {
- // Setup tests.
- val text = "\u2066"
- val fontSize = 20f
- val paragraph = simpleParagraph(
+ withDensity(defaultDensity) {
+ val text = "\u2066"
+ val fontSize = 20f.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
fontSize = fontSize
- )
- paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
- val paragraphImpl = paragraph.paragraphImpl
- val expectedPath = Path()
- val lineLeft = paragraphImpl.getLineLeft(0)
- val lineRight = paragraphImpl.getLineRight(0)
- expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSize))
+ val paragraphImpl = paragraph.paragraphImpl
+ val expectedPath = Path()
+ val lineLeft = paragraphImpl.getLineLeft(0)
+ val lineRight = paragraphImpl.getLineRight(0)
+ expectedPath.addRect(Rect(lineLeft, 0f, lineRight, fontSizeInPx))
- // Run.
- val actualPath = paragraph.getPathForRange(0, 1)
+ val actualPath = paragraph.getPathForRange(0, 1)
- // Assert.
- assertThat(lineLeft, equalTo(lineRight))
- val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
- assertThat(diff, equalTo(Rect.zero))
+ assertThat(lineLeft, equalTo(lineRight))
+ val diff = Path.combine(PathOperation.difference, expectedPath, actualPath).getBounds()
+ assertThat(diff, equalTo(Rect.zero))
+ }
}
@Test
fun testGetWordBoundary() {
val text = "abc def"
- val fontSize = 20f
val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1774,11 +2030,10 @@
@Test
fun testGetWordBoundary_Bidi() {
val text = "abc \u05d0\u05d1\u05d2 def"
- val fontSize = 20f
val paragraph = simpleParagraph(
text = text,
fontFamily = fontFamilyMeasureFont,
- fontSize = fontSize
+ fontSize = 20.sp
)
paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
@@ -1791,18 +2046,46 @@
assertThat(resultHebrew.end, equalTo(text.indexOf('\u05d2') + 1))
}
+ @Test
+ fun test_finalFontSizeChangesWithDensity() {
+ val text = "a"
+ val fontSize = 20.sp
+ val densityMultiplier = 2f
+
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(fontSize = fontSize),
+ density = Density(density = 1f, fontScale = 1f)
+ )
+ paragraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+
+ val doubleFontSizeParagraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(fontSize = fontSize),
+ density = Density(density = 1f, fontScale = densityMultiplier)
+ )
+ doubleFontSizeParagraph.layout(ParagraphConstraints(width = Float.MAX_VALUE))
+
+ assertThat(
+ doubleFontSizeParagraph.maxIntrinsicWidth,
+ equalTo(paragraph.maxIntrinsicWidth * densityMultiplier)
+ )
+ assertThat(doubleFontSizeParagraph.height, equalTo(paragraph.height * densityMultiplier))
+ }
+
private fun simpleParagraph(
text: String = "",
textIndent: TextIndent? = null,
textAlign: TextAlign? = null,
textDirection: TextDirection? = null,
- fontSize: Float? = null,
+ fontSize: Sp? = null,
maxLines: Int? = null,
lineHeight: Float? = null,
textStyles: List<AnnotatedString.Item<TextStyle>> = listOf(),
fontFamily: FontFamily = fontFamilyMeasureFont,
locale: Locale? = null,
- textStyle: TextStyle? = null
+ textStyle: TextStyle? = null,
+ density: Density? = null
): Paragraph {
return Paragraph(
text = text,
@@ -1818,7 +2101,8 @@
textDirection = textDirection,
maxLines = maxLines,
lineHeight = lineHeight
- )
+ ),
+ density = density ?: defaultDensity
)
}
}
diff --git a/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt b/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
index bf4658e..ef72aa4 100644
--- a/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/engine/text/platform/ParagraphAndroidTest.kt
@@ -22,7 +22,10 @@
import androidx.text.style.SkewXSpan
import androidx.text.style.TypefaceSpan
import androidx.text.style.WordSpacingSpan
+import androidx.ui.core.Density
import androidx.ui.core.px
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.text.BaselineShift
import androidx.ui.engine.text.FontStyle
@@ -67,6 +70,7 @@
@SmallTest
class ParagraphAndroidTest {
private lateinit var fontFamily: FontFamily
+ private val defaultDensity = Density(density = 1f)
@Before
fun setup() {
@@ -80,31 +84,33 @@
@Test
fun draw_with_newline_and_line_break_default_values() {
- val fontSize = 50.0f
- for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
- val paragraphAndroid = simpleParagraph(
- text = text,
- textStyle = TextStyle(
- fontSize = fontSize,
- fontFamily = fontFamily
+ withDensity(defaultDensity) {
+ val fontSize = 50.sp
+ for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) {
+ val paragraphAndroid = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(
+ fontSize = fontSize,
+ fontFamily = fontFamily
+ )
)
- )
- // 2 chars width
- paragraphAndroid.layout(width = 2 * fontSize)
+ // 2 chars width
+ paragraphAndroid.layout(width = 2 * fontSize.toPx().value)
- val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
- textPaint.textSize = fontSize
- textPaint.typeface = TypefaceAdapter().create(fontFamily)
+ val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)
+ textPaint.textSize = fontSize.toPx().value
+ textPaint.typeface = TypefaceAdapter().create(fontFamily)
- val staticLayout = StaticLayoutCompat.Builder(
- text,
- textPaint,
- ceil(paragraphAndroid.width).toInt()
- )
- .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt())
- .build()
- assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap()))
+ val staticLayout = StaticLayoutCompat.Builder(
+ text,
+ textPaint,
+ ceil(paragraphAndroid.width).toInt()
+ )
+ .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt())
+ .build()
+ assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap()))
+ }
}
}
@@ -241,60 +247,66 @@
@Test
fun testAnnotatedString_setFontSizeOnWholeText() {
- val text = "abcde"
- val fontSize = 20.0f
- val paragraphWidth = text.length * fontSize
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val paragraphWidth = text.length * fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- paragraph.layout(paragraphWidth)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeOnPartText() {
- val text = "abcde"
- val fontSize = 20.0f
- val paragraphWidth = text.length * fontSize
- val textStyle = TextStyle(fontSize = fontSize)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val paragraphWidth = text.length * fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length))
- )
- paragraph.layout(paragraphWidth)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length))
+ )
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ }
}
@Test
fun testAnnotatedString_setFontSizeTwice_lastOneOverwrite() {
- val text = "abcde"
- val fontSize = 20.0f
- val fontSizeOverwrite = 30.0f
- val paragraphWidth = text.length * fontSizeOverwrite
- val textStyle = TextStyle(fontSize = fontSize)
- val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 20.sp
+ val fontSizeOverwrite = 30.sp
+ val paragraphWidth = text.length * fontSizeOverwrite.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize)
+ val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite)
- val paragraph = simpleParagraph(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(textStyle, 0, text.length),
- AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(textStyle, 0, text.length),
+ AnnotatedString.Item(textStyleOverwrite, 0, "abc".length)
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
- assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
- assertThat(
- paragraph.underlyingText,
- hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length)
- )
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length))
+ assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length))
+ assertThat(
+ paragraph.underlyingText,
+ hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length)
+ )
+ }
}
@Test
@@ -1025,91 +1037,101 @@
@Test
fun testEllipsis_withMaxLineEqualsNull_doesNotEllipsis() {
- val text = "abc"
- val fontSize = 20f
- val paragraphWidth = (text.length - 1) * fontSize
- val paragraph = simpleParagraph(
- text = text,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
- ),
- ellipsis = true
- )
- paragraph.layout(paragraphWidth)
- for (i in 0 until paragraph.lineCount) {
- assertFalse(paragraph.isEllipsisApplied(i))
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 20.sp
+ val paragraphWidth = (text.length - 1) * fontSize.toPx().value
+ val paragraph = simpleParagraph(
+ text = text,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ ),
+ ellipsis = true
+ )
+ paragraph.layout(paragraphWidth)
+ for (i in 0 until paragraph.lineCount) {
+ assertFalse(paragraph.isEllipsisApplied(i))
+ }
}
}
@Test
fun testEllipsis_withMaxLinesLessThanTextLines_doesEllipsis() {
- val text = "abcde"
- val fontSize = 100f
- // Note that on API 21, if the next line only contains 1 character, ellipsis won't work
- val paragraphWidth = (text.length - 1.5f) * fontSize
- val paragraph = simpleParagraph(
- text = text,
- ellipsis = true,
- maxLines = 1,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
+ withDensity(defaultDensity) {
+ val text = "abcde"
+ val fontSize = 100.sp
+ // Note that on API 21, if the next line only contains 1 character, ellipsis won't work
+ val paragraphWidth = (text.length - 1.5f) * fontSize.toPx().value
+ val paragraph = simpleParagraph(
+ text = text,
+ ellipsis = true,
+ maxLines = 1,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- assertTrue(paragraph.isEllipsisApplied(0))
+ assertTrue(paragraph.isEllipsisApplied(0))
+ }
}
@Test
fun testEllipsis_withMaxLinesMoreThanTextLines_doesNotEllipsis() {
- val text = "abc"
- val fontSize = 100f
- val paragraphWidth = (text.length - 1) * fontSize
- val maxLines = ceil(text.length * fontSize / paragraphWidth).toInt()
- val paragraph = simpleParagraph(
- text = text,
- ellipsis = true,
- maxLines = maxLines,
- textStyle = TextStyle(
- fontFamily = fontFamily,
- fontSize = fontSize
+ withDensity(defaultDensity) {
+ val text = "abc"
+ val fontSize = 100.sp
+ val paragraphWidth = (text.length - 1) * fontSize.toPx().value
+ val maxLines = ceil(text.length * fontSize.toPx().value / paragraphWidth).toInt()
+ val paragraph = simpleParagraph(
+ text = text,
+ ellipsis = true,
+ maxLines = maxLines,
+ textStyle = TextStyle(
+ fontFamily = fontFamily,
+ fontSize = fontSize
+ )
)
- )
- paragraph.layout(paragraphWidth)
+ paragraph.layout(paragraphWidth)
- for (i in 0 until paragraph.lineCount) {
- assertFalse(paragraph.isEllipsisApplied(i))
+ for (i in 0 until paragraph.lineCount) {
+ assertFalse(paragraph.isEllipsisApplied(i))
+ }
}
}
@Test
fun testTextStyle_fontSize_appliedOnTextPaint() {
- val fontSize = 100f
- val paragraph = simpleParagraph(
- text = "",
- textStyle = TextStyle(fontSize = fontSize)
- )
- paragraph.layout(0f)
+ withDensity(defaultDensity) {
+ val fontSize = 100.sp
+ val paragraph = simpleParagraph(
+ text = "",
+ textStyle = TextStyle(fontSize = fontSize)
+ )
+ paragraph.layout(0f)
- assertThat(paragraph.textPaint.textSize, equalTo(fontSize))
+ assertThat(paragraph.textPaint.textSize, equalTo(fontSize.toPx().value))
+ }
}
@Test
fun testTextStyle_fontSizeScale_appliedOnTextPaint() {
- val fontSize = 100f
- val fontSizeScale = 2f
- val paragraph = simpleParagraph(
- text = "",
- textStyle = TextStyle(
- fontSize = fontSize,
- fontSizeScale = fontSizeScale
+ withDensity(defaultDensity) {
+ val fontSize = 100.sp
+ val fontSizeScale = 2f
+ val paragraph = simpleParagraph(
+ text = "",
+ textStyle = TextStyle(
+ fontSize = fontSize,
+ fontSizeScale = fontSizeScale
+ )
)
- )
- paragraph.layout(0f)
+ paragraph.layout(0f)
- assertThat(paragraph.textPaint.textSize, equalTo(fontSize * fontSizeScale))
+ assertThat(paragraph.textPaint.textSize, equalTo(fontSize.toPx().value * fontSizeScale))
+ }
}
@Test
@@ -1288,7 +1310,8 @@
textIndent = textIndent,
ellipsis = ellipsis,
maxLines = maxLines
- )
+ ),
+ density = Density(density = 1f)
)
}
}
\ No newline at end of file
diff --git a/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt b/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
index e228eea..2179b8e 100644
--- a/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
+++ b/ui/text/src/androidTest/java/androidx/ui/painting/TextPainterIntegrationTest.kt
@@ -20,7 +20,10 @@
import androidx.test.filters.SmallTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.ui.core.Constraints
+import androidx.ui.core.Density
import androidx.ui.core.ipx
+import androidx.ui.core.sp
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.geometry.Size
@@ -44,6 +47,7 @@
class TextPainterIntegrationTest {
private lateinit var fontFamily: FontFamily
+ private val density = Density(density = 1f)
@Before
fun setup() {
@@ -53,12 +57,14 @@
@Test
fun preferredLineHeight_style_set() {
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val textPainter = TextPainter(style = textStyle)
- val preferredHeight = textPainter.preferredLineHeight
+ withDensity(density) {
+ val fontSize = 20.sp
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textPainter = TextPainter(style = textStyle, density = density)
+ val preferredHeight = textPainter.preferredLineHeight
- assertThat(preferredHeight).isEqualTo(fontSize)
+ assertThat(preferredHeight).isEqualTo(fontSize.toPx().value)
+ }
}
// TODO(Migration/qqd): The default font size should be 14.0 but it returns 15.0. Need further
@@ -75,7 +81,7 @@
@Test
fun minIntrinsicWidth_getter() {
- val fontSize = 20.0f
+ val fontSize = 20.sp
val text = "Hello"
val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
@@ -84,7 +90,8 @@
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
)
textPainter.layout(Constraints())
@@ -94,55 +101,61 @@
@Test
fun maxIntrinsicWidth_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.maxIntrinsicWidth).isEqualTo(fontSize * text.length)
+ assertThat(textPainter.maxIntrinsicWidth).isEqualTo(fontSize.toPx().value * text.length)
+ }
}
@Test
fun width_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints(0.ipx, 200.ipx))
+ textPainter.layout(Constraints(0.ipx, 200.ipx))
- assertThat(textPainter.width).isEqualTo(fontSize * text.length)
+ assertThat(textPainter.width).isEqualTo(fontSize.toPx().value * text.length)
+ }
}
@Test
fun width_getter_with_small_width() {
- val fontSize = 20.0f
val text = "Hello"
val width = 80.ipx
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
)
textPainter.layout(Constraints(maxWidth = width))
@@ -152,41 +165,52 @@
@Test
fun height_getter() {
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val text = "hello"
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val text = "hello"
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.height).isEqualTo(fontSize)
+ assertThat(textPainter.height).isEqualTo(fontSize.toPx().value)
+ }
}
@Test
fun size_getter() {
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
- )
+ withDensity(density) {
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
+ )
- textPainter.layout(Constraints())
+ textPainter.layout(Constraints())
- assertThat(textPainter.size)
- .isEqualTo(Size(width = fontSize * text.length, height = fontSize))
+ assertThat(textPainter.size).isEqualTo(
+ Size(
+ width = fontSizeInPx * text.length,
+ height = fontSizeInPx
+ )
+ )
+ }
}
@Test
@@ -197,7 +221,8 @@
val textPainter = TextPainter(
text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(0.ipx, 200.ipx))
@@ -211,7 +236,8 @@
val annotatedString = AnnotatedString(text = text)
val textPainter = TextPainter(text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(0.ipx, 200.ipx))
@@ -223,7 +249,8 @@
fun layout_build_paragraph() {
val textPainter = TextPainter(
text = AnnotatedString(text = "Hello"),
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints(0.ipx, 20.ipx))
@@ -233,13 +260,12 @@
@Test
fun getPositionForOffset_First_Character() {
- val fontSize = 20.0f
val text = "Hello"
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(
AnnotatedString.Item(
- TextStyle(fontSize = fontSize, fontFamily = fontFamily),
+ TextStyle(fontSize = 20.sp, fontFamily = fontFamily),
0,
text.length
)
@@ -247,7 +273,8 @@
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints())
@@ -258,44 +285,47 @@
@Test
fun getPositionForOffset_other_Character() {
- val fontSize = 20.0f
- val characterIndex = 2 // Start from 0.
- val text = "Hello"
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(
- AnnotatedString.Item(
- TextStyle(fontSize = fontSize, fontFamily = fontFamily),
- 0,
- text.length
+ withDensity(density) {
+ val fontSize = 20.sp
+ val characterIndex = 2 // Start from 0.
+ val text = "Hello"
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(
+ AnnotatedString.Item(
+ TextStyle(fontSize = fontSize, fontFamily = fontFamily),
+ 0,
+ text.length
+ )
)
)
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val selection = textPainter.getPositionForOffset(
- offset = Offset(dx = fontSize * characterIndex + 1f, dy = 0f)
- )
+ val selection = textPainter.getPositionForOffset(
+ offset = Offset(dx = fontSize.toPx().value * characterIndex + 1f, dy = 0f)
+ )
- assertThat(selection).isEqualTo(characterIndex)
+ assertThat(selection).isEqualTo(characterIndex)
+ }
}
@Test
fun hasOverflowShaderFalse() {
- val fontSize = 20.0f
val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
)
val textPainter = TextPainter(
text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints())
@@ -305,12 +335,11 @@
@Test
fun hasOverflowShaderFadeHorizontallyTrue() {
- val fontSize = 20.0f
var text = ""
for (i in 1..15) {
text = text + "Hello World"
}
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
@@ -320,7 +349,8 @@
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
overflow = TextOverflow.Fade,
softWrap = false,
- maxLines = 1
+ maxLines = 1,
+ density = density
)
textPainter.layout(Constraints(maxWidth = 100.ipx))
@@ -330,12 +360,11 @@
@Test
fun hasOverflowShaderFadeVerticallyTrue() {
- val fontSize = 20.0f
var text = ""
for (i in 1..30) {
text = text + "Hello World"
}
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val textStyle = TextStyle(fontSize = 20.sp, fontFamily = fontFamily)
val annotatedString = AnnotatedString(
text = text,
textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
@@ -344,7 +373,8 @@
text = annotatedString,
paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
overflow = TextOverflow.Fade,
- maxLines = 2
+ maxLines = 2,
+ density = density
)
textPainter.layout(Constraints(maxWidth = 100.ipx))
@@ -354,251 +384,268 @@
@Test
fun testBackgroundPaint_paint_wrap_multiLines() {
- // Setup test.
- val fontSize = 20.0f
- val text = "HelloHello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints(maxWidth = 120.ipx))
+ withDensity(density) {
+ // Setup test.
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "HelloHello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints(maxWidth = 120.ipx))
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
- val firstLineLeft = textPainter.paragraph?.getLineLeft(0)
- val secondLineLeft = textPainter.paragraph?.getLineLeft(1)
- val firstLineRight = textPainter.paragraph?.getLineRight(0)
- val secondLineRight = textPainter.paragraph?.getLineRight(1)
- expectedCanvas.drawRect(
- Rect(firstLineLeft!!, 0f, firstLineRight!!, fontSize),
- expectedPaint
- )
- expectedCanvas.drawRect(
- Rect(
- secondLineLeft!!,
- fontSize,
- secondLineRight!!,
- textPainter.paragraph!!.height
- ),
- expectedPaint
- )
+ val firstLineLeft = textPainter.paragraph?.getLineLeft(0)
+ val secondLineLeft = textPainter.paragraph?.getLineLeft(1)
+ val firstLineRight = textPainter.paragraph?.getLineRight(0)
+ val secondLineRight = textPainter.paragraph?.getLineRight(1)
+ expectedCanvas.drawRect(
+ Rect(firstLineLeft!!, 0f, firstLineRight!!, fontSizeInPx),
+ expectedPaint
+ )
+ expectedCanvas.drawRect(
+ Rect(
+ secondLineLeft!!,
+ fontSizeInPx,
+ secondLineRight!!,
+ textPainter.paragraph!!.height
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- // Select all.
- textPainter.paintBackground(
- start = 0,
- end = text.length,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ // Select all.
+ textPainter.paintBackground(
+ start = 0,
+ end = text.length,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert.
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert.
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_default_color() {
- // Setup test.
- val selectionStart = 0
- val selectionEnd = 3
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val selectionStart = 0
+ val selectionEnd = 3
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
- expectedCanvas.drawRect(
- Rect(
- left = 0f,
- top = 0f,
- right = fontSize * (selectionEnd - selectionStart),
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
+ expectedCanvas.drawRect(
+ Rect(
+ left = 0f,
+ top = 0f,
+ right = fontSizeInPx * (selectionEnd - selectionStart),
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionStart,
- end = selectionEnd,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionStart,
+ end = selectionEnd,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_default_color_bidi() {
- // Setup test.
- val textLTR = "Hello"
- // From right to left: שלום
- val textRTL = "\u05e9\u05dc\u05d5\u05dd"
- val text = textLTR + textRTL
- val selectionLTRStart = 2
- val selectionRTLEnd = 2
- val fontSize = 20.0f
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val textPainter = TextPainter(text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val textLTR = "Hello"
+ // From right to left: שלום
+ val textRTL = "\u05e9\u05dc\u05d5\u05dd"
+ val text = textLTR + textRTL
+ val selectionLTRStart = 2
+ val selectionRTLEnd = 2
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- val defaultSelectionColor = Color(0x6633B5E5)
- expectedPaint.color = defaultSelectionColor
- // Select "llo".
- expectedCanvas.drawRect(
- Rect(
- left = fontSize * selectionLTRStart,
- top = 0f,
- right = textLTR.length * fontSize,
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ val defaultSelectionColor = Color(0x6633B5E5)
+ expectedPaint.color = defaultSelectionColor
+ // Select "llo".
+ expectedCanvas.drawRect(
+ Rect(
+ left = fontSizeInPx * selectionLTRStart,
+ top = 0f,
+ right = textLTR.length * fontSizeInPx,
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- // Select "של"
- expectedCanvas.drawRect(
- Rect(
- left = (textLTR.length + textRTL.length - selectionRTLEnd) * fontSize,
- top = 0f,
- right = (textLTR.length + textRTL.length) * fontSize,
- bottom = fontSize
- ),
- expectedPaint
- )
+ // Select "של"
+ expectedCanvas.drawRect(
+ Rect(
+ left = (textLTR.length + textRTL.length - selectionRTLEnd) * fontSizeInPx,
+ top = 0f,
+ right = (textLTR.length + textRTL.length) * fontSizeInPx,
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionLTRStart,
- end = textLTR.length + selectionRTLEnd,
- color = defaultSelectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionLTRStart,
+ end = textLTR.length + selectionRTLEnd,
+ color = defaultSelectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
@Test
fun testBackgroundPaint_paint_with_customized_color() {
- // Setup test.
- val selectionStart = 0
- val selectionEnd = 3
- val fontSize = 20.0f
- val text = "Hello"
- val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
- val annotatedString = AnnotatedString(
- text = text,
- textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
- )
- val selectionColor = Color(0x66AABB33)
- val textPainter = TextPainter(
- text = annotatedString,
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
- )
- textPainter.layout(Constraints())
+ withDensity(density) {
+ // Setup test.
+ val selectionStart = 0
+ val selectionEnd = 3
+ val fontSize = 20.sp
+ val fontSizeInPx = fontSize.toPx().value
+ val text = "Hello"
+ val textStyle = TextStyle(fontSize = fontSize, fontFamily = fontFamily)
+ val annotatedString = AnnotatedString(
+ text = text,
+ textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length))
+ )
+ val selectionColor = Color(0x66AABB33)
+ val textPainter = TextPainter(
+ text = annotatedString,
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
+ )
+ textPainter.layout(Constraints())
- val expectedBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
- val expectedPaint = Paint()
- expectedPaint.color = selectionColor
- expectedCanvas.drawRect(
- Rect(
- left = 0f,
- top = 0f,
- right = fontSize * (selectionEnd - selectionStart),
- bottom = fontSize
- ),
- expectedPaint
- )
+ val expectedBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val expectedCanvas = Canvas(android.graphics.Canvas(expectedBitmap))
+ val expectedPaint = Paint()
+ expectedPaint.color = selectionColor
+ expectedCanvas.drawRect(
+ Rect(
+ left = 0f,
+ top = 0f,
+ right = fontSizeInPx * (selectionEnd - selectionStart),
+ bottom = fontSizeInPx
+ ),
+ expectedPaint
+ )
- val actualBitmap = Bitmap.createBitmap(
- ceil(textPainter.width).toInt(),
- ceil(textPainter.height).toInt(),
- Bitmap.Config.ARGB_8888
- )
- val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
+ val actualBitmap = Bitmap.createBitmap(
+ ceil(textPainter.width).toInt(),
+ ceil(textPainter.height).toInt(),
+ Bitmap.Config.ARGB_8888
+ )
+ val actualCanvas = Canvas(android.graphics.Canvas(actualBitmap))
- // Run.
- textPainter.paintBackground(
- start = selectionStart,
- end = selectionEnd,
- color = selectionColor,
- canvas = actualCanvas,
- offset = Offset.zero
- )
+ // Run.
+ textPainter.paintBackground(
+ start = selectionStart,
+ end = selectionEnd,
+ color = selectionColor,
+ canvas = actualCanvas,
+ offset = Offset.zero
+ )
- // Assert
- Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ // Assert
+ Assert.assertThat(actualBitmap, equalToBitmap(expectedBitmap))
+ }
}
-}
+}
\ No newline at end of file
diff --git a/ui/text/src/main/java/androidx/ui/engine/text/Paragraph.kt b/ui/text/src/main/java/androidx/ui/engine/text/Paragraph.kt
index 5bccfdd..81e6dff 100644
--- a/ui/text/src/main/java/androidx/ui/engine/text/Paragraph.kt
+++ b/ui/text/src/main/java/androidx/ui/engine/text/Paragraph.kt
@@ -15,6 +15,7 @@
*/
package androidx.ui.engine.text
+import androidx.ui.core.Density
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.text.platform.ParagraphAndroid
@@ -36,7 +37,8 @@
private val text: String,
style: TextStyle,
paragraphStyle: ParagraphStyle,
- textStyles: List<AnnotatedString.Item<TextStyle>>
+ textStyles: List<AnnotatedString.Item<TextStyle>>,
+ density: Density
) {
private var needsLayout = true
/** increased visibility for testing **/
@@ -102,7 +104,8 @@
text = text,
style = style,
paragraphStyle = paragraphStyle,
- textStyles = textStyles
+ textStyles = textStyles,
+ density = density
)
}
diff --git a/ui/text/src/main/java/androidx/ui/engine/text/TextStyle.kt b/ui/text/src/main/java/androidx/ui/engine/text/TextStyle.kt
deleted file mode 100644
index 3c2d58d..0000000
--- a/ui/text/src/main/java/androidx/ui/engine/text/TextStyle.kt
+++ /dev/null
@@ -1,70 +0,0 @@
-/*
- * Copyright 2019 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package androidx.ui.engine.text
-
-import androidx.ui.engine.text.font.FontFamily
-import androidx.ui.engine.window.Locale
-import androidx.ui.graphics.Color
-import androidx.ui.painting.Shadow
-
-/**
- * An opaque object that determines the size, position, and rendering of text.
- *
- * Creates a new TextStyle object.
- *
- * @param color The color to use when painting the text. If this is specified, `foreground` must be
- * null.
- * @param decoration The decorations to paint near the text (e.g., an underline).
- * @param fontWeight The typeface thickness to use when painting the text (e.g., bold).
- * @param fontStyle The typeface variant to use when drawing the letters (e.g., italics).
- * @param fontFamily The name of the font to use when painting the text (e.g., Roboto).
- * @param fontSize The size of glyphs (in logical pixels) to use when painting the text.
- * @param fontSizeScale The scale factor of the font size. When [fontSize] is also given in this
- * TextStyle, the final fontSize will be the [fontSize] times this value.
- * Otherwise, the final fontSize will be the current fontSize times this value.
- * @param fontFeatureSettings The advanced typography settings provided by font. The format is the same as the CSS font-feature-settings attribute:
- * https://www.w3.org/TR/css-fonts-3/#font-feature-settings-prop
- * @param letterSpacing The amount of space (in EM) to add between each letter.
- * @param wordSpacing The amount of space (in logical pixels) to add at each sequence of white-space
- * (i.e. between each word). Only works on Android Q and above.
- * @param textBaseline The common baseline that should be aligned between this text span and its
- * parent text span, or, for the root text spans, with the line box.
- * @param baselineShift This parameter specifies how much the baseline is shifted from the current position.
- * @param textGeometricTransform The geometric transformation applied the text.
- * @param locale The locale used to select region-specific glyphs.
- * @param background The background color for the text.
- * @param fontSynthesis Whether to synthesize font weight and/or style when the requested weight or
- * style cannot be found in the provided custom font family.
- * @param shadow The shadow effect applied on the text.
- */
-internal data class TextStyle constructor(
- val color: Color? = null,
- val decoration: TextDecoration? = null,
- val fontWeight: FontWeight? = null,
- val fontStyle: FontStyle? = null,
- val fontFamily: FontFamily? = null,
- val fontSize: Float? = null,
- val fontSizeScale: Float? = null,
- val fontFeatureSettings: String? = null,
- val letterSpacing: Float? = null,
- val wordSpacing: Float? = null,
- val baselineShift: BaselineShift? = null,
- val textGeometricTransform: TextGeometricTransform? = null,
- val locale: Locale? = null,
- val background: Color? = null,
- val fontSynthesis: FontSynthesis? = null,
- val shadow: Shadow? = null
-)
\ No newline at end of file
diff --git a/ui/text/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt b/ui/text/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
index 6dbecf0..2e99110 100644
--- a/ui/text/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
+++ b/ui/text/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
@@ -53,7 +53,10 @@
import androidx.text.style.SkewXSpan
import androidx.text.style.TypefaceSpan
import androidx.text.style.WordSpacingSpan
+import androidx.ui.core.Density
import androidx.ui.core.px
+import androidx.ui.core.toPx
+import androidx.ui.core.withDensity
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.text.FontStyle
@@ -77,7 +80,8 @@
val style: TextStyle,
val paragraphStyle: ParagraphStyle,
val textStyles: List<AnnotatedString.Item<TextStyle>>,
- val typefaceAdapter: TypefaceAdapter = TypefaceAdapter()
+ val typefaceAdapter: TypefaceAdapter = TypefaceAdapter(),
+ val density: Density
) {
@VisibleForTesting
internal val textPaint = TextPaint(android.graphics.Paint.ANTI_ALIAS_FLAG)
@@ -138,12 +142,19 @@
fun layout(width: Float) {
val floorWidth = floor(width)
- val newStyle = style.applyTextStyle(textPaint, typefaceAdapter)
+ val newStyle = style.applyTextStyle(textPaint, typefaceAdapter, density)
val charSequence = applyTextStyle(
- text,
- paragraphStyle.textIndent,
- listOf(AnnotatedString.Item<TextStyle>(newStyle, 0, text.length)) + textStyles
+ text = text,
+ textIndent = paragraphStyle.textIndent,
+ textStyles = listOf(
+ AnnotatedString.Item<TextStyle>(
+ newStyle,
+ 0,
+ text.length
+ )
+ ) + textStyles,
+ density = density
)
val alignment = toLayoutAlign(paragraphStyle.textAlign)
@@ -257,7 +268,7 @@
canvas.translate(-x, -y)
}
- private fun createTypeface(style: androidx.ui.engine.text.TextStyle): Typeface {
+ private fun createTypeface(style: TextStyle): Typeface {
return typefaceAdapter.create(
fontFamily = style.fontFamily,
fontWeight = style.fontWeight ?: FontWeight.normal,
@@ -269,7 +280,8 @@
private fun applyTextStyle(
text: String,
textIndent: TextIndent?,
- textStyles: List<AnnotatedString.Item<TextStyle>>
+ textStyles: List<AnnotatedString.Item<TextStyle>>,
+ density: Density
): CharSequence {
if (textStyles.isEmpty() && textIndent == null) return text
val spannableString = SpannableString(text)
@@ -290,7 +302,7 @@
for (textStyle in textStyles) {
val start = textStyle.start
val end = textStyle.end
- val style = textStyle.style.getTextStyle()
+ val style = textStyle.style
if (start < 0 || start >= text.length || end <= start || end > text.length) continue
@@ -334,12 +346,14 @@
}
style.fontSize?.let {
- spannableString.setSpan(
- AbsoluteSizeSpan(it.roundToInt()),
- start,
- end,
- Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
- )
+ withDensity(density) {
+ spannableString.setSpan(
+ AbsoluteSizeSpan(it.toPx().value.roundToInt()),
+ start,
+ end,
+ Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
+ )
+ }
}
// Be aware that fontSizeScale must be applied after fontSize.
@@ -444,11 +458,14 @@
private fun TextStyle.applyTextStyle(
textPaint: TextPaint,
- typefaceAdapter: TypefaceAdapter
+ typefaceAdapter: TypefaceAdapter,
+ density: Density
): TextStyle {
// TODO(haoyuchang) remove this engine.ParagraphStyle
fontSize?.let {
- textPaint.textSize = it
+ withDensity(density) {
+ textPaint.textSize = it.toPx().value
+ }
}
// fontSizeScale must be applied after fontSize is applied.
@@ -459,7 +476,7 @@
// TODO(siyamed): This default values are problem here. If the user just gives a single font
// in the family, and does not provide any fontWeight, TypefaceAdapter will still get the
// call as FontWeight.normal (which is the default value)
- if (getTextStyle().hasFontAttributes()) {
+ if (hasFontAttributes()) {
textPaint.typeface = typefaceAdapter.create(
fontFamily = fontFamily,
fontWeight = fontWeight ?: FontWeight.normal,
@@ -530,7 +547,7 @@
/**
* Returns true if this [TextStyle] contains any font style attributes set.
*/
-private fun androidx.ui.engine.text.TextStyle.hasFontAttributes(): Boolean {
+private fun TextStyle.hasFontAttributes(): Boolean {
return fontFamily != null || fontStyle != null || fontWeight != null
}
diff --git a/ui/text/src/main/java/androidx/ui/painting/TextPainter.kt b/ui/text/src/main/java/androidx/ui/painting/TextPainter.kt
index ceaeb5a..927f637 100644
--- a/ui/text/src/main/java/androidx/ui/painting/TextPainter.kt
+++ b/ui/text/src/main/java/androidx/ui/painting/TextPainter.kt
@@ -20,10 +20,13 @@
import androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP
import androidx.annotation.VisibleForTesting
import androidx.ui.core.Constraints
+import androidx.ui.core.Density
import androidx.ui.core.IntPxSize
+import androidx.ui.core.Sp
import androidx.ui.core.constrain
import androidx.ui.core.px
import androidx.ui.core.round
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.geometry.Rect
import androidx.ui.engine.geometry.Size
@@ -41,7 +44,7 @@
private val DefaultTextAlign: TextAlign = TextAlign.Start
private val DefaultTextDirection: TextDirection = TextDirection.Ltr
/** The default font size if none is specified. */
-private const val DefaultFontSize: Float = 14.0f
+private val DefaultFontSize: Sp = 14.sp
/**
* Unfortunately, using full precision floating point here causes bad layouts because floating
@@ -108,7 +111,8 @@
maxLines: Int? = null,
softWrap: Boolean = true,
overflow: TextOverflow = TextOverflow.Clip,
- locale: Locale? = null
+ locale: Locale? = null,
+ val density: Density
) {
init {
assert(maxLines == null || maxLines > 0)
@@ -210,7 +214,11 @@
}
private fun createTextStyle(): TextStyle {
- return textStyle.copy(fontSize = (textStyle.fontSize ?: DefaultFontSize) * textScaleFactor)
+ return textStyle.copy(
+ fontSize = (textStyle.fontSize ?: DefaultFontSize).times(
+ textScaleFactor
+ )
+ )
}
internal fun createParagraphStyle(): ParagraphStyle {
@@ -243,7 +251,8 @@
style = createTextStyle(),
// direction doesn't matter, text is just a space
paragraphStyle = createParagraphStyle(),
- textStyles = listOf()
+ textStyles = listOf(),
+ density = density
)
layoutTemplate?.layout(ParagraphConstraints(width = Float.POSITIVE_INFINITY))
}
@@ -359,7 +368,8 @@
text = text!!.text,
style = createTextStyle(),
paragraphStyle = createParagraphStyle(),
- textStyles = text!!.textStyles)
+ textStyles = text!!.textStyles,
+ density = density)
}
lastMinWidth = minWidth
lastMaxWidth = finalMaxWidth
@@ -393,7 +403,8 @@
text = AnnotatedString(text = "\u2026", textStyles = listOf()),
style = textStyle,
paragraphStyle = paragraphStyle,
- textScaleFactor = textScaleFactor
+ textScaleFactor = textScaleFactor,
+ density = density
)
fadeSizePainter.layoutText()
val fadeWidth = fadeSizePainter.paragraph!!.width
diff --git a/ui/text/src/main/java/androidx/ui/painting/TextStyle.kt b/ui/text/src/main/java/androidx/ui/painting/TextStyle.kt
index fc7e7b1..c6ad8035 100644
--- a/ui/text/src/main/java/androidx/ui/painting/TextStyle.kt
+++ b/ui/text/src/main/java/androidx/ui/painting/TextStyle.kt
@@ -16,6 +16,8 @@
package androidx.ui.painting
+import androidx.ui.core.Sp
+import androidx.ui.core.sp
import androidx.ui.engine.text.BaselineShift
import androidx.ui.engine.text.FontStyle
import androidx.ui.engine.text.FontSynthesis
@@ -58,7 +60,7 @@
*/
data class TextStyle(
val color: Color? = null,
- val fontSize: Float? = null,
+ val fontSize: Sp? = null,
val fontSizeScale: Float? = null,
val fontWeight: FontWeight? = null,
val fontStyle: FontStyle? = null,
@@ -74,7 +76,6 @@
val decoration: TextDecoration? = null,
val shadow: Shadow? = null
) {
-
/**
* Returns a new text style that is a combination of this style and the given [other] style.
*
@@ -124,6 +125,13 @@
return lerp(start, end, t)
}
+ private fun lerpSp(a: Sp?, b: Sp?, t: Float, default: Sp = 0f.sp): Sp? {
+ 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
/**
@@ -180,7 +188,7 @@
return TextStyle(
color = lerpColor(a.color, b.color, t),
fontFamily = lerpDiscrete(a.fontFamily, b.fontFamily, t),
- fontSize = lerpFloat(a.fontSize, b.fontSize, t),
+ fontSize = lerpSp(a.fontSize, b.fontSize, t),
fontSizeScale = lerpFloat(a.fontSizeScale, b.fontSizeScale, t, 1f),
fontWeight = FontWeight.lerp(a.fontWeight, b.fontWeight, t),
fontStyle = lerpDiscrete(a.fontStyle, b.fontStyle, t),
@@ -206,28 +214,6 @@
}
}
- /** The style information for text runs, encoded for use by ui. */
- internal fun getTextStyle(textScaleFactor: Float = 1.0f): androidx.ui.engine.text.TextStyle {
- return androidx.ui.engine.text.TextStyle(
- color = color,
- decoration = decoration,
- fontWeight = fontWeight,
- fontStyle = fontStyle,
- fontSynthesis = fontSynthesis,
- fontFeatureSettings = fontFeatureSettings,
- fontFamily = fontFamily,
- fontSize = if (fontSize == null) null else (fontSize * textScaleFactor),
- fontSizeScale = fontSizeScale,
- letterSpacing = letterSpacing,
- wordSpacing = wordSpacing,
- baselineShift = baselineShift,
- textGeometricTransform = textGeometricTransform,
- locale = locale,
- background = background,
- shadow = shadow
- )
- }
-
/**
* Describe the difference between this style and another, in terms of how
* much damage it will make to the rendering.
diff --git a/ui/text/src/test/java/androidx/ui/engine/text/ParagraphTest.kt b/ui/text/src/test/java/androidx/ui/engine/text/ParagraphTest.kt
index f527043..423058d 100644
--- a/ui/text/src/test/java/androidx/ui/engine/text/ParagraphTest.kt
+++ b/ui/text/src/test/java/androidx/ui/engine/text/ParagraphTest.kt
@@ -15,6 +15,7 @@
*/
package androidx.ui.engine.text
+import androidx.ui.core.Density
import androidx.ui.engine.geometry.Offset
import androidx.ui.painting.TextStyle
import com.nhaarman.mockitokotlin2.mock
@@ -125,10 +126,13 @@
}
private fun createParagraph(paragraphStyle: ParagraphStyle): Paragraph {
- return Paragraph(text = "",
+ return Paragraph(
+ text = "",
style = TextStyle(),
paragraphStyle = paragraphStyle,
- textStyles = listOf())
+ textStyles = listOf(),
+ density = Density(density = 1f)
+ )
}
private fun createParagraphStyle(): ParagraphStyle {
diff --git a/ui/text/src/test/java/androidx/ui/painting/TextPainterTest.kt b/ui/text/src/test/java/androidx/ui/painting/TextPainterTest.kt
index 0042fac..f8a005a 100644
--- a/ui/text/src/test/java/androidx/ui/painting/TextPainterTest.kt
+++ b/ui/text/src/test/java/androidx/ui/painting/TextPainterTest.kt
@@ -17,6 +17,8 @@
package androidx.ui.painting
import androidx.ui.core.Constraints
+import androidx.ui.core.Density
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.text.TextAlign
import androidx.ui.engine.text.TextDirection
@@ -30,9 +32,11 @@
@RunWith(JUnit4::class)
class TextPainterTest() {
+ private val density = Density(density = 1f)
+
@Test
fun `constructor with default values`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
assertThat(textPainter.text).isNull()
assertThat(textPainter.textAlign).isEqualTo(TextAlign.Start)
@@ -46,14 +50,17 @@
@Test
fun `constructor with customized text(TextSpan)`() {
val text = AnnotatedString("Hello")
- val textPainter = TextPainter(text = text)
+ val textPainter = TextPainter(text = text, density = density)
assertThat(textPainter.text).isEqualTo(text)
}
@Test
fun `constructor with customized textAlign`() {
- val textPainter = TextPainter(paragraphStyle = ParagraphStyle(textAlign = TextAlign.Left))
+ val textPainter = TextPainter(
+ paragraphStyle = ParagraphStyle(textAlign = TextAlign.Left),
+ density = density
+ )
assertThat(textPainter.textAlign).isEqualTo(TextAlign.Left)
}
@@ -61,7 +68,8 @@
@Test
fun `constructor with customized textDirection`() {
val textPainter = TextPainter(
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Rtl),
+ density = density
)
assertThat(textPainter.textDirection).isEqualTo(TextDirection.Rtl)
@@ -71,7 +79,7 @@
fun `constructor with customized textScaleFactor`() {
val scaleFactor = 2.0f
- val textPainter = TextPainter(textScaleFactor = scaleFactor)
+ val textPainter = TextPainter(textScaleFactor = scaleFactor, density = density)
assertThat(textPainter.textScaleFactor).isEqualTo(scaleFactor)
}
@@ -80,7 +88,7 @@
fun `constructor with customized maxLines`() {
val maxLines = 8
- val textPainter = TextPainter(maxLines = maxLines)
+ val textPainter = TextPainter(maxLines = maxLines, density = density)
assertThat(textPainter.maxLines).isEqualTo(maxLines)
}
@@ -89,7 +97,7 @@
fun `constructor with customized overflow`() {
val overflow = TextOverflow.Ellipsis
- val textPainter = TextPainter(overflow = overflow)
+ val textPainter = TextPainter(overflow = overflow, density = density)
assertThat(textPainter.overflow).isEqualTo(overflow)
}
@@ -98,14 +106,14 @@
fun `constructor with customized locale`() {
val locale = Locale("en", "US")
- val textPainter = TextPainter(locale = locale)
+ val textPainter = TextPainter(locale = locale, density = density)
assertThat(textPainter.locale).isEqualTo(locale)
}
@Test
fun `text setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val text = AnnotatedString(text = "Hello")
textPainter.text = text
@@ -117,7 +125,7 @@
@Test
fun `textAlign setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.textAlign = TextAlign.Left
@@ -128,7 +136,7 @@
@Test
fun `textDirection setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.textDirection = TextDirection.Rtl
@@ -140,7 +148,7 @@
@Test
fun `textScaleFactor setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val scaleFactor = 3.0f
textPainter.textScaleFactor = scaleFactor
@@ -153,7 +161,7 @@
@Test
fun `maxLines setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val maxLines = 5
textPainter.maxLines = maxLines
@@ -165,7 +173,7 @@
@Test
fun `overflow setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val overflow = TextOverflow.Ellipsis
textPainter.overflow = overflow
@@ -177,7 +185,7 @@
@Test
fun `locale setter`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val locale = Locale("en", "US")
textPainter.locale = locale
@@ -203,7 +211,8 @@
textScaleFactor = scaleFactor,
maxLines = maxLines,
overflow = overflow,
- locale = locale
+ locale = locale,
+ density = Density(density = 1f)
)
val paragraphStyle = textPainter.createParagraphStyle()
@@ -216,7 +225,7 @@
@Test
fun `createParagraphStyle with defaultTextDirection`() {
- val fontSize = 15.0f
+ val fontSize = 15.sp
val scaleFactor = 3.0f
val maxLines = 5
val overflow = TextOverflow.Ellipsis
@@ -233,7 +242,8 @@
textScaleFactor = scaleFactor,
maxLines = maxLines,
overflow = overflow,
- locale = locale
+ locale = locale,
+ density = density
)
val paragraphStyle = textPainter.createParagraphStyle()
@@ -261,35 +271,35 @@
@Test(expected = AssertionError::class)
fun `minIntrinsicWidth without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.minIntrinsicWidth
}
@Test(expected = AssertionError::class)
fun `maxIntrinsicWidth without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.maxIntrinsicWidth
}
@Test(expected = AssertionError::class)
fun `width without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.width
}
@Test(expected = AssertionError::class)
fun `height without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.height
}
@Test(expected = AssertionError::class)
fun `size without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
textPainter.size
}
@@ -297,7 +307,8 @@
@Test(expected = AssertionError::class)
fun `layout without text assertion should fail`() {
val textPainter = TextPainter(
- paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr)
+ paragraphStyle = ParagraphStyle(textDirection = TextDirection.Ltr),
+ density = density
)
textPainter.layout(Constraints())
@@ -305,7 +316,7 @@
@Test(expected = AssertionError::class)
fun `paint without layout assertion should fail`() {
- val textPainter = TextPainter()
+ val textPainter = TextPainter(density = density)
val canvas = mock<Canvas>()
textPainter.paint(canvas, Offset(0.0f, 0.0f))
diff --git a/ui/text/src/test/java/androidx/ui/painting/TextSpanTest.kt b/ui/text/src/test/java/androidx/ui/painting/TextSpanTest.kt
index ca20d6e..0ac5f47 100644
--- a/ui/text/src/test/java/androidx/ui/painting/TextSpanTest.kt
+++ b/ui/text/src/test/java/androidx/ui/painting/TextSpanTest.kt
@@ -16,6 +16,7 @@
package androidx.ui.painting
+import androidx.ui.core.sp
import androidx.ui.graphics.Color
import androidx.ui.painting.basictypes.RenderComparison
import com.google.common.truth.Truth.assertThat
@@ -40,7 +41,7 @@
@Test
fun `constructor with customized style`() {
- val textStyle = TextStyle(fontSize = 10.0f, letterSpacing = 1.5f)
+ val textStyle = TextStyle(fontSize = 10.sp, letterSpacing = 1.5f)
val textSpan = TextSpan(style = textStyle)
assertThat(textSpan.style).isEqualTo(textStyle)
@@ -226,7 +227,7 @@
@Test
fun `compareTo with different TextStyle with different fontSize should return LAYOUT`() {
val textStyle1 = TextStyle()
- val textStyle2 = TextStyle(fontSize = 10.0f)
+ val textStyle2 = TextStyle(fontSize = 10.sp)
val textSpan1 = TextSpan(style = textStyle1)
val textSpan2 = TextSpan(style = textStyle2)
@@ -256,7 +257,7 @@
@Test
fun `compareTo with different children with different fontSize should return LAYOUT`() {
val textStyle1 = TextStyle()
- val textStyle2 = TextStyle(fontSize = 10.0f)
+ val textStyle2 = TextStyle(fontSize = 10.sp)
val childTextSpan1 = TextSpan(style = textStyle1)
val childTextSpan2 = TextSpan(style = textStyle2)
val textSpan1 = TextSpan(children = mutableListOf(childTextSpan1))
@@ -291,7 +292,7 @@
@Test
fun `toAnnotatedString with includeRootStyle default value`() {
- val textStyle = TextStyle(fontSize = 10f)
+ val textStyle = TextStyle(fontSize = 10.sp)
val text = "Hello"
val textSpan = TextSpan(style = textStyle, text = text)
val annotatedString = textSpan.toAnnotatedString()
@@ -320,7 +321,7 @@
@Test
fun `toAnnotatedString with nested TextSpan with TextStyle`() {
- val textStyle1 = TextStyle(fontSize = 10f)
+ val textStyle1 = TextStyle(fontSize = 10.sp)
val text1 = "Hello"
val textStyle2 = TextStyle(color = Color.Red)
@@ -363,7 +364,7 @@
textStyles = listOf(Root[0, 17], Leaf1[0, 6], Inner[6, 17], leaf2[6, 12], leaf3[12, 17])
)
*/
- val textStyleRoot = TextStyle(fontSize = 10f)
+ val textStyleRoot = TextStyle(fontSize = 10.sp)
val textStyleLeaf1 = TextStyle(color = Color.Blue)
val text1 = "Lorem "
diff --git a/ui/text/src/test/java/androidx/ui/painting/TextStyleTest.kt b/ui/text/src/test/java/androidx/ui/painting/TextStyleTest.kt
index c83dea8..6360348 100644
--- a/ui/text/src/test/java/androidx/ui/painting/TextStyleTest.kt
+++ b/ui/text/src/test/java/androidx/ui/painting/TextStyleTest.kt
@@ -17,6 +17,7 @@
package androidx.ui.painting
import androidx.ui.core.px
+import androidx.ui.core.sp
import androidx.ui.engine.geometry.Offset
import androidx.ui.engine.text.BaselineShift
import androidx.ui.engine.text.FontStyle
@@ -64,7 +65,7 @@
@Test
fun `constructor with customized fontSize`() {
- val fontSize = 18.0f
+ val fontSize = 18.sp
val textStyle = TextStyle(fontSize = fontSize)
@@ -209,7 +210,7 @@
@Test
fun `merge with other's fontSize is null should use this' fontSize`() {
- val fontSize = 3.5f
+ val fontSize = 3.5.sp
val textStyle = TextStyle(fontSize = fontSize)
val otherTextStyle = TextStyle()
@@ -220,8 +221,8 @@
@Test
fun `merge with other's fontSize is set should use other's fontSize`() {
- val fontSize = 3.5f
- val otherFontSize = 8.7f
+ val fontSize = 3.5.sp
+ val otherFontSize = 8.7.sp
val textStyle = TextStyle(fontSize = fontSize)
val otherTextStyle = TextStyle(fontSize = otherFontSize)
@@ -622,7 +623,7 @@
@Test
fun `lerp fontSize with a is Null and t is smaller than half`() {
- val fontSize = 8.0f
+ val fontSize = 8.sp
val t = 0.3f
val textStyle = TextStyle(fontSize = fontSize)
@@ -633,7 +634,7 @@
@Test
fun `lerp fontSize with a is Null and t is larger than half`() {
- val fontSize = 8.0f
+ val fontSize = 8.sp
val t = 0.8f
val textStyle = TextStyle(fontSize = fontSize)
@@ -644,7 +645,7 @@
@Test
fun `lerp fontSize with b is Null and t is smaller than half`() {
- val fontSize = 8.0f
+ val fontSize = 8.sp
val t = 0.3f
val textStyle = TextStyle(fontSize = fontSize)
@@ -655,7 +656,7 @@
@Test
fun `lerp fontSize with b is Null and t is larger than half`() {
- val fontSize = 8.0f
+ val fontSize = 8.sp
val t = 0.8f
val textStyle = TextStyle(fontSize = fontSize)
@@ -666,8 +667,8 @@
@Test
fun `lerp fontSize with a and b are not Null`() {
- val fontSize1 = 8.0f
- val fontSize2 = 16.0f
+ val fontSize1 = 8.sp
+ val fontSize2 = 16.sp
val t = 0.8f
val textStyle1 = TextStyle(fontSize = fontSize1)
val textStyle2 = TextStyle(fontSize = fontSize2)
@@ -675,7 +676,7 @@
val newTextStyle = TextStyle.lerp(a = textStyle1, b = textStyle2, t = t)
// a + (b - a) * t = 8.0f + (16.0f - 8.0f) * 0.8f = 14.4f
- assertThat(newTextStyle?.fontSize).isEqualTo(14.4f)
+ assertThat(newTextStyle?.fontSize).isEqualTo(14.4.sp)
}
@Test
@@ -1422,42 +1423,6 @@
}
@Test
- fun `getTextStyle`() {
- val fontSize = 10.0f
- val color = Color(0xFF00FF00.toInt())
- val fontSynthesis = FontSynthesis.Style
- val fontFeatureSettings = "\"kern\" 0"
- val baselineShift = BaselineShift.Superscript
- val textStyle = TextStyle(
- fontSize = fontSize,
- fontWeight = FontWeight.w800,
- color = color,
- fontSynthesis = fontSynthesis,
- fontFeatureSettings = fontFeatureSettings,
- baselineShift = baselineShift
- )
-
- assertThat(textStyle.fontFamily).isNull()
- assertThat(textStyle.fontSize).isEqualTo(fontSize)
- assertThat(textStyle.fontWeight).isEqualTo(FontWeight.w800)
- assertThat(textStyle.color).isEqualTo(color)
- assertThat(textStyle.fontFeatureSettings).isEqualTo(fontFeatureSettings)
-
- val newTextStyle = textStyle.getTextStyle()
-
- assertThat(newTextStyle).isEqualTo(
- androidx.ui.engine.text.TextStyle(
- color = color,
- fontWeight = FontWeight.w800,
- fontSize = fontSize,
- fontSynthesis = fontSynthesis,
- fontFeatureSettings = fontFeatureSettings,
- baselineShift = baselineShift
- )
- )
- }
-
- @Test
fun `compareTo with same textStyle returns IDENTICAL`() {
val textStyle = TextStyle()
@@ -1474,7 +1439,7 @@
@Test
fun `compareTo textStyle with different layout returns LAYOUT`() {
- val fontSize = 10.0f
+ val fontSize = 10.sp
val bgColor = Color(0xFFFFFF00.toInt())
val fontFeatureSettings = "\"kern\" 0"
@@ -1501,7 +1466,7 @@
)
).isEqualTo(RenderComparison.LAYOUT)
- assertThat(textStyle.compareTo(textStyle.copy(fontSize = 20.0f)))
+ assertThat(textStyle.compareTo(textStyle.copy(fontSize = 20.sp)))
.isEqualTo(RenderComparison.LAYOUT)
assertThat(textStyle.compareTo(textStyle.copy(fontWeight = FontWeight.w100)))
@@ -1535,7 +1500,7 @@
@Test
fun `compareTo textStyle with different paint returns paint`() {
- val fontSize = 10.0f
+ val fontSize = 10.sp
val color1 = Color(0xFF00FF00.toInt())
val color2 = Color(0x00FFFF00)