Skip to content

Commit

Permalink
update viz and salamander
Browse files Browse the repository at this point in the history
  • Loading branch information
nidi3 committed Mar 26, 2017
1 parent 99f12c6 commit c5dd101
Show file tree
Hide file tree
Showing 25 changed files with 1,429 additions and 101 deletions.
14 changes: 7 additions & 7 deletions README.md
Expand Up @@ -17,7 +17,7 @@ The basic usage is as follows (assuming `import static guru.nidi.graphviz.model.
[//]: # (basic)
```java
Graph g = graph("example1").directed().with(node("a").link(node("b")));
Graphviz.fromGraph(g).render().toFile(new File("example/ex1.png"));
Graphviz.fromGraph(g).render(PNG).toFile(new File("example/ex1.png"));
```
[//]: # (end)
<img src="https://rawgit.com/nidi3/graphviz-java/master/example/ex1.png" width="100">
Expand All @@ -29,10 +29,10 @@ The size of the resulting image, the rendering engine and the output format can
```java
Graph g = graph("example5").directed().with(node("a").link(node("b")));
Graphviz viz = Graphviz.fromGraph(g);
viz.width(200).engine(NEATO).render().toFile(new File("example/ex5.png"));
viz.width(200).engine(NEATO).render(PNG).toFile(new File("example/ex5.png"));
viz.render(SVG).toFile(new File("example/ex5.svg"));
String json = viz.render(JSON).toString();
BufferedImage image = viz.render().toImage();
BufferedImage image = viz.render(PNG).toImage();
```
[//]: # (end)
<img src="https://rawgit.com/nidi3/graphviz-java/master/example/ex5.png" width="200">
Expand Down Expand Up @@ -60,7 +60,7 @@ Graph g = graph("example2").directed().with(
to(compare).with(Color.RED)),
init.link(mkString));

Graphviz.fromGraph(g).render().toFile(new File("example/ex2.png"));
Graphviz.fromGraph(g).render(PNG).toFile(new File("example/ex2.png"));
```
[//]: # (end)
<img src="https://rawgit.com/nidi3/graphviz-java/master/example/ex2.png" width="500">
Expand Down Expand Up @@ -88,7 +88,7 @@ Graph g = graph("example3").directed()
between(loc("f4"), node5.loc("v", NORTH))),
node2.link(between(loc("p"), node6.loc(NORTH_WEST))),
node4.link(between(loc("p"), node7.loc(SOUTH_WEST))));
Graphviz.fromGraph(g).render().toFile(new File("example/ex3.png"));
Graphviz.fromGraph(g).render(PNG).toFile(new File("example/ex3.png"));
```
[//]: # (end)
<img src="https://rawgit.com/nidi3/graphviz-java/master/example/ex3.png" width="500">
Expand Down Expand Up @@ -118,7 +118,7 @@ graph {
[//]: # (manipulate)
```java
MutableGraph g = Parser.read(getClass().getResourceAsStream("/color.dot"));
Graphviz.fromGraph(g).render().toFile(new File("example/ex4-1.png"));
Graphviz.fromGraph(g).render(PNG).toFile(new File("example/ex4-1.png"));

