#svg

bin+lib usvg

An SVG simplification library

19 releases (breaking)

0.14.1 Apr 17, 2021
0.14.0 Mar 9, 2021
0.13.1 Jan 20, 2021
0.12.0 Dec 5, 2020
0.2.0 May 23, 2018

#9 in Images

Download history 1581/week @ 2021-01-15 1505/week @ 2021-01-22 1644/week @ 2021-01-29 2450/week @ 2021-02-05 2506/week @ 2021-02-12 1991/week @ 2021-02-19 1729/week @ 2021-02-26 2877/week @ 2021-03-05 3075/week @ 2021-03-12 1939/week @ 2021-03-19 2343/week @ 2021-03-26 1888/week @ 2021-04-02 1518/week @ 2021-04-09 1893/week @ 2021-04-16 1634/week @ 2021-04-23 1556/week @ 2021-04-30

9,001 downloads per month
Used in 58 crates (29 directly)

MPL-2.0 license

440KB
11K SLoC

usvg

Crates.io Documentation

usvg (micro SVG) is an SVG simplification tool.

Purpose

Imagine, that you have to extract some data from the SVG file, but your library/framework/language doesn't have a good SVG library. And all you need is paths data.

You can try to export it by yourself (how hard can it be, right). All you need is an XML library (I'll hope that your language has one). But soon you realize that paths data has a pretty complex format and a lot of edge-cases. And we didn't mention attributes propagation, transforms, visibility flags, attribute values validation, XML quirks, etc. It will take a lot of time and code to implement this stuff correctly.

So, instead of creating a library that can be used from any language (impossible), usvg takes a different approach. It converts an input SVG into an extremely simple representation, which is still a valid SVG. And now, all you need is an XML library with some small amount of code.

Key features of the simplified SVG

  • No basic shapes (rect, circle, etc). Only paths
  • Simple paths:
    • Only MoveTo, LineTo, CurveTo and ClosePath will be produced
    • All path segments are in absolute coordinates
    • No implicit segment commands
    • All values are separated by a space
  • All (supported) attributes are resolved. No implicit one
  • use will be resolved
  • Invisible elements will be removed
  • Invalid elements (like rect with negative/zero size) will be removed
  • Units (mm, em, etc.) will be resolved
  • Comments will be removed
  • DTD will be resolved
  • CSS will be resolved
  • style attribute will be resolved
  • inherit attribute value will be resolved
  • currentColor attribute value will be resolved
  • Paint fallback will be resolved
  • No script (simply ignoring it)

Full spec can be found here.

Limitations

  • Currently, its not lossless. Some SVG features isn't supported yet and will be ignored.
  • CSS support is minimal.
  • Only static SVG features, e.g. no: a, view, cursor, script and animations.
  • Font-based elements are not supported.

Dependency

The latest stable Rust.

FAQ

How to ensure that SVG is a valid "Micro" SVG?

You can't. The idea is that you should not store files produced by usvg. You should use them immediately. Like an intermediate data.

License

usvg is licensed under the MPLv2.0.

Dependencies

~1.5–2.2MB
~46K SLoC