views:

44

answers:

2

I'm searching for a library that will allow me to perform graphical manipulations on SVG files. By "manipulations" I mean things like:

  • Merge two overlapping shapes into a single shape
  • Find the geometric center of a shape
  • Draw a copy of a shape that is 25% of the original shape's size

...and other sorts of things that one can do in Illustrator.

I need to build a process that can automate these sorts of tasks and perform them on hundreds of SVG files. I realize that I could write scripts to automate this sort of thing in Illustrator, but I need to run this on a remote machine and can't be reliant on having a running instance of Illustrator.

A: 

One of the Qt libraries have similar functionality and you can probably do most of the transformation operations with it:

Qt SVG

Ross
+1  A: 

I don't know of a library which directly fulfils the requirements you've listed, but I think these things would not be difficult to script simply using the SVG DOM.

For the first task, it sounds like the easiest thing would simply be to group the two shapes.


var shape1 = document.getElementById("shape1");
var shape2 = document.getElementById("shape2");
var newG = document.createElementNS(svgNs,"g");
shape1.parentNode.removeChild(shape1);
shape2.parentNode.removeChild(shape2);
newG.appendChild(shape1);
newG.appendChild(shape2);

For the second task, you can just get the bounding box of a shape and find the centre point of that.


var bbox = shape1.getBBox();
var centrePoint = {x:bbox.x + bbox.width/2, y:bbox.y + bbox.height/2};

For the third task, you can copy a shape, and then apply a scale transformation to it.


var shape1Clone = shape1.cloneNode(true);
shape1Clone.setAttributeNS(null,"transform","scale(.75)") 

In order to automate this so that you can run it on a remote machine, you can use the Apache Batik library, and do the scripting with Rhino. Take a look at this for an example how to do this:

echo-flow