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

The reference decoder of the binary canvas (spec/10 §4.8), copied from visualize's
`Visualize.Backend.CanvasBinary.Decoder` and **written from spec/10 §4.2–§4.3 and §4.9,
not from the encoder**, so `decode(encode(x))` is a round trip that tests both
(visualize D-36). Where visualize raised on a bad byte, this answers it: a test or a
host reading a stream it did not write gets a value.

`decode/1` answers the records of a stream in order:

- `{:path, commands}`, the commands of any path record (`path`, `path32`, `path_cubic`,
  `path_cubic32`, `cubic_run`) as `Macaw.Draw.IR.Path` commands; `{:circles, [{cx, cy,
  r}]}`, `{:rects, [{x, y, w, h}]}`;
- `{:style, map}`, its `fill`, `stroke` (RGBA tuples), `stroke_width` and `opacity`,
  `nil` where absent, with `non_scaling: true`, `fill_paint:` and `stroke_paint:` under
  bits 4–6;
- `{:transform, op}`, `op` `{:translate, tx, ty}`, `{:translate_scale, tx, ty, sx, sy}`
  or `{:matrix, a, b, c, d, e, f}`;
- `{:version, v}`, `{:paint, index, paint}` (`%{type: :linear | :radial, geometry,
  stops: [{at, rgba}]}` or `%{type: :hatch, pattern, geometry, color, background}`),
  `{:box, {x, y, w, h}}`, `{:effect, :shadow, radius, {dx, dy}, rgba}` or `{:effect,
  :blur, radius}`, `{:motion, json}`, `:motion_end`;
- `{:paint_state, map}`, its `fill_alpha`, `stroke_alpha`, `cap`, `join`, `dash` and,
  under bit `0x20`, `dash_offset`, `nil` where absent;
- `{:clip, :inside | :outside}`, `{:blend, word}` (the CSS word);
- `:save`, `:restore`, `:fill`, `:stroke`;
- `{:incremental, mode, dx, dy, viewport, regions}`, `{:full_redraw, viewport,
  records}` and `{:clearing, mode, dx, dy, viewport, regions}`, each region `{{x, y, w,
  h}, records}` and `mode` `:in_place`, `:scroll_x`, `:scroll_y` or `:scroll_xy`.

**Refusals** name the byte and its offset: `{:opcode, byte, offset}` for an unknown
opcode, `{:sub_opcode, byte, offset}`, `{:kind, byte, offset}` for a transform, paint,
effect or incremental mode of no known kind, `{:version, v, offset}` for a version above
the decoder's own (`version:`, `5` by default), `{:value, byte, offset}` for a clip keep,
blend mode, cap, join or hatch pattern of no known value, and `{:truncated, offset}` for
a record cut short, the offset where its bytes ran out.

# `refusal`

```elixir
@type refusal() ::
  {:opcode | :sub_opcode | :kind | :version | :value, byte(), non_neg_integer()}
  | {:truncated, non_neg_integer()}
```

A refusal: what was refused and where.

# `decode`

```elixir
@spec decode(binary(), keyword()) :: {:ok, [term()]} | {:error, refusal()}
```

The records of a stream, in order, or the refusal of its first bad byte. `version:`
is the highest version this decoder knows, `5` by default.

    iex> Macaw.Draw.Render.Binary.Decoder.decode(<<0x30, 0x40, 0x31>>)
    {:ok, [:save, :fill, :restore]}
    iex> Macaw.Draw.Render.Binary.Decoder.decode(<<0x30, 0x99>>)
    {:error, {:opcode, 0x99, 1}}

# `decode_path`

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

The one path record of a stream as a `Macaw.Draw.IR.Path`; a stream of anything else is
`{:error, :not_a_path}`.

---

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