Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xml2js] add support for quadTo and stroke #31

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions src/main/java/com/mxgraph/svg2js/Svg2Js.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ public static void main(String[] args) throws IOException {

File svg = new File(args[0]);
File currentDirectory = new File(System.getProperty("user.dir"));
File destinationDirectory = new File(currentDirectory, "mxgraph-stencil-from-svg_" + System.currentTimeMillis());
File tempDirectory = new File(currentDirectory, "mxgraph-stencil-from-svg_" + System.currentTimeMillis());

new Svg2Js().process(svg, destinationDirectory);
// TODO make log level configurable
String code = new Svg2Js().infoLog(true).debugLog(false).convertToJsCode(svg, tempDirectory);
FileUtils.deleteQuietly(tempDirectory);
System.out.println(code);
System.out.println();
}

public void process(File svg, File tempDirectory) throws IOException {
public String convertToJsCode(File svg, File tempDirectory) throws IOException {
logInfo("Start generating mxgraph JS code from SVG " + svg);
logInfo("mxgraph stencil will be generated into " + tempDirectory);

Svg2Xml svg2Xml = new Svg2Xml();
Svg2Xml svg2Xml = new Svg2Xml().infoLog(isInfoLogActivated);
svg2Xml.convertToXml(new File[]{svg}, tempDirectory);
logInfo("SVG converted into mxgraph stencil");

Expand All @@ -39,14 +43,22 @@ public void process(File svg, File tempDirectory) throws IOException {
}

logInfo("Converting SVG into JS code");
String code = new Xml2Js().infoLog(true).debugLog(false).parse(expectedGeneratedStencilFile);
System.out.println(code);
System.out.println();
FileUtils.deleteQuietly(tempDirectory);
logInfo("mxgraph JS code from SVG produced");
String code = new Xml2Js().infoLog(isInfoLogActivated).debugLog(isDebugLogActivated).parse(expectedGeneratedStencilFile);
logInfo("Conversion to JS completed");
return code;
}

// TODO log management duplicated with Xml2Js
private boolean isInfoLogActivated = true;
private boolean isDebugLogActivated = true;
public Svg2Js infoLog(boolean activate) {
this.isInfoLogActivated = activate;
return this;
}
public Svg2Js debugLog(boolean activate) {
this.isDebugLogActivated = activate;
return this;
}
private void logInfo(String msg) {
if (isInfoLogActivated) {
System.out.println(format("Svg2Js [INFO] %s", msg));
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/mxgraph/svg2xml/Svg2Xml.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ public static void main(String[] args)
svg2Xml.convertToXml(sourceFiles.toArray(new File[0]), new File(args[1]));
}

// TODO log management duplicated with Xml2Js
private boolean isInfoLogActivated = true;
public Svg2Xml infoLog(boolean activate) {
this.isInfoLogActivated = activate;
return this;
}
private void logInfo(String msg) {
if (isInfoLogActivated) {
System.out.println(format("Svg2Xml [INFO] %s", msg));
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/mxgraph/xml2js/Xml2Js.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static void main(String[] args) {
}

File source = new File(args[0]);
// TODO make log level configurable
String code = new Xml2Js().infoLog(true).debugLog(false).parse(source);
System.out.println(code);
}
Expand Down Expand Up @@ -92,8 +93,11 @@ public void parseShape(Node shape) {
for (int i = 0; i < shapeChildren.getLength(); i++) {
Node item = shapeChildren.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
if (item.getNodeName().equals("foreground")) {
String nodeName = item.getNodeName();
if (nodeName.equals("foreground")) {
parseForeground(item);
} else {
logDebug("Unsupported element: " + nodeName);
}
}
}
Expand Down Expand Up @@ -127,6 +131,13 @@ private void parseForeground(Node foreground) {
break;
case "path":
parsePath((Element) item);
break;
case "stroke":
logDebug("Parsing stroke");
generateCanvasMethodCall("stroke()");
break;
default:
logDebug("Unsupported element: " + nodeName);
}
}
}
Expand Down Expand Up @@ -183,7 +194,13 @@ private void parsePathElement(Element element) {
case "move":
generateCanvasMethodCall(format("moveTo(%s, %s)", element.getAttribute("x"), element.getAttribute("y")));
break;

case "quad":
generateCanvasMethodCall(format("quadTo(%s, %s, %s, %s)",
element.getAttribute("x1"), element.getAttribute("y1"),
element.getAttribute("x2"), element.getAttribute("y2")));
break;
default:
logDebug("Unsupported element: " + nodeName);
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/mxgraph/FileTooling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mxgraph;

import java.io.File;
import java.util.Arrays;

public class FileTooling {

public static File[] svgSourceFiles(String... fileNames) {
File parent = new File(System.getProperty("user.dir"), "src/test/resources/svg"); // ensure we pass absolute path
return Arrays.stream(fileNames)
.map(fileName -> new File(parent, fileName))
.toArray(File[]::new);
}

public static File svgSourceFile(String fileName) {
return svgSourceFiles(fileName)[0];
}

public static File destinationFolder(String folderName) {
return new File("target/test/output/", folderName);
}

}
38 changes: 38 additions & 0 deletions src/test/java/com/mxgraph/svg2js/Svg2JsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.mxgraph.svg2js;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;

import static com.mxgraph.FileTooling.*;
import static com.mxgraph.utils.FileUtils.EOL;
import static org.assertj.core.api.Assertions.assertThat;

class Svg2JsTest {

private final Svg2Js xml2Js = new Svg2Js().infoLog(false).debugLog(false);

@Test
void process_svg_file() throws IOException {
File workingDirectory = destinationFolder("Svg2Js/work");

assertThat(xml2Js.convertToJsCode(svgSourceFile("simple-02/path-blue.svg"), workingDirectory))
.isEqualTo(toCode("// shape: path-blue",
"// width: 70",
"// height: 50",
"// foreground",
"canvas.begin();",
"canvas.moveTo(0, 25);",
"canvas.quadTo(20, 0, 30, 25);",
"canvas.quadTo(40, 50, 70, 25);",
"canvas.stroke();"
)
);
}

private String toCode(String... lines) {
return String.join(EOL, lines);
}

}
34 changes: 9 additions & 25 deletions src/test/java/com/mxgraph/svg2xml/Svg2XmlTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mxgraph.svg2xml;

import static com.mxgraph.FileTooling.destinationFolder;
import static com.mxgraph.FileTooling.svgSourceFiles;
import static com.mxgraph.utils.FileUtils.EOL;
import static com.mxgraph.utils.FileUtils.fileContent;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -10,9 +12,13 @@

class Svg2XmlTest {

private static Svg2Xml newSvg2Xml() {
return new Svg2Xml().infoLog(false);
}

@Test
void convertToXml_single_file() {
Svg2Xml svg2Xml = new Svg2Xml();
Svg2Xml svg2Xml = newSvg2Xml();

File destPath = destinationFolder("Simple-Single file");
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg"), destPath);
Expand All @@ -29,7 +35,7 @@ void convertToXml_single_file() {

@Test
void convertToXml_two_files_from_the_same_folder() {
Svg2Xml svg2Xml = new Svg2Xml();
Svg2Xml svg2Xml = newSvg2Xml();

File destPath = destinationFolder("Simple-Two files");
svg2Xml.convertToXml(svgSourceFiles("simple-01/circle-green.svg", "simple-01/rectangle-blue.svg"), destPath);
Expand All @@ -48,7 +54,7 @@ void convertToXml_two_files_from_the_same_folder() {

@Test
void convertToXml_files_from_two_folders_without_subfolders_files_given_ordered_by_folders() {
Svg2Xml svg2Xml = new Svg2Xml();
Svg2Xml svg2Xml = newSvg2Xml();

File destPath = destinationFolder("files from 2 folders - no subfolders");
// in the current implementation, the files are supposed to be passed ordered by folder
Expand All @@ -72,26 +78,4 @@ void convertToXml_files_from_two_folders_without_subfolders_files_given_ordered_
);
}

// =================================================================================================================
// UTILS
// =================================================================================================================

private static void assertFirstLine(String fileContent, String expectedStart, String expectedEnd) {
String firstLine = fileContent.substring(0, fileContent.indexOf(EOL));
assertThat(firstLine).describedAs("1st line of the generated file")
.startsWith(expectedStart)
.endsWith(expectedEnd);
}

private static File[] svgSourceFiles(String... fileNames) {
File parent = new File(System.getProperty("user.dir"), "src/test/resources/svg"); // ensure we pass absolute path
return Arrays.stream(fileNames)
.map(fileName -> new File(parent, fileName))
.toArray(File[]::new);
}

private static File destinationFolder(String folderName) {
return new File("target/test/output/", folderName);
}

}
2 changes: 2 additions & 0 deletions src/test/java/com/mxgraph/xml2js/Xml2JsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void parse_code_lines() {
.element("<curve x1='85.52' x2='39.66' x3='39.66' y1='0' y2='45.87' y3='102.25'/>")
.element("<line x='39.66' y='131.4'/>")
.element("<arc large-arc-flag='0' rx='6.43' ry='6.43' sweep-flag='1' x='95.51' x-axis-rotation='0' y='135.03'/>")
.element("<quad x1='20' x2='30' y1='0' y2='25'/>")
.element("<close/>")
.end()
;
Expand All @@ -30,6 +31,7 @@ void parse_code_lines() {
"canvas.curveTo(85.52, 0, 39.66, 45.87, 39.66, 102.25);",
"canvas.lineTo(39.66, 131.4);",
"canvas.arcTo(6.43, 6.43, 0, 0, 1, 95.51, 135.03);",
"canvas.quadTo(20, 0, 30, 25);",
"canvas.close();",
"canvas.fillAndStroke();"
);
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/svg/simple-02/path-blue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.