SVG style cleanup

Last update: 17.10.2006

Contents

Introduction

This program cleans inline style definition, removing properties that are not necessary to proper render image.

  1. Some properties don't apply to all SVG tags — for example markers ("arrows") applies only to paths. And such properties could be removed if present in style of other elements.

    Enabled with option -a

  2. Some properties has the same value as property of parent node. If a property inherits than setting it again in a child node isn't needed. For example:

    <svg ...>
    <g style="stroke-width: 2px">
           <line style="stroke-width: 2px" x1=.../>
           <line style="stroke-width: 3px" x1=.../>
           <line style="stroke-width: 2px" x1=.../>
           <line style="stroke-width: 2px" x1=.../>
    </g>
    </svg>
    

    After transformation:

    <svg ...>
    <g style="stroke-width: 2px">
           <line x1=.../>
           <line style="stroke-width: 3px" x1=.../>
           <line x1=.../>
           <line x1=.../>
    </g>
    </svg>
    

    Enabled with option -i

  3. Properties that has default value are removed too.

    Enabled with option -i

Since program doesn't know anything about CSS, methods 2 and 3 could be safety used with files not using local or external stylesheets.

Experiment result

Here are sizes of SVG files I've drawn using Inkscape and files after lifting.

Columns:

A B C D E
37343 35471 28660 23341 21476
22017 18810 18640 12835 12665
60451 53346 49317 33827 32348
3765 3178 3178 2734 2734
9222 8727 8167 7955 7699
37085 31210 31193 22094 22077
14595 14594 14594 14594 14594
12939 11362 11240 7541 7470
5001 4393 4314 3503 3474
12584 10934 10709 7537 7412
4749 4170 4091 3327 3298
6339 5560 5413 4478 4382
4749 4170 4091 3327 3298
24946 21618 20670 14023 13675
22250 19487 16485 12791 11689
12590 11208 11089 8287 8168
15854 14026 13281 10015 9720
7704 6822 6692 5349 5269
10644 9282 9203 6598 6569
7756 6871 6741 5398 5318
13594 11999 11491 8786 8578
20689 17933 16985 12557 12209
25542 21754 21319 15421 15186
15622 13723 12854 9498 9179
10347 9194 9047 7043 6946
10380 9222 9075 7071 6974
9768 8673 8526 6656 6559
9791 8691 8544 6674 6577
15589 13897 13146 10117 9766
29885 26547 25757 17592 17302
21079 18065 17811 14214 14110
16934 14634 14555 12163 12134
29665 27547 27468 24201 24172
17501 15383 14690 11274 10931
13261 11626 10960 7757 7491
8611 7493 7214 5909 5780
14435 12847 12192 9306 9001
14435 12847 12192 9306 9001
7116 6235 6156 4568 4539
14715 12841 12636 9570 9415
6677 5952 5910 4606 4564
8989 7843 7697 5762 5666
32719 28046 26818 20700 20122

Summary: in the best case file size was reduced about 52%; average ratio: 65%.

Downloads

Author

Wojciech Muła, wojciech_mula@poczta.onet.pl BSD license