jzbrooks / vgo

vector graphic optimization
MIT License
39 stars 1 forks source link

ConvertCurvesToArcs warps some images #60

Closed jzbrooks closed 2 weeks ago

jzbrooks commented 1 month ago

Describe the bug The image below is distorted by vgo as of 4a6b49ce44166648d561b75bc5ea1c2b5162b3ca. When ConvertCurvesToArcs is disabled, no distortion occurs.

To Reproduce View the vector drawable below (https://shapeshifter.design is handy) Run vgo --indent 2 against the vector (saving it into a different file with -o probably makes sense. Compare the output with the source

Expected behavior The image should not be distorted

Screenshots Before / After Screenshot 2024-08-05 at 8 45 34 AM

Additional context

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="312dp"
    android:height="205dp"
    android:viewportWidth="312"
    android:viewportHeight="205">
  <path
      android:pathData="M161.851,18.849C162.174,19.023 162.162,19.285 162.206,19.5C162.649,21.694 163.971,23.02 165.916,23.671C167.186,24.096 168.488,24.414 169.768,24.778C169.816,24.685 169.874,24.625 169.871,24.569C169.77,22.9 169.769,22.9 171.364,22.9L173.323,22.9C173.192,23.464 172.66,23.654 172.444,24.236C173.691,24.681 174.935,25.117 176.174,25.572C176.468,25.68 176.873,25.684 177.012,26.002C177.462,27.033 178.322,26.931 179.137,26.934C181.675,26.944 184.209,27.345 186.772,26.934C186.749,27.413 186.404,27.452 186.19,27.593C183.371,29.448 180.694,31.518 178.048,33.636C177.05,34.435 176.496,35.629 175.935,36.727C175.608,37.365 175.797,38.412 175.983,39.215C176.388,40.965 175.875,42.246 174.441,43.066C172.895,43.95 172.911,45.356 173.131,46.882C173.246,47.681 173.396,48.461 173.619,49.246C173.897,50.223 173.515,51.251 173.397,52.252C173.203,53.893 173.861,55.024 175.367,55.492C176.189,55.748 177.012,56.002 177.829,56.277C178.833,56.615 179.673,57.164 180.068,58.286C180.282,58.894 180.564,59.444 181.185,59.725C181.832,60.019 181.553,60.667 181.565,61.164C181.601,62.764 181.583,62.729 180.072,62.697C171.889,62.525 163.706,62.357 155.522,62.236C154.716,62.224 154.542,61.995 154.537,61.172C154.513,57.578 154.386,53.984 154.366,50.389C154.362,49.619 153.935,49.23 153.554,48.76C152.69,47.694 152.49,46.395 153.291,45.295C154.015,44.301 154.041,43.361 153.823,42.195C153.251,39.144 152.868,36.056 152.538,32.962C152.496,32.562 152.47,32.135 152.551,31.747C152.844,30.349 152.686,29.048 151.854,27.908C150.742,26.384 150.876,24.622 150.896,22.864C150.919,20.852 150.902,20.879 152.749,20.822C155.263,20.744 157.777,20.63 160.291,20.51C161.714,20.442 161.742,20.392 161.851,18.849"
      android:strokeWidth="1"
      android:fillColor="#5B6770"
      android:fillAlpha="0.25"
      android:fillType="evenOdd"
      android:strokeColor="#00000000"/>
</vector>
jzbrooks commented 2 weeks ago

742069174a9ffb7fecfdd713dab789085c8af0a2 somehow seems to have addressed this particular issue, but I'm not totally sure why yet.

with that commit reverted, this registry (modified to only include the absolutely necessary transformations to run ConvertCurvesToArcs and left bottom up commands that are irrelevant to this graphic) reproduces the issue. Commenting out ConvertCurvesToArcs restores expected (non-broken) visuals.

class VectorDrawableOptimizationRegistry : OptimizationRegistry(BOTTOM_UP, TOP_DOWN) {
    companion object {
        private val BOTTOM_UP =
            listOf(
//                BakeTransformations(),
                CollapseGroups(),
                RemoveEmptyGroups(),
//                MergePaths(),
            )

        private val TOP_DOWN =
            listOf(
                BreakoutImplicitCommands(),
                CommandVariant(CommandVariant.Mode.Relative),

                ConvertCurvesToArcs(VectorDrawableCommandPrinter(3)),
//                SimplifyBezierCurveCommands(1e-3f),
//                RemoveRedundantCommands(),
//                CommandVariant(CommandVariant.Mode.Compact(VectorDrawableCommandPrinter(3))),
//                Polycommands(),
            )
    }
}
jzbrooks commented 2 weeks ago

With the above modification to the registry, the data in the zip appears to be a simpler repro where the path first goes wrong.

Archive.zip