Skip to main content

Renderer

Trait Renderer 

Source
pub trait Renderer {
Show 15 methods // Required methods fn rect( &self, rect: Rect, rounding: WorldPx, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, ); fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<PaletteStroke>); fn line(&self, points: Vec<Pos2>, stroke: impl Into<PaletteStroke>); fn circle( &self, center: Pos2, radius: WorldPx, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, ); fn add_convex_polygon( &self, points: Vec<Pos2>, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, ); fn text( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, ) -> Rect; fn rotated_text( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, angle: Angle, ); fn text_size(&self, text: impl ToString, font: &Font) -> Vec2; fn text_layout(&self, text: &str, font: &Font, max_width: WorldPx) -> Layout; fn draw_image(&self, rect: Rect, image: &Asset); fn image_intrinsic_size(&self, image: &Asset) -> Option<Vec2>; // Provided methods fn text_wrapped( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, _max_width: WorldPx, ) -> Rect { ... } fn text_size_wrapped( &self, text: impl ToString, font: &Font, _max_width: WorldPx, ) -> Vec2 { ... } fn animator(&self) -> Option<&dyn Animator> { ... } fn visible_world_bounds(&self) -> Option<Rect> { ... }
}
Expand description

A drawing backend that accepts world-space geometry. Implemented by the on-screen painter, by the SVG exporter, and by the measuring Extent. The render path is generic over this trait so the exact same drawing code drives every backend — the geometry has a single source of truth.

Every magnitude crossing this boundary — radii, corner rounding, stroke widths, wrap widths — is a world-space WorldPx. Each backend converts to its own space exactly once: the on-screen painter multiplies by the zoom, the SVG exporter writes world units straight out.

The API speaks only in palette colors (Swatch / PaletteStroke) — never an arbitrary Color, and never an app-level “role”. Each backend holds a Palette and resolves a Swatch to a concrete color at draw time. App semantics (theme/roles) live one layer up and resolve to swatches before calling in, which keeps the canvas reusable and self-contained.

All drawing methods take &self; a collecting backend uses interior mutability. Fading a run of draws is not backend state — it is a swatch transform, applied one layer up by Style::with_opacity.

Required Methods§

Source

fn rect( &self, rect: Rect, rounding: WorldPx, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, )

Source

fn line_segment(&self, points: [Pos2; 2], stroke: impl Into<PaletteStroke>)

Source

fn line(&self, points: Vec<Pos2>, stroke: impl Into<PaletteStroke>)

Source

fn circle( &self, center: Pos2, radius: WorldPx, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, )

Source

fn add_convex_polygon( &self, points: Vec<Pos2>, fill: impl Into<Swatch>, stroke: impl Into<PaletteStroke>, )

Source

fn text( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, ) -> Rect

Source

fn rotated_text( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, angle: Angle, )

Source

fn text_size(&self, text: impl ToString, font: &Font) -> Vec2

Source

fn text_layout(&self, text: &str, font: &Font, max_width: WorldPx) -> Layout

text laid out in font and word-wrapped at max_width, in world units: the rows Self::text_size_wrapped measures.

Source

fn draw_image(&self, rect: Rect, image: &Asset)

Draw image (an SVG or PNG payload) filling the world-space rect. The caller sizes rect to honor the image’s aspect ratio (see Renderer::image_intrinsic_size), so each backend simply fills rect: the on-screen painter caches the bytes in its own hash → handle table and polls the toolkit’s loader, while the SVG exporter embeds the bytes as a base64 data URI.

Source

fn image_intrinsic_size(&self, image: &Asset) -> Option<Vec2>

The intrinsic point size of image (always strictly positive), used by the caller for aspect-fit and the resize-ring geometry. None if the bytes are not a valid SVG/PNG.

Provided Methods§

Source

fn text_wrapped( &self, pos: Pos2, anchor: Align2, text: impl ToString, font: &Font, color: impl Into<Swatch>, _max_width: WorldPx, ) -> Rect

Like Renderer::text but wraps to max_width (word wrap); WorldPx::UNBOUNDED draws unwrapped. The default ignores max_width and draws unwrapped; backends that lay text out override it. Both the on-screen and the SVG backend break the text with the same layout engine, so they agree on the break points.

Source

fn text_size_wrapped( &self, text: impl ToString, font: &Font, _max_width: WorldPx, ) -> Vec2

Like Renderer::text_size but measured with word wrap at max_width. The default ignores max_width.

Source

fn animator(&self) -> Option<&dyn Animator>

The frame animation this backend drives, for interactive overlays drawn on the generic render path. The on-screen canvas supplies itself; offline backends (the SVG exporter) return None, so those overlays fall back to a static draw.

Source

fn visible_world_bounds(&self) -> Option<Rect>

The world-space rect currently visible on screen, if this backend paints into a bounded viewport. The render path uses it to viewport-cull the scene: shapes outside it are skipped. None (the default) means “no viewport — draw everything”, which is what offline backends (the SVG exporter) want, so an export is never culled.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§