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

The binary canvas encoder (spec/10 §4.2–§4.6), copied from visualize's
`Visualize.Backend.CanvasBinary`, `.Opcodes` and `.PathEncoder`: a scene packed into a
compact little-endian stream of records the `FrameBinary` hook replays (spec/10 §7.9).

**The stream** is a concatenation of records, each beginning with a one-byte opcode
that fixes its layout, so a stream is self-delimiting and two streams concatenated are
one stream. Multi-byte integers are little-endian unsigned; coordinates little-endian
IEEE-754, f64 unless a record says f32; color components single bytes.

| Opcode | Byte | Version | Record |
|---|---|---|---|
| `version` | `0x00` | 2 | the version of the records after it |
| `path` | `0x01` | 1 | polyline, f64 |
| `path_cubic` | `0x02` | 1 | command stream, f64 |
| `circles` | `0x03` | 1 | circle list |
| `rects` | `0x04` | 1 | rectangle list |
| `path32` | `0x05` | 1 | polyline, f32 |
| `path_cubic32` | `0x06` | 1 | command stream, f32 |
| `cubic_run` | `0x07` | 1 | a start point and a run of cubics, f32 |
| `style` | `0x10` | 1 | fill, stroke, width, alpha |
| `transform` | `0x20` | 1 | a transform |
| `save` | `0x30` | 1 | `ctx.save()` |
| `restore` | `0x31` | 1 | `ctx.restore()` |
| `fill` | `0x40` | 1 | fill the current path |
| `stroke` | `0x41` | 1 | stroke the current path |
| `paint` | `0x50` | 2 | a paint defined for the stream |
| `box` | `0x51` | 2 | the box the next draw's paint is built over |
| `effect` | `0x52` | 2 | a shadow or a blur |
| `motion` | `0x53` | 2 | a motion span begins |
| `motion_end` | `0x54` | 2 | the span ends |
| `paint_state` | `0x55` | 3; 5 for the dash offset | a paint's own alpha, cap, join, dash and the dash's offset |
| `clip` | `0x56` | 4 | the current path becomes the clip |
| `blend` | `0x57` | 4 | the compositing operation |
| `incremental` | `0x60` | — | the incremental record, enclosing streams |
| `clearing` | `0x61` | — | the clearing incremental record |

`opcodes/0` answers this table. **Versions** (visualize D-175, D-180, D-208, D-325): a
stream of version 1 records alone has no `version` record; one using any later record
begins with `0x00 v`, `v` the highest it uses: `5` for a dash offset, else `4` for a
clip or a blend, else `3` for a paint state, else `2` for a paint, an effect or a
motion span. Each distinct paint is written once after the `version` record, indexed
from `0` in first-use order.

**Encoding the scene** (§4.4): a `:path`, `:circle` or `:rect` writes its transform, its
style, its geometry record and its draws (`fill` where it `fills?/2`, `stroke` where it
`strokes?/2`), scoped by `save`/`restore` when it writes a transform or a style
(visualize D-179); a `:line` the path `M L` and only a stroke; `:polyline`, `:polygon`
the path of their points; `:ellipse` four cubic quarter-arcs with κ = 0.5522847; a
`:group` `save`, transform, style, children, `restore`; the root its children; a
clipped node `save`, its transform, the shape's geometry, `clip`, its records without
its transform, `restore`. **Text, images, titles, descriptions and definitions write
nothing** (§4.6): a binary layer's text is the Frame's text layer, its tiles the
backdrop. A node's alpha is its composed opacity (§2.3, D-304). A color term is written
from its channels; text is parsed by `Macaw.Layers.Color.parse/1`, one it cannot read
and `:current_color` opaque black.

**Path forms and precision** (§4.5): after `expand_smooth/1`, one `M` then `L`s is
`path32` (`:f32`) or `path` (`:f64`); one `M` then `C`s `cubic_run` (`:f32`) or
`path_cubic` (`:f64`); anything else `path_cubic32` or `path_cubic`. `precision:` is
`:f32` by default, since the coordinates are pixels, and governs path records alone.
**No tensor library**: every record is written by the binary constructor; a value beyond f32's
range is written at its bound, which `:f64` holds exactly. The SVG builder's elements
are `Macaw.Draw.Render.Binary.SVG`'s (§4.7); the reference decoder is
`Macaw.Draw.Render.Binary.Decoder` (§4.8).

| Function | Option | Default | Meaning |
|---|---|---|---|
| every encoder | `:precision` | `:f32` | the path records' coordinate width |
| the target's callbacks | `:style` | `%{}` | the style a path alone is drawn in |

# `error`

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

A refusal: faults with their paths.

# `precision`

```elixir
@type precision() :: :f32 | :f64
```

The coordinate width of the path records.

# `encode`

```elixir
@spec encode(Macaw.Draw.IR.Element.t() | Macaw.Draw.SVG.Element.t(), keyword()) ::
  {:ok, binary()} | error()
```

A scene's stream (§4.4), or a tree of the SVG builder's elements
(`Macaw.Draw.Render.Binary.SVG`, §4.7). A scene is checked as the SVG renderer checks
it, every fault at once; an option it does not take is `{:bad_option, key, value}`.

    iex> Macaw.Draw.Render.Binary.encode(Macaw.Draw.IR.Element.rect(0, 0, 1, 1), [])
    {:ok, <<0x04, 1::little-32, 0.0::little-float-64, 0.0::little-float-64, 1.0::little-float-64, 1.0::little-float-64>>}

# `encode_path`

```elixir
@spec encode_path(Macaw.Draw.IR.Path.t(), keyword()) :: {:ok, binary()} | error()
```

A path's record (§4.5) under the `:precision` option.

    iex> path = Macaw.Draw.IR.Path.from_commands([{:M, 0, 0}, {:L, 1, 2}])
    iex> Macaw.Draw.Render.Binary.encode_path(path, [])
    {:ok, <<0x05, 2::little-32, 0.0::little-float-32, 0.0::little-float-32, 1.0::little-float-32, 2.0::little-float-32>>}

# `opcodes`

```elixir
@spec opcodes() :: [
  %{
    name: atom(),
    byte: byte(),
    version: pos_integer() | nil,
    record: String.t()
  }
]
```

The opcode table of §4.2: each record's `name`, its `byte`, the `version` it needs
(`nil` for the incremental records, which enclose streams) and what it is.

    iex> Enum.find(Macaw.Draw.Render.Binary.opcodes(), &(&1.name == :clip)).byte
    0x56

# `render_node`

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

A node and everything under it: `encode/2`.

# `render_path`

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

A path alone, drawn in the `:style` option's style: `{:ok, binary}` or its faults.

# `render_scene`

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

A list of nodes, one stream: the nodes as the children of one root.

---

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