Add svg exporter

This commit is contained in:
Lonami Exo 2017-01-18 20:03:58 +01:00
parent d2c130b214
commit d819055529
6 changed files with 171 additions and 49 deletions

View File

@ -2,6 +2,11 @@ package com.simplemobiletools.draw;
import android.graphics.Path;
import com.simplemobiletools.draw.actions.Action;
import com.simplemobiletools.draw.actions.Line;
import com.simplemobiletools.draw.actions.Move;
import com.simplemobiletools.draw.actions.Quad;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
@ -45,54 +50,7 @@ class MyPath extends Path implements Serializable {
super.quadTo(x1, y1, x2, y2);
}
private interface Action extends Serializable {
void perform(Path path);
}
private static final class Move implements Action {
private final float x, y;
Move(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public void perform(Path path) {
path.moveTo(x, y);
}
}
private static final class Line implements Action {
private final float x, y;
Line(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public void perform(Path path) {
path.lineTo(x, y);
}
}
private static final class Quad implements Action {
private final float x1, y1, x2, y2;
private Quad(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
@Override
public void perform(Path path) {
path.quadTo(x1, y1, x2, y2);
}
List<Action> getActions() {
return actions;
}
}

View File

@ -0,0 +1,58 @@
package com.simplemobiletools.draw;
import com.simplemobiletools.draw.actions.Action;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Map;
public class Svg {
public static boolean saveSvg(File output,
Map<MyPath, PaintOptions> paths, int width, int height) {
try {
FileOutputStream out = new FileOutputStream(output);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
writeSvg(writer, paths, width, height);
writer.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
private static void writeSvg(Writer writer,
Map<MyPath, PaintOptions> paths, int width, int height)
throws IOException {
writer.write("<svg width=\"");
writer.write(String.valueOf(width));
writer.write("\" height=\"");
writer.write(String.valueOf(height));
writer.write("\" xmlns=\"http://www.w3.org/2000/svg\">");
for (Map.Entry<MyPath, PaintOptions> entry : paths.entrySet()) {
writePath(writer, entry.getKey(), entry.getValue());
}
writer.write("</svg>");
}
private static void writePath(Writer writer, MyPath path, PaintOptions options)
throws IOException {
writer.write("<path d=\"");
for (Action action : path.getActions()) {
action.perform(writer);
writer.write(' ');
}
writer.write("\" fill=\"none\" stroke=\"#");
writer.write(Integer.toHexString(options.color).substring(2)); // Skip the alpha FF
writer.write("\" stroke-width=\"");
writer.write(String.valueOf(options.strokeWidth));
writer.write("\"/>");
}
}

View File

@ -0,0 +1,12 @@
package com.simplemobiletools.draw.actions;
import android.graphics.Path;
import java.io.IOException;
import java.io.Serializable;
import java.io.Writer;
public interface Action extends Serializable {
void perform(Path path);
void perform(Writer writer) throws IOException;
}

View File

@ -0,0 +1,29 @@
package com.simplemobiletools.draw.actions;
import android.graphics.Path;
import java.io.IOException;
import java.io.Writer;
public final class Line implements Action {
private final float x, y;
public Line(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public void perform(Path path) {
path.lineTo(x, y);
}
@Override
public void perform(Writer writer) throws IOException {
writer.write("L");
writer.write(String.valueOf(x));
writer.write(",");
writer.write(String.valueOf(y));
}
}

View File

@ -0,0 +1,30 @@
package com.simplemobiletools.draw.actions;
import android.graphics.Path;
import java.io.IOException;
import java.io.Writer;
public final class Move implements Action {
private final float x, y;
public Move(float x, float y) {
this.x = x;
this.y = y;
}
@Override
public void perform(Path path) {
path.moveTo(x, y);
}
@Override
public void perform(Writer writer) throws IOException {
writer.write('M');
writer.write(String.valueOf(x));
writer.write(',');
writer.write(String.valueOf(y));
}
}

View File

@ -0,0 +1,35 @@
package com.simplemobiletools.draw.actions;
import android.graphics.Path;
import java.io.IOException;
import java.io.Writer;
public final class Quad implements Action {
private final float x1, y1, x2, y2;
public Quad(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
@Override
public void perform(Path path) {
path.quadTo(x1, y1, x2, y2);
}
@Override
public void perform(Writer writer) throws IOException {
writer.write('Q');
writer.write(String.valueOf(x1));
writer.write(',');
writer.write(String.valueOf(y1));
writer.write(' ');
writer.write(String.valueOf(x2));
writer.write(',');
writer.write(String.valueOf(y2));
}
}