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.
Summary
Types
A refusal: faults with their paths.
A world length unit.
A length with its unit.
A screen unit.
Functions
The absolute screen units.
{n, :cm}: a centimeter.
n of one absolute unit in another: {:ok, number}, or
{:error, [{[i], {:unknown, :unit, u}}]} for a unit at argument i that is none.
As convert/3, the number itself, raising Macaw.Draw.Error with its faults.
A world length in another world unit: {:ok, number}, or the unknown units' faults.
As convert_length/3, the number itself, raising Macaw.Draw.Error with its faults.
{n, :em}: n times a font size, the one the context gives.
{n, :in}: an inch, 96 user units (in is an Elixir operator).
The world length units.
{n, :mm}: a millimeter.
{n, :pc}: a pica, 12 points.
{n, :pct}: n percent of a box's length, the one the context gives.
{n, :pt}: a point, 1/72 of an inch.
{n, :px}: a user unit, a CSS pixel.
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.
As resolve/2, the number itself, raising Macaw.Draw.Error with its faults.
A world length in meters: {:ok, number}, or the unknown unit's fault.
As to_meters/2, the number itself, raising Macaw.Draw.Error with its faults.
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.
As to_px/1, the number itself, raising Macaw.Draw.Error with its faults.
Whether a term is {n, unit} with n a number and unit of units/0.
The screen units, absolute then relative.
Types
@type error() :: {:error, [Macaw.Base.Fault.t()]}
A refusal: faults with their paths.
@type length_unit() :: :mm | :cm | :m | :km | :in | :ft | :yd | :mi
A world length unit.
A length with its unit.
@type unit() :: :px | :pt | :pc | :in | :cm | :mm | :em | :pct
A screen unit.
Functions
@spec absolute_units() :: [unit(), ...]
The absolute screen units.
{n, :cm}: a centimeter.
n of one absolute unit in another: {:ok, number}, or
{:error, [{[i], {:unknown, :unit, u}}]} for a unit at argument i that is none.
As convert/3, the number itself, raising Macaw.Draw.Error with its faults.
@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.
@spec convert_length!(number(), length_unit(), length_unit()) :: float()
As convert_length/3, the number itself, raising Macaw.Draw.Error with its faults.
{n, :em}: n times a font size, the one the context gives.
{n, :in}: an inch, 96 user units (in is an Elixir operator).
@spec length_units() :: [length_unit(), ...]
The world length units.
{n, :mm}: a millimeter.
{n, :pc}: a pica, 12 points.
{n, :pct}: n percent of a box's length, the one the context gives.
{n, :pt}: a point, 1/72 of an inch.
{n, :px}: a user unit, a CSS pixel.
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.
As resolve/2, the number itself, raising Macaw.Draw.Error with its faults.
@spec to_meters(number(), length_unit()) :: {:ok, number()} | error()
A world length in meters: {:ok, number}, or the unknown unit's fault.
@spec to_meters!(number(), length_unit()) :: number()
As to_meters/2, the number itself, raising Macaw.Draw.Error with its faults.
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.
As to_px/1, the number itself, raising Macaw.Draw.Error with its faults.
Whether a term is {n, unit} with n a number and unit of units/0.
@spec units() :: [unit(), ...]
The screen units, absolute then relative.