RazrFalcon / svgcleaner

svgcleaner could help you to clean up your SVG files from the unnecessary data.
GNU General Public License v2.0
1.62k stars 93 forks source link

Add an option to remove units #158

Open juanitogan opened 6 years ago

juanitogan commented 6 years ago

Thank you for the excellent tool. It made converting from Inkscape to Unity and SVG Importer much easier.

This is an edge case but SVG Importer does not have a very intelligent parser and the inch units that Inkscape leaves on only the "height" and "width" mess up the canvas sizing. Thus, it would be nice to have an option so scrub all units out for tools like this.

<svg height="3.32282in" viewBox="0 0 7.317276 3.322816" width="7.31728in" xmlns="http://www.w3.org/2000/svg">

RazrFalcon commented 6 years ago

Hi.

Units cannot be converted without knowing the target DPI. And even if you provide one it will make an SVG less portable/correct.

I'm still not sure how this should be implemented in the svgcleaner.

On other hand, if you want to create a very simple SVG file, that can be imported correctly by any application, you can use usvg. It's still a beta quality and not available for end users, but it's the best tool for the job.

juanitogan commented 6 years ago

Yes, I understand that about unit conversion. But I'm not asking for conversion. I'm asking to just assume every length in the SVG is in the same unit and to just delete the unit text like I do manually now. For example, for some reason I don't understand, the default unit with Inkscape SVGs is the inch, but it only marks height and width with "in" even though all other lengths are also in inches. I don't know if this is correct or not but my other SVG tool never reads this correctly and I need to delete the "in" text.

From this:

<svg height="3.32282in" viewBox="0 0 7.317276 3.322816" width="7.31728in" xmlns="http://www.w3.org/2000/svg">

to this:

<svg height="3.32282" viewBox="0 0 7.317276 3.322816" width="7.31728" xmlns="http://www.w3.org/2000/svg">

That's it. I don't care about actual size as long as everything scales the same. This will not be useful to most people so it was just an idea. Thanks for pointing me to usvg. It may be more what I need in the long run.

RazrFalcon commented 6 years ago

This is totally incorrect and will not be implemented.

I presume that Inkscape uses inches for all coordinates implicitly, so removing the in doesn't break your file.

JoKalliauer commented 6 years ago

@juanitogan No the default unit in Inkscape is not "in", depening on your locale Inkscape might change the default to "in". If you want to change it for the current file try this link: http://lmgtfy.com/?t=i&q=inkscape+document+properties.

If you want to change it for new files, then edit the acording template in 'C:\Program Files\Inkscape\share\templates' (for Windows)

And as @RazrFalcon said : Your suggestion does not make sence.

If the file would have all definitions in "in" then it should also have "viewBox="0 0 7.317276in 3.322816in" then it would be possible to remove all in, whithout even changing the size, otherwise you get a different Part of the picture is visuable (if just viewBox does not have "in", otherwise you would break the file).

But if 'height="3.32282in"' and 'width="7.31728in"' is the problem, then https://github.com/svg/svgo/blob/master/plugins/removeDimensions.js might solve your problem.

RazrFalcon commented 6 years ago

@JoKalliauer viewBox can't have units.

JoKalliauer commented 6 years ago

@RazrFalcon : Sorry, you are right.

@juanitogan : Now I understand your querry, sorry missunderstood it.

I just created a svg with inkscape using

  1. <svg width="8.2677164in" height="11.692913in">
  2. <svg><sodipodi:namedview units="in"></svg>
  1. <svg><sodipodi:namedview><inkscape:grid units="mm" /></sodipodi:namedview></svg>

But all the data inside seems to be created without units (same as "px", so same unit as viewBox), only the scaling is done with <svg viewBox="0 0 210 297" width="210mm" height="297mm" (A4) or <svg width="8.5in" height="11in" viewBox="0 0 8.5 11">´(US-letter).

I don't know the exact problem of the tool, but one of the following might solve the problem:

I presume that Inkscape uses inches for all coordinates implicitly, so removing the in doesn't break your file. Tested it. Inkscape can uses different coordinates implicitly "mm" for Templatefile for locale "de" and "in" for the Templatefile locale "en_US", therefore I think removing in seems to be the most appropriate. (So it depends on the svg-file/svg-template you open.)