Clean up of unused gradient

Asked by Yannick Warnier

We use Inkscape 0.91 a lot for drawing new icons for the GPL software Chamilo LMS. Recently, we decided to start testing the direct usage of SVG format instead of PNG. We've added that as a test mode in our recent 1.10 version.
However, we have never (so far) been active on the bugtracker (although we do contribute to a series of other projects).

Things were all good but we realized SVG icons that we generated this way were very heavy-weight (the SVG wheighted more bytes than the 16x16, 32x32, 64x64 *and* 128x128px size icons summed up).

So we started reviewing the components of the SVG and found that the filling and border box was mentioning a series of gradients that were not used in our icons.

Then we looked for an option to remove those unused gradients, but only found it in the XML editor.
When editing an icon based on another one, the list of gradients (or just objects in general) to remove from the XML editor can be very long, and the process to remove them is pretty much removing and seeing if the icon changes, which is... well... suboptimal.

So, my question is this: is there any feature hidden somewhere that would allow me to remove automatically all the unused components of my SVG?

Finally, I'd like to thank the team for your great work. It really enables us to give users a more beautiful application. You can check our icons and use them (CC BY-SA) from https://github.com/chamilo/chamilo-lms/tree/master/main/img/icons/svg/

Question information

Language:
English Edit question
Status:
Solved
For:
Inkscape Edit question
Assignee:
No assignee Edit question
Solved by:
su_v
Solved:
Last query:
Last reply:
Revision history for this message
Best su_v (suv-lp) said :
#1

'File > Cleanup Document' will delete any unused (unreferenced) resources in the <defs> section (gradient definitions, clip-paths and masks, filter definitions, pattern definitions, marker definitions, …). If not all are purged immediately, try save, close and reopen the file and run the command again. If definitions are not cleaned up, they are likely still referenced by one of the drawing's objects.

In case there are unused custom swatches [1] defined in the file's <defs> section (single-stop gradients, with the custom attribute 'osb:paint'), you can (optionally) enable purging those in 'Edit > Preferences > Behavior > Cleanup'.

In 'Edit > Preferences > Input/Output > SVG Output' (pane on the right, bottom half), you find options to remove improper attributes, inappropriate style properties and non-useful style properties (on load (reading), on editing or on save (writing)). This can additionally help to reduce the file size.

Another option would be to save a copy as 'Optimized SVG' - this output format uses the python script 'Scour' [2] with various options to optimize SVG content. I'd recommend to always save the original file as 'Inkscape SVG', and to save a copy as 'Optimized SVG' if needed.

--
[1] http://tavmjong.free.fr/INKSCAPE/MANUAL/html/Attributes-Fill-Stroke.html#Attributes-CustomSwatches
[2] http://www.codedread.com/scour/

Revision history for this message
Yannick Warnier (ywarnier) said :
#2

Great, thanks a lot! I just checked the optimized SVG and that reduced a large 86KB SVG to 51KB with the default options. At the moment we don't have a new icon with unused gradients, but I will try the clean document option as soon as that happens.

As a suggestion, I think it would be good to add an option to remove a gradient in the gradients list, if you know you're not going to use this one ever again, but you still doubt about others. At the moment, you can only remove them one by one from the XML editor (but you can add others with a "+" icon). I'll leave it up to you. Let me know if you feel I should suggest that as a feature request.

Revision history for this message
Yannick Warnier (ywarnier) said :
#3

Thanks ~suv, that solved my question.

Revision history for this message
su_v (suv-lp) said :
#4

On 2015-11-03 01:02 (+0100), Yannick Warnier wrote:
> I think it would be good to add an option to remove a gradient in the
> gradients list, (…)

Did you think this through? The list of gradients being displayed in 'Fill and Stroke' is tied to a current selection, and only available if the fill (or stroke) of the selected object(s) uses a gradient (this did not change compared to earlier versions - they listed the same available defined gradients in a drop down menu instead of a scrollable list). In order to select a gradient in the list, you'd have to assign it to the current selection (either change a selected object's existing gradient to the one meant to be deleted; or change the selection's current fill (or stroke) to gradient first, then assign the gradient meant to be deleted). But now the gradient is no longer "unused", and allowing users to delete gradients currently in use with a simple click in Fill&Stroke probably would result in more problems with gradients accidentally purged for a larger group of users than help the (likely smaller) user group aiming for lean or leanest SVG files while unwilling to use the the provided command to purge unused resources.

Inkscape did and still does automatically garbage-collect unused gradients which had been created via Fill&Stroke and not been edited afterwards:
«Newly created gradients are automatically garbage-collected when no longer used; however, when you edit a gradient in the Gradient Editor, it becomes sticky and can only be removed, when not in use, by the Vacuum Defs command.»
http://wiki.inkscape.org/wiki/index.php/Release_notes/0.40#Interface_and_usability

If you want to fundamentally change how 'Fill and Stroke' works (i.e. no longer tie it to a current selection, and use it as general/independent gradient/resources manager), I'd refer e.g. to this explanation of Inkscape's current implementation (there are likely many more related discussions to be found in the archives of the mailing list):

«1. Fill&Stroke "always on": Sorry but I stand firmly opposed to that. It would be a fundamental disruption for a very marginal gain. If I ever learned anything from Xara, it's this: If you're not sure how to do it, do it on canvas. Therefore, this dialog is not a thing in itself and not some abstract "color editor" (as in Xara); it is a direct view/controller of the selection, tightly bound to the current desktop and its selection. Basically it's the same as the selected style indicator in the statusbar, just expanded. Enabling it to work on some abstract thing ("current color") that you don't SEE on canvas might be fine in itself, but when it interferes with the direct and immediate function of the dialog - watching and changing selection - it's a no go.

As it is now, I know I can trust my Fill&stroke dialog without thinking, on everything it shows me. If I have to _think and decide_ whether it shows me the selection or the current color, every time I glance it, my life will suddenly become so much harder.»
http://sourceforge.net/p/inkscape/mailman/message/15307479/