David's Blog

Using Resvg in Rust

By David Li on Fri, 21 March 2024

Using Resvg in Rust: A Comprehensive Guide

In this article, we will explore Resvg, a pure Rust SVG rendering library, and learn how to use it effectively within a Rust project. Scalable Vector Graphics (SVG) is a widely-used XML-based vector image format for creating two-dimensional graphics with support for interactivity and animation.

Resvg is a high-quality library that aims to provide an efficient and accurate rendering of SVG files. It is a perfect choice for developers who wish to incorporate SVG support in their Rust projects.

Prerequisites

To follow along with this tutorial, you will need:

  1. A basic understanding of Rust programming language
  2. Rust and Cargo installed on your system

Getting Started

To start using Resvg in your Rust project, you need to add it as a dependency in your Cargo.toml file:

[dependencies]
resvg = "0.19.0"

Now, let’s import the necessary modules in our main.rs file:

use resvg::{prelude::*, usvg};

Loading an SVG File

To load an SVG file, we use the usvg::Tree::from_file function. It takes a file path as an argument and returns a Result<usvg::Tree, usvg::Error>. Below is an example of how to load an SVG file:

fn load_svg(file_path: &str) -> Result<usvg::Tree, usvg::Error> {
    let tree = usvg::Tree::from_file(file_path, &usvg::Options::default())?;
    Ok(tree)
}

Rendering an SVG Image

Once we have loaded an SVG file, we can render it using the resvg::render function. This function takes a reference to the usvg::Tree and a resvg::Options object as arguments, and returns a Result<resvg::Image, resvg::Error>. The following example demonstrates how to render an SVG image:

use resvg::Image;

fn render_svg(tree: &usvg::Tree, options: &resvg::Options) -> Result<Image, resvg::Error> {
    let image = resvg::render(&tree, usvg::FitTo::Original, options)?;
    Ok(image)
}

Saving the Rendered Image

After rendering the SVG image, we can save it as a PNG file using the resvg::Image::save_png method. This method takes a reference to a file path as its argument and returns a Result<(), std::io::Error>. Here’s an example of how to save the rendered image:

fn save_image(image: &Image, output_path: &str) -> Result<(), std::io::Error> {
    image.save_png(output_path)
}

Putting It All Together

Now we can combine all the functions to load, render, and save an SVG file as a PNG image:

use resvg::{prelude::*, usvg};
use resvg::Image;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let input_file = "assets/example.svg";
    let output_file = "output/example.png";

    let tree = load_svg(input_file)?;
    let options = resvg::Options::default();
    let image = render_svg(&tree, &options)?;

    save_image(&image, output_file)?;

    println!("SVG file successfully converted to PNG!");
    Ok(())
}

fn load_svg(file_path: &str) -> Result<usvg::Tree, usvg::Error> {
    let tree = usvg::Tree::from_file(file_path, &usvg::Options::default())?;
    Ok(tree)
}

fn render_svg(tree: &usvg::Tree, options: &resvg::Options) -> Result<Image, resvg::Error> {
    let image = resvg::render(&tree, usvg::FitTo::Original, options)?;
    Ok(image)
}

fn save_image(image: &Image, output_path: &str) -> Result<(), std::io::Error> {
    image.save_png(output_path)
}

In this tutorial, we discussed how to use the Resvg library in a Rust project, including loading and rendering SVG files, and saving the rendered image as a PNG file. Resvg is a powerful and flexible library that can help you bring SVG support to your Rust applications with ease.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-06-14.