Skip to content

Commit

Permalink
Consider modified commands for merge path constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
jzbrooks committed Nov 30, 2024
1 parent 21135c7 commit d49e434
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit d49e434

Please sign in to comment.