Adding shape optimization flag. #16.

This commit is contained in:
Rob Greene 2018-06-24 15:13:04 -05:00
parent c16f9c674a
commit 33289a18fb
2 changed files with 42 additions and 18 deletions

View File

@ -14,11 +14,13 @@ import io.github.applecommander.bastools.api.shapes.Shape;
import io.github.applecommander.bastools.api.shapes.ShapeExporter;
import io.github.applecommander.bastools.api.shapes.ShapeTable;
import io.github.applecommander.bastools.api.shapes.VectorCommand;
import io.github.applecommander.bastools.api.shapes.VectorShape;
public class SourceShapeExporter implements ShapeExporter {
private BiConsumer<Shape,PrintWriter> formatFunction = this::exportShapeAsBitmap;
private ShapeExporter textExporter;
private boolean skipEmptyShapes;
private boolean optimize;
/** Use the {@code Builder} to create a TextShapeExporter. */
private SourceShapeExporter() {
@ -58,28 +60,36 @@ public class SourceShapeExporter implements ShapeExporter {
}
public void exportShapeAsShortCommands(Shape shape, PrintWriter pw) {
pw.printf(".short\n");
pw.printf(" %s\n", shape.toVector().toShortCommands());
pw.printf("\n");
VectorShape vshape = shape.toVector();
if (optimize) vshape = vshape.optimize();
if (displayThisShape(vshape)) {
pw.printf(".short\n");
pw.printf(" %s\n", vshape.toShortCommands());
pw.printf("\n");
}
}
public void exportShapeAsLongCommands(Shape shape, PrintWriter pw) {
pw.printf(".long\n");
Queue<VectorCommand> vectors = new LinkedList<>(shape.toVector().vectors);
while (!vectors.isEmpty()) {
VectorCommand vector = vectors.remove();
int count = 1;
while (vectors.peek() == vector) {
vectors.remove();
count += 1;
}
if (count == 1) {
pw.printf(" %s\n", vector.longCommand);
} else {
pw.printf(" %s %d\n", vector.longCommand, count);
VectorShape vshape = shape.toVector();
if (optimize) vshape = vshape.optimize();
if (displayThisShape(vshape)) {
pw.printf(".long\n");
Queue<VectorCommand> vectors = new LinkedList<>(vshape.vectors);
while (!vectors.isEmpty()) {
VectorCommand vector = vectors.remove();
int count = 1;
while (vectors.peek() == vector) {
vectors.remove();
count += 1;
}
if (count == 1) {
pw.printf(" %s\n", vector.longCommand);
} else {
pw.printf(" %s %d\n", vector.longCommand, count);
}
}
pw.printf("\n");
}
pw.printf("\n");
}
public static class Builder {
@ -106,6 +116,14 @@ public class SourceShapeExporter implements ShapeExporter {
return this;
}
public Builder optimize() {
return optimize(true);
}
public Builder optimize(boolean optimize) {
exporter.optimize = optimize;
return this;
}
public ShapeExporter build() {
return exporter;
}

View File

@ -43,7 +43,10 @@ public class ExtractCommand implements Callable<Void> {
private String outputFormat = "text";
@Option(names = "--coding", description = "Select source style (bitmap, long, short)", showDefaultValue = Visibility.ALWAYS)
private String codeStyle;
private String codeStyle = "long";
@Option(names = { "-O", "--optimize" }, description = "Optimize vector shapes (source only)")
private boolean optimize;
@Option(names = "--skip-empty", description = "Skip empty shapes")
private boolean skipEmptyShapesFlag = false;
@ -153,18 +156,21 @@ public class ExtractCommand implements Callable<Void> {
exporter = ShapeExporter.source()
.bitmap()
.skipEmptyShapes(skipEmptyShapesFlag)
.optimize(optimize)
.build();
break;
case "short":
exporter = ShapeExporter.source()
.shortCommands()
.skipEmptyShapes(skipEmptyShapesFlag)
.optimize(optimize)
.build();
break;
case "long":
exporter = ShapeExporter.source()
.longCommands()
.skipEmptyShapes(skipEmptyShapesFlag)
.optimize(optimize)
.build();
break;
default: