Macaw.Draw.IR.Path (macaw_draw v0.1.0)

Copy Markdown View Source

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.

Summary

Types

One drawing command (spec/10 §1.2).

A refusal: faults with their paths.

t()

Functions

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.

The commands of first then those of second.

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.

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

Closes the current subpath.

The number of commands in the path.

One path of the commands of several, in order.

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

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

Whether the path has no command.

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.

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.

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.

A horizontal line to x.

A horizontal line by dx.

A line to (x, y).

A line by (dx, dy).

Moves the pen to (x, y).

Moves the pen by (dx, dy).

An empty path.

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

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

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

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

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

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

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).

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).

A vertical line to y.

A vertical line by dy.

Types

command()

@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()

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

A refusal: faults with their paths.

t()

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

Functions

absolute(path)

@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(first, second)

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

The commands of first then those of second.

arc_to(path, rx, ry, rotation, large_arc, sweep, x, y)

@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(path, rx, ry, rotation, large_arc, sweep, dx, dy)

@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(path)

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

Closes the current subpath.

command_count(path)

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

The number of commands in the path.

concat(paths)

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

One path of the commands of several, in order.

curve_to(path, x1, y1, x2, y2, x, y)

@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(path, dx1, dy1, dx2, dy2, dx, dy)

@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?(path)

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

Whether the path has no command.

expand_smooth(path)

@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(n)

@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(commands)

@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(path, x)

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

A horizontal line to x.

horizontal_to_rel(path, dx)

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

A horizontal line by dx.

line_to(path, x, y)

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

A line to (x, y).

line_to_rel(path, dx, dy)

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

A line by (dx, dy).

move_to(path, x, y)

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

Moves the pen to (x, y).

move_to_rel(path, dx, dy)

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

Moves the pen by (dx, dy).

new()

@spec new() :: t()

An empty path.

quad_to(path, x1, y1, x, y)

@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(path, dx1, dy1, dx, dy)

@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(path, x2, y2, x, y)

@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(path, dx2, dy2, dx, dy)

@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(path, x, y)

@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(path, dx, dy)

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

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

to_string(path)

@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(path, t)

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(path, y)

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

A vertical line to y.

vertical_to_rel(path, dy)

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

A vertical line by dy.