Fix a bug on RoundedPolygon initialization.
The code inadvertely was modified the provided cubics when ensuring that the cubics
before and after dropped zero-length cubics had matching anchors.
Now we make a defensive copy.
Relnote: Fix a bug on RoundedPolygon initialization.
Test: Added
Bug: 361038079
Change-Id: I83ddb2f481dc6cdbbe77f03f7b9ecd4be9462026
diff --git a/graphics/graphics-shapes/src/androidInstrumentedTest/kotlin/androidx/graphics/shapes/PolygonTest.kt b/graphics/graphics-shapes/src/androidInstrumentedTest/kotlin/androidx/graphics/shapes/PolygonTest.kt
index fd9bb84..368f7b1 100644
--- a/graphics/graphics-shapes/src/androidInstrumentedTest/kotlin/androidx/graphics/shapes/PolygonTest.kt
+++ b/graphics/graphics-shapes/src/androidInstrumentedTest/kotlin/androidx/graphics/shapes/PolygonTest.kt
@@ -16,6 +16,7 @@
package androidx.graphics.shapes
+import android.graphics.Matrix
import androidx.test.filters.SmallTest
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
@@ -200,6 +201,34 @@
assertNotEquals(preTransformCenters, postTransformCenters)
}
+ @Test
+ fun transformKeepsContiguousAnchorsEqual() {
+ val poly =
+ RoundedPolygon(radius = 1f, numVertices = 4, rounding = CornerRounding(7 / 15f))
+ .transformed(
+ Matrix().apply {
+ postRotate(45f)
+ postScale(648f, 648f)
+ postTranslate(540f, 1212f)
+ }
+ )
+ poly.cubics.indices.forEach { i ->
+ // It has to be the same point
+ assertEquals(
+ "Failed at X, index $i",
+ poly.cubics[i].anchor1X,
+ poly.cubics[(i + 1) % poly.cubics.size].anchor0X,
+ 0f
+ )
+ assertEquals(
+ "Failed at Y, index $i",
+ poly.cubics[i].anchor1Y,
+ poly.cubics[(i + 1) % poly.cubics.size].anchor0Y,
+ 0f
+ )
+ }
+ }
+
private fun nonzeroCubics(original: List<Cubic>): List<Cubic> {
val result = mutableListOf<Cubic>()
for (i in original.indices) {
diff --git a/graphics/graphics-shapes/src/commonMain/kotlin/androidx/graphics/shapes/RoundedPolygon.kt b/graphics/graphics-shapes/src/commonMain/kotlin/androidx/graphics/shapes/RoundedPolygon.kt
index b2fc8f2..051588a 100644
--- a/graphics/graphics-shapes/src/commonMain/kotlin/androidx/graphics/shapes/RoundedPolygon.kt
+++ b/graphics/graphics-shapes/src/commonMain/kotlin/androidx/graphics/shapes/RoundedPolygon.kt
@@ -67,6 +67,7 @@
// enough discontinuity to throw an exception later, even though the
// distances are quite small. Account for that by making the last
// cubic use the latest anchor point, always.
+ lastCubic = Cubic(lastCubic.points.copyOf()) // Make a copy before mutating
lastCubic.points[6] = cubic.anchor1X
lastCubic.points[7] = cubic.anchor1Y
}