Macaw.Layers.Text.Estimate (macaw_layers v0.6.0)

Copy Markdown View Source

The estimating measurer (spec/05 §2): 0.6 em per grapheme, whatever the font, from visualize's Visualize.Chart.Text.width/2.

iex> Macaw.Layers.Text.Estimate.width("Wednesday", "sans-serif", 11)
59.4
iex> Macaw.Layers.Text.Estimate.lines("one two three", "sans-serif", 10, 50)
["one two", "three"]

width(text, font, size) = 0.6 × size × String.length(text), the number visualize's estimate and macaw_frame's Macaw.Draw.Text.width/2 give for the text at that size, so a layout moving onto the seam keeps its numbers. No font is read: a wide glyph runs over the estimate. It is Macaw.Layers.Text's default measurer.

lines/4 breaks at word boundaries: each line of the text (a newline always breaks) is its words, separated by whitespace, set greedily, one space apart, as many to a line as fit max_width by width/3. A word wider than max_width stands alone on its line, never cut, and a line holding no word is "".

Both answer a value on any input: an argument of the wrong type is {:type, type} at its name ([:text], [:font], [:size], [:max_width]), every one at once.

Summary

Types

A refusal: every fault with its path.

Functions

0.6, the advance per grapheme in ems.

The lines text wraps to at size pixels within max_width pixels, broken at word boundaries by width/3.

The estimated width of text at size pixels: 0.6 × size × String.length(text).

Types

error()

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

A refusal: every fault with its path.

Functions

em()

@spec em() :: float()

0.6, the advance per grapheme in ems.

lines(text, font, size, max_width)

@spec lines(String.t(), String.t(), number(), number()) :: [String.t()] | error()

The lines text wraps to at size pixels within max_width pixels, broken at word boundaries by width/3.

width(text, font, size)

@spec width(String.t(), String.t(), number()) :: float() | error()

The estimated width of text at size pixels: 0.6 × size × String.length(text).