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

The raster target (spec/10 §9), copied from visualize's `Visualize.Render.Raster`: the
package's SVG rasterized to PNG through **resvg run as a port** (visualize D-121,
D-213). It is the one module that runs resvg, so the rasterizer is replaceable in one
place, and the one place the package holds a process: a render's own, for one call.

    {:ok, png, []} = Macaw.Draw.Render.Raster.to_png(root, scale: 2, background: "#ffffff")

**It takes its program and its fonts as options and reads no application environment**
(spec/06 §1.1), probing the binary's version on every call and caching nothing: no
`:persistent_term`, no ETS, so a binary replaced in place is read on the next call. A
render is two port opens, the version probe and the render (spec/12 §5.8).

| Option | Default | Meaning | Flag |
|---|---|---|---|
| `:resvg` | `System.find_executable("resvg")` | the resvg program's path, expanded; a path that names no executable is `:no_rasterizer` | none |
| `:scale` | `1` | the device pixel ratio, positive | `--zoom` |
| `:background` | `nil` | a CSS color painted beneath; `nil` transparent | `--background` |
| `:fonts` | `Macaw.Draw.Render.Fonts.default()` | the font configuration | `--use-fonts-dir`, `--skip-system-fonts`, the `--*-family` flags |
| `:font_family` | the default theme's font list | the root's `font-family` unless it has one | none |
| `:timeout` | `30_000` | milliseconds before the render is killed | none |

Every render passes `--resources-dir` with `System.tmp_dir!/0`. An option of the wrong
type is `{:error, {:option, key, value}}`, checked before anything else, so it is
answered whether or not a binary is installed.

**Input** (§9.1): a `:root` node is rendered in `:literal` resolution and embedded, the
rules its classes select in a `<style>` (resvg reads a document's `<style>`); a binary
is an SVG document, embedded the same way, one already embedded taken as given; anything
else is `:not_a_root`. An SVG holding `var(--` is `:css_references`.

**Results** (§9.3): `{:ok, png, warnings}`, a warning `{:missing_family, list}` or
`{:rasterizer, line}` (`warnings/1`), `[]` when there were none; or `:no_rasterizer`,
`{:rasterizer_version, found, required}`, `:css_references`, `:timeout`,
`{:rasterizer, message}`, `{:option, key, value}` or `:not_a_root`. There is no bang
variant: a caller that needs a complete picture matches `{:ok, png, []}`.

**The port** (§9.5): the SVG goes in on stdin and the PNG out on stdout, no temp file;
the port is owned by a process of the render's own, linked to the caller and trapping
exits, so a program that ended before its input was written answers as a failed exit;
the caller waits in a `receive`, holding no scheduler; the timeout kills the port's
process group, repeated every 10 ms until the port reports its exit, every wait bounded.

# `failure`

```elixir
@type failure() ::
  :no_rasterizer
  | {:rasterizer_version, String.t(), String.t()}
  | :css_references
  | :timeout
  | {:rasterizer, String.t()}
  | {:option, atom(), term()}
  | :not_a_root
```

Why a render produced no PNG (spec/10 §9.3).

# `warning`

```elixir
@type warning() :: {:missing_family, String.t()} | {:rasterizer, String.t()}
```

A warning of a render (spec/10 §9.7).

# `to_png`

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

The PNG of a root node or an SVG document (spec/10 §9): `to_png/3` with the binary's
availability read from the options, the `:resvg` path or the `PATH`'s resvg.

# `to_png`

```elixir
@spec to_png(Macaw.Draw.IR.Element.t() | String.t(), keyword(), boolean()) ::
  {:ok, binary(), [warning()]} | {:error, failure()}
```

`to_png/2` with the binary's availability as an argument, the seam a test uses to reach
the absent branch (spec/10 §9.4): `false` answers `:no_rasterizer` once the options and
the input are checked.

---

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