Macaw.Draw.Units (macaw_draw v0.1.0)

Copy Markdown View Source

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.

t()

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

error()

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

A refusal: faults with their paths.

length_unit()

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

A world length unit.

t()

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

A length with its unit.

unit()

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

A screen unit.

Functions

absolute_units()

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

The absolute screen units.

cm(n)

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

{n, :cm}: a centimeter.

convert(n, from, to)

@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!(n, from, to)

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

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

convert_length(n, from, to)

@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!(n, from, to)

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

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

em(n)

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

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

inch(n)

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

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

length_units()

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

The world length units.

mm(n)

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

{n, :mm}: a millimeter.

pc(n)

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

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

pct(n)

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

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

pt(n)

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

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

px(n)

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

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

resolve(value, context)

@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!(value, context)

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

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

to_meters(n, unit)

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

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

to_meters!(n, unit)

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

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

to_px(n)

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

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

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

unit?(arg1)

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

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

units()

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

The screen units, absolute then relative.