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

A scene node (spec/10 §1.1), copied from visualize's `Visualize.IR.Element`.

A node is `%Macaw.Draw.IR.Element{type, attrs, style, transform, children, content,
path, clip}`: its type, its geometric and data attributes (atom keys for the attributes
the scene names, string keys such as `"data-mw-id"` for any other), its style keys, a
`Macaw.Draw.IR.Transform` or `nil`, its child nodes, a text's content, a `:path`'s
`Macaw.Draw.IR.Path` and its clip, `nil` or `%{shape: node, keep: :inside | :outside}`.

    iex> alias Macaw.Draw.IR.{Element, Path}
    iex> path = Path.new() |> Path.move_to(0, 0) |> Path.line_to(100, 100)
    iex> node = Element.path(path, %{stroke: "blue", fill: :none})
    iex> {node.type, node.style}
    {:path, %{fill: :none, stroke: "blue"}}

**The node types** are `:path`, `:rect`, `:circle`, `:ellipse`, `:line`, `:polyline`,
`:polygon`, `:text`, `:tspan`, `:group`, `:root`, `:image`, `:defs`, `:clip_path`,
`:linear_gradient`, `:radial_gradient`, `:pattern`, `:stop`, `:filter`,
`:fe_drop_shadow`, `:fe_gaussian_blur`, `:title` and `:desc` (`types/0`); visualize's
`:style` node is not one, since a page holds no `<style>` (spec/10 §1.1).

A constructor builds its node from arguments of the right type and answers a type fault,
`{:error, [{path, {:type, expected}}]}`, for an argument of the wrong one (spec/06 §1.2);
a node built by hand with values of the wrong shape is the renderer's to refuse, as a
fault by its path (spec/10 §2.5), so building a scene never raises.

