Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge duplicate defs #802

Open
drewnoakes opened this issue Sep 28, 2017 · 9 comments
Open

Merge duplicate defs #802

drewnoakes opened this issue Sep 28, 2017 · 9 comments

Comments

@drewnoakes
Copy link
Contributor

drewnoakes commented Sep 28, 2017

An Inkscape document I created used mid-markers on paths.

After running SVGO I see:

<defs>
  <marker id="a" orient="auto" overflow="visible">
    <path fill="#727272" fill-rule="evenodd" stroke-width="1pt" d="M-2.5-1c0 2.76-2.24 5-5 5s-5-2.24-5-5 2.24-5 5-5 5 2.24 5 5z" transform="matrix(.2 0 0 .2 1.48 .2)"/>
  </marker>
  <marker id="b" orient="auto" overflow="visible">
    <path fill="#727272" fill-rule="evenodd" stroke-width="1pt" d="M-2.5-1c0 2.76-2.24 5-5 5s-5-2.24-5-5 2.24-5 5-5 5 2.24 5 5z" transform="matrix(.2 0 0 .2 1.48 .2)"/>
  </marker>
  ...
</defs>

<path marker-mid="url(#a)" ... > ... </path>
<path marker-mid="url(#b)" ... > ... </path>
...

This particular document has 15 duplicate marker definitions. Merging them reduces the file size by 30%.

Would merging these duplicate markers be a safe optimisation for SVGO? Are there similar examples I can look at to help build a PR?

As I said elsewhere, I'm interested in making some contributions to this project. I'd like some feedback on ideas before putting together PRs.

@GreLI
Copy link
Member

GreLI commented Sep 29, 2017

I guess, it can be. However, I think it's a rare case, so it will not give much profit in general. There is no same plugin. mergePaths concatenates sibling paths with same params, but you'll have to track IDs.

@drewnoakes
Copy link
Contributor Author

@GreLI thanks for your responses.

I suppose there are two general scenarios that could be looked at here:

  1. Merge duplicate defs
  2. Remove unused defs

Both will require the ability to track IDs, as you say.

My questions are:

  • Is the removal of unused defs already implemented? (I didn't find it when I looked)
  • Is there any guidance on how defs can be referenced/discovered elsewhere in the document? (e.g. specific attributes, specific syntax, whole/partial attribute value matching on url\(#[^)\s]+\) or similar)

@drewnoakes drewnoakes changed the title Merging duplicate markers Merging duplicate and removing unused defs Sep 29, 2017
@drewnoakes
Copy link
Contributor Author

cleanupIDs.js discovers IDs and can serve as a reference for this.

@GreLI
Copy link
Member

GreLI commented Sep 29, 2017

Yes, useless defs are being removed by removeUselessDefs. cleanupIDs removes unused IDs, so it removes defs without IDs.
No guidance for that for now. Only cleanupIDs saves IDs for its internal need.s

@drewnoakes drewnoakes changed the title Merging duplicate and removing unused defs Merge duplicate defs Sep 30, 2017
@jakwings
Copy link

jakwings commented Dec 12, 2017

I want this feature. These duplicates are usually generated by copy&paste in the SVG editor.

@flipflop97
Copy link

Yes please, Inkscape creates lots of duplicate defs.

@guignonv
Copy link

guignonv commented May 1, 2018

+1. I do a lot of copy/paste and generate a huge lot of duplicate defs. My document takes a lot of time to load! :-S Would be nice to optimize that.

@PRR24
Copy link

PRR24 commented Oct 3, 2021

My +1, very useful to remove duplicate raster images (for exmaple created by inkscape or poppler), significantly reducting some svg-s.

@nschloe
Copy link

nschloe commented Dec 1, 2023

I also find SVGs in the wild with excessive use of duplicate <def>s. Most of the time those are simple red lines like

b

<defs>
  <image ns1:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAABCAIAAABR8BlyAAAABmJLR0QA/wD/AP+gvaeTAAAADklEQVR42mO4lZZGNgIA9wYg+cZ4RWoAAAAASUVORK5CYII=" id="f" width="20" height="1" x="0" y="0" />
...
</defs>

with hundreds of repetitions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants