Visualize.Units (Visualize v0.8.3)

Copy Markdown View Source

Lengths with their units (spec/08 §13, spec/14 §3.9, #651, D-174).

A unit value is {n, unit}: data, stored and compared as a value. The absolute screen units convert to user units — CSS pixels — 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, are measured against a font size and a box the caller gives (resolve/2). A bare number is user units, as it always was.

iex> import Visualize.Units
iex> pt(12)
{12, :pt}
iex> Visualize.Units.to_px(pt(12))
16.0
iex> Visualize.Units.resolve(em(1.5), %{em: 12})
18.0
iex> Visualize.Units.resolve(pct(50), %{box: 30})
15.0

The world lengths a scene measures its ground in (#620) — :mm … :mi — are a table of their own, in meters (to_meters/2, convert_length/3): a world length reaches the screen only through a scale a scene declares.

Summary

Types

A world length unit (spec/08 §13.3).

t()

A length with its unit.

A screen unit (spec/08 §13.1, §13.2).

Functions

The absolute screen units (spec/08 §13.1).

{n, :cm}.

n of one absolute unit in another.

A world length in another world unit.

{n, :em}: n times a font size, the one the context gives (spec/14 §3.9).

{n, :in}: an inch, 96 user units. Named inch because in is an Elixir operator.

The world length units (spec/08 §13.3).

{n, :mm}.

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

{n, :pct}: n percent of a box's length, the one the context gives (spec/14 §3.9).

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

{n, :px}.

A size in user units: a number as it is, an absolute unit value by to_px/1, an em against the context's :em (a font size) and a pct against its :box (a length). Raises ArgumentError naming the unit when the context lacks its measure.

A world length in meters.

A number as it is, an absolute unit value in user units; ArgumentError for a relative one, which has nothing to be measured against here.

Whether a term is a unit value.

The screen units, absolute then relative.

Types

length_unit()

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

A world length unit (spec/08 §13.3).

t()

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

A length with its unit.

unit()

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

A screen unit (spec/08 §13.1, §13.2).

Functions

absolute_units()

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

The absolute screen units (spec/08 §13.1).

cm(n)

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

{n, :cm}.

convert(n, from, to)

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

n of one absolute unit in another.

iex> Visualize.Units.convert(1, :in, :pt)
72.0

convert_length(n, from, to)

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

A world length in another world unit.

iex> Visualize.Units.convert_length(2500, :m, :km)
2.5

em(n)

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

{n, :em}: n times a font size, the one the context gives (spec/14 §3.9).

inch(n)

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

{n, :in}: an inch, 96 user units. Named inch because in is an Elixir operator.

length_units()

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

The world length units (spec/08 §13.3).

mm(n)

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

{n, :mm}.

pc(n)

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

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

pct(n)

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

{n, :pct}: n percent of a box's length, the one the context gives (spec/14 §3.9).

pt(n)

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

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

px(n)

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

{n, :px}.

resolve(n, arg2)

@spec resolve(number() | t(), %{optional(:em) => number(), optional(:box) => number()}) ::
  number()

A size in user units: a number as it is, an absolute unit value by to_px/1, an em against the context's :em (a font size) and a pct against its :box (a length). Raises ArgumentError naming the unit when the context lacks its measure.

to_meters(n, unit)

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

A world length in meters.

iex> Visualize.Units.to_meters(1, :ft)
0.3048

to_px(n)

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

A number as it is, an absolute unit value in user units; ArgumentError for a relative one, which has nothing to be measured against here.

unit?(arg1)

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

Whether a term is a unit value.

iex> Visualize.Units.unit?({3, :mm})
true
iex> Visualize.Units.unit?({3, :furlong})
false

units()

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

The screen units, absolute then relative.