Drafter.Widget.Digits.Font (drafter v0.3.2)

Copy Markdown View Source

The font catalogue for Drafter.Widget.Digits.

Built-in fonts are monospaced and share one repertoire: digits, upper and lower case, and common punctuation. Lower case falls back to the upper-case glyph, and a character the font cannot draw renders as blanks of the font's widest glyph rather than raising.

Built-in fonts

NameCellBuilt from
:block7×5Box-drawing outlines. The default.
:compact5×3The same outlines at half the height.
:tall8×4Half blocks — 1×2 pixels a cell.
:pixel4×4Quadrant blocks — 2×2 pixels a cell.
:braille4×4Braille — 2×4 pixels a cell.

:tall, :pixel and :braille are rasterised at compile time from the 8×16 bitmaps in Drafter.Widget.Digits.Bitmap by Drafter.Widget.Digits.Raster. See the large text guide for how to choose between them.

FIGlet fonts

Drafter.Widget.Digits.Figlet reads .flf files. Register one and it is used like any other font:

{:ok, font} = Drafter.Widget.Digits.Figlet.load("fonts/slant.flf")
Drafter.Widget.Digits.Font.register(:slant, font)
digits("Vellum", font: :slant)

FIGlet fonts are proportional, so width/1 reports the widest glyph rather than a fixed cell. Measure a string with text_width/2.

Examples

iex> Drafter.Widget.Digits.Font.builtin_names()
[:block, :braille, :compact, :pixel, :tall]

iex> Drafter.Widget.Digits.Font.height(:block)
5

iex> Drafter.Widget.Digits.Font.glyph_width(:compact, "7")
5

iex> Drafter.Widget.Digits.Font.text_width(:compact, "42%")
15

iex> Drafter.Widget.Digits.Font.supports?(:block, "a")
true

Summary

Functions

The names of the fonts compiled into the library.

Looks up a font by name, falling back to :block for an unknown name.

The rows of one character, upper-casing when a font has no lower-case form.

Columns one character occupies in a font.

Row height of a font's glyphs.

Every font name available, built in and registered.

Adds a font under name, making it available as digits(text, font: name).

Every registered font, keyed by name.

Every character a font can draw, sorted.

Whether a font can draw a character in some form.

Columns a whole string occupies in a font.

Removes a registered font. Built-in fonts are unaffected.

Column width of the widest glyph in a font.

Types

font()

@type font() :: %{
  height: pos_integer(),
  width: pos_integer(),
  glyphs: %{required(String.t()) => [String.t()]}
}

name()

@type name() :: :block | :compact | :braille | :pixel | :tall | atom()

Functions

builtin_names()

@spec builtin_names() :: [name()]

The names of the fonts compiled into the library.

get(font)

@spec get(name() | font() | term()) :: font()

Looks up a font by name, falling back to :block for an unknown name.

A font map is returned as given, so one loaded by Drafter.Widget.Digits.Figlet.load/1 may be passed straight through without registering it.

glyph(name, character)

@spec glyph(name() | font(), String.t()) :: [String.t()]

The rows of one character, upper-casing when a font has no lower-case form.

A character the font cannot draw yields blanks of the font's widest glyph.

glyph_width(name, character)

@spec glyph_width(name() | font(), String.t()) :: non_neg_integer()

Columns one character occupies in a font.

height(name)

@spec height(name() | font()) :: pos_integer()

Row height of a font's glyphs.

names()

@spec names() :: [name()]

Every font name available, built in and registered.

register(name, font)

@spec register(atom(), font()) :: :ok

Adds a font under name, making it available as digits(text, font: name).

Registered fonts live for the life of the node. A registered name takes precedence over a built-in one of the same name.

registered()

@spec registered() :: %{required(atom()) => font()}

Every registered font, keyed by name.

repertoire(name)

@spec repertoire(name() | font()) :: [String.t()]

Every character a font can draw, sorted.

supports?(name, character)

@spec supports?(name() | font(), String.t()) :: boolean()

Whether a font can draw a character in some form.

text_width(name, text)

@spec text_width(name() | font(), String.t()) :: non_neg_integer()

Columns a whole string occupies in a font.

unregister(name)

@spec unregister(atom()) :: :ok

Removes a registered font. Built-in fonts are unaffected.

width(name)

@spec width(name() | font()) :: pos_integer()

Column width of the widest glyph in a font.

FIGlet fonts are proportional; use glyph_width/2 or text_width/2 to measure what will actually be drawn.