pub struct SvgRenderer { /* private fields */ }Expand description
SVG renderer: accumulates the SVG string.
Implementations§
Source§impl SvgRenderer
impl SvgRenderer
Sourcepub fn new(width: f64, height: f64) -> Self
pub fn new(width: f64, height: f64) -> Self
Create a renderer for an output canvas of width × height (scene units / px).
A non-positive size means “unset”: the value from the rendered Scene is then used,
so SvgRenderer::new(0.0, 0.0) still produces a correctly sized document.
Examples found in repository?
More examples
10fn main() {
11 let mut scene = Scene::new(400.0, 300.0).with_background(Color::WHITE);
12
13 let style = TextStyle::new(Color::BLACK, 20.0, "sans-serif")
14 .with_align(TextAlign::Left)
15 .with_baseline(TextBaseline::Top)
16 .with_rotation(45_f64.to_radians());
17
18 scene.push(Element::text(
19 "2026-10-01",
20 Point::new(50.0, 80.0),
21 style.clone(),
22 ));
23 scene.push(Element::text(
24 "不旋转的对照",
25 Point::new(50.0, 220.0),
26 style.with_rotation(0.0),
27 ));
28
29 // SVG
30 let mut r = SvgRenderer::new(400.0, 300.0);
31 r.render_scene(&scene);
32 std::fs::write("rot_text.svg", r.into_string()).unwrap();
33 println!("wrote rot_text.svg");
34
35 // PNG
36 let mut r = VelloPixmapRenderer::new(400, 300);
37 let pixmap = r.render_scene_to_pixmap(&scene);
38 std::fs::write("rot_text.png", pixmap.to_png()).unwrap();
39 println!("wrote rot_text.png");
40}276fn main() {
277 let scene = build_scene();
278
279 // SVG 输出(render_scene 会自动捕获 scene 的 title/description/scale 与各图层)
280 // 两个后端同一契约:构造尺寸 = 输出尺寸,故这里显式乘 scale 得到 2× 画布。
281 let mut svg = SvgRenderer::new(scene.width * scene.scale, scene.height * scene.scale)
282 .with_background(scene.background);
283 scene.render(&mut svg);
284 let svg_str = svg.into_string();
285 std::fs::write("demo.svg", &svg_str).expect("写 SVG 失败");
286 println!("SVG 已写入 demo.svg ({} 字节)", svg_str.len());
287 println!(" 图层数: {}", scene.layers().len());
288 for l in scene.layers() {
289 println!(
290 " - {} ({} 节点, visible={})",
291 l.name,
292 l.nodes.len(),
293 l.visible
294 );
295 }
296 // 演示 measure_text / TextMetrics(canvas 风格)
297 let demo_style = lievisual::TextStyle::new(Color::BLACK, 20.0, "sans-serif");
298 let tm = lievisual::measure_text(
299 std::slice::from_ref(&lievisual::RichSpan::new(
300 "Measure 示例",
301 demo_style.clone(),
302 )),
303 None,
304 );
305 println!(
306 " 测量 \"Measure 示例\": width={:.2} height={:.2} alphabetic_baseline={:.2} ascent={:.2} descent={:.2}",
307 tm.metrics.width,
308 tm.metrics.height,
309 tm.metrics.alphabetic_baseline,
310 tm.metrics.font_bounding_box_ascent,
311 tm.metrics.font_bounding_box_descent
312 );
313
314 // PNG 输出
315 {
316 use lievisual::render::VelloPixmapRenderer;
317 // 与 SVG 端同一契约:构造尺寸 = 输出尺寸,乘 scale 提升栅格分辨率。
318 let mut renderer = VelloPixmapRenderer::new(
319 (scene.width * scene.scale) as u32,
320 (scene.height * scene.scale) as u32,
321 )
322 .with_background(scene.background);
323 let png = renderer.render_png(&scene);
324 std::fs::write("demo.png", &png).expect("写 PNG 失败");
325 println!("PNG 已写入 demo.png ({} 字节)", png.len());
326 }
327}Sourcepub fn with_background(self, bg: Color) -> Self
pub fn with_background(self, bg: Color) -> Self
Explicit background color; when unset, Scene::background is used.
Examples found in repository?
More examples
276fn main() {
277 let scene = build_scene();
278
279 // SVG 输出(render_scene 会自动捕获 scene 的 title/description/scale 与各图层)
280 // 两个后端同一契约:构造尺寸 = 输出尺寸,故这里显式乘 scale 得到 2× 画布。
281 let mut svg = SvgRenderer::new(scene.width * scene.scale, scene.height * scene.scale)
282 .with_background(scene.background);
283 scene.render(&mut svg);
284 let svg_str = svg.into_string();
285 std::fs::write("demo.svg", &svg_str).expect("写 SVG 失败");
286 println!("SVG 已写入 demo.svg ({} 字节)", svg_str.len());
287 println!(" 图层数: {}", scene.layers().len());
288 for l in scene.layers() {
289 println!(
290 " - {} ({} 节点, visible={})",
291 l.name,
292 l.nodes.len(),
293 l.visible
294 );
295 }
296 // 演示 measure_text / TextMetrics(canvas 风格)
297 let demo_style = lievisual::TextStyle::new(Color::BLACK, 20.0, "sans-serif");
298 let tm = lievisual::measure_text(
299 std::slice::from_ref(&lievisual::RichSpan::new(
300 "Measure 示例",
301 demo_style.clone(),
302 )),
303 None,
304 );
305 println!(
306 " 测量 \"Measure 示例\": width={:.2} height={:.2} alphabetic_baseline={:.2} ascent={:.2} descent={:.2}",
307 tm.metrics.width,
308 tm.metrics.height,
309 tm.metrics.alphabetic_baseline,
310 tm.metrics.font_bounding_box_ascent,
311 tm.metrics.font_bounding_box_descent
312 );
313
314 // PNG 输出
315 {
316 use lievisual::render::VelloPixmapRenderer;
317 // 与 SVG 端同一契约:构造尺寸 = 输出尺寸,乘 scale 提升栅格分辨率。
318 let mut renderer = VelloPixmapRenderer::new(
319 (scene.width * scene.scale) as u32,
320 (scene.height * scene.scale) as u32,
321 )
322 .with_background(scene.background);
323 let png = renderer.render_png(&scene);
324 std::fs::write("demo.png", &png).expect("写 PNG 失败");
325 println!("PNG 已写入 demo.png ({} 字节)", png.len());
326 }
327}Sourcepub fn with_scale(self, scale: f64) -> Self
pub fn with_scale(self, scale: f64) -> Self
Output scale (pixel density): enlarges width/height while keeping viewBox unchanged.
Takes precedence over Scene::scale; when unset, the scene’s scale is used.
Sourcepub fn with_sizing(self, sizing: SvgSizing) -> Self
pub fn with_sizing(self, sizing: SvgSizing) -> Self
Root width / height strategy; see SvgSizing.
Sourcepub fn with_style(self, style: impl Into<String>) -> Self
pub fn with_style(self, style: impl Into<String>) -> Self
CSS appended to the root’s style attribute (XML-attribute escaped).
Used for things the backends cannot express: capping the rendered width
(max-width: 130px), declaring a background-color, …
Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Document title.
Sourcepub fn with_description(self, description: impl Into<String>) -> Self
pub fn with_description(self, description: impl Into<String>) -> Self
Document description.
Sourcepub fn into_string(self) -> String
pub fn into_string(self) -> String
Take the complete SVG document (including the <svg> root and background).
Sizing contract (same as the raster backend): the size given to
SvgRenderer::new is the output size — it lands verbatim in the root
width/height attributes, and viewBox is that size divided by scale:
width/height = 输出尺寸 (SvgRenderer::new 传入)
viewBox = 0 0 (输出尺寸/scale) (绘制使用的用户坐标系)scale is therefore a density knob, applied symmetrically on both backends, and it
never silently enlarges the output. To export a scene at 2× density, pass
new(w * 2.0, h * 2.0) (the new(0.0, 0.0) fallback does this for you, treating
scene.width/scene.height as logical size).
That size only reaches the root width / height attributes under
SvgSizing::Fixed; under SvgSizing::Intrinsic the viewport is left to the host
CSS and only viewBox carries the geometry.
Examples found in repository?
More examples
10fn main() {
11 let mut scene = Scene::new(400.0, 300.0).with_background(Color::WHITE);
12
13 let style = TextStyle::new(Color::BLACK, 20.0, "sans-serif")
14 .with_align(TextAlign::Left)
15 .with_baseline(TextBaseline::Top)
16 .with_rotation(45_f64.to_radians());
17
18 scene.push(Element::text(
19 "2026-10-01",
20 Point::new(50.0, 80.0),
21 style.clone(),
22 ));
23 scene.push(Element::text(
24 "不旋转的对照",
25 Point::new(50.0, 220.0),
26 style.with_rotation(0.0),
27 ));
28
29 // SVG
30 let mut r = SvgRenderer::new(400.0, 300.0);
31 r.render_scene(&scene);
32 std::fs::write("rot_text.svg", r.into_string()).unwrap();
33 println!("wrote rot_text.svg");
34
35 // PNG
36 let mut r = VelloPixmapRenderer::new(400, 300);
37 let pixmap = r.render_scene_to_pixmap(&scene);
38 std::fs::write("rot_text.png", pixmap.to_png()).unwrap();
39 println!("wrote rot_text.png");
40}276fn main() {
277 let scene = build_scene();
278
279 // SVG 输出(render_scene 会自动捕获 scene 的 title/description/scale 与各图层)
280 // 两个后端同一契约:构造尺寸 = 输出尺寸,故这里显式乘 scale 得到 2× 画布。
281 let mut svg = SvgRenderer::new(scene.width * scene.scale, scene.height * scene.scale)
282 .with_background(scene.background);
283 scene.render(&mut svg);
284 let svg_str = svg.into_string();
285 std::fs::write("demo.svg", &svg_str).expect("写 SVG 失败");
286 println!("SVG 已写入 demo.svg ({} 字节)", svg_str.len());
287 println!(" 图层数: {}", scene.layers().len());
288 for l in scene.layers() {
289 println!(
290 " - {} ({} 节点, visible={})",
291 l.name,
292 l.nodes.len(),
293 l.visible
294 );
295 }
296 // 演示 measure_text / TextMetrics(canvas 风格)
297 let demo_style = lievisual::TextStyle::new(Color::BLACK, 20.0, "sans-serif");
298 let tm = lievisual::measure_text(
299 std::slice::from_ref(&lievisual::RichSpan::new(
300 "Measure 示例",
301 demo_style.clone(),
302 )),
303 None,
304 );
305 println!(
306 " 测量 \"Measure 示例\": width={:.2} height={:.2} alphabetic_baseline={:.2} ascent={:.2} descent={:.2}",
307 tm.metrics.width,
308 tm.metrics.height,
309 tm.metrics.alphabetic_baseline,
310 tm.metrics.font_bounding_box_ascent,
311 tm.metrics.font_bounding_box_descent
312 );
313
314 // PNG 输出
315 {
316 use lievisual::render::VelloPixmapRenderer;
317 // 与 SVG 端同一契约:构造尺寸 = 输出尺寸,乘 scale 提升栅格分辨率。
318 let mut renderer = VelloPixmapRenderer::new(
319 (scene.width * scene.scale) as u32,
320 (scene.height * scene.scale) as u32,
321 )
322 .with_background(scene.background);
323 let png = renderer.render_png(&scene);
324 std::fs::write("demo.png", &png).expect("写 PNG 失败");
325 println!("PNG 已写入 demo.png ({} 字节)", png.len());
326 }
327}Trait Implementations§
Source§impl Renderer for SvgRenderer
impl Renderer for SvgRenderer
Source§fn render_scene(&mut self, scene: &Scene)
fn render_scene(&mut self, scene: &Scene)
Scene metadata (title / description /
scale), then call Renderer::render_scene_ordered to reuse the default traversal.Source§fn draw_rect(&mut self, rect: Rect, style: &FillStrokeStyle)
fn draw_rect(&mut self, rect: Rect, style: &FillStrokeStyle)
Source§fn draw_circle(&mut self, center: Point, radius: f64, style: &FillStrokeStyle)
fn draw_circle(&mut self, center: Point, radius: f64, style: &FillStrokeStyle)
Source§fn draw_ellipse(
&mut self,
center: Point,
radii: Vec2,
rotation: f64,
style: &FillStrokeStyle,
)
fn draw_ellipse( &mut self, center: Point, radii: Vec2, rotation: f64, style: &FillStrokeStyle, )
radii are the two semi-axis lengths, rotation is the angle
around the center).Source§fn draw_rounded_rect(
&mut self,
rect: Rect,
radius: f64,
style: &FillStrokeStyle,
)
fn draw_rounded_rect( &mut self, rect: Rect, radius: f64, style: &FillStrokeStyle, )
Source§fn draw_polyline(&mut self, points: &[Point], style: &Stroke)
fn draw_polyline(&mut self, points: &[Point], style: &Stroke)
Source§fn draw_polygon(&mut self, points: &[Point], style: &FillStrokeStyle)
fn draw_polygon(&mut self, points: &[Point], style: &FillStrokeStyle)
Source§fn draw_arc(
&mut self,
center: Point,
radii: Vec2,
start_angle: f64,
sweep_angle: f64,
style: &Stroke,
)
fn draw_arc( &mut self, center: Point, radii: Vec2, start_angle: f64, sweep_angle: f64, style: &Stroke, )
Source§fn draw_pie(
&mut self,
center: Point,
radius: f64,
start_angle: f64,
sweep_angle: f64,
style: &FillStrokeStyle,
)
fn draw_pie( &mut self, center: Point, radius: f64, start_angle: f64, sweep_angle: f64, style: &FillStrokeStyle, )
Source§fn draw_path(&mut self, path: &BezPath, style: &FillStrokeStyle, closed: bool)
fn draw_path(&mut self, path: &BezPath, style: &FillStrokeStyle, closed: bool)
Source§fn draw_gradient_path(
&mut self,
path: &BezPath,
gradient: &LinearGradient,
stroke: Option<&Stroke>,
)
fn draw_gradient_path( &mut self, path: &BezPath, gradient: &LinearGradient, stroke: Option<&Stroke>, )
Source§fn draw_text(
&mut self,
spans: &[RichSpan],
position: Point,
style: &TextStyle,
layout: Option<&TextLayout>,
)
fn draw_text( &mut self, spans: &[RichSpan], position: Point, style: &TextStyle, layout: Option<&TextLayout>, )
spans are the core content; when layout is provided it
takes precedence. Read moreSource§fn draw_image(&mut self, image: &SceneImage, frame: Rect, opacity: f64)
fn draw_image(&mut self, image: &SceneImage, frame: Rect, opacity: f64)
image is a self-contained RGBA8 bitmap and frame is the display
rectangle (pt/px coordinates). Read moreSource§fn push_transform(&mut self, t: Transform)
fn push_transform(&mut self, t: Transform)
Source§fn pop_transform(&mut self)
fn pop_transform(&mut self)
Renderer::push_transform.Source§fn push_opacity(&mut self, opacity: f64)
fn push_opacity(&mut self, opacity: f64)
opacity < 1). Empty by default.Source§fn pop_opacity(&mut self)
fn pop_opacity(&mut self)
Renderer::push_opacity.Source§fn push_blend(&mut self, mode: BlendMode)
fn push_blend(&mut self, mode: BlendMode)
Renderer::pop_blend is composited
with the backdrop using mode. Empty by default (backends without blend support ignore
it, degrading to BlendMode::Normal).Source§fn pop_blend(&mut self)
fn pop_blend(&mut self)
Renderer::push_blend.Source§fn push_mask(&mut self, mask: &Mask)
fn push_mask(&mut self, mask: &Mask)
Renderer::pop_mask has its composited alpha
multiplied by mask. Empty by default (backends without mask support ignore it).Source§fn pop_mask(&mut self)
fn pop_mask(&mut self)
Renderer::push_mask.Source§fn push_filter(&mut self, filter: &Filter)
fn push_filter(&mut self, filter: &Filter)
Renderer::pop_filter is rendered into an
offscreen layer and filtered (blur / drop shadow). Empty by default.Source§fn pop_filter(&mut self)
fn pop_filter(&mut self)
Renderer::push_filter.Source§fn push_name(&mut self, name: Option<&str>)
fn push_name(&mut self, name: Option<&str>)
name; emits id on the SVG side).
name is an Option: None means no id attribute is emitted.Source§fn pop_name(&mut self)
fn pop_name(&mut self)
Renderer::push_name.Source§fn push_clip(&mut self, clip: &Clip)
fn push_clip(&mut self, clip: &Clip)
clip). Empty by default.Source§fn pop_clip(&mut self)
fn pop_clip(&mut self)
Renderer::push_clip.Source§fn push_color_matrix(&mut self, matrix: ColorMatrix)
fn push_color_matrix(&mut self, matrix: ColorMatrix)
Renderer::pop_color_matrix is transformed by matrix. Read moreSource§fn pop_color_matrix(&mut self)
fn pop_color_matrix(&mut self)
Renderer::push_color_matrix.Source§fn push_antialias(&mut self, enabled: bool)
fn push_antialias(&mut self, enabled: bool)
Renderer::pop_antialias use
enabled anti-aliasing. Read moreSource§fn pop_antialias(&mut self)
fn pop_antialias(&mut self)
Renderer::push_antialias.Source§fn draw_rounded_rect_radii(
&mut self,
rect: Rect,
radii: CornerRadii,
style: &FillStrokeStyle,
)
fn draw_rounded_rect_radii( &mut self, rect: Rect, radii: CornerRadii, style: &FillStrokeStyle, )
Source§fn push_scope(&mut self, scope: &NodeScope<'_>)
fn push_scope(&mut self, scope: &NodeScope<'_>)
Source§fn pop_scope(&mut self, scope: &NodeScope<'_>)
fn pop_scope(&mut self, scope: &NodeScope<'_>)
Renderer::push_scope (pops in reverse order).Source§fn push_node_scope(&mut self, node: &SceneNode)
fn push_node_scope(&mut self, node: &SceneNode)
Renderer::push_scope).Source§fn pop_node_scope(&mut self, node: &SceneNode)
fn pop_node_scope(&mut self, node: &SceneNode)
Renderer::push_node_scope.Source§fn render_children(&mut self, nodes: &[SceneNode])
fn render_children(&mut self, nodes: &[SceneNode])
z_index order. Read moreSource§fn render_scene_ordered(&mut self, scene: &Scene)
fn render_scene_ordered(&mut self, scene: &Scene)
nodes layer first (bottommost, sorted
stably by ascending z_index), then each Layer in array order from bottom to
top.Source§fn render_layer(&mut self, layer: &Layer)
fn render_layer(&mut self, layer: &Layer)
z_index (including container recursion). Rarely needs to be
overridden. Read moreSource§fn render_node(&mut self, node: &SceneNode)
fn render_node(&mut self, node: &SceneNode)
Source§fn draw_element(&mut self, element: &Element)
fn draw_element(&mut self, element: &Element)
draw_* methods. Read more