Jérôme Belleman
Home  •  Tools  •  Posts  •  Talks  •  Travels  •  Graphics  •  About Me

Inkscape: Avoiding Transforms Moving Objects

23 Jul 2017

Object positions can be read from the XML that Inkscape writes. They can be subject to transforms, harder to understand. Here's how to make sense out of this.

1 The Problem

Suppose we've got a simple rectangle somewhere in the middle of the canvas. Inkscape would store it as follows in its SVG file:

<rect
   id="rect3336"
   width="10"
   height="10"
   x="247.14285"
   y="296.63052" />

A program needing to work with its position would only have to collect x and y. There only remains the small hassle that an object which Inkscape reports as being located at (0,0) is in fact stored at something like (0,1042.3622) in the XML file for an A4 sheet. They don't use the same origin. But that's OK to deal with, it's just a question of a few subtractions to translate one into the other.

The problem arises when you start working with groups. Make a 1-object group for the rectangle and move it somewhere else on the canvas. The resulting XML will be:

<g
   id="g3370"
   transform="translate(113.13709,125.25892)">
  <rect
     y="296.63052"
     x="247.14285"
     height="10"
     width="10"
     id="rect3336" />
</g>

See how the x and y values haven't changed. But the group has got a transform attribute which you'd have to take into account to determine the actual position of your object. Annoyingly, if I kept the original position of my object (e.g. with guides) and moved it back there to check:

<g
   id="g3370"
   transform="translate(-4.7765415e-6,-3.0989256e-6)">
  <rect
     y="296.63052"
     x="247.14285"
     height="10"
     width="10"
     id="rect3336" />
</g>

... I see that the translate attribute doesn't even disappear but only bears very small values of the order of 10-6 – like it was moved just a little.

Now of course I could have my programs make the effort to apply the transform to my object to determine its position. It becomes quickly more annoying when I realise that other types of transforms comprise a matrix, a scale, a rotate, a skewX or a skewY. There's quite a bit of geometry work involved there, Inkscape doesn't provide libraries to deal with them and I couldn't be bothered so far to write functions to apply transforms.

2 Where Do Transforms Come From?

As we saw in this previous example, it appears transforms come into play with groups. Making a clone (the use SVG tag) would cause Inkscape to add a transform attribute too, even though I tried adding x and y attributes and this works just as well (as a position which is relative to the object it originates from). There might be other cases where Inkscape uses transforms, I can't think of any other ones off the top of my head.

3 Turning Transforms into Absolute Positions

If you have a group which you moved, you can simply ungroup it and regroup it immediately. The x and y attributes will be updated to become values easier to work with and the transform attribute will be altogether removed.

Likewise, unlinking a clone will set x and y to more absolute values and the transform attribute will also be removed. Now, it becomes clear that this approaches is rather destructive and is best used on a temporary copy of your work which you will use only at the end of your workflow for output purposes.

4 References