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::{
parse::roxmltree::parse,
serialize::{Node as _},
visitor::Info,
};
use oxvg_optimiser::Jobs;
let result: String = parse(
r#"<svg xmlns="http://www.w3.org/2000/svg">
test
</svg>"#,
|dom, allocator| {
let jobs = Jobs::default();
jobs.run(dom, &Info::new(allocator)).unwrap();
dom.serialize().unwrap()
}
).unwrap();Modules§
- error
- Error types.
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
classattribute of the root<svg>element, omitting duplicates - Apply
Transforms - Apply transformations of a
transformattribute to the path data, removing thetransformin the process. - Cleanup
Attrs - Removes redundant whitespace from attribute values.
- Cleanup
Enable Background - Cleans up
enable-backgroundattributes 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
pxunit in attributes specified with number lists. - Cleanup
Numeric Values - Rounds number and removes default
pxunit 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
linearGradientandradialGradientnodes 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 thedattributes 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 thestyleattribute of matching elements. - Jobs
- Each task for optimising an SVG document.
- Merge
Paths - Merge multiple 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
widthandheightfrom the<svg>and replaces it withviewBoxif 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
viewBoxattribute, 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 javascripthrefs 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
xmlnsprefixed elements that are never referenced by a qualified name. - Remove
Useless Defs - Removes unreferenced
<defs>elements - Remove
Useless Stroke AndFill - Removes useless
strokeandfillattributes - Remove
View Box - Removes the
viewBoxattribute when it matches thewidthandheight. - RemoveXMLNS
- Removes the
xmlnsattribute from<svg>. - RemoveXML
Proc Inst - Removes the xml declaration from the document.
- Remove
Xlink - Replaces
xlinkprefixed 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§
- Extends
- A preset which the specified jobs can overwrite