项目作者: rasviitanen

项目描述 :
A Rust library (macro) for writing any SVG efficiently.
高级语言: Rust
项目地址: git://github.com/rasviitanen/svgmacro.git
创建时间: 2018-03-08T09:59:45Z
项目社区:https://github.com/rasviitanen/svgmacro

开源协议:

下载


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.

Questions or requests can be emailed to
rasviitanen@gmail.com.

Add the following to your Cargo.toml

  1. svgmacro = "0.2.2"

To use the crate in your module, simply add:

  1. #[macro_use]
  2. 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. Other types are also supported.

  1. use std::fmt::Write;
  2. 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.

  1. use std::fmt::Write;
  2. let mut out = String::new();
  3. SVG!(&mut out,
  4. svg (width="100" height="100") [
  5. circle (cx="50" cy="50" r="30")
  6. ]
  7. );

Result written to out:

  1. <svg width="100" height="100">
  2. <circle cx="50" cy="50" r="30"></circle>
  3. </svg>

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 do not have to use both brackets and parantheses).

  1. g (fill="red") [circle (cx="10" cy="10" r="10")]
  2. g () [circle (cx="10" cy="10" r="10")]
  3. g (fill="red") []
  4. circle (cx="10" cy="10" r="10")
  5. 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.

  1. SVG!(&mut out,
  2. svg [
  3. g(size="12" color="red") [
  4. circle(cx="10" cy="10" r="10")
  5. circle(cx="20" cy="20" r="10")
  6. ]
  7. g(color="red") [
  8. circle(cx="20" cy="20" r="10")
  9. ]
  10. ]
  11. );

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 “;”.

  1. use std::fmt::Write;
  2. let mut out = String::new()
  3. // Define your variables
  4. let width = "200";
  5. let height = "100";
  6. SVG!(&mut out,
  7. svg (width={width} height={height}) [
  8. g(fill={get_color()}) [
  9. @ create_cool_shape(&mut out);
  10. ]
  11. g [
  12. @ for i in 1..3 {
  13. let radius = 15*i;
  14. // It is important to call the macro again, when using it from inside and expression.
  15. SVG!(&mut out, circle(cx={10*i} cy="10" r={radius}));
  16. };
  17. ]
  18. ]
  19. );