Modern Apps git

← Modern-Apps.git

wordmaker: size wheel and grid letters from dp so 200% font fits (#ux-audit)

Commit
32fa32561f476a58207cc38c2387de955c3caf8e
Author
Vayun Mathur <[email protected]>
When
2026-11-13 01:19 UTC (23 days ago)
Parents
34947cc
Symptom: with a large system font (200%) the letters on the wheel and
in the crossword grid were cut off by their tiles.

Root cause: the glyphs were sized in sp inside fixed-size dp tiles -
42 sp in a 70 dp wheel tile and 18 sp in a 35 dp grid cell. sp scales
with the user's font size while the tiles do not, so at 2x the glyph
line height exceeded the tile and was clipped. The in-flight letter
copies (WordGameOverlays) used the same 18 sp.

Fix: the letters are part of the tile graphics, so they are sized
from dp: the wheel glyph is 60% of the (measured, possibly shrunk)
tile, i.e. 42 dp at full size; grid cells and the flying letters use
18 dp, scaled with the board zoom as before. Their line height is
1.2 em (SurfaceText gained a lineHeight parameter): the theme's body
line height is in sp and alone overflowed the 35 dp grid cells at 2x.
Text that is not a tile graphic (the formed-word box, dialogs, status
bars) keeps sp and still follows the user's font size.

User-visible effect: wheel and grid letters stay whole at any font
scale; at 100% font they look exactly as before (1 sp == 1 dp).

Verification: new LargeFontTest (Robolectric, native graphics for
real text metrics) renders the wheel and the grid with fontScale 2
and asserts no letter's text layout overflows its tile.

Diff

games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameOverlays.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameOverlays.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameOverlays.ktindex 7f955b5d41..10f32711d3 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameOverlays.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/WordGameOverlays.kt@@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.lerp import androidx.compose.ui.graphics.graphicsLayer+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.IntOffset@@ -25,6 +26,7 @@ import com.vayunmathur.games.wordmaker.data.CrosswordData import com.vayunmathur.games.wordmaker.platform.WordGameActions import com.vayunmathur.games.wordmaker.ui.components.AnimatedLetter import com.vayunmathur.games.wordmaker.ui.components.SurfaceText+import com.vayunmathur.games.wordmaker.ui.components.TILE_GLYPH_LINE_HEIGHT import com.vayunmathur.games.wordmaker.ui.dialogs.BonusWordsDialog import com.vayunmathur.games.wordmaker.ui.dialogs.DefinitionDialog import com.vayunmathur.library.ui.AlertDialog@@ -113,7 +115,9 @@ fun WordGameOverlays(                     )             )         }-        val (size, fontSize) = Pair(35.dp * scale, 18.sp * scale)+        // Matches the crossword cell it lands in (dp-sized there too, so large fonts do not clip).+        val size = 35.dp * scale+        val fontSize = with(LocalDensity.current) { (18.dp * scale).toSp() }         animatedLetters.forEach { letter ->             val progress = letter.progress.value             val offset = lerp(letter.startOffset, letter.endOffset, progress)@@ -122,7 +126,8 @@ fun WordGameOverlays(                 RoundedCornerShape(4.dp * scale),                 MaterialTheme.colorScheme.primary, letter.char.toString(),                 Modifier, FontWeight.Bold, fontSize, size,-                textColor = MaterialTheme.colorScheme.onPrimary)+                textColor = MaterialTheme.colorScheme.onPrimary,+                lineHeight = TILE_GLYPH_LINE_HEIGHT)         }     } }
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/CrosswordBoard.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/CrosswordBoard.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/CrosswordBoard.ktindex ed175a82a2..00520e4433 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/CrosswordBoard.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/CrosswordBoard.kt@@ -28,9 +28,9 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.RectangleShape import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.layout.onGloballyPositioned+import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp-import androidx.compose.ui.unit.sp import com.vayunmathur.games.wordmaker.data.CrosswordData  @Composable@@ -55,7 +55,10 @@ fun CrosswordBoard(             }         }     }-    val (size, fontSize) = Pair(35.dp, 18.sp)+    // The board is a fixed-size graphic that pinch-zoom scales as a whole, so the letters are+    // sized in dp: 18 sp clipped inside the 35 dp cells at large font scales.+    val size = CELL_SIZE+    val fontSize = with(LocalDensity.current) { CELL_GLYPH.toSp() }      BoxWithConstraints(         modifier = Modifier@@ -127,7 +130,8 @@ fun CrosswordBoard(                                     letter?.toString() ?: " ",                                     Modifier,                                     FontWeight.Bold, fontSize, size,-                                    textColor = if (letter != null) MaterialTheme.colorScheme.onPrimary else Color.Unspecified+                                    textColor = if (letter != null) MaterialTheme.colorScheme.onPrimary else Color.Unspecified,+                                    lineHeight = TILE_GLYPH_LINE_HEIGHT,                                 )                             } else {                                 Box(Modifier.padding(1.dp).size(size))@@ -139,3 +143,9 @@ fun CrosswordBoard(         }     } }++/** A crossword cell (excluding its 1 dp margin). */+private val CELL_SIZE = 35.dp++/** A cell's letter. */+private val CELL_GLYPH = 18.dp
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.ktindex 85a22d0b22..b57746200c 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/LetterChooser.kt@@ -171,6 +171,9 @@ fun LetterChooser(                     widthPx, heightPx, with(density) { wheelSpacing.ringRadius.toPx() }, tilePx / 2, letters.size                 )                 val tileSize = with(density) { tilePx.toDp() }+                // Sized from the tile, not in sp: the glyph is part of the tile graphic, and at+                // 200% font a 42 sp letter no longer fit its 70 dp tile and was clipped.+                val glyphSize = with(density) { (tilePx * LETTER_GLYPH_FRACTION).toSp() }                 val radiusDp = with(density) { geometry.ringRadius.toDp() }                 // The gesture coroutine outlives recompositions; it reads the geometry through                 // updated state, so a size or spacing change mid-puzzle is hit-tested at the new@@ -259,8 +262,9 @@ fun LetterChooser(                                 chooserLetter.char.toString(),                                 Modifier.padding(1.dp),                                 FontWeight.Bold,-                                42.sp,-                                tileSize+                                glyphSize,+                                tileSize,+                                lineHeight = TILE_GLYPH_LINE_HEIGHT,                             )                         }                     }@@ -298,3 +302,6 @@ private val SIDE_COLUMN_WIDTH = 56.dp  /** A letter tile at full size. */ private val LETTER_TILE = 70.dp++/** Letter glyph size as a fraction of its tile (42 at the full 70). */+private const val LETTER_GLYPH_FRACTION = 0.6f
games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/WordComponents.kt
diff --git a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/WordComponents.kt b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/WordComponents.ktindex 4f4cf95f34..dc8df4487a 100644--- a/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/WordComponents.kt+++ b/games/wordmaker/src/main/java/com/vayunmathur/games/wordmaker/ui/components/WordComponents.kt@@ -21,6 +21,7 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.TextUnit import androidx.compose.ui.unit.dp+import androidx.compose.ui.unit.em import androidx.compose.ui.unit.sp  data class AnimatedLetter(@@ -32,13 +33,20 @@ data class AnimatedLetter(  data class WordToAnimate(val word: String, val letterIds: List<Int>) +/**+ * Line height for a letter drawn on a tile, relative to its glyph. The theme's body line height+ * is in sp and grows with the user's font size on its own, which overflows a dp-sized tile at+ * large font scales even when the glyph itself is dp-sized.+ */+internal val TILE_GLYPH_LINE_HEIGHT = 1.2.em+  @Composable-fun SurfaceText(modifier: Modifier, surfaceShape: Shape, surfaceColor: Color, text: String, textModifier: Modifier, fontWeight: FontWeight? = null, fontSize: TextUnit = TextUnit.Unspecified, surfaceSize: Dp?, textColor: Color = Color.Unspecified) {+fun SurfaceText(modifier: Modifier, surfaceShape: Shape, surfaceColor: Color, text: String, textModifier: Modifier, fontWeight: FontWeight? = null, fontSize: TextUnit = TextUnit.Unspecified, surfaceSize: Dp?, textColor: Color = Color.Unspecified, lineHeight: TextUnit = TextUnit.Unspecified) {     val modifier2 = if(surfaceSize != null) modifier.size(surfaceSize) else modifier     Surface(modifier2, surfaceShape, surfaceColor) {         Box(if(surfaceSize == null) Modifier else Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {-            Text(text, textModifier, color = textColor, fontWeight = fontWeight, fontSize = fontSize)+            Text(text, textModifier, color = textColor, fontWeight = fontWeight, fontSize = fontSize, lineHeight = lineHeight)         }     } }
games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/LargeFontTest.kt
diff --git a/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/LargeFontTest.kt b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/LargeFontTest.ktnew file mode 100644index 0000000000..361bcba234--- /dev/null+++ b/games/wordmaker/src/test/java/com/vayunmathur/games/wordmaker/ui/LargeFontTest.kt@@ -0,0 +1,95 @@+package com.vayunmathur.games.wordmaker.ui++import androidx.compose.runtime.Composable+import androidx.compose.runtime.CompositionLocalProvider+import androidx.compose.ui.platform.LocalDensity+import androidx.compose.ui.semantics.SemanticsActions+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.text.TextLayoutResult+import androidx.compose.ui.unit.Density+import com.vayunmathur.games.wordmaker.data.CrosswordData+import com.vayunmathur.games.wordmaker.data.WheelSpacing+import com.vayunmathur.games.wordmaker.ui.components.CrosswordBoard+import com.vayunmathur.games.wordmaker.ui.components.LetterChooser+import com.vayunmathur.library.ui.DynamicTheme+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config+import org.robolectric.annotation.GraphicsMode+import kotlin.test.Test+import kotlin.test.assertTrue++/**+ * Letters on the wheel and the grid must not clip at 200% font. Native graphics, so text is+ * measured with real font metrics.+ */+@RunWith(RobolectricTestRunner::class)+@GraphicsMode(GraphicsMode.Mode.NATIVE)+@Config(qualifiers = "w411dp-h891dp")+class LargeFontTest {+    @get:Rule val rule = createComposeRule()++    private fun setAtDoubleFont(content: @Composable () -> Unit) {+        rule.setContent {+            DynamicTheme {+                val d = LocalDensity.current+                CompositionLocalProvider(LocalDensity provides Density(d.density, fontScale = 2f)) { content() }+            }+        }+    }++    private fun layoutOf(text: String): TextLayoutResult {+        val results = mutableListOf<TextLayoutResult>()+        rule.onAllNodesWithText(text)[0].fetchSemanticsNode()+            .config[SemanticsActions.GetTextLayoutResult].action!!.invoke(results)+        return results.first()+    }++    /**+     * The glyph's laid-out line must fit the tile it was measured in: its height (line height+     * included) within the height constraint, and its unwrapped width within the width one.+     * hasVisualOverflow is not used: the semantics layout result reports the paragraph at the+     * full constraint width, so it flags any glyph narrower than its tile.+     */+    private fun assertFits(text: String, what: String) {+        val layout = layoutOf(text)+        val input = layout.layoutInput+        val paragraph = layout.multiParagraph+        val fits = paragraph.height <= input.constraints.maxHeight &&+            paragraph.intrinsics.maxIntrinsicWidth <= input.constraints.maxWidth+        assertTrue(+            fits,+            "$what clipped at 200% font: constraints=${input.constraints} " ++                "height=${paragraph.height} width=${paragraph.intrinsics.maxIntrinsicWidth} " ++                "fontSize=${input.style.fontSize} lineHeight=${input.style.lineHeight} " ++                "fontScale=${input.density.fontScale}",+        )+    }++    @Test fun wheelLettersFitTheirTilesAtDoubleFont() {+        setAtDoubleFont {+            LetterChooser(+                letters = listOf(ChooserLetter(0, 'W'), ChooserLetter(1, 'M'), ChooserLetter(2, 'G')),+                tapToSpell = false, wheelSpacing = WheelSpacing.DEFAULT,+                onShuffle = {}, onWordSubmitted = { _, _ -> },+                onWordBoxPositioned = {}, onLetterPositioned = { _, _ -> },+            )+        }+        assertFits("W", "wheel letter")+        assertFits("M", "wheel letter")+    }++    @Test fun gridLettersFitTheirCellsAtDoubleFont() {+        setAtDoubleFont {+            CrosswordBoard(+                foundWords = setOf("CAT"), revealedHints = emptySet(),+                crosswordData = CrosswordData.fromString("CAT\n C \n T ")!!, wordToAnimate = null,+                onCellPositioned = { _, _ -> }, onCellClicked = { _, _ -> }, scaleUpdated = {},+            )+        }+        assertFits("C", "grid letter")+        assertFits("A", "grid letter")+    }+}