g.generalAttrs()
.add(Color.WHITE.gradient(Color.rgb("888888")).background().angle(90))
Expand All @@ -127,7 +127,7 @@ g.generalAttrs()
node.add(
Color.named(node.label().toString()),
Style.lineWidth(4).and(Style.FILLED)));
Graphviz.fromGraph(g).render().toFile(new File("example/ex4-2.png"));
Graphviz.fromGraph(g).render(PNG).toFile(new File("example/ex4-2.png"));
```
[//]: # (end)
<img src="https://rawgit.com/nidi3/graphviz-java/master/example/ex4-2.png" width="400">
Binary file modified example/ex1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/ex2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/ex3.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/ex4-1.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/ex4-2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified example/ex5.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions pom.xml
Expand Up @@ -66,6 +66,23 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<unjar src="svg-salamander-core-2017-03-26.jar" dest="target/classes"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/guru/nidi/graphviz/attribute/Font.java
Expand Up @@ -16,6 +16,9 @@
package guru.nidi.graphviz.attribute;

public final class Font {
private Font() {
}

public static Attribute[] def(String name, int size) {
return new Attribute[]{name(name), size(size)};
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public String execute(String src, Engine engine, Format format) {
} catch (InterruptedException e) {
//ignore
}
return doExecute(vizExec(src, engine, format));
return doExecute(src.startsWith("Viz(") ? src : vizExec(src, engine, format));
}

private void init() {
Expand All @@ -71,13 +71,13 @@ public void release() {

protected abstract String doExecute(String call);

protected String vizCode() throws IOException {
try (final InputStream in = getClass().getResourceAsStream("/viz-1.7.1.js")) {
protected String vizCode(String version) throws IOException {
try (final InputStream in = getClass().getResourceAsStream("/viz-" + version + ".js")) {
return IoUtils.readStream(in);
}
}

protected String initEnv(){
protected String initEnv() {
return "var $$prints=[], print=function(s){$$prints.push(s);};";
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/guru/nidi/graphviz/engine/Engine.java
@@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 Stefan Niederhauser (nidin@gmx.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.graphviz.engine;

public enum Engine {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/guru/nidi/graphviz/engine/Format.java
@@ -1,3 +1,18 @@
/*
* Copyright (C) 2015 Stefan Niederhauser (nidin@gmx.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.graphviz.engine;

public enum Format {
Expand Down
Expand Up @@ -30,6 +30,11 @@ public GraphvizJdkEngine() {

public GraphvizJdkEngine(EngineInitListener engineInitListener) {
super(false, engineInitListener);
final String[] version = System.getProperty("java.version").split("\\.");
if (version[1].equals("8") && Integer.parseInt(version[2].substring(2)) > 31) {
throw new IllegalStateException("JDK 1.8 javascript engines of versions greater than 1.8.0_31 do not run viz.js, sorry!\n" +

This comment has been minimized.

Copy link
@ScalaWilliam

ScalaWilliam Jun 9, 2017

I'm curious what the reason for this is? Can't seem to find any issue for this anywhere.

"Downgrade the JDK, use V8 engine or try with a 1.9 version.");
}
}

@Override
Expand All @@ -55,7 +60,7 @@ protected String doExecute(String call) {
@Override
protected void doInit() throws Exception {
ENGINE.eval(initEnv());
ENGINE.eval(vizCode());
ENGINE.eval(vizCode("1.4.1"));
ENGINE.eval("Viz('digraph g { a -> b; }');");
}
}
6 changes: 5 additions & 1 deletion src/main/java/guru/nidi/graphviz/engine/GraphvizServer.java
Expand Up @@ -19,6 +19,8 @@
import java.net.ServerSocket;
import java.net.Socket;

import static guru.nidi.graphviz.engine.Format.SVG_STANDALONE;

final class GraphvizServer {
static final int PORT = 10234;

Expand All @@ -37,6 +39,8 @@ public static void main(String[] args) throws IOException {
System.out.println("starting graphviz server...");
Graphviz.useEngine(new GraphvizV8Engine(e -> new GraphvizJdkEngine()));
System.out.println("started.");
Graphviz.initEngine();
System.out.println("inited.");
try (final ServerSocket ss = new ServerSocket(PORT)) {
while (true) {
try (final Socket socket = ss.accept();
Expand All @@ -48,7 +52,7 @@ public static void main(String[] args) throws IOException {
}
final String s = com.readContent(len);
try {
final String svg = Graphviz.fromString(s).render().toString();
final String svg = Graphviz.fromString(s).render(SVG_STANDALONE).toString();
com.writeStatus("ok");
com.writeContent(svg);
} catch (GraphvizException e) {
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/guru/nidi/graphviz/engine/GraphvizV8Engine.java
Expand Up @@ -20,12 +20,14 @@
import com.eclipsesource.v8.V8RuntimeException;
import com.eclipsesource.v8.utils.V8ObjectUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class GraphvizV8Engine extends AbstractGraphvizEngine {
private final static Pattern ABORT = Pattern.compile("^undefined:\\d+: abort");
private final static Pattern ERROR = Pattern.compile("^undefined:\\d+: (.*?)\n");
private V8 v8;
private V8Array messages;

Expand All @@ -48,19 +50,23 @@ protected void doInit() throws Exception {
v8 = V8.createV8Runtime();
v8.executeVoidScript(initEnv());
messages = v8.getArray("$$prints");
v8.executeVoidScript(vizCode());
v8.executeVoidScript(vizCode("1.7.1"));
}

@Override
protected String doExecute(String call) {
try {
return v8.executeStringScript("$$prints.splice(0,100); " + call);
return v8.executeStringScript(call);
} catch (V8RuntimeException e) {
if (ABORT.matcher(e.getMessage()).find()) {
throw new GraphvizException(IntStream.range(0, messages.length())
.mapToObj(i -> V8ObjectUtils.getValue(messages, i).toString())
.collect(Collectors.joining("\n")));
}
final Matcher em = ERROR.matcher(e.getMessage());
if (em.find()) {
throw new GraphvizException(em.group(1));
}
throw new GraphvizException("Problem executing graphviz", e);
}
}
Expand Down
47 changes: 38 additions & 9 deletions src/main/java/guru/nidi/graphviz/engine/Renderer.java
@@ -1,8 +1,23 @@
/*
* Copyright (C) 2015 Stefan Niederhauser (nidin@gmx.ch)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package guru.nidi.graphviz.engine;

import com.kitfox.svg.SVGDiagram;
import com.kitfox.svg.SVGException;
import com.kitfox.svg.SVGUniverse;
import com.kitfox.svg.*;
import com.kitfox.svg.animation.AnimationElement;
import com.kitfox.svg.xml.StyleAttribute;

import javax.imageio.ImageIO;
import java.awt.*;
Expand Down Expand Up @@ -39,18 +54,14 @@ public String toString() {

public void toFile(File file) throws IOException {
if (format == PNG) {
toPngFile(file);
writeToFile(file, "png", toImage());
} else {
try (final Writer out = new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8)) {
out.write(toString());
}
}
}

public void toPngFile(File file) {
writeToFile(file, "png", toImage());
}

public BufferedImage toImage() {
if (format != PNG && format != SVG && format != SVG_STANDALONE) {
throw new IllegalStateException("Images can only be rendered from PNG and SVG formats.");
Expand Down Expand Up @@ -81,12 +92,30 @@ public BufferedImage toImage() {

private SVGDiagram createDiagram(String svg) {
final SVGUniverse universe = new SVGUniverse();
final URI uri = universe.loadSVG(new StringReader(svg), "graph");
final URI uri = universe.loadSVG(new StringReader(svg), "//graph/");
final SVGDiagram diagram = universe.getDiagram(uri);
replaceTransparent(diagram.getRoot());
diagram.setIgnoringClipHeuristic(true);
return diagram;
}

private void replaceTransparent(SVGElement element) {
final StyleAttribute stroke = element.getPresAbsolute("stroke");
if (stroke != null && "transparent".equals(stroke.getStringValue())) {
try {
element.setAttribute("stroke", AnimationElement.AT_XML, "#fff");
if (!element.hasAttribute("stroke-opacity", AnimationElement.AT_XML)) {
element.addAttribute("stroke-opacity", AnimationElement.AT_XML, "0.0");
}
} catch (SVGElementException e) {
e.printStackTrace();
}
}
for (int i = 0; i < element.getNumChildren(); i++) {
replaceTransparent(element.getChild(i));
}
}

private void renderDiagram(SVGDiagram diagram, Graphics2D graphics) {
try {
diagram.render(graphics);
Expand Down
53 changes: 0 additions & 53 deletions src/main/resources/viz-1.0.1.js

This file was deleted.

0 comments on commit c5dd101

Please sign in to comment.