Reduce SVG file size by removing unnecessary attributes

Bug #171983 reported by Bug Importer
10
This bug affects 1 person
Affects Status Importance Assigned to Milestone
Inkscape
Triaged
Medium
Unassigned
inkscape (Debian)
Confirmed
Unknown
inkscape (Ubuntu)
Triaged
Low
Unassigned

Bug Description

When Inkscape saves an SVG file, even in Plain SVG mode, it leaves a lot of
unnecessary attributes in the XML file, which increase file size. For
example, all objects have a style attribute which specifies values for many
CSS attributes such as marker, opacity, stroke-dasharray, etc. even if it
is specifying the default values. If a CSS attribute is not needed because
it is set to the default value according to the SVG spec, it should be
omitted. Also, Inkscape adds an "id" attribute to all objects, even if it
is just a generic value like "path2042" that hasn't been changed by the
user. Is this really needed?

<email address hidden>

nightrow (jb-benoit)
Changed in inkscape:
importance: Undecided → Medium
status: New → Confirmed
Revision history for this message
Halley (ed-halley) wrote :

This patch done against SVN as of 1 March 2009.

I've put in a draft of a quick-n-dirty filter that optimizes some known defaults out of the style="" attributes. This is done only at the final saving time. It can be made smarter by working against a known SVG schema version number, in case any schema specifies different default style values.

Only style="" attributes are filtered. If stroke:none, don't save any stroke-X:Y parts. Same with fill:none and marker:none. Other known defaults are skipped based on a short list hardcoded into the function for now.

I get about 25% savings on file size on a path-heavy SVG file. Let me know [ e d @ h a l l e y . c c ] if there are any problems.

Revision history for this message
Halley (ed-halley) wrote :

Updated the patch to make this a preference. The default would be to minimize size, but now the user can specify to "Force default styles" in SVG output. (The present default styles are also forced in output to extension filters, which may be more likely to expect them.)

Revision history for this message
bbyak (buliabyak) wrote : Re: [Bug 171983] Re: Reduce SVG file size by removing unnecessary attributes

Halley:

thanks for the patch, but I think you're working in a wrong place. The
logic of writing/skipping style properties is in style.cpp, namely
sp_style_write_string and sp_style_write_difference. As you will
notice, certain props (fill/stroke) are already skipped based on some
conditions. What you need to do is just extend this logic and cover
more properties and contexts. However, be VERY careful with this, and
start by carefully reading the SVG spec for each property, to find out
when and when not it is redundant. For example, some property may look
redundant in most cases but it may take effect when a different value
is inherited from its parent. Some props are inherited, some not, they
have different default values, etc. All this needs to be taken into
account before you decide to skip some property, and you need to write
a "proof" of skippability for each case in comments. When in doubt,
compare to Batik rendering. Thanks, and good luck with Inkscape
hacking! :)

tags: added: svg
Changed in inkscape:
status: Confirmed → In Progress
Changed in inkscape:
status: In Progress → Confirmed
Changed in inkscape (Ubuntu):
status: New → Confirmed
Changed in inkscape (Debian):
status: Unknown → Confirmed
David Futcher (bobbo)
tags: added: patch-forwarded-upstream
Changed in inkscape (Ubuntu):
importance: Undecided → Low
status: Confirmed → Triaged
Changed in inkscape:
status: Confirmed → Triaged
Revision history for this message
Kris (kris-degussem) wrote :

Is not this request not something for scour? That seems to be the right place for cleaning up the svg...

Revision history for this message
su_v (suv-lp) wrote :

> Is not this request not something for scour?

Not necessarily - see for example the recent merge of last year's GSoC project into trunk:
<http://thread.gmane.org/gmane.comp.graphics.inkscape.devel/37649>

Revision history for this message
Martin Owens (doctormo) wrote :

Considering the functionality exists, is this bug now just whether these options should be default on or not?

Revision history for this message
Tavmjong Bah (tavmjong-free) wrote :

In the long term, the GSoC code should be more integrated with the SPStyle code as there is some duplication taking place.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.