Modern Apps git

← Modern-Apps.git

image: parse packed SVG arc flags as single characters

Commit
30b54bfff2a0b637206882f84ce18db4ea6e9674
Author
Vayun Mathur <[email protected]>
When
2026-12-02 00:54 UTC (4 days ago)
Parents
0e612b1
Symptom: SVG paths from generators that pack arc flags without
separators (for example A10 10 0 01100 0) lost the arc segment
entirely, leaving the cursor behind so later segments also drew in
the wrong place and icons rendered with missing arcs.

Root cause: applySingleArc read seven numeric tokens and validated
the flags as floats, so a packed flag pair like 01100 was consumed
as one number (1100) and the token count never matched. The
single-character parseFlag helper existed for exactly this grammar
but was unused.

Fix: read rx/ry/rotation as numbers, then consume each flag as one
character via parseFlag, then read the endpoint as a point. Spaced
flags keep working unchanged.

Verification: new SvgPathArcFlagTest failed 3 of 4 cases before the
fix (packed, packed-negative and relative-packed) and passes after;
:library:image:testDebugUnitTest, :library:image:lint and
:library:image:lintVitalRelease are all green.

Diff

library/image/src/main/java/com/vayunmathur/library/image/decoders/SvgPathParser.kt
diff --git a/library/image/src/main/java/com/vayunmathur/library/image/decoders/SvgPathParser.kt b/library/image/src/main/java/com/vayunmathur/library/image/decoders/SvgPathParser.ktindex 0337f58718..6c5fb616c5 100644--- a/library/image/src/main/java/com/vayunmathur/library/image/decoders/SvgPathParser.kt+++ b/library/image/src/main/java/com/vayunmathur/library/image/decoders/SvgPathParser.kt@@ -29,7 +29,6 @@ internal const val CUBIC_POINTS = 6 internal const val SMOOTH_CUBIC_POINTS = 4 internal const val QUADRATIC_POINTS = 4 internal const val SMOOTH_QUADRATIC_POINTS = 2-internal const val ARC_POINTS = 7  internal const val CUBIC_SMOOTH_PREVIOUS = 'C' internal const val CUBIC_SMOOTH_CONTINUATION = 'S'@@ -436,10 +435,15 @@ internal fun PathDataParser.applyArc(relative: Boolean) { }  internal fun PathDataParser.applySingleArc(relative: Boolean): Boolean {-    val values = readNumbers(ARC_POINTS) ?: return false-    val largeArc = readArcFlag(values[ARC_LARGE_ARC_INDEX]) ?: return false-    val sweep = readArcFlag(values[ARC_SWEEP_INDEX]) ?: return false-    val (x, y) = absolutePair(values[ARC_END_X_INDEX], values[ARC_END_Y_INDEX], relative)+    // rx, ry and the x-axis rotation are numbers, but the two flags are+    // single characters, not numbers: a generator may pack them against+    // each other and the endpoint (`0 01100 0`), so each flag consumes+    // exactly one character via parseFlag instead of a numeric token.+    val values = readNumbers(ARC_HEADER_POINTS) ?: return false+    val largeArc = parseFlag() ?: return false+    val sweep = parseFlag() ?: return false+    val endpoint = readPoint() ?: return false+    val (x, y) = absolutePair(endpoint.first, endpoint.second, relative)     val (cursorX, cursorY) = cursorPosition()     arcTo(         path,@@ -460,24 +464,10 @@ internal fun PathDataParser.applySingleArc(relative: Boolean): Boolean { }  internal const val ARC_COMMAND = 'A'+internal const val ARC_HEADER_POINTS = 3 internal const val ARC_RADIUS_X_INDEX = 0 internal const val ARC_RADIUS_Y_INDEX = 1 internal const val ARC_ANGLE_INDEX = 2-internal const val ARC_LARGE_ARC_INDEX = 3-internal const val ARC_SWEEP_INDEX = 4-internal const val ARC_END_X_INDEX = 5-internal const val ARC_END_Y_INDEX = 6--internal fun readArcFlag(value: Float): Int? {-    if (value == FLAG_ZERO_VALUE) return ARC_FLAG_ZERO-    if (value == FLAG_ONE_VALUE) return ARC_FLAG_ONE-    return null-}--internal const val FLAG_ZERO_VALUE = 0f-internal const val FLAG_ONE_VALUE = 1f-internal const val ARC_FLAG_ZERO = 0-internal const val ARC_FLAG_ONE = 1  internal fun PathDataParser.applyClose() {     path.close()
library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathArcFlagTest.kt
diff --git a/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathArcFlagTest.kt b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathArcFlagTest.ktnew file mode 100644index 0000000000..5c45fd2580--- /dev/null+++ b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathArcFlagTest.kt@@ -0,0 +1,49 @@+package com.vayunmathur.library.image.decoders++import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse++/**+ * SVG arc flags are single characters, not numbers: a generator may pack+ * them against each other and against the endpoint with no separator+ * (`A10 10 0 01100 0` is rx=10, ry=10, angle=0, large=0, sweep=1,+ * x=100, y=0). The parser must consume exactly one character per flag.+ */+@RunWith(RobolectricTestRunner::class)+class SvgPathArcFlagTest {++    @Test+    fun packedArcFlagsParseAndMoveCursorToEndpoint() {+        val parser = PathDataParser("M0 0 A10 10 0 01100 0")+        val path = parser.parse()+        assertFalse(path.isEmpty)+        assertEquals(100f to 0f, parser.cursorPosition())+    }++    @Test+    fun spacedArcFlagsStillParse() {+        val parser = PathDataParser("M0 0 A10 10 0 0 1 100 0")+        val path = parser.parse()+        assertFalse(path.isEmpty)+        assertEquals(100f to 0f, parser.cursorPosition())+    }++    @Test+    fun packedArcFlagsWithNegativeEndpoint() {+        val parser = PathDataParser("M100 0 A10 10 0 010 -50")+        val path = parser.parse()+        assertFalse(path.isEmpty)+        assertEquals(0f to -50f, parser.cursorPosition())+    }++    @Test+    fun relativePackedArcFlagsParse() {+        val parser = PathDataParser("M10 10 a10 10 0 0150 0")+        val path = parser.parse()+        assertFalse(path.isEmpty)+        assertEquals(60f to 10f, parser.cursorPosition())+    }+}