Overview
Kuva’s backend system converts abstractScene primitives into concrete output formats. All backends implement the same interface: they accept a Scene and produce format-specific output.
Available Backends
Kuva provides four backends:| Backend | Output | Feature Flag | Always Available |
|---|---|---|---|
| SVG | Vector graphics | None | ✓ |
| PNG | Raster image | png | |
| Vector document | pdf | ||
| Terminal | ANSI/Unicode text | None | ✓ |
SVG Backend
The SVG backend is always available and produces standards-compliant SVG 1.1 output.Basic Usage
Convenience Function
Implementation Details
The SVG backend (src/backend/svg.rs:14-152) directly mapsPrimitive elements to SVG:
Circle→<circle>Text→<text>with text-anchor and optional rotationLine→<line>with stroke-dasharray for dashed linesPath→<path>with fill and strokeRect→<rect>with optional fill-opacityGroupStart/GroupEnd→<g>with optional transform
- Inline styles (no external CSS)
- Optional font-family attribute
- Background rect when specified
<defs>block for gradients (used by Sankey plots)
SVG Features
- Scalable: Vector format scales to any size without quality loss
- Editable: Open in Inkscape, Adobe Illustrator, or text editor
- Small file size: Text-based, compresses well
- Web-native: Embed directly in HTML with
<img>or inline - Accessible: Screen readers can access text content
PNG Backend
The PNG backend rasterizes SVG to pixel-based images. Requires thepng feature.
Feature Configuration
Basic Usage
Convenience Function
Scale Factor
Thescale parameter controls pixel density:
- 1.0: Matches SVG logical dimensions (600px SVG → 600px PNG)
- 2.0: Retina/HiDPI quality (600px SVG → 1200px PNG) - default
- 3.0: Extra-high resolution for printing
Implementation Details
The PNG backend (src/backend/png.rs:4-48) uses theresvg crate:
- Generates SVG string via
SvgBackend - Parses SVG with
resvg::usvg::Tree - Loads system fonts via
fontdb - Rasterizes to
tiny_skia::Pixmap - Encodes as PNG
Result<Vec<u8>, String> - the Vec contains PNG file bytes.
Error Handling
- SVG parsing failure (malformed SVG)
- Font loading issues
- Memory allocation failure for very large images
- Scale factor too large for canvas dimensions
PDF Backend
The PDF backend produces vector PDF documents. Requires thepdf feature.
Feature Configuration
Basic Usage
Convenience Function
Implementation Details
The PDF backend (src/backend/pdf.rs:4-36) uses thesvg2pdf crate:
- Generates SVG string via
SvgBackend - Parses SVG with
svg2pdf::usvg::Tree - Loads system fonts via
fontdb - Converts SVG to PDF primitives
- Outputs PDF byte stream
Result<Vec<u8>, String> - the Vec contains PDF file bytes.
PDF Features
- Vector format: Scales without quality loss (like SVG)
- Printable: Ideal for publications and reports
- Self-contained: Embeds fonts and all resources
- Standard format: Opens in any PDF reader
Error Handling
Terminal Backend
The terminal backend renders plots as ANSI-colored Unicode text using braille dots and box-drawing characters. Always available.Basic Usage
Dimensions
Rendering Layers
The terminal backend (src/backend/terminal.rs:1-21) uses three rendering layers:- Character grid - Filled
Rects (█) andTextcharacters - Box-drawing grid - Axis and grid lines using Unicode box characters
- Braille layer - Circles and diagonal lines/paths as dot patterns
Box-Drawing Characters
Horizontal and vertical lines use Unicode box-drawing (U+2500-U+257F):Braille Dots
Diagonal lines, circles, and paths use Braille patterns (U+2800-U+28FF):Coordinate Mapping
Terminal coordinates are derived from scene pixel coordinates:C= terminal columnsR= terminal rowsW= scene widthH= scene height
Color Support
The terminal backend uses ANSI 24-bit true color:- Theme text color for all terminal text
- Plot colors for braille dots and characters
Limitations
- Resolution: Limited by terminal dimensions and Unicode character set
- Color: Requires terminal with true color support
- Text: Small text may be unreadable at low resolutions
- Fonts: Requires a font with full Unicode coverage
Best Practices
Multi-Backend Workflow
Generate All Formats
Conditional Compilation
Feature Flags
Enable backends inCargo.toml:
full feature enables both png and pdf.
Performance Considerations
SVG
- Fast: Direct string generation
- Output size: ~50-500 KB for typical plots
PNG
- Slower: SVG parsing + rasterization
- Output size: 100 KB - 2 MB depending on scale and complexity
- Memory: Requires allocating full pixmap (width × height × 4 bytes)
- Moderate: SVG parsing + PDF primitive conversion
- Output size: Similar to SVG (50-500 KB)
- May embed fonts (adds ~100 KB per font)
Terminal
- Fast: Direct character/color generation
- Output size: Tiny (~5-20 KB for 80×24)
- No file I/O overhead
Source Code References
- SVG backend:
src/backend/svg.rs - PNG backend:
src/backend/png.rs - PDF backend:
src/backend/pdf.rs - Terminal backend:
src/backend/terminal.rs - Convenience functions:
src/lib.rs:59-115
Next Steps
- Architecture Overview - Understanding the rendering pipeline
- Theme System - Styling for different backends
- Layout System - Optimizing for backend constraints