diff --git a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/MergePaths.kt b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/MergePaths.kt index ebeebbbb..d32649da 100644 --- a/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/MergePaths.kt +++ b/vgo-core/src/main/kotlin/com/jzbrooks/vgo/core/optimization/MergePaths.kt @@ -112,14 +112,15 @@ class MergePaths( for (current in paths.drop(1)) { val previous = mergedPaths.last() - val currentLength = current.commands.joinToString("", transform = constraints.commandPrinter::print).length + val mergeableCommands = makeFirstCommandAbsolute(current.commands) + val currentLength = mergeableCommands.joinToString("", transform = constraints.commandPrinter::print).length val accumulatedLength = pathLength + currentLength if (accumulatedLength > constraints.maxLength || unableToMerge(previous, current)) { mergedPaths.add(current) pathLength = currentLength } else { - previous.commands += makeFirstCommandAbsolute(current.commands) + previous.commands += mergeableCommands pathLength = accumulatedLength } } diff --git a/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/MergePathsTests.kt b/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/MergePathsTests.kt index 174eb90a..97325787 100644 --- a/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/MergePathsTests.kt +++ b/vgo-core/src/test/kotlin/com/jzbrooks/vgo/core/optimization/MergePathsTests.kt @@ -491,4 +491,32 @@ class MergePathsTests { .isEqualTo(listOf(Point(10f, 10f), Point(20f, 20f))) } } + + @Test + fun mergedPathsInitialCommandIsMadeAbsoluteBeforeConstraints() { + // This would be merged if directly considered by constraints (merged length is 15) + // M0,0 + // m10,10 1,1 -> M0,0 m10,10 1, 1 + + // When the relative command is made absolute for merging, the merged path would + // be longer (17 chars) than the constraint. + // M0,0 + // M10,10 11,11 -> M0,0 M10,10 11,11 + val paths = + listOf( + createPath( + listOf(MoveTo(CommandVariant.ABSOLUTE, listOf(Point(0f, 0f)))), + ), + createPath( + listOf(MoveTo(CommandVariant.RELATIVE, listOf(Point(10f, 10f), Point(1f, 1f), Point(1f, 1f)))), + ), + ) + + val graphic = createGraphic(paths) + val optimization = MergePaths(MergePaths.Constraints.PathLength(FakeCommandPrinter(), 16)) + + traverseBottomUp(graphic) { it.accept(optimization) } + + assertThat(graphic::elements).hasSize(2) + } }