image: fix SVG smooth-curve reflection after non-curves
- Commit
0e612b1bcb11e277df861d2473a851c84b6fa15e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-02 00:45 UTC (4 days ago)
- Parents
2268677
Symptom: an SVG path whose first smooth curve (S or T) follows a move, line, close or other non-curve command rendered with a kinked first control point, reflecting a stale (or zero) control point from an unrelated earlier curve instead of starting at the current point. Root cause: reflectedCubicControl/reflectedQuadControl read the parser command field to decide whether the previous segment was a curve, but by the time an S/T handler runs that field already holds the incoming S/T token, so the check always saw a continuation. Fix: track the last executed command separately (previousCommand, updated per segment so chained smooth segments in one group still reflect each other) and key both reflection helpers off it. Verification: new SvgPathSmoothReflectionTest failed 3 of 6 cases before the fix (after-move/after-line for S and T) 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 98cfe17300..0337f58718 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@@ -50,6 +50,17 @@ internal class PathDataParser(private val data: String) { var command: Char = NO_COMMAND internal set + /**+ * The last command whose segment actually executed, normalised by the+ * handlers (`M` becomes `L`, a relative curve becomes its uppercase+ * family). Smooth-curve reflection keys off this, not [command]: by the+ * time an `S`/`T` handler runs, [command] already holds the incoming+ * `S`/`T` token, so reading it there would always see a continuation and+ * reflect a stale control point after moves, lines and closes.+ */+ var previousCommand: Char = NO_COMMAND+ internal set+ val path = Path() fun parse(): Path {@@ -92,6 +103,7 @@ internal class PathDataParser(private val data: String) { 'A' -> applyArc(command.isLowerCase()) 'Z' -> applyClose() }+ previousCommand = command } fun isRelative(): Boolean = command.isLowerCase()@@ -201,6 +213,7 @@ internal fun PathDataParser.applyMovePoint(point: Pair<Float, Float>, relative: } moveCursorTo(absolute.first, absolute.second) command = if (relative) MOVE_AS_LINE_RELATIVE else MOVE_AS_LINE_ABSOLUTE+ previousCommand = command return false } @@ -224,6 +237,7 @@ internal fun PathDataParser.applyLinePoint(point: Pair<Float, Float>) { val y = absoluteY(point.second) path.lineTo(x, y) moveCursorTo(x, y)+ previousCommand = command } internal fun PathDataParser.applyHorizontal() {@@ -243,6 +257,7 @@ internal fun PathDataParser.applyHorizontalValue(x: Float) { val (_, y) = cursorPosition() path.lineTo(absolute, y) moveCursorTo(absolute, y)+ previousCommand = command } internal fun PathDataParser.applyVertical() {@@ -262,6 +277,7 @@ internal fun PathDataParser.applyVerticalValue(y: Float) { val absolute = absoluteY(y) path.lineTo(x, absolute) moveCursorTo(x, absolute)+ previousCommand = command } internal fun PathDataParser.applyCubic(relative: Boolean) {@@ -284,6 +300,7 @@ internal fun PathDataParser.applyCubicValues(values: List<Float>, relative: Bool recordCubicControl(x2, y2) moveCursorTo(x, y) command = CUBIC_COMMAND+ previousCommand = command } internal const val CUBIC_COMMAND = 'C'@@ -330,6 +347,7 @@ internal fun PathDataParser.applySmoothCubicValues(values: List<Float>, relative recordCubicControl(x2, y2) moveCursorTo(x, y) command = SMOOTH_CUBIC_COMMAND+ previousCommand = command } internal const val SMOOTH_CUBIC_COMMAND = 'S'@@ -339,7 +357,7 @@ internal const val SMOOTH_CUBIC_X_INDEX = 2 internal const val SMOOTH_CUBIC_Y_INDEX = 3 internal fun PathDataParser.reflectedCubicControl(): Pair<Float, Float> {- val previous = command.uppercaseChar()+ val previous = previousCommand.uppercaseChar() if (previous != CUBIC_SMOOTH_PREVIOUS && previous != CUBIC_SMOOTH_CONTINUATION) { return cursorPosition() }@@ -367,6 +385,7 @@ internal fun PathDataParser.applyQuadraticValues(values: List<Float>, relative: recordQuadControl(controlX, controlY) moveCursorTo(x, y) command = QUADRATIC_COMMAND+ previousCommand = command } internal const val QUADRATIC_COMMAND = 'Q'@@ -394,12 +413,13 @@ internal fun PathDataParser.applySmoothQuadPoint(point: Pair<Float, Float>, rela recordQuadControl(controlX, controlY) moveCursorTo(x, y) command = SMOOTH_QUAD_COMMAND+ previousCommand = command } internal const val SMOOTH_QUAD_COMMAND = 'T' internal fun PathDataParser.reflectedQuadControl(): Pair<Float, Float> {- val previous = command.uppercaseChar()+ val previous = previousCommand.uppercaseChar() if (previous != QUAD_SMOOTH_PREVIOUS && previous != QUAD_SMOOTH_CONTINUATION) { return cursorPosition() }@@ -435,6 +455,7 @@ internal fun PathDataParser.applySingleArc(relative: Boolean): Boolean { ) moveCursorTo(x, y) command = ARC_COMMAND+ previousCommand = command return true } @@ -463,6 +484,7 @@ internal fun PathDataParser.applyClose() { val (x, y) = subpathStart() moveCursorTo(x, y) command = 'Z'+ previousCommand = command } internal fun parsePathData(data: String): Path = PathDataParser(data).parse()
library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathSmoothReflectionTest.kt
diff --git a/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathSmoothReflectionTest.kt b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathSmoothReflectionTest.ktnew file mode 100644index 0000000000..86980fe214--- /dev/null+++ b/library/image/src/test/java/com/vayunmathur/library/image/decoders/SvgPathSmoothReflectionTest.kt@@ -0,0 +1,79 @@+package com.vayunmathur.library.image.decoders++import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import kotlin.test.Test+import kotlin.test.assertEquals++/**+ * Pins the SVG smooth-curve reflection rule: `S`/`T` reflect the previous+ * segment's control point only when that segment was itself a cubic/quadratic+ * curve. After any other command (move, line, close, ...) the first control+ * point is the current point, so the curve starts without a kink.+ *+ * The parser state is driven through [parsePathData]-equivalent parsing of a+ * prefix, then the dispatcher state for the incoming `S`/`T` token is set+ * exactly as `consumeCommand` sets it, and the reflected control is read back.+ */+@RunWith(RobolectricTestRunner::class)+class SvgPathSmoothReflectionTest {++ @Test+ fun smoothCubicAfterLineStartsAtCursor() {+ val parser = PathDataParser("M10 10 L30 10")+ parser.parse()+ // A stale cubic control from an earlier, unrelated curve.+ parser.recordCubicControl(0f, 0f)+ parser.command = 'S'+ assertEquals(30f to 10f, parser.reflectedCubicControl())+ }++ @Test+ fun smoothCubicAfterMoveStartsAtCursor() {+ val parser = PathDataParser("M25 40")+ parser.parse()+ parser.recordCubicControl(100f, 100f)+ parser.command = 'S'+ assertEquals(25f to 40f, parser.reflectedCubicControl())+ }++ @Test+ fun smoothQuadraticAfterLineStartsAtCursor() {+ val parser = PathDataParser("M10 10 L30 10")+ parser.parse()+ parser.recordQuadControl(0f, 0f)+ parser.command = 'T'+ assertEquals(30f to 10f, parser.reflectedQuadControl())+ }++ @Test+ fun smoothCubicAfterCubicReflectsPreviousControl() {+ val parser = PathDataParser("M0 0 C10 10 20 10 30 0")+ parser.parse()+ parser.command = 'S'+ // Previous cubic's second control was (20, 10); cursor is (30, 0), so+ // the reflection is (40, -10).+ assertEquals(40f to -10f, parser.reflectedCubicControl())+ }++ @Test+ fun smoothQuadraticAfterQuadraticReflectsPreviousControl() {+ val parser = PathDataParser("M0 0 Q10 10 20 0")+ parser.parse()+ parser.command = 'T'+ // Previous quad control was (10, 10); cursor is (20, 0) -> (30, -10).+ assertEquals(30f to -10f, parser.reflectedQuadControl())+ }++ @Test+ fun secondSmoothCubicInOneGroupReflectsFirst() {+ // The second S segment in the same command group must reflect the+ // first S segment's control (20, 10) about its end point (30, 0),+ // even though the group was preceded by a line.+ val parser = PathDataParser("M0 0 L10 0 S20 10 30 0 40 -10 50 0")+ parser.parse()+ parser.command = 'S'+ // Last cubic control is now (40, -10); cursor is (50, 0) -> (60, 10).+ assertEquals(60f to 10f, parser.reflectedCubicControl())+ }+}