# `Macaw.Draw.IR.Path`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/ir/path.ex#L1)

A path as data: drawing commands and their SVG path string (spec/10 §1.2), copied from
visualize's `Visualize.IR.Path`.

A path is `%Macaw.Draw.IR.Path{commands, metadata}`. A command is a tuple tagged with
its SVG letter, absolute upper-case and relative lower-case: `{:M|:m, x, y}`,
`{:L|:l, x, y}`, `{:H|:h, x}`, `{:V|:v, y}`, `{:C|:c, x1, y1, x2, y2, x, y}`,
`{:S|:s, x2, y2, x, y}`, `{:Q|:q, x1, y1, x, y}`, `{:T|:t, x, y}`,
`{:A|:a, rx, ry, rotation, large_arc, sweep, x, y}` (`large_arc` and `sweep` `0` or
`1`) and `:Z`. `metadata` is the producer's and is never written.

    iex> alias Macaw.Draw.IR.Path
    iex> Path.new() |> Path.move_to(10, 20) |> Path.line_to(100, 200.5) |> Path.close() |> Path.to_string()
    "M10,20L100,200.5Z"

The builders append one command each, at the cost of the list; **a path drawn per datum
is built in time linear in its commands** by collecting them in order and calling
`from_commands/1` once (visualize spec/02 §2.2, #414). `expand_smooth/1` and
`absolute/1` rewrite a path into the same figure; `transform/2` maps it under an affine
matrix, arcs exactly. A builder's coordinate that is no number is the fault
`{[n], {:type, :number}}` at its position.

# `command`

```elixir
@type command() ::
  {:M | :m | :L | :l | :T | :t, number(), number()}
  | {:H | :h | :V | :v, number()}
  | {:C | :c, number(), number(), number(), number(), number(), number()}
  | {:S | :s | :Q | :q, number(), number(), number(), number()}
  | {:A | :a, number(), number(), number(), 0 | 1, 0 | 1, number(), number()}
  | :Z
```

One drawing command (spec/10 §1.2).

# `error`

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

A refusal: faults with their paths.

# `t`

```elixir
@type t() :: %Macaw.Draw.IR.Path{commands: [command()], metadata: map()}
```

# `absolute`

```elixir
@spec absolute(t()) :: t() | error()
```

Every command rewritten as an absolute `M`, `L`, `C`, `Q`, `A` or `Z` drawing the same
figure (spec/10 §1.2): relative commands resolved against the current point (a leading
`m` is absolute, as SVG reads it), `H`/`V` as `L`, the smooth commands expanded as
`expand_smooth/1` does. What a target without relative commands replays.

    iex> alias Macaw.Draw.IR.Path
    iex> Path.new() |> Path.move_to(1, 1) |> Path.line_to_rel(2, 0) |> Path.vertical_to(5)
    ...> |> Path.close() |> Path.absolute() |> Map.fetch!(:commands)
    [{:M, 1, 1}, {:L, 3, 1}, {:L, 3, 5}, :Z]

# `append`

```elixir
@spec append(t(), t()) :: t() | error()
```

The commands of `first` then those of `second`.

# `arc_to`

```elixir
@spec arc_to(t(), number(), number(), number(), 0 | 1, 0 | 1, number(), number()) ::
  t() | error()
```

An elliptical arc to `(x, y)`: radii `rx`, `ry`, the ellipse's rotation in degrees, and
the `large_arc` and `sweep` flags, each `0` or `1`.

# `arc_to_rel`

```elixir
@spec arc_to_rel(t(), number(), number(), number(), 0 | 1, 0 | 1, number(), number()) ::
  t() | error()
```

An elliptical arc to a point relative to the current one.

# `close`

```elixir
@spec close(t()) :: t() | error()
```

Closes the current subpath.

# `command_count`

```elixir
@spec command_count(t()) :: non_neg_integer() | error()
```

The number of commands in the path.

# `concat`

```elixir
@spec concat([t()]) :: t() | error()
```

One path of the commands of several, in order.

# `curve_to`

```elixir
@spec curve_to(t(), number(), number(), number(), number(), number(), number()) ::
  t() | error()
```

A cubic Bézier curve through the controls `(x1, y1)` and `(x2, y2)` to `(x, y)`.

# `curve_to_rel`

```elixir
@spec curve_to_rel(t(), number(), number(), number(), number(), number(), number()) ::
  t() | error()
```

A cubic Bézier curve with every point relative to the current one.

# `empty?`

```elixir
@spec empty?(t()) :: boolean() | error()
```

Whether the path has no command.

# `expand_smooth`

```elixir
@spec expand_smooth(t()) :: t() | error()
```

The smooth commands `S`/`s`/`T`/`t` rewritten as `C`/`c`/`Q`/`q` (spec/10 §1.2, D-36):
the first control point the reflection of the previous cubic's (for `S`) or quadratic's
(for `T`) last control point about the current point, else the current point itself.
Every other command is kept, so relative commands stay relative.

    iex> alias Macaw.Draw.IR.Path
    iex> Path.new() |> Path.move_to(0, 0) |> Path.curve_to(1, 1, 2, 1, 3, 0)
    ...> |> Path.smooth_curve_to(5, -1, 6, 0) |> Path.expand_smooth() |> Map.fetch!(:commands)
    [{:M, 0, 0}, {:C, 1, 1, 2, 1, 3, 0}, {:C, 4, -1, 5, -1, 6, 0}]

# `format_number`

```elixir
@spec format_number(term()) :: String.t()
```

The one number formatter every serializer shares (spec/10 §1.2; visualize D-12, D-32,
#812): an integer as-is; a float with at most four decimals, trailing zeros and point
trimmed, never in exponent notation and never `-0`; a float that rounds to a whole
number as that integer's digits at any magnitude. A term that is no number is written by
`Kernel.to_string/1` where it can be, else inspected.

    iex> Enum.map([1.5, 1.0e20, -0.00001, 2.0, 0.1 + 0.2], &Macaw.Draw.IR.Path.format_number/1)
    ["1.5", "100000000000000000000", "0", "2", "0.3"]

# `from_commands`

```elixir
@spec from_commands([command()]) :: t() | error()
```

A path of the commands given, in the order given (spec/10 §1.2; visualize #414): a
producer drawing one command per datum collects its commands and makes the path once.

    iex> Macaw.Draw.IR.Path.from_commands([{:M, 0, 0}, {:L, 10, 5}]).commands
    [{:M, 0, 0}, {:L, 10, 5}]

# `horizontal_to`

```elixir
@spec horizontal_to(t(), number()) :: t() | error()
```

A horizontal line to `x`.

# `horizontal_to_rel`

```elixir
@spec horizontal_to_rel(t(), number()) :: t() | error()
```

A horizontal line by `dx`.

# `line_to`

```elixir
@spec line_to(t(), number(), number()) :: t() | error()
```

A line to `(x, y)`.

# `line_to_rel`

```elixir
@spec line_to_rel(t(), number(), number()) :: t() | error()
```

A line by `(dx, dy)`.

# `move_to`

```elixir
@spec move_to(t(), number(), number()) :: t() | error()
```

Moves the pen to `(x, y)`.

# `move_to_rel`

```elixir
@spec move_to_rel(t(), number(), number()) :: t() | error()
```

Moves the pen by `(dx, dy)`.

# `new`

```elixir
@spec new() :: t()
```

An empty path.

# `quad_to`

```elixir
@spec quad_to(t(), number(), number(), number(), number()) :: t() | error()
```

A quadratic Bézier curve through the control `(x1, y1)` to `(x, y)`.

# `quad_to_rel`

```elixir
@spec quad_to_rel(t(), number(), number(), number(), number()) :: t() | error()
```

A quadratic Bézier curve with every point relative to the current one.

# `smooth_curve_to`

```elixir
@spec smooth_curve_to(t(), number(), number(), number(), number()) :: t() | error()
```

A smooth cubic curve: its first control the reflection of the previous one.

# `smooth_curve_to_rel`

```elixir
@spec smooth_curve_to_rel(t(), number(), number(), number(), number()) ::
  t() | error()
```

A smooth cubic curve with every point relative to the current one.

# `smooth_quad_to`

```elixir
@spec smooth_quad_to(t(), number(), number()) :: t() | error()
```

A smooth quadratic curve: its control the reflection of the previous one.

# `smooth_quad_to_rel`

```elixir
@spec smooth_quad_to_rel(t(), number(), number()) :: t() | error()
```

A smooth quadratic curve to a point relative to the current one.

# `to_string`

```elixir
@spec to_string(t()) :: String.t() | error()
```

The SVG path data (spec/10 §1.2): commands with no separator, arguments comma-separated,
every number through `format_number/1`, in work linear in the commands. A command of no
shape in a path built by hand is written as nothing here; the renderer refuses it as a
fault (spec/10 §2.5).

# `transform`

```elixir
@spec transform(t(), Macaw.Draw.IR.Transform.matrix() | Macaw.Draw.IR.Transform.t()) ::
  t() | error()
```

The path under an affine transform, a matrix `{a, b, c, d, e, f}` or a
`Macaw.Draw.IR.Transform` (spec/10 §1.2; visualize spec/02 §2.2, D-35).

Absolute points get the full map, relative deltas its linear part. `H`/`V` (and
`h`/`v`) stay single-axis when the transform keeps that axis, else become `L` (`l`). An
arc's end point is mapped, its radii and rotation are those of the exact image ellipse,
and its sweep flips under a reflection. `:Z` is untouched.

    iex> alias Macaw.Draw.IR.Path
    iex> Path.new() |> Path.move_to(1, 2) |> Path.line_to_rel(3, 4) |> Path.horizontal_to(9)
    ...> |> Path.transform({1, 0, 0, 1, 10, 20}) |> Map.fetch!(:commands)
    [{:M, 11, 22}, {:l, 3, 4}, {:H, 19}]

# `vertical_to`

```elixir
@spec vertical_to(t(), number()) :: t() | error()
```

A vertical line to `y`.

# `vertical_to_rel`

```elixir
@spec vertical_to_rel(t(), number()) :: t() | error()
```

A vertical line by `dy`.

---

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