• Stars
    star
    2,684
  • Rank 17,009 (Top 0.4 %)
  • Language
    Rust
  • License
    Mozilla Public Li...
  • Created about 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An SVG rendering library.

resvg

Build Status Crates.io Documentation Rust 1.65+

resvg is an SVG rendering library.

It can be used as a Rust library, as a C library, and as a CLI application to render static SVG files.

The core idea is to make a fast, small, portable SVG library with the goal to support the whole SVG spec.

Features

Designed for edge-cases

SVG is a very complicated format with a large specification (SVG 1.1 is almost 900 pages). You basically need a web browser to handle all of it. But the truth is that even browsers fail at this (see SVG support). Yes, unlike resvg, browsers do support dynamic SVG features like animations and scripting. But using a browser to render SVG correctly is sadly not an option.

To prove its correctness, resvg has a vast test suite that includes around 1500 tests. And those are only SVG-to-PNG regression tests. This doesn't include tests in resvg dependencies. And the best thing is that resvg test suite is available to everyone. It's not tied to resvg in any way. Which should help people who plan to develop their own SVG libraries.

Safety

It's hard not to mention safety when we talk about Rust and processing of a random input. And we're talking not only about SVG/XML, but also about CSS, TTF, PNG, JPEG, GIF, and GZIP.

While resvg is not the only SVG library written in Rust, it's the only one that is written completely in Rust. There is no non-Rust code in the final binary.

Moreover, there is almost no unsafe code either. Still, some dependencies have some unsafe code and font memory-mapping is inherently unsafe, but it's best you can get in terms of memory safety.

However, this doesn't stop at memory safety. resvg has extensive checks to prevent endless loops (freezes) and stack overflows (via recursion).

Zero bloat

Right now, the resvg CLI application is less than 3MB in size and doesn't require any external dependencies. The binary contains nothing that isn't needed for rendering SVG files.

Portable

resvg is guaranteed to work everywhere where you can compile the Rust itself, including WASM. There are some rough edges with obscure CPU architectures and mobile OSs (mainly system fonts loading), but it should be pretty painless otherwise.

SVG preprocessing

Another major difference from other SVG rendering libraries is that in resvg SVG parsing and rendering are two completely separate steps. Those steps are also split into two separate libraries: resvg and usvg. Meaning you can easily write your own renderer on top of usvg using any 2D library of your liking.

Performance

Comparing performance between different SVG rendering libraries is like comparing apples and oranges. Everyone has a very different set of supported features, languages, build flags, etc... Anyhow, as resvg is written in Rust and uses tiny-skia for rendering - it's pretty fast. There should also still be quite a lot of room for improvement.

Reproducibility

Since resvg doesn't rely on any system libraries it allows us to have reproducible results on all supported platforms. Meaning if you render an SVG file on x86 Windows and then render it on ARM macOS - the produced image will be identical. Each pixel would have the same value.

Limitations

  • No animations
    There are no plans on implementing them either.
  • No native text rendering
    resvg doesn't rely on any system libraries, which implies that we cannot use native text rendering. Nevertheless, native text rendering is optimized for small horizontal text, which is not that common is SVG.
  • Unicode-only
    It's the 21th century. Text files that aren't UTF-8 encoded are no longer relevant.

SVG support

resvg aims to only support the static SVG subset; i.e. no a, script, view or cursor elements, no events and no animations.

SVG 2 support is being worked on. You can search for relevant issues with the svg2 tag or our SVG 2 changelog.

SVG Tiny 1.2 is not supported and support is also not planned.

Results of the resvg test suite:

SVG 2 only results:

You can find a complete table of supported features here. It also includes some alternative libraries.

We're not testing against all SVG libraries since many of them are pretty bad. Some libraries are not on the list because they don't pass the 25% mark. Such libraries are: wxSvg, LunaSVG and nanosvg.

resvg project

There is a subtle difference between resvg as a library and resvg as a project. While most users will interact only with the resvg library, it's just a tip of an iceberg. There are a lot of libraries that I had to write to make resvg possible. Here are some of them:

  • resvg - the actual SVG renderer
  • usvg - an SVG preprocessor/simplifier
  • tiny-skia - a Skia subset ported to Rust
  • rustybuzz - a harfbuzz subset ported to Rust
  • ttf-parser - a TrueType/OpenType font parser
  • fontdb - a simple, in-memory font database with CSS-like queries
  • roxmltree + xmlparser - an XML parsing libraries
  • simplecss - a pretty decent CSS 2 parser and selector
  • pico-args - an absolutely minimal, but surprisingly popular command-line arguments parser

So while the resvg library is deceptively small (around 2500 LOC), the resvg project is nearing 75'000 LOC. Which is not that much considering how much resvg does. It's definitely the smallest option out there.

License

resvg project is licensed under the MPLv2.0.

More Repositories

1

cargo-bloat

Find out what takes most of the space in your executable.
Rust
2,295
star
2

svgcleaner

svgcleaner could help you to clean up your SVG files from the unnecessary data.
Rust
1,621
star
3

tiny-skia

A tiny Skia subset ported to Rust
Rust
1,089
star
4

ttf-parser

A high-level, safe, zero-allocation TrueType font parser.
Rust
609
star
5

pico-args

An ultra simple CLI arguments parser.
Rust
560
star
6

rustybuzz

A complete harfbuzz's shaping algorithm port to Rust
Rust
533
star
7

roxmltree

Represent an XML document as a read-only tree.
Rust
428
star
8

svgcleaner-gui

GUI for svgcleaner.
C++
306
star
9

fontdb

A simple, in-memory font database with CSS-like queries.
Rust
136
star
10

xmlparser

A low-level, pull-based, zero-allocation XML 1.0 parser.
Rust
130
star
11

color-thief-rs

Grabs the dominant color or a representative color palette from an image.
Rust
75
star
12

svgtypes

A collection of parsers for SVG types.
Rust
67
star
13

rctree

A "DOM-like" tree implemented using reference counting
Rust
37
star
14

simplecss

A simple CSS 2.1 parser and selector
Rust
35
star
15

svgdom

Library to represent an SVG as a DOM.
Rust
31
star
16

ttf-explorer

A simple tool to explore a TrueType font content as a tree
C++
30
star
17

xmlwriter

A simple, streaming XML writer.
Rust
24
star
18

resvg-test-suite

resvg test suite
C++
23
star
19

svgparser

(DEPRECATED) Featureful, pull-based, zero-allocation SVG parser.
Rust
22
star
20

notes-on-svg-parsing

Notes on SVG parsing
21
star
21

barh

A simple horizontal bar chart generator.
Rust
9
star
22

strict-num

A collection of bounded numeric types.
Rust
8
star
23

unicode-vo

Unicode vertical orientation detection
Rust
6
star
24

unicode-ccc

Unicode Canonical Combining Class detection
Rust
4
star
25

unicode-bidi-mirroring

Unicode Bidi Mirroring property detection
Rust
3
star
26

RazrFalcon

2
star
27

stb_truetype_meson

C
1
star