xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andres Toussaint <and...@onemileup.com>
Subject Re: SVG to Java2D
Date Thu, 19 Feb 2004 14:33:43 GMT
Hello:

Peter, thanks for the code, I am sure a lot of us will find it useful.

For the meanwhile, I am using Thomas suggestion (thanks!) to get access 
the the Java2D Shape object in the GVT items and from that i intend to 
build the DXF descriptions. It was really clarifing what the GVT stands 
for, before it was a little confusing.

Thanks you guys, and if i come with something useful for all, i will 
post it.

Andres.

On Tuesday, February 17, 2004, at 06:00  PM, Peter Becker wrote:

> Hi Andres,
>
> I did something similar, but the code is just a quick little hack for 
> a particular subset of SVG (only some elements, limited attributes, no 
> groups, no transforms). That was good enough to use little SVGs as 
> icons in Java. And it was also good enough to export some shapes from 
> OpenOffice into SVG and import them into our programs.
>
> Code is attached, feel free to use it. It is not much anyway.
>
>  Peter
>
>
>
> Andres Toussaint wrote:
>
>> Hello:
>>
>> I am interested to get some guideance in creating Java2D objects from 
>> my
>> JSVGCanvas SVGDocument. The purpose of this is to bridge it into a 
>> Java2D
>> to DXF (autoCad) converter that i am also working on.
>>
>> Ultimately I think I would need to have a SVGtoDXFTranscoder, but i 
>> think
>> having a SVGtoJava2DTranscoder is more useful in terms of using the 
>> Java2D
>> for other formats as well.
>>
>> Thanks in advance,
>>
>> Andres.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
>> For additional commands, e-mail: batik-users-help@xml.apache.org
>>
>>
>>
>
> import java.awt.Shape;
> import java.awt.geom.*;
> import java.util.Iterator;
> import java.util.StringTokenizer;
>
> import org.jdom.Element;
>
> /**
>  * This class imports simple SVG images into AWT Shape objects.
>  *
>  * Supported are: <rect>, <circle>, <ellipse>, <line>, <polyline>,

> <polygon>.
>  *
>  * Styles are ignored, transformations are ignored. Group elements are
>  * processed via recursion, but ignored otherwise, the result is the 
> same as
>  * if the group elements have been removed before. The methods work 
> for simple
>  * SVG, but have not been tested on anything complex.
>  *
>  * @todo adding path would greatly enhance this
>  * @todo add tests
>  */
> public class SVG2Shape {
> 	public static Shape importShape(Element svgElement) {
>         GeneralPath shape = importShapeUncentered(svgElement);
>         return centerShape(shape);
> 	}
>
>     private static GeneralPath importShapeUncentered(Element 
> svgElement) {
>         GeneralPath shape = new GeneralPath();
>         Iterator it = svgElement.getChildren().iterator();
>         while(it.hasNext()) {
>         	Element cur = (Element) it.next();
>         	if(cur.getName().equals("rect")) {
>         	    double x = Double.parseDouble(cur.getAttributeValue("x"));
>         	    double y = Double.parseDouble(cur.getAttributeValue("y"));
>         	    double width = 
> Double.parseDouble(cur.getAttributeValue("width"));
>         	    double height = 
> Double.parseDouble(cur.getAttributeValue("height"));
>         		shape.append(new Rectangle2D.Double(x, y, width, 
> height),false);
>             } else if(cur.getName().equals("circle")) {
>         	    double cx = 
> Double.parseDouble(cur.getAttributeValue("cx"));
>         	    double cy = 
> Double.parseDouble(cur.getAttributeValue("cy"));
>         	    double r = Double.parseDouble(cur.getAttributeValue("r"));
>         	    shape.append(new Ellipse2D.Double(cx - r, cy - r, 2*r, 
> 2*r),false);
>             } else if(cur.getName().equals("ellipse")) {
>                 double cx = 
> Double.parseDouble(cur.getAttributeValue("cx"));
>                 double cy = 
> Double.parseDouble(cur.getAttributeValue("cy"));
>                 double rx = 
> Double.parseDouble(cur.getAttributeValue("rx"));
>                 double ry = 
> Double.parseDouble(cur.getAttributeValue("ry"));
>                 shape.append(new Ellipse2D.Double(cx - rx, cy - ry, 
> 2*rx, 2*ry),false);
>             } else if(cur.getName().equals("line")) {
>                 double x1 = 
> Double.parseDouble(cur.getAttributeValue("x1"));
>                 double y1 = 
> Double.parseDouble(cur.getAttributeValue("y1"));
>                 double x2 = 
> Double.parseDouble(cur.getAttributeValue("x2"));
>                 double y2 = 
> Double.parseDouble(cur.getAttributeValue("y2"));
>                 shape.append(new Line2D.Double(x1, y1, x2, y2),false);
>             } else if(cur.getName().equals("polyline") || 
> cur.getName().equals("polygon")) {
>             	StringTokenizer tokenizer = new 
> StringTokenizer(cur.getAttributeValue("points"), " ,");
>             	boolean first = true;
>             	while(tokenizer.hasMoreElements()) {
>             	    float x = Float.parseFloat(tokenizer.nextToken());
>             	    float y = Float.parseFloat(tokenizer.nextToken());
>             	    if(first) {
>             	    	shape.moveTo(x,y);
>             	    	first = false;
>             	    } else {
>             	    	shape.lineTo(x,y);
>             	    }
>             	}
>             	if(cur.getName().equals("polygon")) {
>             		shape.closePath();
>             	}
>             } else if(cur.getName().equals("g")) {
>             	shape.append(importShapeUncentered(cur),false);
>         	}
>         }
>         return shape;
>     }
> 	
>     private static Shape centerShape(Shape shape) {
>     	Rectangle2D bounds = shape.getBounds2D();
>         double xOffset = bounds.getX() - bounds.getWidth()/2;
>         double yOffset = bounds.getY() - bounds.getHeight()/2;
>         return AffineTransform.getTranslateInstance(xOffset, 
> yOffset).createTransformedShape(shape);
>     }
>
>     public static Shape importShape(Element svgElement, double width, 
> double height) {
>     	Shape untransformedShape = importShape(svgElement);
>         float scaleX = (float) (width / 
> untransformedShape.getBounds2D().getWidth());
>         float scaleY = (float) (height / 
> untransformedShape.getBounds2D().getHeight());
>         AffineTransform transform;
>         if(scaleX > scaleY) {
>         	transform = AffineTransform.getScaleInstance(scaleY, scaleY);
>         } else {
>             transform = AffineTransform.getScaleInstance(scaleX, 
> scaleX);
>         }
>         return transform.createTransformedShape(untransformedShape);
>     }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-users-help@xml.apache.org


Mime
View raw message