SVGN Plugins
Plugins are the core of svgn
’s optimization capabilities, just as they are for svgo
. They perform specific transformations on the SVG’s Abstract Syntax Tree (AST) to reduce file size and improve rendering efficiency. svgn
aims to port all of svgo
’s plugins, maintaining functional parity and API compatibility where it makes sense in a Rust context.
Plugin Architecture
Similar to svgo
, svgn
utilizes a plugin-based architecture. Each plugin is a distinct module responsible for a specific optimization task. This modularity allows for flexible configuration and extensibility.
In svgn
, plugins are implemented as Rust functions or structs that operate on the SVG’s AST. The core optimizer iterates through the enabled plugins, applying their transformations sequentially.
Default Preset
svgn
, like svgo
, includes a default preset of plugins that are generally safe and provide good optimization results. This preset is applied by default when no custom plugin configuration is provided.
The default preset currently includes most implemented plugins but is more conservative than SVGO’s default preset. SVGN’s default preset is being actively aligned with SVGO’s. Currently, several complex plugins from SVGO’s default preset (mergePaths, moveElemsAttrsToGroup, moveGroupAttrsToElems) are not yet implemented and are excluded from the default configuration.
SVGO v4 Default Preset Order
The SVGO v4 default preset runs plugins in this specific order:
- removeDoctype
- removeXMLProcInst
- removeComments
- removeMetadata
- removeEditorsNSData
- cleanupAttrs
- mergeStyles
- inlineStyles (not yet implemented)
- minifyStyles
- cleanupIds
- removeUselessDefs
- cleanupNumericValues
- convertColors
- removeUnknownsAndDefaults
- removeNonInheritableGroupAttrs
- removeUselessStrokeAndFill (not implemented - see note)
- cleanupEnableBackground
- removeHiddenElems
- removeEmptyText
- convertShapeToPath
- convertEllipseToCircle
- moveElemsAttrsToGroup (not yet implemented)
- moveGroupAttrsToElems (not yet implemented)
- collapseGroups
- convertPathData (✅ fully implemented)
- convertTransform (not yet implemented)
- removeEmptyAttrs
- removeEmptyContainers
- removeUnusedNS
- mergePaths (not yet implemented)
- sortAttrs
- sortDefsChildren
- removeDesc
Implementation Status
Fully Implemented Plugins (55/53)
The following svgo
plugins have been successfully ported to svgn
:
Basic Optimization Plugins
cleanupAttrs
: Cleans up attributes from newlines, trailing, and repeating spacescleanupEnableBackground
: Removes or cleans up theenable-background
attributecleanupIds
: Minifies and removes unused IDscleanupListOfValues
: Rounds numeric values in attributes that have a list of numberscleanupNumericValues
: Rounds numeric values to a fixed precisionremoveComments
: Removes comments (preserves legal comments starting with!
)removeDesc
: Removes<desc>
elementsremoveDoctype
: Removes doctype declarationsremoveEmptyAttrs
: Removes empty attributesremoveEmptyContainers
: Removes empty container elementsremoveEmptyText
: Removes empty text elementsremoveMetadata
: Removes<metadata>
elementsremoveTitle
: Removes<title>
elementsremoveXMLProcInst
: Removes XML processing instructionssortAttrs
: Sorts element attributes for better gzip compressionsortDefsChildren
: Sorts children of<defs>
to improve compression
Style and Color Plugins
convertColors
: Converts colors to hex format (rgb→#rrggbb, names→hex)convertStyleToAttrs
: Converts styles from style attributes to presentation attributesmergeStyles
: Merges multiple<style>
elements into oneminifyStyles
: Basic CSS minification (removes comments, normalizes whitespace)removeStyleElement
: Removes<style>
elements
Structure Optimization Plugins
collapseGroups
: Collapses useless groups (<g>
)convertEllipseToCircle
: Converts<ellipse>
to<circle>
when possibleconvertOneStopGradients
: Converts single-stop gradients to solid colorsconvertPathData
: Optimizes path data, converts coordinates, removes redundant commandsconvertShapeToPath
: Converts basic shapes to<path>
elementsremoveHiddenElems
: Removes hidden elements (display:none, visibility:hidden)removeNonInheritableGroupAttrs
: Removes non-inheritable group attributesremoveOffCanvasPaths
: Removes elements outside the viewBoxremoveUselessDefs
: Removes<defs>
elements without IDsremoveUselessStrokeAndFill
: Removes unnecessary stroke and fill attributesremoveUselessTransforms
: Removes identity transforms
Attribute Management Plugins
addAttributesToSVGElement
: Adds attributes to the root<svg>
elementaddClassesToSVGElement
: Adds class names to the root<svg>
elementprefixIds
: Adds a prefix to IDsremoveAttrs
: Removes attributes by pattern/nameremoveAttributesBySelector
: Removes attributes matching CSS selectorsremoveDeprecatedAttrs
: Removes deprecated SVG attributesremoveDimensions
: Removes width/height attributes (preserves viewBox)removeEditorsNSData
: Removes editor namespaces and metadataremoveElementsByAttr
: Removes elements by ID or classremoveUnknownsAndDefaults
: Removes unknown elements and default valuesremoveUnusedNS
: Removes unused namespace declarationsremoveViewBox
: Removes viewBox when possibleremoveXlink
: Removes deprecated xlink attributesremoveXMLNS
: Removes xmlns attribute from root element
Not Yet Implemented (5/58)
These complex plugins require additional work:
applyTransforms
: Applies transform matrices to path coordinates and shapesmergePaths
: Merge multiple paths into onemoveElemsAttrsToGroup
: Move common attributes to parent groupmoveGroupAttrsToElems
: Move group attributes to child elementsreusePaths
: Replace duplicate paths with<use>
elements
Plugin Configuration
Configuring plugins in svgn
is similar to svgo
. You can enable or disable plugins, and for some, provide specific parameters to control their behavior. This is done through the SvgnConfig
structure, as shown in the Usage documentation.
Example: Disabling a Plugin
To disable a plugin from the command line:
svgn input.svg -o output.svg --disable removeComments
Or in Rust code, omit it from your plugins
list in the SvgnConfig
.
Example: Configuring a Plugin with Parameters
use svgn::config::{SvgnConfig, PluginConfig};
use serde_json::json;
let config = SvgnConfig {
plugins: vec![
PluginConfig::WithParams {
name: "cleanupNumericValues".to_string(),
params: json!({
"floatPrecision": 2,
"leadingZero": false,
}),
},
],
..SvgnConfig::default()
};
This example demonstrates how to configure the cleanupNumericValues
plugin to round to 2 decimal places and keep leading zeros, mirroring svgo
’s parameter structure.
Viewing Available Plugins
To see all available plugins with their descriptions:
svgn --show-plugins
This will list all 45 implemented plugins, making it easy to understand what optimizations are available.