Macaw.Layers.Text (macaw_layers v0.6.0)

Copy Markdown View Source

Text measured through the seam (spec/05): its width, the lines it wraps to, and the font a theme names for it.

iex> Macaw.Layers.Text.width("Wednesday", "sans-serif", 11)
{:ok, 59.4}
iex> Macaw.Layers.Text.wrap("one two three", "sans-serif", 10, 50)
{:ok, ["one two", "three"]}
iex> {:ok, theme} = Macaw.Layers.Theme.new([])
iex> Macaw.Layers.Text.font(theme, :label_size)
{:ok, {"sans-serif", 12}}

The measurer is passed in, as metrics:, a module implementing Macaw.Layers.Text.Metrics; without one it is Macaw.Layers.Text.Estimate. Nothing is configured: a library reads no application environment (macaw_compose spec/00 §4.4, rule 3), so the caller that lays a text out says what measures it.

Resolve once, then measure. measurer/1 judges the options and the font once and answers a Macaw.Layers.Text.Measurer; width/3 and lines/4 on it cost the measurer's own work and a constant, so a caller measuring many texts resolves first. width/4 and wrap/5 are the conveniences, defined through it, with the same answers.

iex> {:ok, measurer} = Macaw.Layers.Text.measurer(font: "sans-serif")
iex> Macaw.Layers.Text.width(measurer, "Wednesday", 11)
{:ok, 59.4}

A theme names the font measured. font/2 is a theme's font_family with one of its size slots (font_size, label_size, title_size, spec/04 §2.1), the font and size a text of that kind is set in, which width/4 and wrap/5 take as they are.

Errors are values (spec/00 §3): every function answers {:ok, value} or {:error, faults}, every fault at once by path, and format/1 words them. An argument of the wrong type is {:type, type} at its name ([:text], [:font], [:size], [:max_width], [:theme]), a first argument of lines/4 that is no resolved measurer {:type, :measurer} at [:measurer], and a missing font: of measurer/1 :required at [:font]; options that are no keyword list {:type, :keyword} at [:opts], an option of no such name :unknown_key at it, and a metrics: that is no measurer {:type, :metrics} at [:metrics]. A measurer that answers no width (a number) or no lines (a list of strings) is {:bad_answer, :width | :lines} at [:metrics].

Summary

Types

A refusal: every fault with its path.

metrics:, the measurer: a module implementing Macaw.Layers.Text.Metrics.

Functions

Macaw.Layers.Text.Estimate, the measurer used without metrics:.

The font a theme sets a text of one size slot in: {:ok, {font_family, size}}. The slot is font_size, label_size or title_size, its atom or its string; any other is {:enum, slots} at [:slot], and a theme that is none {:type, :theme} at [:theme].

A fault of this module as path: words: its own reasons, then layers' (Macaw.Layers.Words), then the general ones as everywhere (Macaw.Base.Fault.format/2).

The lines text wraps to at size pixels within max_width pixels on a resolved measurer (measurer/1), in its font, broken at word boundaries by its lines/4: {:ok, lines}. It costs the measurer's own work, and a check of each line it answers. A first argument that is no resolved measurer is {:type, :measurer} at [:measurer].

A resolved measurer: the measurer and the font its texts are set in, judged once, so that width/3 and lines/4 on it cost the measurer's own work and a constant. {:ok, measurer} or every fault at once.

The width of text at size pixels: {:ok, width}, by the measurer's width/3.

The lines text wraps to set in font at size pixels within max_width pixels, broken at word boundaries by the measurer: {:ok, lines}. The convenience of lines/4, judging its options on each call and then wrapping through the measurer they resolve to, with the same answer.

Types

error()

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

A refusal: every fault with its path.

option()

@type option() :: {:metrics, module()}

metrics:, the measurer: a module implementing Macaw.Layers.Text.Metrics.

Functions

default_metrics()

@spec default_metrics() :: module()

Macaw.Layers.Text.Estimate, the measurer used without metrics:.

font(theme, slot)

@spec font(Macaw.Layers.Theme.t(), atom() | String.t()) ::
  {:ok, {String.t(), number()}} | error()

The font a theme sets a text of one size slot in: {:ok, {font_family, size}}. The slot is font_size, label_size or title_size, its atom or its string; any other is {:enum, slots} at [:slot], and a theme that is none {:type, :theme} at [:theme].

format(fault)

@spec format(Macaw.Base.Fault.t()) :: String.t()

A fault of this module as path: words: its own reasons, then layers' (Macaw.Layers.Words), then the general ones as everywhere (Macaw.Base.Fault.format/2).

iex> Macaw.Layers.Text.format({[:metrics], {:type, :metrics}})
"metrics: must be a text measurer: a module implementing Macaw.Layers.Text.Metrics"

lines(measurer, text, size, max_width)

@spec lines(Macaw.Layers.Text.Measurer.t(), String.t(), number(), number()) ::
  {:ok, [String.t()]} | error()

The lines text wraps to at size pixels within max_width pixels on a resolved measurer (measurer/1), in its font, broken at word boundaries by its lines/4: {:ok, lines}. It costs the measurer's own work, and a check of each line it answers. A first argument that is no resolved measurer is {:type, :measurer} at [:measurer].

measurer(opts)

@spec measurer([{:font, String.t()} | option()]) ::
  {:ok, Macaw.Layers.Text.Measurer.t()} | error()

A resolved measurer: the measurer and the font its texts are set in, judged once, so that width/3 and lines/4 on it cost the measurer's own work and a constant. {:ok, measurer} or every fault at once.

font: is required, a CSS font-family string (a theme's, font/2); metrics: is the measurer, default_metrics/0 without it. Every fault comes here, never at a later call: a missing font is :required at [:font], one that is no string {:type, :string} there, and the options' faults are width/4's.

iex> {:ok, measurer} = Macaw.Layers.Text.measurer(font: "sans-serif")
iex> Macaw.Layers.Text.lines(measurer, "one two three", 10, 50)
{:ok, ["one two", "three"]}
iex> Macaw.Layers.Text.measurer(metrics: :nope)
{:error, [{[:font], :required}, {[:metrics], {:type, :metrics}}]}

width(text, text, size)

@spec width(Macaw.Layers.Text.Measurer.t(), String.t(), number()) ::
  {:ok, number()} | error()
@spec width(String.t(), String.t(), number()) :: {:ok, number()} | error()

The width of text at size pixels: {:ok, width}, by the measurer's width/3.

On a resolved measurer (measurer/1), width(measurer, text, size) is in the measurer's font, and costs the measurer's own work and a constant. Otherwise width(text, font, size, opts \\ []) is the convenience, set in font, judging its options on each call and then measuring through the measurer they resolve to, with the same answer.

iex> Macaw.Layers.Text.width("", "serif", 12)
{:ok, 0.0}
iex> Macaw.Layers.Text.width(:label, "serif", "12")
{:error, [{[:text], {:type, :string}}, {[:size], {:type, :number}}]}

width(text, font, size, opts)

@spec width(String.t(), String.t(), number(), [option()]) :: {:ok, number()} | error()

wrap(text, font, size, max_width, opts \\ [])

@spec wrap(String.t(), String.t(), number(), number(), [option()]) ::
  {:ok, [String.t()]} | error()

The lines text wraps to set in font at size pixels within max_width pixels, broken at word boundaries by the measurer: {:ok, lines}. The convenience of lines/4, judging its options on each call and then wrapping through the measurer they resolve to, with the same answer.