Skip to main content

Crate pvg

Crate pvg 

Source
Expand description

§⚡ PVG — Procedural Vector Graphics Core Engine

PVG is a deterministic, human-readable 2D vector graphics and procedural scene description language. It combines the declarative clarity of vector graphics with native loops, typography, trigonometry, and microsecond CPU evaluation.

§Key Features

  • 🦀 Zero GPU Dependency: Evaluates pure vector geometry directly on the CPU.
  • ⏱️ Microsecond CPU Evaluation: Evaluates animated scenes in < 40 µs per frame.
  • 🪶 Sub-50 KB Memory Footprint: Evaluates flat contiguous 2D draw lists with zero DOM overhead.
  • 🔄 AST Caching: Parse once into an AST, re-evaluate per frame across timeline ticks with no re-parsing.
  • 🌐 Built-in Standalone SVG Emitter: Direct serialization into W3C SVG XML (static & SMIL animated).

§Quickstart Example

use pvg::{compile, compile_at_time, Color, DrawCmd};

let source = r#"
PVG 0.1
canvas 400 400
  background #000000

circle
  center [200, 200]
  radius 50 + 20 * sin(time * 3.0)
  fill #00ffcc
"#;

// 1. Compile at time t = 0.0s
let draw_list = compile(source).expect("Compilation failed");
assert_eq!(draw_list.canvas_width, 400.0);
assert_eq!(draw_list.canvas_height, 400.0);
assert_eq!(draw_list.items.len(), 1);

// 2. Serialize directly to W3C SVG
let svg_xml = draw_list.to_svg();
assert!(svg_xml.contains("<svg"));
assert!(svg_xml.contains("<circle"));

// 3. Animate at time t = 1.5s
let animated_list = compile_at_time(source, 1.5).expect("Evaluation failed");
assert_eq!(animated_list.items.len(), 1);

§Two-Phase Compilation (AST Caching Optimization)

For real-time 60 FPS animation loops, avoid parsing the source string repeatedly. Parse the AST document once and evaluate it on each frame tick:

use pvg::{parse, Evaluator};

let source = "PVG 0.1\ncanvas 200 200\ncircle\n  center [100, 100]\n  radius 40\n";
let doc = parse(source).expect("Failed to parse document");

// Re-evaluate the cached AST with updated timeline timestamps
for frame in 0..60 {
    let time = frame as f64 / 60.0;
    let evaluator = Evaluator::new_with_time(time);
    let draw_list = evaluator.evaluate_document(&doc).expect("Evaluation failed");
    assert_eq!(draw_list.items.len(), 1);
}

Re-exports§

pub use ast::CanvasDecl;
pub use ast::CircleNode;
pub use ast::Color;
pub use ast::Document;
pub use ast::EllipseNode;
pub use ast::Expr;
pub use ast::FunctionDef;
pub use ast::GlowExpr;
pub use ast::GradientStop as AstGradientStop;
pub use ast::GroupNode;
pub use ast::LineNode;
pub use ast::ParamDecl;
pub use ast::PathCommand;
pub use ast::PathNode;
pub use ast::PatternDef;
pub use ast::PixelFilter;
pub use ast::PolygonNode;
pub use ast::RectNode;
pub use ast::ShadowExpr;
pub use ast::SplineNode;
pub use ast::SpriteNode;
pub use ast::Stmt;
pub use ast::TextNode;
pub use ast::UnaryOp;
pub use ast::BinaryOp;
pub use draw_list::BlendMode;
pub use draw_list::DrawCmd;
pub use draw_list::DrawList;
pub use draw_list::DrawPathCommand;
pub use draw_list::DrawPattern;
pub use draw_list::DrawStyle;
pub use draw_list::Glow;
pub use draw_list::GradientStop;
pub use draw_list::LineCap;
pub use draw_list::LineJoin;
pub use draw_list::Paint;
pub use draw_list::Shadow;
pub use draw_list::StrokeAlign;
pub use draw_list::TextAlign;
pub use draw_list::Transform2D;
pub use error::PvgError;
pub use error::PvgErrorKind;
pub use eval::pvg_noise2;
pub use eval::pvg_noise3;
pub use eval::spline_to_bezier;
pub use eval::Evaluator;
pub use eval::Scene;
pub use eval::Value;
pub use eval::MAX_CALL_STACK_DEPTH;
pub use eval::MAX_SCENE_PRIMITIVES;
pub use lexer::Lexer;
pub use lexer::Token;
pub use lexer::TokenKind;
pub use parser::Parser;
pub use svg::emit_animated_svg;
pub use svg::emit_draw_commands;
pub use svg::emit_svg;
pub use svg::escape_xml;
pub use svg::format_svg_attributes;

Modules§

ast
draw_list
error
eval
lexer
parser
svg

Functions§

compile
Compiles a PVG source string into a flat 2D DrawList at time = 0.0.
compile_at_time
Compiles a PVG source string into a flat 2D DrawList at a specific timeline timestamp.
compile_pvg
Alias for compile (backwards compatibility).
compile_pvg_at_time
Alias for compile_at_time (backwards compatibility).
compile_with_params
Compiles with host uniforms (param) overridden without re-parsing twice by the caller. params is a list of (name, value) pairs.
parse
Parses a PVG source string into an Abstract Syntax Tree (Document).
parse_pvg
Alias for parse (backwards compatibility).
to_svg
Transpiles a PVG source string directly into a W3C SVG XML string at time = 0.0.
to_svg_at_time
Transpiles a PVG source string directly into a W3C SVG XML string at a specific timeline timestamp.