svgmacro

Cargo Documentation
A Rust library for writing SVGs. Can write any SVG-element. Function calls and variables defined in Rust can be used in the SVG without trouble.

Add the following to your Cargo.toml svgmacro = "0.2.0"

To use the crate in your module, simply add: ```

[macro_use]

extern crate svgmacro; ```

Examples

To use the macro, create a new String to store the result, and add "use std::fmt:Write" to be able to successfully write to it. use std::fmt::Write; let mut out = String::new(); Below is a quick example on how to use the macro. SVG elements and attributes are defined by their regular names found in the SVG reference. ``` use std::fmt::Write; let mut out = String::new();

SVG!(&mut out, svg (width="100" height="100") [ circle (cx="50" cy="50" r="30") ] ); Result written to out: ```

Elements, parantheses and brackets

Define elements in plain text by their element tag, their attributes in a parenthesis, (), and their children in a bracket, []. These are all valid syntax (Note that you must not use both brackets and parantheses, however just "circle" is not viable). g (fill="red") [circle (cx="10" cy="10" r="10")] g () [circle (cx="10" cy="10" r="10")] g (fill="red") [] circle (cx="10" cy="10" r="10") text ["Test"]

Nested elements and multiple attributes

Nest elements by putting new elements inside the []-brackets of their parent. Add multiple attributes by delmimiting them with a space.

SVG!(&mut out, svg [ g(size="12" color="red") [ circle(cx="10" cy="10" r="10") circle(cx="20" cy="20" r="10") ] g(color="red") [ circle(cx="20" cy="20" r="10") ] ] );

Rust expressions, functions and variables in SVG

Handle variables and returning function calls by wrapping them in a {} closure, expressions such as void functions, for loops and if expressions (any Rust code is ok) with a @-symbol and terminate with a ";". ``` use std::fmt::Write; let mut out = String::new()

// Define your variables let width = "200"; let height = "100";

SVG!(&mut out, svg (width={width} height={height}) [ g(fill={getcolor()}) [ @ createcool_shape(&mut out); ] g [ @ for i in 1..3 { let radius = 15i; // It is important to call the macro again, when using it from inside and expression. SVG!(&mut out, circle(cx={10i} cy="10" r={radius})); }; ] ] ); ```