I have been trying desperately for a few months now to develop a Java utility
using Batik to scale a few SVG files.  I suck at it.  I've read every
website out there trying to figure this S**T out and I just can't get it. 
Between, SVG, Batik and Java, I'm at my wits end now.  I've discovered
Nabble today and I'm trying to wade through the "Scaling" posts but even so,
I'm not seeing/getting it.

Please help.

Here is what I'm trying to do:

1.) Take a random SVG file. I have no interaction with this file, no idea
where it came from or anything about it.

2.) I want to get the maximum bounding box around the entire drawing
objects, paths, etc. I have been using an example of GetBB.java that uses
the GetSensitiveBounds to retrieve the X, Y, Width and Height.

Seemed ok.

3.) Input arbitrary new size (height, width) in inches.
4.) Compute old scale from bounding box height and width.
5.) Compute new scale from inputed height and width.
8.) Move origin point to 0,0.
7.) Transform existing SVG file to new SVG file using new scale.


I am just about ready to pull my F**King hair out trying to get this to
work. I thought I had it but now some of my images are exact MIRRORed
copies.

The end result that I wish to accomplish is:

Take random SVG image, move origin to 0,0; scale to my new value (in inches)
and save to new SVG file.

I'm using Batik and Java.

Please help!

I would like to add that Java is NOT my first language and is frustrating to
me.




Here is the code:

String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);

String svgURI = new File(args[0]).toURL().toString();
dNewWidth = new Double(args[1]);
SVGDocument indoc = (SVGDocument)f.createDocument(svgURI);

//Perform processing
SVGSVGElement svg = (SVGSVGElement)indoc.getRootElement();

NodeList nl = indoc.getElementsByTagName("g");
SVGGElement nd = (SVGGElement)nl.item(0);
Element elt = (Element) nd;
elt.removeAttribute("transform");
svg.removeAttribute("viewBox");



GVTBuilder builder = new GVTBuilder();
BridgeContext ctx;
ctx = new BridgeContext(new UserAgentAdapter());
GraphicsNode gvtRoot = builder.build(ctx, indoc);

Rectangle2D aoi = new Rectangle2D.Float();

dPixelsPerInch = 1d;

dOldX = gvtRoot.getSensitiveBounds().getX();
dOldY = gvtRoot.getSensitiveBounds().getY();
dOldWidth = gvtRoot.getSensitiveBounds().getWidth();
dOldHeight = gvtRoot.getSensitiveBounds().getHeight();

// System.out.println("dOldX: " + dOldX);
// System.out.println("dOldY: " + dOldY);
// System.out.println("dOldWidth: " + dOldWidth);
// System.out.println("dOldHeight: " + dOldHeight);


dNewWidth = dNewWidth * dPixelsPerInch;
dScale = (dNewWidth * 90) / dOldWidth;
dNewHeight = (dOldHeight * dScale) / 90;
dNewX = dOldX * dScale;
dNewY = dOldY * dScale;

aoi.setRect(dNewX, dNewY, dNewWidth, dNewHeight);

// System.out.println("dNewX: " + dNewX);
// System.out.println("dNewY: " + dNewY);
// System.out.println("dNewWidth: " + dNewWidth);
// System.out.println("dNewHeight: " + dNewHeight);

elt.setAttribute("transform", "translate(" + (dNewX * -1) + ", " + (dNewY *
-1) + ") scale(" + dScale + ")");
svg.setAttribute("width", dNewWidth + "in");
svg.setAttribute("height", dNewHeight+ "in");
svg.setAttribute("viewBox", "0 0 " + dNewWidth + " " + dNewHeight);
-- 
View this message in context: 
http://www.nabble.com/SVG-Scaling%2C-Resizing---Manipulation-tp14518243p14518243.html
Sent from the Batik - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to