Crate oxvg_optimiser

Source
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§

AddAttributesToSVGElement
Adds attributes to SVG elements in the document. This is not an optimisation and will increase the size of SVG documents.
AddClassesToSVGElement
Adds to the class attribute of the root <svg> element, omitting duplicates
ApplyTransforms
Apply transformations of a transform attribute to the path data, removing the transform in the process.
CleanupAttrs
Removes redundant whitespace from attribute values.
CleanupEnableBackground
Cleans up enable-background attributes and styles. It will only remove it if
CleanupIds
Removes unused ids and minifies used ids.
CleanupListOfValues
Rounds number and removes default px unit in attributes specified with number lists.
CleanupNumericValues
Rounds number and removes default px unit in attributes specified with a number number.
CollapseGroups
Filters <g> elements that have no effect.
ConvertColors
Converts color references to their shortest equivalent.
ConvertEllipseToCircle
Converts non-eccentric <ellipse> to <circle> elements.
ConvertOneStopGradients
Converts linearGradient and radialGradient nodes that are a solid colour to the equivalent colour.
ConvertPathData
Converts paths found in <path>, <glyph>, and <missing-glyph> elements. Path commands are used within the d attributes of these elements.
ConvertShapeToPath
Converts basic shapes to <path> elements
ConvertStyleToAttrs
Converts presentation attributes in element styles to the equivalent XML attribute.
ConvertTransform
Merge transforms and convert to shortest form.
InlineStyles
Merges styles from a <style> element to the style attribute of matching elements.
Jobs
Each task for optimising an SVG document.
MergePaths
Merge multipe paths into one
MergeStyles
Merge multiple <style> elements into one
MinifyStyles
Minify <style> elements with lightningcss
MoveElemsAttrsToGroup
Move an element’s attributes to it’s enclosing group.
MoveGroupAttrsToElems
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.
PrefixIds
Prefix element ids and classnames with the filename or provided string. This is useful for reducing the likelihood of conflicts when inlining SVGs.
RemoveAttributesBySelector
Removes attributes from elements that match a selector.
RemoveAttrs
Remove attributes based on whether it matches a pattern.
RemoveComments
Removes XML comments from the document.
RemoveDeprecatedAttrs
Removes deprecated attributes from elements.
RemoveDesc
Removes the <desc> element from the document when empty or only contains editor attribution.
RemoveDimensions
Removes width and height from the <svg> and replaces it with viewBox if missing.
RemoveDoctype
Removes doctype definitions from the document.
RemoveEditorsNSData
Removes all xml namespaces associated with editing software.
RemoveElementsByAttr
Remove elements by ID or classname
RemoveEmptyAttrs
Removes empty attributes from elements when safe to do so.
RemoveEmptyContainers
Removes container elements with no functional children or meaningful attributes.
RemoveEmptyText
Removes empty <text> and <tspan> elements. Removes <tref> elements that don’t reference anything within the document.
RemoveHiddenElems
Removes hidden or invisible elements from the document.
RemoveMetadata
Removes <metadata> from the document.
RemoveNonInheritableGroupAttrs
Remove attributes on groups that won’t be inherited by it’s children.
RemoveOffCanvasPaths
For SVGs with a viewBox attribute, removes <path> element outside of it’s bounds.
RemoveRasterImages
Removes inline JPEGs, PNGs, and GIFs from the document.
RemoveScripts
Removes <script> elements, event attributes, and javascript hrefs from the document.
RemoveStyleElement
Removes all <style> elements from the document.
RemoveTitle
Removes the <title> element from the document.
RemoveUnknownsAndDefaults
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.
RemoveUnusedNS
Removes xmlns prefixed elements that are never referenced by a qualified name.
RemoveUselessDefs
Removes unreferenced <defs> elements
RemoveUselessStrokeAndFill
Removes useless stroke and fill attributes
RemoveViewBox
Removes the viewBox attribute when it matches the width and height.
RemoveXMLNS
Removes the xmlns attribute from <svg>.
RemoveXMLProcInst
Removes the xml declaration from the document.
RemoveXlink
Replaces xlink prefixed attributes to the native SVG equivalent.
ReusePaths
For duplicate <path> elements, replaces it with a <use> that references a single <path> definition.
SortAttrs
Sorts attributes into a predictable order.
SortDefsChildren
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
PrefixGenerator
Various types of ways prefixes can be generated for an id.