vexy-vsvg 2.4.2

Core library for vexy-vsvg SVG optimizer
Documentation

vexy-vsvg

This is the engine room. vexy-vsvg is the core library that powers the Vexy optimizer. It parses SVG text into a mutable Abstract Syntax Tree (AST), runs it through a gauntlet of optimization plugins, and spits out a leaner SVG string.

It is a Rust port of SVGO, designed to be fast, safe, and compatible.

What it does

  1. Parsing: Uses quick-xml to turn SVG text into a DOM-like structure.
  2. Plugin Pipeline: Orchestrates 52 distinct optimization passes (e.g., removing comments, merging paths, minifying styles).
  3. Stringification: Serializes the AST back to SVG, with configurable indentation and formatting.

Usage

Add this to your Cargo.toml:

[dependencies]
vexy-vsvg = "2.3.1"

Then optimize some vector graphics:

use vexy_vsvg::{optimize_default, parse_svg, stringify};

fn main() {
    let svg_input = r#"<svg><rect width="100" height="100"/></svg>"#;

    // The easy way: one-shot optimization
    let result = optimize_default(svg_input).expect("Failed to optimize");
    println!("Optimized: {}", result.data);

    // The manual way: parsing and stringifying
    let doc = parse_svg(svg_input).expect("Failed to parse");
    // ... modify doc here ...
    let output = stringify(&doc).expect("Failed to stringify");
}

Features

  • parallel: multithreaded processing for batch operations (enabled by default in the CLI).
  • wasm: Helper traits for WebAssembly compilation.
  • python: Bindings for Python integration.

Architecture

We use a custom AST optimized for SVG's quirks.

  • Document: The root container.
  • Element: Represents tags like <rect> or <g>. Attributes are stored in an IndexMap to preserve order.
  • Node: The atomic unit of the tree (Element, Text, Comment, etc.).

Text nodes use Box<str> instead of String to save 8 bytes per node, because immutable text is usually enough for SVG data.