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

Copy Markdown View Source

The SVG renderer (spec/10 §3), copied from visualize's Visualize.Backend.SVG: a scene written as SVG markup, iodata, through the one serializer of tags, attributes and text, Macaw.Draw.SVG.Renderer (spec/10 §3.2, visualize D-34).

FunctionOptionDefaultMeaning
render_path/2:style%{}the style written on the <path>
render_scene/2:wraptruewrap the nodes with wrap_root/2
wrap_root/2:width, :height800, 600the root's size
wrap_root/2:view_box"0 0 W H"the root's viewBox
every function:resolve:cssthe theme resolution the scene was drawn in

Nodes to tags (§3.1): :path → path (attrs, style over them, d from Macaw.Draw.IR.Path.to_string/1, transform); :group → g (style, attrs over it, transform, children); :root → svg with xmlns, its style and transform ignored; :rect, :circle, :ellipse, :line, :image → the same name; :polyline, :polygon → the same name with points and style; :text → text with its content escaped, then its tspans; :tspan, :title, :desc → the same name with escaped content; the definitions → defs, clipPath, linearGradient, radialGradient, pattern, stop, filter, feDropShadow, feGaussianBlur. A clipped node is its definition followed by the node: a <clipPath id="mw-clip-<h>"> or, outside, a <mask id="mw-mask-<h>"> of a white rectangle of ±100,000 with the shape in black, <h> the base-36 :erlang.phash2/1 of the shape, which is written without a transform in the node's own user space (visualize D-211).

Style (§3.3) is style_to_attrs/1's; no render writes a style attribute or a <style> element (macaw spec/10 §1): a blend is the class mw-blend-<word>, whose rule is in the Frame's static stylesheet, and the motion keys are never attributes. Transforms and points (§3.4) go through Macaw.Draw.IR.Transform.to_string/1 and Macaw.Draw.IR.Path.format_number/1; every other number is written by to_string/1.

Faults (§2.5): a node it cannot write is a fault {path, reason}, the path the child indexes from the node given ([2, 0]), every fault at once, the reasons {:unknown_type, type}, {:missing_attr, name}, {:not_a_number, name}, {:bad_command, command}, {:bad_transform, op} and {:bad_option, key, value}. Macaw.Draw.IR.Element implements String.Chars (and Phoenix.HTML.Safe when it is loaded) through write/1, which leaves a faulted node out, since a protocol cannot answer a fault (§3.5).

Summary

Types

A refusal: faults with their paths.

Functions

The class a blend writes (spec/10 §3.3): mw-blend-<word>, the atom's underscores as hyphens, for the fifteen words of spec/10 §4.3.19; nil for :normal, any other word and any other term.

The id of the filter an effect shares (spec/10 §3.3): mw-effect-<effect>-<radius>, the radius 3 when absent, and for a shadow with an offset or a color mw-effect-shadow-<radius>-<h>, <h> the base-36 hash of the two, a color term hashed as the text it writes.

The points attribute (spec/10 §3.4): x,y pairs joined by spaces, each number through Macaw.Draw.IR.Path.format_number/1; a string as given.

A node and everything under it: {:ok, iodata} or every fault by its path.

A path alone, as <path d="…"/> with the :style option's attributes.

A list of nodes, wrapped by wrap_root/2 unless wrap: false.

A style as SVG attributes (spec/10 §3.3): nil dropped; :none → "none"; :current_color → "currentColor"; a paint → "url(#<id>)"; the enumerated keys by their names; :vector_effect → "non-scaling-stroke"; :effect → a filter reference url(#mw-effect-<effect>-<radius>) (a shadow with an offset or a color url(#mw-effect-shadow-<radius>-<h>)); :class → the names joined by spaces; :blend → the class mw-blend-<word> after the node's own, for the fifteen blend words; the motion keys and :line_height → nothing; every other key passes through.

Written markup in an <svg> root: xmlns, :width and :height (800 and 600 by default) and :view_box ("0 0 W H").

Types

error()

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

A refusal: faults with their paths.

Functions

blend_class(blend)

@spec blend_class(term()) :: String.t() | nil

The class a blend writes (spec/10 §3.3): mw-blend-<word>, the atom's underscores as hyphens, for the fifteen words of spec/10 §4.3.19; nil for :normal, any other word and any other term.

iex> {Macaw.Draw.Render.SVG.blend_class(:color_dodge), Macaw.Draw.Render.SVG.blend_class(:normal)}
{"mw-blend-color-dodge", nil}

effect_id(effect, radius, offset, color)

@spec effect_id(atom(), number() | nil, [number()] | nil, term()) :: String.t()

The id of the filter an effect shares (spec/10 §3.3): mw-effect-<effect>-<radius>, the radius 3 when absent, and for a shadow with an offset or a color mw-effect-shadow-<radius>-<h>, <h> the base-36 hash of the two, a color term hashed as the text it writes.

iex> Macaw.Draw.Render.SVG.effect_id(:blur, nil, nil, nil)
"mw-effect-blur-3"

points(points)

@spec points([{number(), number()}] | String.t()) :: String.t() | error()

The points attribute (spec/10 §3.4): x,y pairs joined by spaces, each number through Macaw.Draw.IR.Path.format_number/1; a string as given.

iex> Macaw.Draw.Render.SVG.points([{0, 0.5}, {10, 1 / 3}])
"0,0.5 10,0.3333"

render_node(node, opts)

@spec render_node(Macaw.Draw.IR.Element.t(), keyword()) :: {:ok, iodata()} | error()

A node and everything under it: {:ok, iodata} or every fault by its path.

render_path(path, opts)

@spec render_path(Macaw.Draw.IR.Path.t(), keyword()) :: {:ok, iodata()} | error()

A path alone, as <path d="…"/> with the :style option's attributes.

render_scene(nodes, opts)

@spec render_scene([Macaw.Draw.IR.Element.t()], keyword()) ::
  {:ok, iodata()} | error()

A list of nodes, wrapped by wrap_root/2 unless wrap: false.

style_to_attrs(style)

@spec style_to_attrs(map()) :: map() | error()

A style as SVG attributes (spec/10 §3.3): nil dropped; :none → "none"; :current_color → "currentColor"; a paint → "url(#<id>)"; the enumerated keys by their names; :vector_effect → "non-scaling-stroke"; :effect → a filter reference url(#mw-effect-<effect>-<radius>) (a shadow with an offset or a color url(#mw-effect-shadow-<radius>-<h>)); :class → the names joined by spaces; :blend → the class mw-blend-<word> after the node's own, for the fifteen blend words; the motion keys and :line_height → nothing; every other key passes through.

iex> Macaw.Draw.Render.SVG.style_to_attrs(%{fill: :none, blend: :color_dodge, class: ["mark", "a"], transition: [{:fill, 200, :linear}]})
%{class: "mark a mw-blend-color-dodge", fill: "none"}

wrap_root(content, opts)

@spec wrap_root(iodata(), keyword()) :: {:ok, iodata()} | error()

Written markup in an <svg> root: xmlns, :width and :height (800 and 600 by default) and :view_box ("0 0 W H").