Skip to content

Commit

Permalink
Made SVG load and export match dimentions
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Mar 23, 2021
1 parent b2bd79c commit 95edbb1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 26 deletions.
31 changes: 10 additions & 21 deletions src/main/java/eu/mihosoft/vrl/v3d/svg/SVGExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SVGExporter {
//0.376975
//public static final double Scale = 3.543307;// SVG px to MM scale facto
private static final double Scale = 3.543307;
private static final double VueBoxSize = 100;
private int colorTicker=0;
public static List<String> colorNames = Arrays.asList("crimson","gray","darkmagenta","darkolivegreen","darkgreen",
"darkblue",
Expand All @@ -32,7 +33,7 @@ public class SVGExporter {
"black",
"tomato");
double min[] = { 0, 0 };
double max[] = { 100, 100 };
double max[] = { VueBoxSize, VueBoxSize };
private ArrayList<String> polylines= new ArrayList<>() ;
private ArrayList<String> groups= new ArrayList<>() ;
private ArrayList<String> layers= new ArrayList<>() ;
Expand Down Expand Up @@ -60,11 +61,11 @@ public String make(){
" xmlns:svg=\"http://www.w3.org/2000/svg\""+
" xmlns=\"http://www.w3.org/2000/svg\""+
" xmlns:sodipodi=\"http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd\""+
" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" version=\"1.1\" viewBox=\"" + (min[0] ) + " "
+ (min[1] ) + " " + (totalX) + " "+
" xmlns:inkscape=\"http://www.inkscape.org/namespaces/inkscape\" version=\"1.1\"\n viewBox=\"" + (0 ) + " "
+ (0) + " " + (totalX) + " "+
(totalY ) + "\""+
" id=\"svg2\" "+
"width=\""+totalXmm+"mm\""+
"\n id=\"svg2\" "+
"\n width=\""+totalXmm+"mm\""+
"\n height=\""+totalYmm+"mm\""+
">\n";
header+= " <defs \n"+
Expand Down Expand Up @@ -131,27 +132,15 @@ private void toPolyLine(Polygon p){
String section = " <polyline points=\"";

for (Vertex v : p.vertices) {
Vector3d position = v.pos;
Vector3d position = v.pos.transformed(new Transform().rotX(180));
double x = (position.x * Scale);
double y = -(position.y * Scale);
double y = (position.y * Scale)+VueBoxSize;
section += x + "," + y + " ";
if (x > max[0]) {
max[0] = x;
}
if (x < min[0]) {
min[0] = x;
}
if (y > max[1]) {
max[1] = y;
}
if (y < min[1]) {
min[1] = y;
}
}
// Close loop
Vector3d position = p.vertices.get(0).pos;
Vector3d position = p.vertices.get(0).pos.transformed(new Transform().rotX(180));
double x = (position.x * Scale);
double y = -(position.y * Scale);
double y = (position.y * Scale)+VueBoxSize;
section += x + "," + y + " ";
section= section + "\" \nstroke=\""+color+"\" \nstroke-width=\"1\" \nfill=\"none\"\nid=\"line"+(lineCounter++)+"\" />\n";
polylines.add(section);
Expand Down
76 changes: 71 additions & 5 deletions src/main/java/eu/mihosoft/vrl/v3d/svg/SVGLoad.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
import org.apache.batik.dom.svg.SVGOMGElement;
import org.apache.batik.dom.svg.SVGOMImageElement;
import org.apache.batik.dom.svg.SVGOMPathElement;
import org.apache.batik.dom.svg.SVGOMPolylineElement;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGImageElement;
import org.w3c.dom.svg.SVGPathSegList;
import org.w3c.dom.svg.SVGPointList;

import com.piro.bezier.BezierPath;
import eu.mihosoft.vrl.v3d.CSG;
import eu.mihosoft.vrl.v3d.Edge;
Expand Down Expand Up @@ -409,12 +412,16 @@ private void loadPath(Node pathNode, double resolution, Transform startingFrame,
if (pathNode != null) {
// System.out.println("\tPath
// "+pathNode.getAttributes().getNamedItem("id").getNodeValue());
if (pathNode.getAttributes() != null) {
//System.out.println("Path loading "+pathNode);
Node transforms = pathNode.getAttributes().getNamedItem("transform");


newFrame = getNewframe(startingFrame, transforms);

newFrame = startingFrame;
try {
Node transforms = pathNode.getAttributes().getNamedItem("transform");
newFrame = getNewframe(startingFrame, transforms);
}catch(Exception ex) {
// no transform
}
try {
if(SVGOMPathElement.class.isInstance(pathNode)) {
// NamedNodeMap attribs = pathNode.getAttributes();
Expand Down Expand Up @@ -468,6 +475,66 @@ private void loadPath(Node pathNode, double resolution, Transform startingFrame,
String code = mpp.toCode();
//System.out.println("\tPath "+pathNode.getAttributes().getNamedItem("id").getNodeValue()+" "+newFrame);
loadComposite(code, resolution, newFrame,encapsulatingLayer,c);
}else if(SVGOMPolylineElement.class.isInstance(pathNode)) {
Color c = null;
//System.out.println("Layer "+encapsulatingLayer);
try {
String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
for(String s:style) {
if(s.startsWith("fill:")) {
String string = s.split(":")[1];
c=Color.web(string);
break;
}
}
}catch(java.lang.IllegalArgumentException ex) {
// this means the fill is set to "none"
}catch(Exception ex) {
//ex.printStackTrace();
}
try {
c=Color.web(pathNode.getAttributes().getNamedItem( "stroke").getNodeValue());
}catch(java.lang.IllegalArgumentException ex) {
// this means the fill is set to "none"
}catch(Exception ex) {
//ex.printStackTrace();
}
if(c==null) {
try {
String []style = pathNode.getAttributes().getNamedItem( "style").getNodeValue().split(";");
for(String s:style) {
if(s.startsWith("stroke:")) {
String string = s.split(":")[1];
c=Color.web(string);
break;
}
}
}catch(java.lang.IllegalArgumentException ex1) {
// this means the stroke is set to "none" the default green color will be used

}catch(Exception ex1) {
//ex1.printStackTrace();
}
}


String sb =null;
SVGOMPolylineElement pathElement = (SVGOMPolylineElement)pathNode;
SVGPointList pathList = pathElement.getPoints();
// String offset = pathElement.getOwnerSVGElement();

int pathObjects = pathList.getNumberOfItems();

for (int i = 0; i < pathObjects; i++) {
SVGItem item = (SVGItem) pathList.getItem(i);
String itemLine = String.format("%s%n", item.getValueAsString());
if(sb==null) {
sb="M "+itemLine;
}
sb += "L "+itemLine;
}
sb+="z\n";
loadComposite(sb, resolution, newFrame,encapsulatingLayer,c);
}else if(SVGOMImageElement.class.isInstance(pathNode)) {
SVGImageElement image = (SVGOMImageElement) pathNode;
//System.out.println("Loading Image element..");
Expand Down Expand Up @@ -496,7 +563,6 @@ private void loadPath(Node pathNode, double resolution, Transform startingFrame,
System.out.println("Found "+pathNode.getClass());
//ex.printStackTrace();
}
}

}

Expand Down

0 comments on commit 95edbb1

Please sign in to comment.