vexy-vsvg-plugin-sdk 2.4.0

Plugin SDK for vexy-vsvg
Documentation

vexy-vsvg-plugin-sdk

This crate contains the tools to build your own SVG optimization plugins, plus the implementation of the standard 52 plugins ported from SVGO.

If you want to manipulate the SVG AST, you do it here.

How it works

Plugins implement the Plugin trait and use a Visitor to traverse the DOM.

pub trait Plugin: Send + Sync {
    fn name(&self) -> &'static str;
    fn apply(&self, doc: &mut Document) -> Result<()>;
}

Writing a Plugin

You don't manually crawl the tree. You define a Visitor that hooks into specific events, like entering an element.

use vexy_vsvg_plugin_sdk::{Plugin, Visitor, Document, Element, walk_document};
use anyhow::Result;

struct RemoveGroups;

impl Plugin for RemoveGroups {
    fn name(&self) -> &'static str { "removeGroups" }
    fn apply(&self, doc: &mut Document) -> Result<()> {
        let mut visitor = GroupRemover;
        walk_document(&mut visitor, doc)?;
        Ok(())
    }
}

struct GroupRemover;

impl Visitor for GroupRemover {
    fn visit_element_enter(&mut self, element: &mut Element) -> Result<()> {
        if element.tag_name == "g" && element.children.is_empty() {
            // Logic to remove the group would go here
        }
        Ok(())
    }
}

Components

  • plugins: The collection of built-in plugins (removeComments, convertPathData, etc.).
  • registry: A central lookup to create plugins by name.
  • selector: A CSS selector engine, because sometimes you need to find all path.icon.
  • css_matching: Logic to check if an element matches a selector.