razrfalcon / svgcleaner Goto Github PK
View Code? Open in Web Editor NEWsvgcleaner could help you to clean up your SVG files from the unnecessary data.
License: GNU General Public License v2.0
svgcleaner could help you to clean up your SVG files from the unnecessary data.
License: GNU General Public License v2.0
Had been already implemented, but there are a lot of code to port and it's not really helping in size reducing.
And no em, ex units.
Detect than groups and all their children are equal.
Hi,
Is svgcleaner
installable through cargo install
command?
It's not specified in the README.
In our tools JOSM we use svgcleaner permanently to reduce file sizes. After an server upgrading (and switch to latest source) we detected an error in one cleaning operation. See https://josm.openstreetmap.de/ticket/13158 for more details.
Essentially a color information is missing in the newest versions path element.
<path d="m 10 20 l 30 40 l 50 60 m 10 20 l 30 40 l 50 60 m 10 20 l 30 40 l 50 60"/>
into
<path id="p" d="m 10 20 l 30 40 l 50 60"/>
<use xlink:href="#p"/>
<use xlink:href="#p"/>
The problem is in detecting this subpaths and estimating that it's actually shorter.
Right now the title element is included in the option "Remove invisible elements" which also handles desc, script and others.
Depending on the context, the title element is not exactly invisible, as it is for example used in browsers (Firefox, Chrome/Safari/Opera) to display a tooltip.
For me it would be very useful to
btw, thanks for this useful piece of software. :)
M 10 10 H 10 H 20
-> M 10 10 H 20
.
But M 10 10 H 10 H -50
should be the same.
Aka:
cat img.svg | svgcleaner > cleaned.svg
If a use
is linked to the element in the defs
and this element is used only by this use
we can replace use
with this element.
The trick is in resolving attributes inheritance.
Most parsers are pretty lenient, but some aren't and spew when they see the illegal declaration. It has to be "UTF-8" (or "csUTF8" or one of the other legal encodings) rather than any variation.
Here is the list of standard encodings:
https://www.iana.org/assignments/character-sets/character-sets.xml
<path d="m 50 50 l 10 10"/>
<path d="m 30 30 l 60 60"/>
into
<path d="m 50 50 l 10 10 m 30 30 l 60 60"/>
Main problem is in detecting that all attributes is equal.
Partly reimplement this:
svgcleaner/src/cli/replacer.cpp
Line 1123 in af3726a
When applying SVGCleaner it enables page border and shadow.
Sample svg without page border and shadow:
https://www.dropbox.com/s/mma67f0g239korg/WithoutPageBorder%26Shadow.svg?dl=0
Sample svg with page border and shadow after apllying SVGCleaner:
https://www.dropbox.com/s/llwovgp7u3j7vfm/WithPageBorder%26Shadow.svg?dl=0
Suggestion: add a functionality that removes unneeded transform attributes (which Inkscape continually adds), to further compress files.
Example:
<use id=use5620 transform=translate(2772.5505,2000.6447) height=570 width=1000 x=1435.58 y=93.66 xlink:href=#foobar />
to
<use id=use5620 x=4208.1305 y=2094.3047 xlink:href=#foobar>
(The height
and width
attributes would ideally also be removed in the compression process, as the element is <use>
and will look the same regardless of what height
and width
are.)
Usually created by Adobe Illustrator.
Using SVGCleaner the following line:
transform="matrix(-1,0,0,1,552.55,-389.63)"
is shortened to:
transform="matrix(-1 0 0 1 552.55-389.63)"
The missing space before -389.63 confuses FireFox 27.0.1 and the svg is not displayed. Chrome and IE seem to be doing fine. If you believe the problem is with FF I will forward the report.
You can find the .svg causing the issue at:
http://pastebin.com/bz5AmLuz
The problem is that we have to represent all shapes via Bézier curves, aka paths. Apply transforms to them. And detect that they are covered, using unknown for now, formula.
Also we have to account: opacity, filters, fill, stroke, clip-path, mask, marker, etc...
Just stumbled across this project and I like the idea!
I am starting to only provide SVG graphics for websites I design (because why not!) and excess spaces, tabs and newlines etc. are extra bytes to transfer. Do you plan to provide a "minify" feature similar to what exists for JS and CSS?
I apologise if it's already implemented. I haven't looked at the source just yet.
When svgcleaner re-computes coordinates and writes them back it does not place white-spaces or seperators like comma. For example in various cases I got following result after svgcleaner-cli "cleaned" SVGs created with InkScape:
<path id="polygon5" d="... -3.498l35.916 ..."/>
The problem is that these two values could be mis-interpreted. That would increase robustness. In my case I used librsvg with cairo-backend. The resulting image had infinite long lines.
Better would be to write
-3.498l35 0.916
or
-3.498l35 .916
Now I got correct results.
So I have links in my svg and when I use any of the presets, the links are not longer clickable...
I tried tweaking almost every options but I every time the links just won't work. Any idea why this is the case?
Thanks
It is common in charts to specify the axis labels in one text or tspan element with multiple coordinates to spread the characters across the axis. eg.
<text x="0 10 20 30 40" y="0">01234</text>
SVGCleaner is removing the extra coordinates like so:
<text x="0" y="0">01234</text>
this has the undesirable effect of scrunching the labels together.
It is permissible for both coordinates to be multiple.
While I am here I'd like to add my voice to those who said that squeezing every last byte out is not the only reason to use SVGCleaner. I want SVG I can edit by hand. These goals are not contradictory, well crafted SVG will almost always be smaller than machine generated.
When I use Visio 2007 to export a drawing to SVG, some of the text (tspan) elements have dy-attributes specified in em-units. These values are rounded to "0" (when --skip-rounding-numbers is not used).
With --skip-rounding-numbers (correct):
<tspan x="4.11" dy="1.2em" font-size="1em">text</tspan>
Without --skip-rounding-numbers:
<tspan x="4.11" dy="0" font-size="1em">text</tspan>
Hi!
Running svg cleaner in cli through ANT build and when sag cleaner runs into problems it terminates in "an unusual way" according to ANT.
The problem is that SVG Cleaner gets into trouble converting height to px. I'm working around this issue since the problematic value of height is due to a failure in a previous transformation of the svg.
This is just to inform about that this issue might arise when there is an error in converting with svg cleaner as cli.
[echo] Clean pixelized/101757.svg
[exec] The initial file size is: 1308308
[exec] The initial number of elements is: 2779
[exec] The initial number of attributes is: 9630
[exec] Error: could not convert height to px
[exec]
[exec] This application has requested the Runtime to terminate it in an unusual way.
[exec] Please contact the application's support team for more information.
[exec] Result: 3
´´´
From version 0.5 (tested 0.5.1 and 0.5.2), svgcleaner fails to process files when a custom option is selected.
For example, keep existing paths data or inside transform attributes, inside coordinate attrributes, inside other attributes (set to 1).
Steps to reproduce :
For steps 2 to 5, see screenshots.
step 4 :
step 5 :
The icon path in svgcleaner.desktop is hardcoded as:
Icon=/usr/share/icons/hicolor/scalable/apps/svgcleaner.svg
This contradicts freedesktop desktop entry specification which's the standard followed by Linux desktop. It also breaks compatibility with icon themes. 😢 The correct way to set app icon is:
Icon=svgcleaner
Join LineTo segments which are lying on the same line:
M 10 10 L 50 50 L 100 100
-> M 10 10 L 100 100
Won't install on Ubuntu 16.04 via PPA:
E: Failed to fetch http://ppa.launchpad.net/svg-cleaner-team/svgcleaner/ubuntu/dists/xenial/main/binary-i386/Packages 404 Not Found
E: Unable to locate package svg-cleaner
I noticed that even using --attributes-prec=1, the attributes and coordinates of absolute paths are not changed :
./svgcleaner-cli /Users/aadant/Downloads/input/test.svg /Users/aadant/Downloads/test_svgcleaner/out/out.svg --transfs-prec=1 --coords-prec=1 --keep-absolute-paths --attributes-prec=1 --keep-unused-symbols --keep-unref-ids
(for test files, see issue 6)
then check the path precision is still the same while version 0.4 processed them.
This is what I get:
Compiling svgcleaner v0.6.92 (file:///home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner)
Running rustc src/lib.rs --crate-name svgcleaner --crate-type lib -C opt-level=3 --out-dir /home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release --emit=dep-info,link -L dependency=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release -L dependency=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps --extern svgdom=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps/libsvgdom-a1497506a80f39da.rlib --extern clap=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps/libclap-315a0a334d3f6b4c.rlib
error: file not found for module rm_invalid_stops
--> src/task/mod.rs:64:5
|
64 | mod rm_invalid_stops;
| ^^^^^^^^^^^^^^^^
|
= help: name the file either rm_invalid_stops.rs or rm_invalid_stops/mod.rs inside the directory "src/task"
error: aborting due to previous error
error: Could not compile svgcleaner
.
Caused by:
Process didn't exit successfully: rustc src/lib.rs --crate-name svgcleaner --crate-type lib -C opt-level=3 --out-dir /home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release --emit=dep-info,link -L dependency=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release -L dependency=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps --extern svgdom=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps/libsvgdom-a1497506a80f39da.rlib --extern clap=/home/haawda/paketierung/meine_Pakete/svgcleaner-git/src/SVGCleaner/target/release/deps/libclap-315a0a334d3f6b4c.rlib
(exit code: 101)
For some reason Transforms created in Sketch 3 are no longer applied to the paths. I can provide samples if that helps.
I just tried v0.6.1 GUI wizard on Windows 7 x64, with a directory in the files list. When starting the process I get a number of the cli crash messages.
The command line for the launched svgcleaner-cli.exe
is following: C:\svg-cleaner\svgcleaner-cli C:/Users/ljani/Desktop/svg-input/C:/Users/ljani/svg-input/1.svg C:/Users/ljani/svg-output//1.svg --preset=complete --short-output
. Take a look at the input path, it seems that the path is actually inserted twice and thus crashing the cli. I tried using the cli manually and it works beautifully.
Thanks for the awesome software!
EDIT: specifying the images one-by-one, there's no such a problem, only when you specify a directory input.
I am unable to find a documentation explaining how to use this wonderful software in CLI, any help would be appreciated.
We can apply only a translate
transform.
The install file it's not compete because not explain the folder to use for launch the command.
Please add an option to create SVG sprites like here:
https://github.com/btel/svg_utils
Regards
Stephan
We can remove the 'stroke-linecap' attribute if all path's subpaths are closed. Aka ended with Z.
We can convert length units only when we know DPI. And it should be set via cli argument.
When creating SVG files with inkscape that involves text, I often convert the text to paths. However inkscape still saves the relevant SVG tags with font and text information even though there is no text. SVG cleaner does not seem to be removing those font and text properties.
So when running an SVG file through the cleaner, it always breaks the file by replacing the trailing '>' with '>'. I've tried unchecking every option and it still occurs. This is the bit of code at the top of the file it's affecting.
I don't know if this is a bug or I'm missing something I can do to keep it from replacing that. Any help would be appreciated. Thanks
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_extend "http://ns.adobe.com/Extensibility/1.0/">
<!ENTITY ns_ai "http://ns.adobe.com/AdobeIllustrator/10.0/">
<!ENTITY ns_graphs "http://ns.adobe.com/Graphs/1.0/">
<!ENTITY ns_vars "http://ns.adobe.com/Variables/1.0/">
<!ENTITY ns_imrep "http://ns.adobe.com/ImageReplacement/1.0/">
<!ENTITY ns_sfw "http://ns.adobe.com/SaveForWeb/1.0/">
<!ENTITY ns_custom "http://ns.adobe.com/GenericCustomNamespace/1.0/">
<!ENTITY ns_adobe_xpath "http://ns.adobe.com/XPath/1.0/">
]>
Implement this in tools/files-testing
.
Collect ranges of sequential elements with the same attribute and move this elements to group.
<rect fill="red"/>
<rect fill="red"/>
<rect fill="red"/>
<rect fill="red" stroke="blue"/>
<rect stroke="blue"/>
<rect stroke="blue"/>
<rect stroke="blue"/>
<rect stroke="blue"/>
into
<g fill="red">
<rect/>
<rect/>
<rect/>
</g>
<g stroke="blue">
<rect fill="red"/>
<rect/>
<rect/>
<rect/>
<rect/>
</g>
The trick in detecting optimal ranges.
Hi RazrFalcon,
would you consider to add the possibility to use tab chars instead of spaces for indenting?
tried trunk and v0.6.2 on Gentoo, GCC 4.9.2. Qt 4.8.6. Fails in both cases with
wizarddialog.cpp: In member function ‘void WizardDialog::onRadioSelected()’:
wizarddialog.cpp:299:59: error: ‘class QRadioButton’ has no member named ‘accessibleName’
settings.setValue(SettingKey::Wizard::SaveMode, rBtn->accessibleName());
^
wizarddialog.cpp: In member function ‘QString WizardDialog::findLabel(const QString&)’:
wizarddialog.cpp:549:22: error: ‘class QLabel’ has no member named ‘accessibleName’
if (lbl->accessibleName() == accessibleName)
^
Find duplicated graphical elements. Move one of them to the defs
and link other to it via use
.
Start from path
and basic shapes.
I was able to build and run SVGCleaner just fine on my Mac, but even though 7-Zip is installed and working (7z in the terminal works, built from sources w/ homebrew) on the PATH, SVGCleaner always errors with the "You have to install 7-Zip to use SVGCleaner" Alert box... which is especially odd because I have "Compressing" unchecked in the wizard. Is there a way around this?
EDIT: editted the wizarddialog.cpp to point to /usr/local/bin instead of /usr/bin and the alert still popped up.
Can the file checking also check the PATH in the future instead of hard coding one path in?
Hi!
I have a problem that the class attribute is removed on text elements. The class attribute is very important for the usage of SVG I try to clean.
I've tried to turn all settings to off but it is still removed.
A setting to keep specific element or attribute would be a very nice feature.
Using version 0.6.2
Example of text element:
<text stroke="none" fill="#000000" font-family="'Arial'" id="AUTOID_24591" class="15" itedo:apstype="grobject" x="279.23244094488194" y="523.8396850393701" font-size="10.000629921259844">15</text>
cleaned version:
<text stroke="none" fill="#000000" font-family="'Arial'" id="AUTOID_24591" itedo:apstype="grobject" x="279.23244" y="523.83969" font-size="10.00063">15</text>
Also the crappy software that generates the SVG in the first place are adding ' around the font family name....
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.