1

Is there a command-line tool to remove most "g transform..."s from my SVG files, so I can see the "absolute" location of my points/lines/polygons?

In other words, has anyone implemented the matrix multiplication technique explained in: Flattening SVG matrix transforms in Inkscape and How to get the actual x/y position of an element in SVG with transformations and matrices as a command-line tool?

SVG Cleaner appears to require Qt and does not appear to be a command-line tool (plus, I couldn't get it to compile)

Community
  • 1
  • 1

1 Answers1

2

No, nobody has made this tool. At least, not that either you or I can find. Part of the problem is in your definition. You ask to remove the transforms,

[…] so I can see the "absolute" location of my points/lines/polygons.

For a subset of SVG elements and transforms it is possible to bake the transform into the element's attributes, but it can't work in the general case. For example, consider this simple case:

<svg xmlns="http://www.w3.org/2000/svg">
  <g transform="translate(-10,10) skewX(57) rotate(45) translate(17,17)">
    <rect fill="red" stroke="blue" width="20" height="30" />
  </g>
</svg>

Skewed, rotated rectangle

There is no possible way to modify the <rect> element's specific attributes so that it shows the same result. The best you can do is to put a transform="…" attribute on the <rect> element representing the total global transforms. This is relatively easy to do in an SVG UA, slightly harder to do in a standalone command-line tool (but still possible). I would help you do it, but I figure the results don't help you, as they don't tell you anything about the absolute location of the points.

You might be tempted to say "No, I can't do it with a <rect>, but I could convert the <rect> into a <polygon> or a <path> and use absolute coordinates for those points." But you'd only be half correct. If you notice the varying apparent stroke width due to the skew, you will see that no single <polygon> can accurately represent the skewed <rect>.

In general, the only thing you can do is bake the sum of all <g> transforms onto the individual elements, and realize that such transforms may drastically affect the final location and appearance of the content.

Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Just to be cantankerous... in my opinion, 'stroke' is really a decoration, not an actual object. I'm still wondering if one could write a program that at least puts all the "true objects" into an absolute coordinate system. Aside from my whining, your answer was helpful and will work with the specific SVGs I'm dealing with (ones created by Mathematica's ListContourPlot function), so upvoting and approving. Will see if I can write the SVG "true" convertor myself. –  Sep 01 '13 at 07:32
  • @barrycarter So: a) what SVG elements do you need to convert? b) Is it OK if some shapes are converted `` instead? – Phrogz Sep 01 '13 at 15:20
  • I'm trying to convert the output of Mathematica's ListContourPlot to KML. svg2kml should do this, but I can't get it working. Ideally, I'd like to generate the contours in KML and outside Mathematica entirely, but haven't gotten that working yet. –  Sep 01 '13 at 18:48
  • I'd agree that is the best approach. It should be possible to transfrom any object into a path (there are approximations for circles to Bezier which should do). And any path can be transformed to another. I do something similar in a different context. – peter.murray.rust Sep 01 '13 at 19:46