From 9cbb51d88a46acb4048e22ca2acf8b541f1e69db Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Sun, 15 Sep 2024 22:45:34 -0400 Subject: [PATCH] Avoid inserting grouping commas into large numbers (#78) * Avoid inserting grouping commas into large numbers * Update baselines afer re-enabling convert curves to arc * Add a regression case * Use consistent tolerences for circle fits * Add a changelog entry --- changelog.md | 10 +--------- .../vgo/core/optimization/ConvertCurvesToArcs.kt | 6 +++--- .../vgo/svg/ScalableVectorGraphicCommandPrinter.kt | 1 + .../jzbrooks/vgo/vd/VectorDrawableCommandPrinter.kt | 1 + .../vgo/vd/VectorDrawableOptimizationRegistry.kt | 3 ++- .../baseline/charging_battery_optimized.xml | 4 ++-- .../baseline/dribbble_ball_mark_optimized.xml | 2 +- .../baseline/eleven_below_single_optimized.xml | 8 ++++---- vgo/src/test/resources/baseline/nasa_optimized.xml | 6 +++--- .../resources/baseline/regression_31_optimized.xml | 2 +- .../resources/baseline/regression_33_optimized.xml | 2 +- .../resources/baseline/regression_60_optimized.xml | 3 +++ .../resources/baseline/simple_heart_optimized.xml | 2 +- vgo/src/test/resources/baseline/tiger_optimized.xml | 6 +++--- vgo/src/test/resources/regression_60.xml | 13 +++++++++++++ 15 files changed, 40 insertions(+), 29 deletions(-) create mode 100644 vgo/src/test/resources/baseline/regression_60_optimized.xml create mode 100644 vgo/src/test/resources/regression_60.xml diff --git a/changelog.md b/changelog.md index ba4e3d55..98c81c2d 100644 --- a/changelog.md +++ b/changelog.md @@ -2,17 +2,9 @@ ## Unreleased -### Added - -### Changed - -### Deprecated - -### Removed - ### Fixed -### Security +- A bug that would sometimes introduce a grouping comma into a large number, breaking path data. This most often occurred in the convert curves to arcs optimization when an arc with a large radius was more compact than the corresponding Bézier curve. ## 2.2.2 - 2024-08-30 diff --git a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/ConvertCurvesToArcs.kt b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/ConvertCurvesToArcs.kt index 510da821..351a8da1 100644 --- a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/ConvertCurvesToArcs.kt +++ b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/ConvertCurvesToArcs.kt @@ -20,7 +20,7 @@ import com.jzbrooks.vgo.core.util.math.liesOnCircle import com.jzbrooks.vgo.core.util.math.toCubicBezierCurve /** - * Converts cubic bezier curves to arcs, when they are shorter. + * Converts cubic Bézier curves to arcs, when they are shorter. */ class ConvertCurvesToArcs(private val printer: CommandPrinter) : TopDownOptimization { override fun visit(graphic: Graphic) {} @@ -148,7 +148,7 @@ class ConvertCurvesToArcs(private val printer: CommandPrinter) : TopDownOptimiza } // If the next curve is a shorthand, it must be converted - // to longhand if it the previous curve is replaced with an + // to longhand if it is the previous curve is replaced with an // elliptical arc. if (nextCommand is SmoothCubicBezierCurve) { ellipticalArcs.add(nextCommand.toCubicBezierCurve(pendingCurves.last())) @@ -191,7 +191,7 @@ class ConvertCurvesToArcs(private val printer: CommandPrinter) : TopDownOptimiza assert(command.parameters.size == 1) val currentParameter = command.parameters[0] - val circle = command.fitCircle(1e-2f) + val circle = command.fitCircle() if (circle != null && command.isConvex()) { val radius = circle.radius val sweep = diff --git a/vgo/src/main/kotlin/com/jzbrooks/vgo/svg/ScalableVectorGraphicCommandPrinter.kt b/vgo/src/main/kotlin/com/jzbrooks/vgo/svg/ScalableVectorGraphicCommandPrinter.kt index 85328062..0367dc00 100644 --- a/vgo/src/main/kotlin/com/jzbrooks/vgo/svg/ScalableVectorGraphicCommandPrinter.kt +++ b/vgo/src/main/kotlin/com/jzbrooks/vgo/svg/ScalableVectorGraphicCommandPrinter.kt @@ -23,6 +23,7 @@ class ScalableVectorGraphicCommandPrinter(private val decimalDigits: Int) : Comm DecimalFormat().apply { maximumFractionDigits = decimalDigits isDecimalSeparatorAlwaysShown = false + isGroupingUsed = false roundingMode = RoundingMode.HALF_UP minimumIntegerDigits = 0 } diff --git a/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableCommandPrinter.kt b/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableCommandPrinter.kt index 61642a39..b79925ed 100644 --- a/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableCommandPrinter.kt +++ b/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableCommandPrinter.kt @@ -23,6 +23,7 @@ class VectorDrawableCommandPrinter(private val decimalDigits: Int) : CommandPrin DecimalFormat().apply { maximumFractionDigits = decimalDigits isDecimalSeparatorAlwaysShown = false + isGroupingUsed = false roundingMode = RoundingMode.HALF_UP } diff --git a/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableOptimizationRegistry.kt b/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableOptimizationRegistry.kt index a05320ba..f716e4c1 100644 --- a/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableOptimizationRegistry.kt +++ b/vgo/src/main/kotlin/com/jzbrooks/vgo/vd/VectorDrawableOptimizationRegistry.kt @@ -4,6 +4,7 @@ import com.jzbrooks.vgo.core.optimization.BakeTransformations import com.jzbrooks.vgo.core.optimization.BreakoutImplicitCommands import com.jzbrooks.vgo.core.optimization.CollapseGroups import com.jzbrooks.vgo.core.optimization.CommandVariant +import com.jzbrooks.vgo.core.optimization.ConvertCurvesToArcs import com.jzbrooks.vgo.core.optimization.MergePaths import com.jzbrooks.vgo.core.optimization.OptimizationRegistry import com.jzbrooks.vgo.core.optimization.Polycommands @@ -29,7 +30,7 @@ class VectorDrawableOptimizationRegistry : OptimizationRegistry(BOTTOM_UP, TOP_D BreakoutImplicitCommands(), CommandVariant(CommandVariant.Mode.Relative), SimplifyLineCommands(1e-3f), -// ConvertCurvesToArcs(VectorDrawableCommandPrinter(3)), + ConvertCurvesToArcs(VectorDrawableCommandPrinter(3)), SimplifyBezierCurveCommands(1e-3f), RemoveRedundantCommands(), CommandVariant(CommandVariant.Mode.Compact(VectorDrawableCommandPrinter(3))), diff --git a/vgo/src/test/resources/baseline/charging_battery_optimized.xml b/vgo/src/test/resources/baseline/charging_battery_optimized.xml index d7c44cea..ce5ebde8 100644 --- a/vgo/src/test/resources/baseline/charging_battery_optimized.xml +++ b/vgo/src/test/resources/baseline/charging_battery_optimized.xml @@ -1,6 +1,6 @@ - - + + diff --git a/vgo/src/test/resources/baseline/dribbble_ball_mark_optimized.xml b/vgo/src/test/resources/baseline/dribbble_ball_mark_optimized.xml index a7b31116..431451df 100644 --- a/vgo/src/test/resources/baseline/dribbble_ball_mark_optimized.xml +++ b/vgo/src/test/resources/baseline/dribbble_ball_mark_optimized.xml @@ -1,3 +1,3 @@ - + diff --git a/vgo/src/test/resources/baseline/eleven_below_single_optimized.xml b/vgo/src/test/resources/baseline/eleven_below_single_optimized.xml index 2b8ba72c..5856851e 100644 --- a/vgo/src/test/resources/baseline/eleven_below_single_optimized.xml +++ b/vgo/src/test/resources/baseline/eleven_below_single_optimized.xml @@ -8,8 +8,8 @@ - - + + @@ -64,7 +64,7 @@ - + @@ -96,7 +96,7 @@ - + diff --git a/vgo/src/test/resources/baseline/nasa_optimized.xml b/vgo/src/test/resources/baseline/nasa_optimized.xml index 49e2a7fe..6ccb97ed 100644 --- a/vgo/src/test/resources/baseline/nasa_optimized.xml +++ b/vgo/src/test/resources/baseline/nasa_optimized.xml @@ -1,6 +1,6 @@ - - - + + + diff --git a/vgo/src/test/resources/baseline/regression_31_optimized.xml b/vgo/src/test/resources/baseline/regression_31_optimized.xml index 442d4188..adf34053 100644 --- a/vgo/src/test/resources/baseline/regression_31_optimized.xml +++ b/vgo/src/test/resources/baseline/regression_31_optimized.xml @@ -1,3 +1,3 @@ - + diff --git a/vgo/src/test/resources/baseline/regression_33_optimized.xml b/vgo/src/test/resources/baseline/regression_33_optimized.xml index c3eec54e..db9fe912 100644 --- a/vgo/src/test/resources/baseline/regression_33_optimized.xml +++ b/vgo/src/test/resources/baseline/regression_33_optimized.xml @@ -1,3 +1,3 @@ - + diff --git a/vgo/src/test/resources/baseline/regression_60_optimized.xml b/vgo/src/test/resources/baseline/regression_60_optimized.xml new file mode 100644 index 00000000..3b63bfa3 --- /dev/null +++ b/vgo/src/test/resources/baseline/regression_60_optimized.xml @@ -0,0 +1,3 @@ + + + diff --git a/vgo/src/test/resources/baseline/simple_heart_optimized.xml b/vgo/src/test/resources/baseline/simple_heart_optimized.xml index c1ca9b04..ca3d67c5 100644 --- a/vgo/src/test/resources/baseline/simple_heart_optimized.xml +++ b/vgo/src/test/resources/baseline/simple_heart_optimized.xml @@ -1,3 +1,3 @@ - + diff --git a/vgo/src/test/resources/baseline/tiger_optimized.xml b/vgo/src/test/resources/baseline/tiger_optimized.xml index 8931d88e..99a0a71b 100644 --- a/vgo/src/test/resources/baseline/tiger_optimized.xml +++ b/vgo/src/test/resources/baseline/tiger_optimized.xml @@ -81,7 +81,7 @@ - + @@ -98,7 +98,7 @@ - + @@ -108,7 +108,7 @@ - + diff --git a/vgo/src/test/resources/regression_60.xml b/vgo/src/test/resources/regression_60.xml new file mode 100644 index 00000000..b53e2fbb --- /dev/null +++ b/vgo/src/test/resources/regression_60.xml @@ -0,0 +1,13 @@ + + + \ No newline at end of file