Add .svg loading logic

This commit is contained in:
Lonami Exo 2017-03-21 17:36:56 +01:00
parent e4ba503c70
commit d4c041d06e
6 changed files with 108 additions and 4 deletions

View File

@ -98,6 +98,11 @@ public class MyCanvas extends View {
return mPaths; return mPaths;
} }
public void addPath(MyPath path, PaintOptions options) {
mPaths.put(path, options);
pathsUpdated();
}
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);

View File

@ -10,6 +10,7 @@ import com.simplemobiletools.draw.actions.Quad;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.Serializable; import java.io.Serializable;
import java.security.InvalidParameterException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -26,12 +27,47 @@ class MyPath extends Path implements Serializable {
} }
} }
public void readObject(String pathData) throws InvalidParameterException {
String[] tokens = pathData.split("\\s+");
for (int i = 0; i < tokens.length; ++i) {
switch (tokens[i].charAt(0)) {
case 'M':
addAction(new Move(tokens[i]));
break;
case 'L':
addAction(new Line(tokens[i]));
break;
case 'Q':
// Quad actions are of the following form:
// "Qx1,y1 x2,y2"
// Since we split the tokens by whitespace, we need to join them again
if (i+1 >= tokens.length)
throw new InvalidParameterException("Error parsing the data for a Quad.");
addAction(new Quad(tokens[i]+" "+tokens[i+1]));
++i;
break;
}
}
}
@Override @Override
public void reset() { public void reset() {
actions.clear(); actions.clear();
super.reset(); super.reset();
} }
private void addAction(Action action) {
if (action instanceof Move) {
moveTo(((Move)action).x, ((Move)action).y);
} else if (action instanceof Line) {
lineTo(((Line)action).x, ((Line)action).y);
} else if (action instanceof Quad) {
final Quad q = (Quad)action;
quadTo(q.x1, q.y1, q.x2, q.y2);
}
}
@Override @Override
public void moveTo(float x, float y) { public void moveTo(float x, float y) {
actions.add(new Move(x, y)); actions.add(new Move(x, y));

View File

@ -76,7 +76,22 @@ public class Svg {
//region Loading //region Loading
public static void parseSvg(File file) throws Exception { public static void loadSvg(File file, MyCanvas canvas) throws Exception {
SSvg svg = parseSvg(file);
canvas.clearCanvas();
canvas.setBackgroundColor(svg.background.color);
for (SPath sp : svg.paths) {
MyPath path = new MyPath();
path.readObject(sp.data);
PaintOptions options = new PaintOptions(sp.color, sp.strokeWidth);
canvas.addPath(path, options);
}
}
public static SSvg parseSvg(File file) throws Exception {
InputStream is = null; InputStream is = null;
final SSvg svg = new SSvg(); final SSvg svg = new SSvg();
try { try {
@ -125,6 +140,7 @@ public class Svg {
if (is != null) if (is != null)
is.close(); is.close();
} }
return svg;
} }
//region Svg serializable classes //region Svg serializable classes

View File

@ -4,10 +4,24 @@ import android.graphics.Path;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.security.InvalidParameterException;
public final class Line implements Action { public final class Line implements Action {
private final float x, y; public final float x, y;
public Line(String data) {
if (data.startsWith("L"))
throw new InvalidParameterException("The Line data should start with 'L'.");
try {
String[] xy = data.substring(1).split(",");
x = Float.parseFloat(xy[0].trim());
y = Float.parseFloat(xy[1].trim());
} catch (Exception ignored) {
throw new InvalidParameterException("Error parsing the given Line data.");
}
}
public Line(float x, float y) { public Line(float x, float y) {
this.x = x; this.x = x;

View File

@ -4,10 +4,24 @@ import android.graphics.Path;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.security.InvalidParameterException;
public final class Move implements Action { public final class Move implements Action {
private final float x, y; public final float x, y;
public Move(String data) throws InvalidParameterException {
if (data.startsWith("M"))
throw new InvalidParameterException("The Move data should start with 'M'.");
try {
String[] xy = data.substring(1).split(",");
x = Float.parseFloat(xy[0].trim());
y = Float.parseFloat(xy[1].trim());
} catch (Exception ignored) {
throw new InvalidParameterException("Error parsing the given Move data.");
}
}
public Move(float x, float y) { public Move(float x, float y) {
this.x = x; this.x = x;

View File

@ -4,10 +4,29 @@ import android.graphics.Path;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.security.InvalidParameterException;
public final class Quad implements Action { public final class Quad implements Action {
private final float x1, y1, x2, y2; public final float x1, y1, x2, y2;
public Quad(String data) {
if (data.startsWith("Q"))
throw new InvalidParameterException("The Quad data should start with 'Q'.");
try {
String[] parts = data.split("\\s+");
String[] xy1 = parts[0].substring(1).split(",");
String[] xy2 = parts[1].split(","); // No need to skip the 'Q' here
x1 = Float.parseFloat(xy1[0].trim());
y1 = Float.parseFloat(xy1[1].trim());
x2 = Float.parseFloat(xy2[0].trim());
y2 = Float.parseFloat(xy2[1].trim());
} catch (Exception ignored) {
throw new InvalidParameterException("Error parsing the given Quad data.");
}
}
public Quad(float x1, float y1, float x2, float y2) { public Quad(float x1, float y1, float x2, float y2) {
this.x1 = x1; this.x1 = x1;