# `Macaw.Frame.Layer.Device`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/layer/device.ex#L1)

Device pixels, covered points and provisional parts (spec/04 §1.12), copied from
visualize's `Visualize.Chart.Mark.M4`, `.Covered`, `.Provisional` and `.Cut`. Each rule
is close, not identical, to drawing every row, and each is held to the ink bound of
visualize spec/15 §8.2 on the raster: beyond one device pixel, at most 0.08 % of the ink
lost and 0.03 % invented.

- **A straight line draws at its device pixels** (`m4/3`): a cartesian `line` whose
  curve is linear draws a defined run of more than `4 × w` points (`w` the plot's
  width in device pixels, `ceil(width × resolution)`) as M4 over the points already
  scaled: the first, last, lowest and highest point of each device pixel column the run
  visits. Only the path's points are reduced.
- **A covered point is not drawn** (`covered/3`): a cartesian `circle` or `symbol`
  layer placing more than `4 × w` points leaves out a point when a later point of the
  layer stands in the same device pixel with the same glyph, size, style and series,
  and that style is fully opaque (`opaque?/1`). The point drawn last is kept.
- **What a source has not settled is drawn provisional** (`claim/3`, `cut/2`): with a
  delivery's `served.complete_until`, a value at or after the claim is provisional; a
  path is generated whole and then cut at the claim's `x` (an `L` or `H` by
  interpolation, a cubic by de Casteljau), so no curve changes shape. A layer reads the
  claim only when its rows keep their `x` from the source (through `filter`, `take`,
  `window`, `lttb`, `m4`, `sort`, `fold`, `stack`) and its `x` is the slot's window
  field.

# `at`

```elixir
@type at() :: {number(), number(), non_neg_integer()}
```

Where a reduction draws: `{left, resolution, limit}`.

# `cut_at`

```elixir
@type cut_at() :: {number(), 1 | -1}
```

Where to cut: the boundary's `x`, and the sign of the way time runs along `x`.

# `at`

```elixir
@spec at(map(), Macaw.Frame.Coordinates.Model.t()) :: at() | nil
```

Where a layer's dense drawing reduces in a model: `{left, resolution, limit}` in a
cartesian model, `nil` in any other (every point drawn).

# `boundary`

```elixir
@spec boundary(map(), Macaw.Data.Table.t(), Macaw.Frame.Coordinates.Model.t()) ::
  cut_at() | nil
```

Where a layer's claim cuts it (§1.12): `{x, sign}`, the claim's place on the `x` scale
and the way time runs along `x`, or `nil` when the delivery serves none or the layer
does not read it. A path is cut here and needs no row's state, so the boundary is read
without the mask `claim/3` adds, and a claim costs a line or an area its cut alone
(spec/12 §5.7).

# `claim`

```elixir
@spec claim(map(), Macaw.Data.Table.t(), Macaw.Frame.Coordinates.Model.t()) ::
  map() | nil
```

A layer's claim over its table (§1.12, D-274): `nil` when the delivery serves none or
the layer does not read it, else `%{x: pixel, sign: 1 | -1, mask: [boolean]}`, `x` the
claim's place on the `x` scale, `sign` the way time runs along `x`, `mask` each row's
state (`true` provisional: its `x` at or after the claim).

# `commands`

```elixir
@spec commands(
  binary(),
  binary(),
  [{non_neg_integer(), non_neg_integer()}],
  at() | nil
) :: [tuple()]
```

A straight line's path commands drawn straight from two pixel binaries over its runs
(§1.12): `M` at each run's first point and `L` to each next, a run of one point drawing
nothing, as a linear curve draws the same runs; each run over the limit of `at` written
from its M4 points, read from the binaries in its pass, so a command is made for each
point drawn and no term for a row left out. `at` `nil` draws every point.

# `covered`

```elixir
@spec covered([tuple()], at() | nil, boolean()) :: [tuple()]
```

The points a dense opaque point layer keeps (§1.12, D-270): of `points`, each `{index,
x, y, key}` (`key` the glyph, size, style and series that must agree), the last of each
device pixel and key, in order, when there are more than the limit; every point
otherwise.

# `cut`

```elixir
@spec cut(Macaw.Draw.IR.Path.t(), cut_at()) ::
  {Macaw.Draw.IR.Path.t(), Macaw.Draw.IR.Path.t()}
```

A path cut at the claim's `x` into `{settled, provisional}` paths (§1.12): each run
generated whole, then split where it crosses the boundary, an `L` or `H` by
interpolation, a cubic by de Casteljau, so no curve changes shape. A closed run is
never cut.

# `cut_run`

```elixir
@spec cut_run([tuple() | :Z], cut_at()) :: {list(), list()}
```

