Skip to content

Commit

Permalink
Remove redundant close path commands
Browse files Browse the repository at this point in the history
Resolves #22
  • Loading branch information
Justin Brooks committed Jun 20, 2020
1 parent c66190a commit 8838dd8
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.jzbrooks.vgo.core.graphic.PathElement
import com.jzbrooks.vgo.core.graphic.command.*
import com.jzbrooks.vgo.core.graphic.command.CommandVariant
import com.jzbrooks.vgo.core.util.math.Point
import com.jzbrooks.vgo.core.util.math.computeAbsoluteCoordinates
import kotlin.math.abs

/**
Expand All @@ -14,7 +15,9 @@ class RemoveRedundantCommands : TopDownOptimization, PathElementVisitor {
val commandCount = pathElement.commands.size

if (commandCount > 0) {
val commands = mutableListOf<Command>((pathElement.commands.first() as MoveTo).copy())
val firstCommand = pathElement.commands.first() as MoveTo

val commands = mutableListOf<Command>(firstCommand)
loop@ for (current in pathElement.commands.drop(1)) {
assert((current as? ParameterizedCommand<*>)?.variant != CommandVariant.ABSOLUTE)

Expand All @@ -32,6 +35,17 @@ class RemoveRedundantCommands : TopDownOptimization, PathElementVisitor {
commands.add(current)
}

if (pathElement.commands.last() is ClosePath) {
val commandsWithoutFinalClosePath = commands.dropLast(1)
val current = computeAbsoluteCoordinates(commandsWithoutFinalClosePath)
val firstCurrentPoint = firstCommand.parameters.last()

if (current == firstCurrentPoint) {
pathElement.commands = commandsWithoutFinalClosePath
return
}
}

pathElement.commands = commands
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.jzbrooks.vgo.core.optimization

import assertk.assertThat
import assertk.assertions.containsNone
import assertk.assertions.hasSize
import com.jzbrooks.vgo.core.graphic.Path
import com.jzbrooks.vgo.core.graphic.command.ClosePath
Expand Down Expand Up @@ -43,4 +44,21 @@ class RemoveRedundantCommandsTests {

assertThat(path.commands).hasSize(4)
}

@Test
fun testRedundantClosePathsAreRemoved() {
val path = Path(
listOf(
MoveTo(CommandVariant.ABSOLUTE, listOf(Point(100f, 1f))),
LineTo(CommandVariant.RELATIVE, listOf(Point(1f, 1f))),
LineTo(CommandVariant.RELATIVE, listOf(Point(2f, 1f))),
LineTo(CommandVariant.RELATIVE, listOf(Point(-3f, -2f))),
ClosePath
)
)

RemoveRedundantCommands().visit(path)

assertThat(path.commands).containsNone(ClosePath)
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp"><group android:name="rotationGroup"><path android:fillAlpha=".3" android:fillColor="#FF000000" android:name="vect" android:pathData="M17.03,5.672 15.417,5.24l0.518,-1.932L12.071,2.273l-0.518,1.932L9.94,3.772a1.336,1.336,0,0,0,-1.629,0.94l-0.95,3.545 4.762,1.276 1.551,-1.655-0.518,1.932 3.864,1.035 0.95,-3.545A1.336,1.336,0,0,0,17.03,5.672Z"/><path android:fillColor="#FF000000" android:name="draw" android:pathData="M12.251,13.191l1.932,0.518-5.805,6.209 1.424,-5.313L7.869,14.088l4.254,-4.554L7.361,8.258 4.341,19.53a1.336,1.336,0,0,0,0.94,1.629l7.08,1.897A1.338,1.338,0,0,0,14,22.118l3.02,-11.272L13.157,9.811l-0.906,3.381Z"/></group></vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp"><group android:name="rotationGroup"><path android:fillAlpha=".3" android:fillColor="#FF000000" android:name="vect" android:pathData="M17.03,5.672 15.417,5.24l0.518,-1.932L12.071,2.273l-0.518,1.932L9.94,3.772a1.336,1.336,0,0,0,-1.629,0.94l-0.95,3.545 4.762,1.276 1.551,-1.655-0.518,1.932 3.864,1.035 0.95,-3.545A1.336,1.336,0,0,0,17.03,5.672"/><path android:fillColor="#FF000000" android:name="draw" android:pathData="M12.251,13.191l1.932,0.518-5.805,6.209 1.424,-5.313L7.869,14.088l4.254,-4.554L7.361,8.258 4.341,19.53a1.336,1.336,0,0,0,0.94,1.629l7.08,1.897A1.338,1.338,0,0,0,14,22.118l3.02,-11.272L13.157,9.811l-0.906,3.381"/></group></vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8838dd8

Please sign in to comment.