Macaw.Draw.Text (macaw_draw v0.1.0)

Copy Markdown View Source

A text's width, measured through macaw_layers' seam on a resolved measurer, and the estimate's inverse (spec/07 §20; visualize spec/14 §5.5, #489).

iex> Macaw.Draw.Text.width("Wednesday", 11)
59.4
iex> Macaw.Draw.Text.capacity(59.4, 11)
9

The arithmetic is macaw_layers' (macaw_layers#59, #63): every width is Macaw.Layers.Text.width/3 on a resolved measurer (Macaw.Layers.Text.Measurer, a measurer and a font judged once by Macaw.Layers.Text.measurer/1), so there is one measuring path. width/2 measures on estimate/0, Macaw.Layers.Text.Estimate resolved in "sans-serif" when this module compiles, whose width(text, font_size) = 0.6 × font_size × String.length(text) is 0.6 em per grapheme. width/3 measures on the resolved measurer it is given, or resolves one from metrics: and font: first. Nothing is configured: a Frame resolves its measurer once, when it compiles, and passes it down to everything that lays text out (a legend's layout, a mark label's fit, a coordinate model's margins, a diagram's boxes and the raster's layout), so where a label goes is decided once, whatever draws it. capacity/2 is the estimate's inverse, the characters that fit a width, never negative, so a label's fit and the layout can never disagree.

Summary

Types

A refusal: faults with their paths.

metrics: the measurer (Macaw.Layers.Text.Estimate by default, nil the same) and font: the CSS font family measured ("sans-serif").

Functions

The characters that fit width at font_size by the estimate: trunc(width / (0.6 × font_size)), at least 0; a non-positive font size is 0.

0.6, the estimate's advance per character in ems.

The estimate resolved in "sans-serif": the measurer width/2 measures on and a Frame without a measurer of its own measures by.

Whether a resolved measurer measures by the estimate, whose inverse capacity/2 is.

A resolved measurer from opts: metrics: the measurer (the estimate without one, or nil) and font: the font family ("sans-serif" without one, or nil), judged once by Macaw.Layers.Text.measurer/1, {:ok, measurer} or every fault of the options at once ({:type, :metrics} at [:metrics] for a module that is no measurer).

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

The width of text at font_size on measurer, a resolved measurer (measurer/1, estimate/0), in its font: Macaw.Layers.Text.width/3's number, or its fault ({:bad_answer, :width} at [:metrics] for a measurer answering no number). Given options instead, it resolves them with measurer/1 first, answering their faults.

Types

error()

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

A refusal: faults with their paths.

option()

@type option() :: {:metrics, module() | nil} | {:font, String.t() | nil}

metrics: the measurer (Macaw.Layers.Text.Estimate by default, nil the same) and font: the CSS font family measured ("sans-serif").

Functions

capacity(width, font_size)

@spec capacity(number(), number()) :: non_neg_integer() | error()

The characters that fit width at font_size by the estimate: trunc(width / (0.6 × font_size)), at least 0; a non-positive font size is 0.

em()

@spec em() :: float()

0.6, the estimate's advance per character in ems.

estimate()

@spec estimate() :: Macaw.Layers.Text.Measurer.t()

The estimate resolved in "sans-serif": the measurer width/2 measures on and a Frame without a measurer of its own measures by.

estimate?(arg1)

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

Whether a resolved measurer measures by the estimate, whose inverse capacity/2 is.

iex> Macaw.Draw.Text.estimate?(Macaw.Draw.Text.estimate())
true

measurer(opts)

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

A resolved measurer from opts: metrics: the measurer (the estimate without one, or nil) and font: the font family ("sans-serif" without one, or nil), judged once by Macaw.Layers.Text.measurer/1, {:ok, measurer} or every fault of the options at once ({:type, :metrics} at [:metrics] for a module that is no measurer).

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

width(text, font_size)

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

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

width(text, font_size, measurer)

@spec width(String.t(), number(), Macaw.Layers.Text.Measurer.t() | [option()]) ::
  number() | error()

The width of text at font_size on measurer, a resolved measurer (measurer/1, estimate/0), in its font: Macaw.Layers.Text.width/3's number, or its fault ({:bad_answer, :width} at [:metrics] for a measurer answering no number). Given options instead, it resolves them with measurer/1 first, answering their faults.