Hardly Batik related, but here's a small SVG path element "d" attrib
parser that I wrote for a game I'm working on. I tried to use Batik at
first but the project dependencies needed to stay small so I went with
this. I'm sure it's not efficient, but it works for me. I have some
more code to go with this to parse nested groups, line and fill style
and associated transforms if you're interested.
I use Inkscape to draw graphics and then use "convert-to-path" on all my
primitives so rectangles, circles, etc are converted to paths. Mostly
because I was lazy and didn't want to write parsers for these.
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PathParser {
Pattern elemPtn =
Pattern.compile("([a-zA-Z])\\s*([^a-zA-Z]*)?");
Pattern coordPtn =
Pattern.compile("\\s*([^,\\s]+)\\s*[\\s,]?\\s*([^,\\s]+)");
public static GeneralPath parse(String string) throws
Exception {
GeneralPath gp = new GeneralPath();
PathParser parser = new PathParser();
parser.Input(gp,string);
return gp;
}
/**
* @param gp
* @param string
* @throws Exception
*/
private void Input(GeneralPath gp, String string) throws
Exception {
Matcher m = elemPtn.matcher(string);
while(m.find()) {
//System.out.println(">" + m.group(1));
switch(m.group(1).charAt(0)) {
case('M'):
case('m'):
handleMoveTo(gp,m.group(2));
break;
case('L'):
case('l'):
handleLineTo(gp,m.group(2));
break;
case('C'):
case('c'):
handleCurveTo(gp,m.group(2));
break;
case('Q'):
case('q'):
handleQuadTo(gp,m.group(2));
break;
case('Z'):
case('z'):
gp.closePath();
break;
default:
//do Nothing
}
}
}
/**
* @param gp
* @param string
* @throws Exception
*/
private void handleQuadTo(GeneralPath gp, String string) throws
Exception {
Vector v = parseCoords(string);
Iterator it = v.iterator();
while(it.hasNext()) {
Point2D.Float p1 = (Point2D.Float)it.next();
Point2D.Float p2 = (Point2D.Float)it.next();
if(p1==null || p2==null) {
throw new Exception("");
}
gp.quadTo(p1.x,p1.y,p2.x,p2.y);
}
}
/**
* @param gp
* @param string
* @throws Exception
*/
private void handleCurveTo(GeneralPath gp, String string) throws
Exception {
Vector v = parseCoords(string);
Iterator it = v.iterator();
while(it.hasNext()) {
Point2D.Float p1 = (Point2D.Float)it.next();
Point2D.Float p2 = (Point2D.Float)it.next();
Point2D.Float p3 = (Point2D.Float)it.next();
if(p1==null || p2==null || p3==null) {
throw new Exception("");
}
gp.curveTo(p1.x,p1.y,p2.x,p2.y,p3.x,p3.y);
}
}
/**
* @param gp
* @param string
* @throws Exception
*/
private void handleLineTo(GeneralPath gp, String string) throws
Exception {
Vector v = parseCoords(string);
handleLineTo(gp,v);
}
private void handleLineTo(GeneralPath gp, Vector v) throws
Exception {
Iterator it = v.iterator();
while(it.hasNext()) {
Point2D.Float p1 = (Point2D.Float)it.next();
if(p1==null) {
throw new Exception("");
}
gp.lineTo(p1.x,p1.y);
}
}
/**
* @param gp
* @param string
* @throws Exception
*/
private void handleMoveTo(GeneralPath gp, String string) throws
Exception {
Vector coords = parseCoords(string);
Iterator it = coords.iterator();
Point2D.Float mpt = (Point2D.Float)it.next();
if(mpt==null) {
throw new Exception("");
}
gp.moveTo(mpt.x,mpt.y);
handleLineTo(gp,coords);
}
/**
* @param string
* @return
*/
private Vector parseCoords(String string) {
string = string.trim();
String coords[] = string.split("\\s+");
Vector v = new Vector();
for(int i=0;i<coords.length;i++) {
Matcher m = coordPtn.matcher(coords[i]);
if(m.find()) {
//System.out.println(" (" +
m.group(1) + " , " + m.group(2) + ")");
Point2D pt = new
Point2D.Float(Float.parseFloat(m.group(1)),Float.parseFloat(m.group(2)))
;
v.add(pt);
}
}
return v;
}
}
-----Original Message-----
From: Danilo Costa [mailto:[EMAIL PROTECTED]
Sent: Tuesday, August 24, 2004 8:23 AM
To: [EMAIL PROTECTED]
Subject: SVG to Java2D
Hi!
I 'm new here!
I need to convert SVG Elements to java2D element such as elipses,
rectangles, lines, how ca n I do that easily? Using GVT?
[]s
Dan
--
"...YOU CANNOT KNOW THE MEANING OF YOUR LIFE UNTIL YOU ARE CONNECTED
WITH THE POWER THAT CREATED YOU..."
Shri Mataji Nirmala Devi
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]