Macaw.Draw.SVG.Renderer (macaw_draw v0.1.0)

Copy Markdown View Source

The writer of the plain SVG builder (spec/07 §24; visualize D-11, #812), copied from visualize's Visualize.SVG.Renderer: the one serializer of tags, attributes and text every SVG the package writes goes through (spec/10 §3.2).

iex> alias Macaw.Draw.SVG.{Element, Renderer}
iex> Element.rect(%{x: 1, stroke_width: 2, fill: nil}) |> Renderer.render_to_string()
~s(<rect stroke-width="2" x="1"/>)
  • Attribute names (attribute_name/1): a snake_case atom hyphenates unless it names a camel-cased SVG attribute (view_box → viewBox, …); an already camel-cased atom and a binary key pass through.
  • Attributes with a nil value are omitted; the rest are written sorted by their SVG name. A string value escapes &, ", < and >; a number is to_string/1, an atom its name, a %Macaw.Layers.Color{} its CSS by Macaw.Layers.Color.to_css/1.
  • Text content escapes &, < and > only; text with none of them is written as it is, uncopied.
  • Self-closing tags (circle, ellipse, line, path, polygon, polyline, rect, image, use, stop, feDropShadow, feGaussianBlur) close themselves when they have no children and no content.

No style attribute is ever written by the writer itself.

Summary

Types

A refusal: faults with their paths.

Functions

The SVG attribute name of a key, shared by every SVG writer.

An element tree's markup as iodata.

An element tree's markup as a binary.

Types

error()

@type error() :: {:error, [Macaw.Base.Fault.t()]}

A refusal: faults with their paths.

Functions

attribute_name(key)

@spec attribute_name(atom() | String.t()) :: String.t() | error()

The SVG attribute name of a key, shared by every SVG writer.

iex> Macaw.Draw.SVG.Renderer.attribute_name(:stroke_width)
"stroke-width"
iex> Macaw.Draw.SVG.Renderer.attribute_name(:view_box)
"viewBox"
iex> Macaw.Draw.SVG.Renderer.attribute_name(:viewBox)
"viewBox"

render(element)

@spec render(Macaw.Draw.SVG.Element.t()) :: iodata() | error()

An element tree's markup as iodata.

render_to_string(element)

@spec render_to_string(Macaw.Draw.SVG.Element.t()) :: String.t() | error()

An element tree's markup as a binary.