Expand description
The OXVG optimiser is library for optimising SVG documents.
The optimiser should be used with a document from oxvg_ast
which can be processed by the
optimiser’s Jobs
.
§Example
Parsing and optimising a document
ⓘ
use oxvg_ast::{
implementations::{roxmltree::parse, shared::Element},
serialize::{Node, Options},
visitor::Info,
};
use oxvg_optimiser::Jobs;
let mut jobs = Jobs::default();
let arena = typed_arena::Arena::new();
let dom = parse(
r#"<svg xmlns="http://www.w3.org/2000/svg">
test
</svg>"#,
&arena,
)
.unwrap();
jobs.run(&dom, &Info::<Element>::new(&arena)).unwrap();
dom.serialize_with_options(Options::default()).unwrap();
Structs§
- AddAttributes
ToSVG Element - Adds attributes to SVG elements in the document. This is not an optimisation and will increase the size of SVG documents.
- AddClasses
ToSVG Element - Adds to the
class
attribute of the root<svg>
element, omitting duplicates - Apply
Transforms - Apply transformations of a
transform
attribute to the path data, removing thetransform
in the process. - Cleanup
Attrs - Removes redundant whitespace from attribute values.
- Cleanup
Enable Background - Cleans up
enable-background
attributes and styles. It will only remove it if - Cleanup
Ids - Removes unused ids and minifies used ids.
- Cleanup
List OfValues - Rounds number and removes default
px
unit in attributes specified with number lists. - Cleanup
Numeric Values - Rounds number and removes default
px
unit in attributes specified with a number number. - Collapse
Groups - Filters
<g>
elements that have no effect. - Convert
Colors - Converts color references to their shortest equivalent.
- Convert
Ellipse ToCircle - Converts non-eccentric
<ellipse>
to<circle>
elements. - Convert
OneStop Gradients - Converts
linearGradient
andradialGradient
nodes that are a solid colour to the equivalent colour. - Convert
Path Data - Converts paths found in
<path>
,<glyph>
, and<missing-glyph>
elements. Path commands are used within thed
attributes of these elements. - Convert
Shape ToPath - Converts basic shapes to
<path>
elements - Convert
Style ToAttrs - Converts presentation attributes in element styles to the equivalent XML attribute.
- Convert
Transform - Merge transforms and convert to shortest form.
- Inline
Styles - Merges styles from a
<style>
element to thestyle
attribute of matching elements. - Jobs
- Each task for optimising an SVG document.
- Merge
Paths - Merge multipe paths into one
- Merge
Styles - Merge multiple
<style>
elements into one - Minify
Styles - Minify
<style>
elements with lightningcss - Move
Elems Attrs ToGroup - Move an element’s attributes to it’s enclosing group.
- Move
Group Attrs ToElems - Moves some of a group’s attributes to the contained elements.
- Precheck
- Runs a series of checks to more confidently be sure the document won’t break due to unsupported/unstable features.
- Prefix
Ids - Prefix element ids and classnames with the filename or provided string. This is useful for reducing the likelihood of conflicts when inlining SVGs.
- Remove
Attributes BySelector - Removes attributes from elements that match a selector.
- Remove
Attrs - Remove attributes based on whether it matches a pattern.
- Remove
Comments - Removes XML comments from the document.
- Remove
Deprecated Attrs - Removes deprecated attributes from elements.
- Remove
Desc - Removes the
<desc>
element from the document when empty or only contains editor attribution. - Remove
Dimensions - Removes
width
andheight
from the<svg>
and replaces it withviewBox
if missing. - Remove
Doctype - Removes doctype definitions from the document.
- Remove
EditorsNS Data - Removes all xml namespaces associated with editing software.
- Remove
Elements ByAttr - Remove elements by ID or classname
- Remove
Empty Attrs - Removes empty attributes from elements when safe to do so.
- Remove
Empty Containers - Removes container elements with no functional children or meaningful attributes.
- Remove
Empty Text - Removes empty
<text>
and<tspan>
elements. Removes<tref>
elements that don’t reference anything within the document. - Remove
Hidden Elems - Removes hidden or invisible elements from the document.
- Remove
Metadata - Removes
<metadata>
from the document. - Remove
NonInheritable Group Attrs - Remove attributes on groups that won’t be inherited by it’s children.
- Remove
OffCanvas Paths - For SVGs with a
viewBox
attribute, removes<path>
element outside of it’s bounds. - Remove
Raster Images - Removes inline JPEGs, PNGs, and GIFs from the document.
- Remove
Scripts - Removes
<script>
elements, event attributes, and javascripthref
s from the document. - Remove
Style Element - Removes all
<style>
elements from the document. - Remove
Title - Removes the
<title>
element from the document. - Remove
Unknowns AndDefaults - Removes elements and attributes that are not expected in an SVG document. Removes attributes that are not expected on a given element. Removes attributes that are the default for a given element. Removes elements that are not expected as a child for a given element.
- Remove
UnusedNS - Removes
xmlns
prefixed elements that are never referenced by a qualified name. - Remove
Useless Defs - Removes unreferenced
<defs>
elements - Remove
Useless Stroke AndFill - Removes useless
stroke
andfill
attributes - Remove
View Box - Removes the
viewBox
attribute when it matches thewidth
andheight
. - RemoveXMLNS
- Removes the
xmlns
attribute from<svg>
. - RemoveXML
Proc Inst - Removes the xml declaration from the document.
- Remove
Xlink - Replaces
xlink
prefixed attributes to the native SVG equivalent. - Reuse
Paths - For duplicate
<path>
elements, replaces it with a<use>
that references a single<path>
definition. - Sort
Attrs - Sorts attributes into a predictable order.
- Sort
Defs Children - Sorts the children of
<defs>
into a predictable order.
Enums§
- Error
- The type of errors which may occur while optimising a document.
- Extends
- A preset which the specified jobs can overwrite
- Prefix
Generator - Various types of ways prefixes can be generated for an id.