One run's commands cut at the claim's `x`: `{settled, provisional}` command lists.

# `keeps_x?`

```elixir
@spec keeps_x?(map()) :: boolean()
```

Whether a layer's steps keep its rows' `x` from the source (§1.12).

# `limit`

```elixir
@spec limit(number(), number()) :: non_neg_integer()
```

The point limit at a plot width and a resolution: `4 × ceil(width × resolution)`.

    iex> Macaw.Frame.Layer.Device.limit(100, 2)
    800

# `m4`

```elixir
@spec m4([[{number(), number()}]], at()) :: [[{number(), number()}]]
```

Each run of points `{x, y}` over the limit reduced by M4 at its device pixel columns,
every other run as it is: the first, the lowest, the highest and the last point of each
column the run visits, in order.

# `m4_slots`

```elixir
@spec m4_slots(binary(), binary(), non_neg_integer(), non_neg_integer(), at()) :: [
  {float(), float()}
]
```

M4 of the run of rows `first` to `last` of two pixel binaries at its device pixel
columns (§1.12), as `m4/2` reduces the same run's points: when the run is over the limit,
the first, lowest, highest and last point of each column it visits, in order, read in
one pass over the binaries; else every point.

# `measure`

```elixir
@spec measure(term()) :: number() | nil
```

An instant measured in seconds (§6.5): a `DateTime` or a `NaiveDateTime` by its epoch
microseconds over 10⁶, a `Date` by its days × 86,400, a number as itself; `nil` for
anything else.

# `opaque?`

```elixir
@spec opaque?(map()) :: boolean()
```

Whether a resolved style is fully opaque (§1.12): opacities `1` or unset, solid paints,
the default blend, no effect, a center stroke and no class.

# `over?`

```elixir
@spec over?([[term()]], at()) :: boolean()
```

Whether any run is over the limit.

# `points`

```elixir
@spec points(binary(), binary(), non_neg_integer(), non_neg_integer()) :: [
  {float(), float()}
]
```

The points `{x, y}` of rows `first` to `last` of two pixel binaries, each read in place:
a run's points, `m4_slots/5`'s when the run is over the limit.

# `records`

```elixir
@spec records(
  binary(),
  binary(),
  [{non_neg_integer(), non_neg_integer()}],
  at() | nil,
  :f32 | :f64
) ::
  iodata()
```

A straight line's binary canvas records written straight from two pixel binaries over
its runs (spec/10 §4.3.1, §4.3.8, §4.5): one polyline record a run of two points or more,
`path32` (`0x05`, f32 pairs) under `:f32`, the default, or `path` (`0x01`, f64 pairs)
under `:f64`, each `count:u32` little-endian then its points; a run over the limit of `at`
written from its M4 points. The points are those `commands/4` draws, so the stream and
the SVG path draw the same line, re-encoded only to the record's width.

    iex> xs = <<0.0::float-little-64, 2.0::float-little-64>>
    iex> ys = <<1.0::float-little-64, 3.0::float-little-64>>
    iex> Macaw.Frame.Layer.Device.records(xs, ys, [{0, 1}], nil, :f64) |> IO.iodata_to_binary()
    <<0x01, 2::little-32, 0.0::little-float-64, 1.0::little-float-64, 2.0::little-float-64, 3.0::little-float-64>>

# `reduced?`

```elixir
@spec reduced?(map(), Macaw.Frame.Coordinates.Model.t()) :: boolean()
```

Whether a layer's rows are already reduced by M4 at its device pixel columns (spec/05
§4.3): its delivery's `served.reduced_with` is `{"m4", n}` with `n` at least
`ceil(width × resolution)`, so it draws them without reducing again.

# `resolution`

```elixir
@spec resolution(map()) :: number()
```

A layer's device resolution: its context's, `1` by default.

# `runs`

```elixir
@spec runs(binary(), binary(), [non_neg_integer()]) :: [
  {non_neg_integer(), non_neg_integer()}
]
```

The runs of a path drawn from two pixel binaries (§1.12): `{first, last}` row ranges of
consecutive rows where both `xs` and `ys` are placed, broken at an unplaced row and
before every row of `breaks` (ascending), in one pass over the binaries and no term a
row.

    iex> xs = <<0.0::float-little-64, 1.0::float-little-64, 2.0::float-little-64, 3.0::float-little-64>>
    iex> ys = <<5.0::float-little-64, 0, 0, 0, 0, 0, 0, 0xF8, 0x7F, 6.0::float-little-64, 7.0::float-little-64>>
    iex> Macaw.Frame.Layer.Device.runs(xs, ys, [3])
    [{0, 0}, {2, 2}, {3, 3}]

---

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