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

The binary encoder of the SVG builder's elements (spec/10 §4.7), copied from visualize's
`Visualize.Backend.CanvasBinary.SVG` and `.SVG.Defs`: a tree of
`Macaw.Draw.SVG.Element`s written to the stream of `Macaw.Draw.Render.Binary`
(spec/10 §4.2–§4.3), reading presentation attributes rather than a style map.
`Macaw.Draw.Render.Binary.encode/2` dispatches a builder tree here.

**Groups.** `svg` and `g` write their group state (the effective fill, stroke, width,
opacities, cap, join, dash and dash offset, filter and blend) inside `save`/`restore`,
inherited as SVG inherits them: a descendant's own value wins. A group's `opacity` is
its children's global alpha, the groups' above it multiplied in. A group that writes no
state and no transform is its children's records alone.

**Drawing elements.** `rect`, `circle`, `path` (its `d` parsed: `M m L l H h V v C c Q
q A a Z z`, numbers with sign, point and exponent, implicit repetition, a move's as a
line; `S s T t` end the parse), `line`, `ellipse`, `polygon` and `polyline` write their
own records, scoped by `save`/`restore` when they write a transform, a style, an
effect, a paint state or a blend. Element opacity and paint opacity are folded into the
colors' alphas. `text` and every other tag write nothing.

**Attributes.** A `transform` is read as SVG reads it, an unreadable one written as
none; a number that does not read is written as absent. A blend is read from
`mix-blend-mode` as an attribute, else from an inline `style` declaration, else from an
`mw-blend-<word>` class, the form `Macaw.Draw.Render.SVG` writes (spec/10 §3.3). A
`url(#id)` resolves against every element's `id`: a `clipPath` of one shape and the
library's own masks are `clip` records; a filter of one `feDropShadow` or
`feGaussianBlur` is an `effect`; a `linearGradient`, a `radialGradient` or the hatch
`pattern` is a `paint`, with its fallback painted where the records cannot express it,
never black.

**What it does not draw**: a clip of several shapes, a filter of several primitives, a
reflected or repeated gradient, a focal radial gradient, a pattern other than the hatch,
a whole-group filter or blend as one composite; each is drawn as if the attribute were
absent.

# `encode_tree`

```elixir
@spec encode_tree(Macaw.Draw.SVG.Element.t(), Macaw.Draw.Render.Binary.precision()) ::
  binary() | {:error, [Macaw.Base.Fault.t()]}
```

A builder tree's stream (spec/10 §4.7): the least version its records need first (`5`
for a dash offset, `4` for a clip or a blend, `3` for a paint state, `2` for a paint or
an effect, none for version 1 records alone), then the paints its elements draw, each
once, in the order they first draw them, then its records. `precision` is the path
records' coordinate width.

    iex> rect = Macaw.Draw.SVG.Element.rect(x: 0, y: 0, width: 1, height: 1)
    iex> Macaw.Draw.Render.Binary.SVG.encode_tree(rect, :f32)
    <<0x04, 1::little-32, 0.0::little-float-64, 0.0::little-float-64, 1.0::little-float-64, 1.0::little-float-64>>

---

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