svgwriter

Crates.iosvgwriter
lib.rssvgwriter
version0.1.1
created_at2022-09-25 14:38:50.919652+00
updated_at2026-01-05 19:17:09.825902+00
descriptionTyped SVG Writer
homepage
repositoryhttps://codeberg.org/msrd0/svgwriter
max_upload_size
id673646
size881,406
(msrd0)

documentation

README

svgwriter License svgwriter on crates.io svgwriter on docs.rs Source Code Repository

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

Commit count: 40

cargo fmt