Crate svgwriter

Crate svgwriter 

Source
Expand description

svgwriter is a typed library for writing correct SVG files. It includes SVG specification and documentation from mdn.

§Example

use svgwriter::{
	tags::{Path, TagWithPresentationAttributes as _},
	Data, Graphic
};

let mut svg = Graphic::new();
let size = 100;
svg.set_width(size);
svg.set_height(size);
svg.set_view_box(format!("0 0 {size} {size}"));

// draw a heart, inspired by https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d#example
let d = 40;
let padding = size / 2 - d;
let mut heart = Data::new();
heart
	.move_to(padding, padding + d / 2)
	.arc_by(d / 2, d / 2, 0, false, true, d, 0)
	.arc_by(d / 2, d / 2, 0, false, true, d, 0)
	.quad_by(0, d * 3 / 4, -d, d * 3 / 2)
	.quad_by(-d,-d * 3 / 4, -d, -d * 3 / 2);
svg.push(
	Path::new()
		.with_d(heart)
		.with_fill("#A919FA")
		.with_fill_opacity(0.5)
		.with_stroke("#A919FA")
		.with_stroke_width(3)
);

// write the svg to a file
write!(file, "{}", svg.to_string());

This code produces the following image:

Image produced by example code

Modules§

tags

Structs§

Data
Graphic
An SVG graphic.
Transform

Traits§

Container
Value
This trait implements an SVG attribute value.