Merge "Fix paragraph.getLineForVertical" into androidx-main
diff --git a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/TabTest.kt b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/TabTest.kt
index 47dd16b..6094e85 100644
--- a/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/TabTest.kt
+++ b/compose/material3/material3/src/androidAndroidTest/kotlin/androidx/compose/material3/TabTest.kt
@@ -65,6 +65,7 @@
 import androidx.compose.ui.unit.Density
 import androidx.compose.ui.unit.dp
 import androidx.compose.ui.unit.height
+import androidx.compose.ui.unit.sp
 import androidx.compose.ui.unit.width
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import androidx.test.filters.LargeTest
@@ -851,23 +852,19 @@
         rule
             .setMaterialContentForSizeAssertions {
                 CompositionLocalProvider(
-                    LocalDensity provides
-                        Density(
-                            density = LocalDensity.current.density,
-                            fontScale = 2.0f
-                        )
+                    LocalDensity provides Density(density = 1f, fontScale = 10f)
                 ) {
                     Surface {
                         Tab(
                             selected = true,
                             onClick = {},
-                            text = { Text("Text") },
+                            text = { Text(text = "Text", fontSize = 10.sp) },
                             icon = { Icon(icon, null) }
                         )
                     }
                 }
             }
-            .assertHeightIsAtLeast(100.dp)
+            .assertHeightIsAtLeast(90.dp)
     }
 
     @Test
diff --git a/compose/ui/ui-text/src/androidAndroidTest/kotlin/androidx/compose/ui/text/ParagraphIntegrationTest.kt b/compose/ui/ui-text/src/androidAndroidTest/kotlin/androidx/compose/ui/text/ParagraphIntegrationTest.kt
index 1dba872..1220a5b 100644
--- a/compose/ui/ui-text/src/androidAndroidTest/kotlin/androidx/compose/ui/text/ParagraphIntegrationTest.kt
+++ b/compose/ui/ui-text/src/androidAndroidTest/kotlin/androidx/compose/ui/text/ParagraphIntegrationTest.kt
@@ -459,6 +459,78 @@
     }
 
     @Test
+    fun getLineForVerticalPosition_ltr_lineTopCenterBottom_paddingFalse() {
+        val text = "ab\ncde\n\nfg"
+        // default density for the pixel 2 XL where test fails.
+        val density = Density(3.5f, 1.0f)
+        // font size where test fails
+        val fontSize = 14.sp
+
+        @Suppress("DEPRECATION") val paragraph = simpleParagraph(
+            text = text,
+            style = TextStyle(
+                fontSize = fontSize,
+                platformStyle = PlatformTextStyle(includeFontPadding = false)
+            ),
+            density = density
+        )
+
+        assertThat(paragraph.lineCount).isEqualTo(4)
+
+        for (index in 0 until paragraph.lineCount) {
+            assertThat(
+                paragraph.getLineForVerticalPosition(paragraph.getLineTop(index))
+            ).isEqualTo(index)
+
+            assertThat(
+                paragraph.getLineForVerticalPosition(
+                    (paragraph.getLineTop(index) + paragraph.getLineBottom(index)) / 2f
+                )
+            ).isEqualTo(index)
+
+            assertThat(
+                paragraph.getLineForVerticalPosition(paragraph.getLineBottom(index) - 1f)
+            ).isEqualTo(index)
+        }
+    }
+
+    @Test
+    fun getLineForVerticalPosition_ltr_lineTopCenterBottom_paddingTrue() {
+        val text = "ab\ncde\n\nfg"
+        // default density for the pixel 2 XL where test fails.
+        val density = Density(3.5f, 1.0f)
+        // font size where test fails
+        val fontSize = 14.sp
+
+        @Suppress("DEPRECATION") val paragraph = simpleParagraph(
+            text = text,
+            style = TextStyle(
+                fontSize = fontSize,
+                platformStyle = PlatformTextStyle(includeFontPadding = true)
+            ),
+            density = density
+        )
+
+        assertThat(paragraph.lineCount).isEqualTo(4)
+
+        for (index in 0 until paragraph.lineCount) {
+            assertThat(
+                paragraph.getLineForVerticalPosition(paragraph.getLineTop(index))
+            ).isEqualTo(index)
+
+            assertThat(
+                paragraph.getLineForVerticalPosition(
+                    (paragraph.getLineTop(index) + paragraph.getLineBottom(index)) / 2f
+                )
+            ).isEqualTo(index)
+
+            assertThat(
+                paragraph.getLineForVerticalPosition(paragraph.getLineBottom(index) - 1f)
+            ).isEqualTo(index)
+        }
+    }
+
+    @Test
     fun getBoundingBox_ltr_singleLine() {
         with(defaultDensity) {
             val text = "abc"
diff --git a/text/text/src/main/java/androidx/compose/ui/text/android/TextLayout.kt b/text/text/src/main/java/androidx/compose/ui/text/android/TextLayout.kt
index f581e35..cb51b971 100644
--- a/text/text/src/main/java/androidx/compose/ui/text/android/TextLayout.kt
+++ b/text/text/src/main/java/androidx/compose/ui/text/android/TextLayout.kt
@@ -490,7 +490,7 @@
 
     fun getLineEllipsisCount(lineIndex: Int): Int = layout.getEllipsisCount(lineIndex)
 
-    fun getLineForVertical(vertical: Int): Int = layout.getLineForVertical(topPadding + vertical)
+    fun getLineForVertical(vertical: Int): Int = layout.getLineForVertical(vertical - topPadding)
 
     fun getOffsetForHorizontal(line: Int, horizontal: Float): Int {
         return layout.getOffsetForHorizontal(line, horizontal + -1 * getHorizontalPadding(line))