Skip to main content

Module svg

Module svg 

Source
Expand description

The SVG subsystem: parse SVG documents and rasterize them to pixels.

Documents parse into a usvg::Tree — an immutable, Send + Sync simplified scene graph — and rasterize CPU-side via resvg onto the same tiny-skia Pixmap type the crate::canvas module paints, so SVG output plugs into the existing “an image we paint into” ImageNode plumbing without touching Bevy’s render internals. Like canvas, this is a leaf module: the bridge machinery reaches into crate::svg, never the reverse.

Licensing note: resvg/usvg were historically MPL-2.0, but as of the linebender-maintained releases (0.47 included) they are dual-licensed MIT OR Apache-2.0 — no license caveat applies.

Structs§

PathData
A parsed d attribute: the normalized segment list (possibly empty — an empty d string is a valid, paint-nothing path).
ShapeAttrs
The folded attribute object of one SVG shape child (<circle>, <rect>, <line>, <polyline>, <polygon>, <path>, <g>, …). All-Option: absent means “attribute not set”, and the shape kind decides which fields it reads. On update the whole object replaces atomically (see crate::protocol::props::Props::merge_delta).
ShapeTransform
An SVG transform list resolved to a 2D affine, in SVG matrix order [a, b, c, d, e, f]: x' = a·x + c·y + e, y' = b·x + d·y + f.
ShapeTransitionSpec
The shape transition spec: per-attr easing timing, keyed by the [NUMERIC_ATTRS] wire names (shapes have no style, so the spec rides the shape object itself — no all fallback, no non-numeric channels). Entries are stored positionally in [NUMERIC_ATTRS] order; reuse of [ChannelTransition] (the style-transition timing type) is verbatim — same wire shape (duration/easing/delay/springs), same driver.
SvgAssetLoader
Loads .svg files into SvgDocument assets via parse_svg_bytes.
SvgDocument
A parsed SVG document: the resolution-independent scene graph, rasterized per node at laid-out size by the consumers of this asset.
SvgParseError
The document failed to parse as SVG. Wraps usvg::Error, which already covers non-UTF-8 input, malformed gzip, and XML parse failures — usvg::Tree::from_data handles all of those itself.
SvgShape
One shape child of a JSX <svg> element: a Node-less entity (the textSpan precedent — no layout box, no style, no stamp_common) carrying only its kind and folded attrs. The rasterizer walks the <svg> root’s Children to paint these, and the hit-tester reads the same data. Updates rewrite attrs compare-before-write, so Changed<SvgShape> is a sound dirt signal for the raster.
SvgSurface
The element-owned raster surface of a node that displays an SVG.
SvgUserPos
The cursor in the enclosing <svg> root’s user space while a pointer hovers this shape; None while not hovered. Written by [sync_shape_interactions]; stamped/removed alongside the shape’s handler components (see reconcile::svg_ops). The position lives in an interior Option (rather than inserting/removing the component per hover flip) so a hover boundary never causes archetype churn.
ViewBox
The <svg> element’s viewBox: the user-unit rectangle mapped onto the element’s layout box.

Enums§

FillRuleKind
fill-rule keyword.
LinecapKind
stroke-linecap keyword.
LinejoinKind
stroke-linejoin keyword.
PathSeg
One normalized path segment. Coordinates are always absolute, in the SVG user-unit space of the enclosing <svg>’s viewBox.
ShapeKind
The kind of a JSX SVG shape child: which wire intrinsic (<circle>, <rect>, …, <g>) spawned it, and therefore which ShapeAttrs fields the rasterizer reads.
ShapePaint
A resolved SVG paint: the explicit "none" keyword (don’t paint — the web meaning of fill="none", distinct from an absent paint, which uses the SVG defaults: fill black, stroke none) or a CSS color.

Functions§

parse_svg_bytes
Parse raw SVG bytes (plain or gzip-compressed) into an SvgDocument. All parsing logic lives here; the asset loader only feeds it file bytes.
rasterize_document
Rasterize doc into a w×h pixmap with the web’s <img> behavior for SVG sources: uniform scale, xMidYMid meet centering — the document’s intrinsic aspect is letterboxed into the target box, never stretched. None on a zero-sized target.
stamp_svg_measures
Re-stamp the svg intrinsic measure after bevy_ui may have cleared it.
update_svg_surfaces
Repaint every svg surface whose raster is stale — a repaint request (dirty), a layout resize, a document load/hot-reload (file mode), or a shape/child-list change (JSX mode, derived below) — and upload the result into the backing image. Reads the node’s size from ComputedNode (already physical px, so HiDPI rasters crisp); a freshly-mounted node has no size yet and rasters next frame, once layout ran.