A text as one string with holes (spec/02 §7.4): "Uptime for #{var(host)}".
A :text (a label's text, a legend's title, meta.name) is a string in which
#{var(name)} stands for a variable, #{field(name)} for a datum's field (inline
labels and tooltips only) and #{fact(slot, field, key)} for a scalar fact of a slot's
schema. A literal #{ is written \#{. parse/1 reads the string into its parts and
unparse/1 writes parts back; the two are inverse. A hole's names are words
([A-Za-z_][A-Za-z0-9_]*), and a fact's key is looked up among the fact keys, never
made an atom.
iex> Macaw.Frame.Text.parse("Uptime for \#{var(host)} over 24h")
{:ok, ["Uptime for ", %Macaw.Layers.Var{name: "host"}, " over 24h"]}
iex> Macaw.Frame.Text.parse("cost \\\#{literal}")
{:ok, ["cost \#{literal}"]}
iex> Macaw.Frame.Text.unparse(["u: ", %Macaw.Layers.Var{name: "unit"}])
"u: \#{var(unit)}"
iex> Macaw.Frame.Text.parse("open \#{var(x")
{:error, :unterminated}
Summary
Functions
A resolved measurer set in family instead, the font a text's style names: the same
measurer when family is its font or no string, else its measurer resolved again in
family, once per laid-out element, never per text.
Parts as lines, split at every newline of a string part; [[]] for no parts. A string
is parsed first; one that does not parse is a single line of itself.
The resolved measurer a text laid out over source measures on: a realized
Macaw.Frame.Coordinates.Model's measurer, a context map's or keyword's :measurer,
or the measurer itself; without one (a model realized outside a Frame), the estimate in
the font theme sets texts in.
The Frame's measurer, resolved once when the Frame compiles (spec/01 §2.4): metrics
(a module implementing Macaw.Layers.Text.Metrics, nil the estimate) judged with the
font theme sets texts in (its font_family, "sans-serif" without a theme), by
Macaw.Layers.Text.measurer/1. {:ok, measurer} or its faults, {:type, :metrics} at
[:metrics] for a module that is no measurer.
The parts of a text: {:ok, parts}, empty strings dropped; {:error, :unterminated} for
a #{ never closed, {:error, {:hole, text}} for a hole naming none of the three. A
list is parts already and is answered as it is; any other term is
{:error, {:type, :text}}.
Whether a string carries a hole.
The string form of parts: a variable as #{var(name)}, a field as #{field(name)}, a
fact as #{fact(slot, field, key)}, a literal #{ escaped; a resolved number or atom
with to_string/1. A string is answered as it is.
The estimated width of a text at a font size: Macaw.Draw.Text.width/2, the estimate a
Frame measures by without a measurer, which layout and the raster target share (macaw_draw
spec/07 §20).
The width of a text at a font size on the Frame's resolved measurer, in its font:
Macaw.Draw.Text.width/3, the measurer measure/2 answers (macaw_layers'
Macaw.Layers.Text.Measurer, the estimate by default).
Types
@type part() :: String.t() | Macaw.Layers.Var.t() | {:field, String.t()} | Macaw.Frame.Fact.t()
One part of a text: a string, a variable, a datum's field, or a schema's fact.
Functions
@spec in_font(Macaw.Layers.Text.Measurer.t(), term()) :: Macaw.Layers.Text.Measurer.t()
A resolved measurer set in family instead, the font a text's style names: the same
measurer when family is its font or no string, else its measurer resolved again in
family, once per laid-out element, never per text.
iex> Macaw.Frame.Text.in_font(Macaw.Draw.Text.estimate(), "serif").font
"serif"
Parts as lines, split at every newline of a string part; [[]] for no parts. A string
is parsed first; one that does not parse is a single line of itself.
iex> Macaw.Frame.Text.lines(["Uptime\n24 h ", %Macaw.Layers.Var{name: "unit"}])
[["Uptime"], ["24 h ", %Macaw.Layers.Var{name: "unit"}]]
iex> Macaw.Frame.Text.lines([])
[[]]
@spec measure(term(), term()) :: Macaw.Layers.Text.Measurer.t()
The resolved measurer a text laid out over source measures on: a realized
Macaw.Frame.Coordinates.Model's measurer, a context map's or keyword's :measurer,
or the measurer itself; without one (a model realized outside a Frame), the estimate in
the font theme sets texts in.
iex> Macaw.Frame.Text.measure(nil, Macaw.Layers.Theme.default()) == Macaw.Draw.Text.estimate()
true
@spec measurer(term(), term()) :: {:ok, Macaw.Layers.Text.Measurer.t()} | {:error, [Macaw.Base.Fault.t(), ...]}
The Frame's measurer, resolved once when the Frame compiles (spec/01 §2.4): metrics
(a module implementing Macaw.Layers.Text.Metrics, nil the estimate) judged with the
font theme sets texts in (its font_family, "sans-serif" without a theme), by
Macaw.Layers.Text.measurer/1. {:ok, measurer} or its faults, {:type, :metrics} at
[:metrics] for a module that is no measurer.
iex> {:ok, measurer} = Macaw.Frame.Text.measurer(nil, Macaw.Layers.Theme.default())
iex> Macaw.Frame.Text.width("Wednesday", 11, measurer)
59.4
iex> Macaw.Frame.Text.measurer(:nope, nil)
{:error, [{[:metrics], {:type, :metrics}}]}
The parts of a text: {:ok, parts}, empty strings dropped; {:error, :unterminated} for
a #{ never closed, {:error, {:hole, text}} for a hole naming none of the three. A
list is parts already and is answered as it is; any other term is
{:error, {:type, :text}}.
Whether a string carries a hole.
iex> Macaw.Frame.Text.template?("a \#{var(x)}")
true
iex> Macaw.Frame.Text.template?(:title)
false
The string form of parts: a variable as #{var(name)}, a field as #{field(name)}, a
fact as #{fact(slot, field, key)}, a literal #{ escaped; a resolved number or atom
with to_string/1. A string is answered as it is.
@spec width(term(), term()) :: float() | {:error, [Macaw.Base.Fault.t(), ...]}
The estimated width of a text at a font size: Macaw.Draw.Text.width/2, the estimate a
Frame measures by without a measurer, which layout and the raster target share (macaw_draw
spec/07 §20).
iex> Macaw.Frame.Text.width("Wednesday", 11)
59.4
@spec width(term(), term(), term()) :: number() | {:error, [Macaw.Base.Fault.t(), ...]}
The width of a text at a font size on the Frame's resolved measurer, in its font:
Macaw.Draw.Text.width/3, the measurer measure/2 answers (macaw_layers'
Macaw.Layers.Text.Measurer, the estimate by default).
iex> Macaw.Frame.Text.width("Wednesday", 11, Macaw.Frame.Text.measure(nil, nil))
59.4