The effective paint decides in one place what a canvas draws (spec/10 §1.1, visualize
#934, D-320, D-322): `fills?/2`, `strokes?/2`, `stroke_first?/2`, `font/2` and
`font_size/2` each read a node's own style over the `inherited` style its groups give
it.

# `clip`

```elixir
@type clip() :: %{shape: t(), keep: :inside | :outside}
```

Where a node is drawn: only where the shape is, or only where it is not.

# `error`

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

A refusal: faults with their paths.

# `style`

```elixir
@type style() :: map()
```

A node's style: the keys of spec/10 §1.1.

# `t`

```elixir
@type t() :: %Macaw.Draw.IR.Element{
  attrs: map(),
  children: [t()],
  clip: clip() | nil,
  content: String.t() | nil,
  path: Macaw.Draw.IR.Path.t() | nil,
  style: style(),
  transform: Macaw.Draw.IR.Transform.t() | nil,
  type: type()
}
```

# `type`

```elixir
@type type() ::
  :path
  | :rect
  | :circle
  | :ellipse
  | :line
  | :polyline
  | :polygon
  | :text
  | :tspan
  | :group
  | :root
  | :image
  | :defs
  | :clip_path
  | :linear_gradient
  | :radial_gradient
  | :pattern
  | :stop
  | :filter
  | :fe_drop_shadow
  | :fe_gaussian_blur
  | :title
  | :desc
```

A node type (spec/10 §1.1).

# `append`

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

Appends a node, or a list of nodes, to a node's children.

# `circle`

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

A `:circle` node at `(cx, cy)` of radius `r`.

# `clip`

```elixir
@spec clip(t(), t(), :inside | :outside) :: t() | error()
```

The node drawn only where `shape` is (`:inside`) or only where it is not (`:outside`)
(spec/10 §1.1, visualize D-211): the shape keeps its geometry alone, its transform,
style, clip and data attributes dropped, so it is in the node's own user space and a
node clipped to itself is clipped to its outline wherever its transform puts it.

    iex> square = Macaw.Draw.IR.Element.rect(0, 0, 10, 10, %{fill: "red"})
    iex> clipped = Macaw.Draw.IR.Element.clip(square, square, :inside)
    iex> {clipped.clip.keep, clipped.clip.shape.style}
    {:inside, %{}}

# `clip_path`

```elixir
@spec clip_path(String.t(), [t()]) :: t() | error()
```

A `:clip_path` definition of `id` holding `children`.

# `datum`

```elixir
@spec datum(t(), map() | keyword(), map()) :: t() | error()
```

Writes a datum's fields onto the node as the data attributes the tooltip reads
(spec/10 §1.1, §7.3; visualize D-75, D-225).

`fields` is a map, written in `Enum.sort/1` order, or a keyword list, in its own order.
`"data-mw-datum"` lists the field names space-separated; `"data-mw-datum-<field>"`
carries `to_string/1` of each value, every `_` in the name as `-`, a `nil` value and a
value with no text form (`text?/1`) writing no attribute; `"data-mw-named-<field>"` is
the value's display name for each field written that `names` holds. Keys are strings,
so no atom is made from a row's names.

    iex> Macaw.Draw.IR.Element.circle(1, 2, 3)
    ...> |> Macaw.Draw.IR.Element.datum([state: "degraded", big_n: 2], %{state: "Degraded"})
    ...> |> Map.fetch!(:attrs)
    ...> |> Map.drop([:cx, :cy, :r])
    %{
      "data-mw-datum" => "state big_n",
      "data-mw-datum-big-n" => "2",
      "data-mw-datum-state" => "degraded",
      "data-mw-named-state" => "Degraded"
    }

# `defs`

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

A `:defs` node: definitions, drawn nowhere.

# `desc`

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

A `:desc`: the accessible description of its parent.

# `drop_shadow`

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

A drop shadow primitive of the offset and the blur radius, at 0.35 opacity.

# `drop_shadow`

```elixir
@spec drop_shadow(
  number(),
  number(),
  number(),
  String.t() | Macaw.Layers.Color.t() | nil
) ::
  t() | error()
```

A drop shadow primitive of the offset, the blur radius and a color: `flood-color` the
color at `flood-opacity` 1, so the color's own alpha is the shadow's; a `nil` color is
`drop_shadow/3`.

# `ellipse`

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

An `:ellipse` node at `(cx, cy)` of radii `rx` and `ry`.

# `fills?`

```elixir
@spec fills?(t(), style()) :: boolean() | error()
```

Whether a canvas draws the node's fill, by its effective fill: its own `:fill`, else
`inherited`'s (spec/10 §1.1, visualize #934, D-320). `:none` is never drawn whatever a
group above set; a `:line` has no area; a node that draws no shape has no fill; an
absent fill is SVG's initial black on a `:text` and nothing on any other shape (D-122).

    iex> alias Macaw.Draw.IR.Element
    iex> Element.fills?(Element.text("A", 0, 0, %{fill: :none}), %{fill: "#000"})
    false
    iex> Element.fills?(Element.text("A", 0, 0), %{})
    true
    iex> Element.fills?(Element.rect(0, 0, 2, 2), %{})
    false

# `filter`

```elixir
@spec filter(String.t(), [t()]) :: t() | error()
```

A `:filter` of `id` holding its primitives, for a node's `:effect`.

# `font`

```elixir
@spec font(t(), style()) :: String.t() | nil | error()
```

The font a canvas draws a `:text` in, the CSS shorthand `[style] [weight] size family`
of its effective `:font_style`, `:font_weight`, `:font_size` and `:font_family`
(spec/10 §1.1, visualize #1150, D-322): the size `font_size/2`'s with `px`, the family
`"sans-serif"` where none is set, the style and the weight only where set. `nil` when
none of the four is set on the text or above it, and for every node but a `:text`.

    iex> alias Macaw.Draw.IR.Element
    iex> Element.font(Element.text("A", 0, 10, %{font_weight: :bold}), %{font_size: 20, font_family: "serif"})
    "bold 20px serif"
    iex> Element.font(Element.text("A", 0, 10), %{})
    nil

# `font_size`

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

A text's effective `:font_size` where it is a number, else `10`, the canvas default's:
the size `font/2` writes and the one an `em` `dy` is read at (spec/10 §1.1, D-321).

    iex> alias Macaw.Draw.IR.Element
    iex> {Element.font_size(Element.text("A", 0, 10), %{font_size: 20}), Element.font_size(Element.text("A", 0, 10), %{})}
    {20, 10}

# `gaussian_blur`

```elixir
@spec gaussian_blur(number()) :: t() | error()
```

A Gaussian blur primitive of the radius.

# `group`

```elixir
@spec group([t()], keyword()) :: t() | error()
```

A `:group` node with options `:style`, `:transform` and `:attrs`.

# `image`

```elixir
@spec image(String.t(), number(), number(), number(), number(), style()) ::
  t() | error()
```

An `:image` node: a same-origin `href` (spec/10 §8) at `(x, y)`, `width` by `height`.

# `line`

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

A `:line` node from `(x1, y1)` to `(x2, y2)`.

# `linear_gradient`

```elixir
@spec linear_gradient(String.t(), keyword()) :: t() | error()
```

A `:linear_gradient` of `id` with options `:x1`, `:y1`, `:x2`, `:y2` and `:stops`.

# `new`

```elixir
@spec new(atom(), keyword()) :: t() | error()
```

A node of `type` with options `:attrs`, `:style`, `:transform`, `:children`, `:content`
and `:path`.

# `path`

```elixir
@spec path(Macaw.Draw.IR.Path.t(), style()) :: t() | error()
```

A `:path` node drawing a `Macaw.Draw.IR.Path`.

# `polygon`

```elixir
@spec polygon([{number(), number()}] | String.t(), style()) :: t() | error()
```

A `:polygon` node, the closed form of `polyline/2`.

# `polyline`

```elixir
@spec polyline([{number(), number()}] | String.t(), style()) :: t() | error()
```

A `:polyline` node through a list of `{x, y}` points, or a `points` string.

# `prepend`

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

Prepends a node, or a list of nodes, to a node's children.

# `put_attr`

```elixir
@spec put_attr(t(), atom() | String.t(), term()) :: t() | error()
```

The node with one attribute set: `key` an atom, or a string for an attribute the scene
has no name for (`"data-mw-id"`).

    iex> Macaw.Draw.IR.Element.group([]) |> Macaw.Draw.IR.Element.put_attr("data-mw-id", "f.1.#1") |> Map.fetch!(:attrs)
    %{"data-mw-id" => "f.1.#1"}

# `radial_gradient`

```elixir
@spec radial_gradient(String.t(), keyword()) :: t() | error()
```

A `:radial_gradient` of `id` with options `:cx`, `:cy`, `:r`, `:fx`, `:fy` and `:stops`.

# `rect`

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

A `:rect` node at `(x, y)`, `width` by `height`.

# `root`

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

The whole drawing (spec/10 §1.1; visualize D-54, D-56, D-142, D-201): attrs `width`,
`height`, `view_box` (`"0 0 W H"` by default) and `role: "img"`.

Options:
  * `:view_box`, `:preserve_aspect_ratio` and `:children`;
  * `:title`, the root's `aria_label`, never a `<title>` child, which a browser shows as
    a tooltip under the pointer;
  * `:description`, a `desc/1` first child with the id `<id>-desc`, which
    `aria_describedby` names; a description with no title is named by
    `"aria-labelledby"` alone, a string key as the attribute is spelled;
  * `hidden: true`, `aria_hidden: "true"`: one layer of a picture another element names;
  * `:id`, the root's id and the description's prefix, by default `"mw-"` and the
    base-36 `:erlang.phash2/1` of the title and the description, set when either is;
  * `:class`, the root's own class, and `responsive: true`, which adds the class
    `mw-frame-responsive` after it (spec/10 §3.6) and `preserveAspectRatio="xMidYMid
    meet"` unless given, never an inline `style`.

    iex> root = Macaw.Draw.IR.Element.root(200, 100, title: "Sales", responsive: true)
    iex> Map.take(root.attrs, [:aria_label, :class, :view_box, :role])
    %{aria_label: "Sales", class: "mw-frame-responsive", role: "img", view_box: "0 0 200 100"}

# `set_attrs`

```elixir
@spec set_attrs(t(), map()) :: t() | error()
```

The node with attributes merged over its own.

# `set_content`

```elixir
@spec set_content(t(), String.t() | nil) :: t() | error()
```

The node with its text content set; `nil` clears it.

# `set_style`

```elixir
@spec set_style(t(), style()) :: t() | error()
```

The node with style keys merged over its own.

# `set_transform`

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

The node with its transform replaced.

# `stop`

```elixir
@spec stop(number() | String.t(), Macaw.Layers.Color.t() | String.t(), keyword()) ::
  t() | error()
```

A gradient's `:stop` at `offset` (a number from `0` to `1`, or a percentage string) of
`color` (a color term or a string), with the option `:opacity`.

# `stroke_first?`

```elixir
@spec stroke_first?(t(), style()) :: boolean() | error()
```

Whether the node paints its stroke under its fill: a `:text` that `strokes?/2`, its
stroke its own or its groups' (spec/10 §1.1, visualize D-235). A text's stroke is a
halo, which frames the glyphs and never covers them; every target asks this.

    iex> alias Macaw.Draw.IR.Element
    iex> Element.stroke_first?(Element.text("A", 0, 0, %{fill: "#222"}), %{stroke: "#fff"})
    true
    iex> Element.stroke_first?(Element.rect(0, 0, 2, 2, %{stroke: "#fff"}), %{})
    false

# `strokes?`

```elixir
@spec strokes?(t(), style()) :: boolean() | error()
```

Whether a canvas draws the node's stroke: a shape or a `:text` whose effective stroke is
neither absent nor `:none`, SVG's initial stroke being none (spec/10 §1.1, D-320).

    iex> alias Macaw.Draw.IR.Element
    iex> Element.strokes?(Element.rect(0, 0, 2, 2, %{stroke: :none}), %{stroke: "#fff"})
    false
    iex> Element.strokes?(Element.line(0, 0, 2, 2), %{stroke: "#fff"})
    true

# `text`

```elixir
@spec text(String.t(), keyword()) :: t() | error()
```

A `:text` node with options `:x`, `:y`, `:dx`, `:dy` and `:style`.

# `text`

```elixir
@spec text(String.t(), number(), number(), style()) :: t() | error()
```

A `:text` node at `(x, y)`.

# `text?`

```elixir
@spec text?(term()) :: boolean()
```

Whether a value has a text form a tooltip shows (spec/10 §1.1; visualize #1111, D-303):
a string, a number, an atom, or a `Date`, `Time`, `NaiveDateTime` or `DateTime`. A path,
a geometry, a point, a list, a tuple and any other struct have none.

    iex> Enum.map(["north", ~D[2024-01-01], %{"type" => "Point"}, {1, 2}], &Macaw.Draw.IR.Element.text?/1)
    [true, true, false, false]

# `title`

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

A `:title`: the accessible name of the node it is the first child of.

# `translate`

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

The node translated by `(x, y)` after its own transform's operations.

# `translate_group`

```elixir
@spec translate_group(t() | [t()], number(), number()) :: t() | error()
```

A node, or a list of nodes, in a group translated by `(x, y)`.

# `tspan`

```elixir
@spec tspan(String.t(), map()) :: t() | error()
```

One line of a `:text`: a `:tspan` with its content and attrs, `x` and `dy` among them. A
`:text` whose children are tspans and whose content is `nil` is a text of several lines.

# `types`

```elixir
@spec types() :: [type()]
```

The node types a target writes or knowingly drops (spec/10 §1.1).

    iex> :path in Macaw.Draw.IR.Element.types()
    true

---

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