-1

Is there any commandline tool that can remove transforms from an SVG file (or rather apply the transforms), translating all coordinates to stay in place?

This is one of the thing that none of the most well-known SVG cleaning tools (svgo, svgcleaner, etc) handles, as far as I understand.

In other words, I would like to go from something like this:

<g
   transform="matrix(0.9,0,0,0.99,650,280)"
>
  <path
     d="M591,1037 L589,1044 ...
  >
</g>

to something like this

<g>
  <path
     d="M204,503 L321,403 ...
  >
</g>

...or from something like this:

<path
   transform="matrix(0.9,0,0,0.99,650,280)"
   d="M591,1037 L589,1044 ...
>

to something like this

<path
   d="M204,503 L321,403 ...
>

Links

For reference, there is an old, unmaintained Inkscape extension that does this: Apply Transforms)

Stack Overflow question, with tips on how to do this in Inkscape (through the GUI), including a note that this used to be possible with svgo: Removing transforms in SVG files

Open ticket at the svgo repo: Please add the option to flatten transforms #624

leo
  • 339
  • Can you give a specific example of what you want to do? Ideally, provide an svg input file and show the output you expect. Some things can likely be done using ImageMagic or even command line options for Gimp or Inkscape, but we'll need specific examples to be able to check. – terdon Feb 01 '24 at 13:26

1 Answers1

2

SVGO works fine for your example. I tested on 3.2.0. npm i -g svgo and svgo in1.svg in2.svg -o out1.svg out2.svg:

in1.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10000 10000">
<g
   transform="matrix(0.9,0,0,0.99,650,280)"
>
  <path
     d="M591,1037 L589,1044"
  />
</g>
</svg>

in2.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10000 10000">
<path
   transform="matrix(0.9,0,0,0.99,650,280)"
   d="M591,1037 L589,1044"
/>
</svg>

out1.svg = out2.svg

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10000 10000"><path d="m1181.9 1306.63-1.8 6.93"/></svg>

Observe that all transforms are removed (applied) and things stay in place. I don't know why that issue is still open, possibly it's because of edge cases, but removing transforms is enabled by default.

Keep an eye on the recent pull requests, and update when there are new releases. There are some small bugs that will be fixed soon. Shapes will be supported but the question talks about paths.

Daniel T
  • 4,594
  • Oh, I was mislead by the open issue, and for some reason it didn't work for me with my particular file. But with a simpler file it does indeed work, so there must be a bug with some specific cases (as would be expected, with the complexity and possible weird structures that can arise in XML....). Thanks a lot! – leo Feb 01 '24 at 14:44
  • 1
    @leo One of the linked PRs will implement transform removal for cases https://github.com/svg/svgo/issues/594 and https://github.com/svg/svgo/issues/989 . Development on this topic seems to be very active, so I expect this to be working next month – Daniel T Feb 01 '24 at 14:46