# `Macaw.Draw.Render`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/render.ex#L1)

The dispatch over targets (spec/10 §2), copied from visualize's `Visualize.Render` and
`Visualize.Backend`.

`render/3` sends a node, a path or a list of nodes to the target's `render_node/2`,
`render_path/2` or `render_scene/2` (`Macaw.Draw.Render.Target`), every option passing
through to it; `to_string/3` is `render/3` as a binary. There is **no configured default
target** and no per-process override (visualize D-2): the caller names the target, by
its name in `targets/0` or as a module implementing `Macaw.Draw.Render.Target`, and
nothing is read from the application environment (spec/06 §1.1). The registered targets
are `:svg` (`Macaw.Draw.Render.SVG`), `:canvas` (`Macaw.Draw.Render.Canvas`) and `:binary` (`Macaw.Draw.Render.Binary`).

Every target is a pure function of its scene and options (§2.4): equal inputs write
equal bytes, and a definition id is a content hash. A renderer never raises on a scene
(§2.5): it answers `{:error, faults}`, each fault `{path, reason}` with the path the
child indexes from the root, every fault at once; `format_fault/1` words one.

`to_string/2` is the axis's form (spec/06 §17.6): the target named by its `:backend`
option, `:svg` when there is none, the rest passing through.

# `error`

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

A refusal: faults with their paths.

# `scene`

```elixir
@type scene() ::
  Macaw.Draw.IR.Element.t()
  | Macaw.Draw.IR.Path.t()
  | [Macaw.Draw.IR.Element.t()]
```

What a target writes: a node, a path or a list of nodes.

# `target`

```elixir
@type target() :: atom() | module()
```

A target: a registered name or a module implementing `Macaw.Draw.Render.Target`.

# `format_fault`

```elixir
@spec format_fault(Macaw.Base.Fault.t()) :: String.t()
```

A fault worded as one line: its path, then its reason.

    iex> Macaw.Draw.Render.format_fault({[2, 0], {:missing_attr, :r}})
    "at [2, 0]: the attribute r is missing"

# `mode`

```elixir
@spec mode(term()) :: :css | :literal
```

The theme resolution a target draws in: `:literal` for a canvas, `:css` for SVG.

# `path_data`

```elixir
@spec path_data(Macaw.Draw.IR.Path.t(), :svg | :canvas) ::
  String.t() | [term()] | error()
```

A path's data for a target: the SVG `d` string for `:svg`. A target without a path form
is `{[], {:bad_option, :target, target}}`.

    iex> Macaw.Draw.Render.path_data(Macaw.Draw.IR.Path.from_commands([{:M, 0, 0}, {:L, 1, 2}]), :svg)
    "M0,0L1,2"

# `render`

```elixir
@spec render(scene(), target(), keyword()) :: {:ok, iodata() | [term()]} | error()
```

The scene written by `target`: a `Macaw.Draw.IR.Path` to `render_path/2`, a node to
`render_node/2`, a list of nodes to `render_scene/2`, with `opts`. `{:ok, output}` or
`{:error, faults}`; a target that names none is `{[], {:bad_option, :target, target}}`.

    iex> {:ok, svg} = Macaw.Draw.Render.render(Macaw.Draw.IR.Element.circle(1, 2, 3), :svg, [])
    iex> IO.iodata_to_binary(svg)
    ~s(<circle cx="1" cy="2" r="3"/>)

# `targets`

```elixir
@spec targets() :: %{required(atom()) =&gt; module()}
```

The registered targets, by name.

# `to_string`

```elixir
@spec to_string(scene(), keyword() | target()) :: {:ok, String.t()} | error()
```

With a list, the axis's form: `to_string/3` to the target its `:backend` option names,
`:svg` when there is none. With a target, `to_string(scene, target, [])`.

# `to_string`

```elixir
@spec to_string(scene(), target(), keyword()) :: {:ok, String.t()} | error()
```

`render/3` as a binary: `{:ok, string}` or `{:error, faults}`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
