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

Lengths with screen units, and world lengths (spec/07 §23), copied from visualize's
`Visualize.Units`.

A length is `{n, unit}`, data stored and compared as a value; a bare number is user
units, CSS pixels on every renderer. The **absolute** units convert at CSS's fixed
ratios, 96 px = 1 in = 2.54 cm = 25.4 mm = 72 pt and 1 pc = 12 pt; the **relative** ones,
`:em` and `:pct`, resolve against a font size and a box (`resolve/2`), never by `to_px/1`.

    iex> import Macaw.Draw.Units
    iex> to_px(pt(12))
    {:ok, 16.0}
    iex> resolve(em(1.5), %{em: 12})
    {:ok, 18.0}
    iex> to_px(em(1))
    {:error, [{[], {:relative, :em}}]}

**World lengths** are a scene's ground, in meters (`:mm` … `:mi`, the international inch,
foot, yard and mile); one reaches the screen only through a scale a scene declares.
These are screen lengths, not data units: a field's unit is a UCUM code of `macaw_units`.
A unit is an atom of the vocabulary or its string, read by spelling. Each refusable
function has a `!` twin.

# `error`

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

A refusal: faults with their paths.

# `length_unit`

```elixir
@type length_unit() :: :mm | :cm | :m | :km | :in | :ft | :yd | :mi
```

A world length unit.

# `t`

```elixir
@type t() :: {number(), unit()}
```

A length with its unit.

# `unit`

```elixir
@type unit() :: :px | :pt | :pc | :in | :cm | :mm | :em | :pct
```

A screen unit.

# `absolute_units`

```elixir
@spec absolute_units() :: [unit(), ...]
```

The absolute screen units.

# `cm`

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

`{n, :cm}`: a centimeter.

# `convert`

```elixir
@spec convert(number(), unit(), unit()) :: {:ok, float()} | error()
```

`n` of one absolute unit in another: `{:ok, number}`, or
`{:error, [{[i], {:unknown, :unit, u}}]}` for a unit at argument `i` that is none.

# `convert!`

```elixir
@spec convert!(number(), unit(), unit()) :: float()
```

As `convert/3`, the number itself, raising `Macaw.Draw.Error` with its faults.

# `convert_length`

```elixir
@spec convert_length(number(), length_unit(), length_unit()) ::
  {:ok, float()} | error()
```

A world length in another world unit: `{:ok, number}`, or the unknown units' faults.

# `convert_length!`

```elixir
@spec convert_length!(number(), length_unit(), length_unit()) :: float()
```

As `convert_length/3`, the number itself, raising `Macaw.Draw.Error` with its faults.

# `em`

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

`{n, :em}`: `n` times a font size, the one the context gives.

# `inch`

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

`{n, :in}`: an inch, 96 user units (`in` is an Elixir operator).

# `length_units`

```elixir
@spec length_units() :: [length_unit(), ...]
```

The world length units.

# `mm`

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

`{n, :mm}`: a millimeter.

# `pc`

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

`{n, :pc}`: a pica, 12 points.

# `pct`

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

`{n, :pct}`: `n` percent of a box's length, the one the context gives.

# `pt`

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

`{n, :pt}`: a point, 1/72 of an inch.

# `px`

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

`{n, :px}`: a user unit, a CSS pixel.

# `resolve`

```elixir
@spec resolve(number() | t(), map()) :: {:ok, number()} | error()
```

A length in user units against `%{em: font_size, box: length}`: a number as it is, an
absolute length by `to_px/1`, an `em` against `em` and a `pct` against `box`;
`{:error, [{[], {:no_measure, unit}}]}` when the context lacks the measure.

# `resolve!`

```elixir
@spec resolve!(number() | t(), map()) :: number()
```

As `resolve/2`, the number itself, raising `Macaw.Draw.Error` with its faults.

# `to_meters`

```elixir
@spec to_meters(number(), length_unit()) :: {:ok, number()} | error()
```

A world length in meters: `{:ok, number}`, or the unknown unit's fault.

# `to_meters!`

```elixir
@spec to_meters!(number(), length_unit()) :: number()
```

As `to_meters/2`, the number itself, raising `Macaw.Draw.Error` with its faults.

# `to_px`

```elixir
@spec to_px(number() | t()) :: {:ok, number()} | error()
```

A number as it is, an absolute length in user units: `{:ok, number}`; an `em` or a `pct`
is `{:error, [{[], {:relative, unit}}]}`, with nothing to be measured against here.

# `to_px!`

```elixir
@spec to_px!(number() | t()) :: number()
```

As `to_px/1`, the number itself, raising `Macaw.Draw.Error` with its faults.

# `unit?`

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

Whether a term is `{n, unit}` with `n` a number and `unit` of `units/0`.

# `units`

```elixir
@spec units() :: [unit(), ...]
```

The screen units, absolute then relative.

---

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