Macaw.Draw.Render.Fonts (macaw_draw v0.1.0)

Copy Markdown View Source

The raster target's font configuration and the bundled faces (spec/10 §9.6): a raster never depends on the host's fonts. New here: visualize's options of Visualize.Render.to_png/2 (:font_dirs, :system_fonts, :generic_families), made one value whose default is the bundled faces with the system's fonts off.

%Macaw.Draw.Render.Fonts{dirs, system, families}:

FieldDefaultMeaning
dirs[bundled_dir()]font directories resvg loads, each existing, expanded, passed once
systemfalsewhether the system's fonts are loaded too
familiesevery CSS generic → "DejaVu Sans"the family each of serif, sans_serif, monospace, cursive and fantasy names

The bundled faces are priv/fonts/DejaVuSans.ttf and DejaVuSans-Bold.ttf, DejaVu Sans regular and bold, under the Bitstream Vera license with DejaVu's changes in the public domain (priv/fonts/LICENSE); no serif, monospace or italic face is bundled, so every generic maps to DejaVu Sans until a design asks for another. A host adds directories and maps generics to families it ships with new/1, or turns the system on. Nothing is cached: resvg builds its font database per call from the flags, so a font added to a directory is seen by the next render.

iex> fonts = Macaw.Draw.Render.Fonts.default()
iex> {fonts.system, fonts.families[:sans_serif], fonts.dirs == [Macaw.Draw.Render.Fonts.bundled_dir()]}
{false, "DejaVu Sans", true}

Summary

Types

A refusal: faults with their paths.

t()

Functions

The directory of the bundled faces, priv/fonts of the package.

The default configuration: the bundled faces alone, every generic DejaVu Sans.

The faults of a configuration a render would pass resvg: a directory that does not exist, a family that is not a string, a field of the wrong type; [] for one that can be used.

The resvg flags of a configuration: each directory as --use-fonts-dir, the system's fonts skipped unless on, each generic's family by its flag.

A configuration over the default: :dirs, directories added after the bundled one; :families, a keyword list or map of generics to the families they name; :system, whether the system's fonts load too; bundled: false leaves the bundled directory out. A value of the wrong type is a fault at its key; a directory's existence is checked where it is used, by the raster, when the render runs.

Types

error()

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

A refusal: faults with their paths.

generic()

@type generic() :: :serif | :sans_serif | :monospace | :cursive | :fantasy

t()

@type t() :: %Macaw.Draw.Render.Fonts{
  dirs: [String.t()],
  families: %{required(generic()) => String.t()},
  system: boolean()
}

Functions

bundled_dir()

@spec bundled_dir() :: String.t()

The directory of the bundled faces, priv/fonts of the package.

default()

@spec default() :: t()

The default configuration: the bundled faces alone, every generic DejaVu Sans.

faults(fonts)

@spec faults(t()) :: [Macaw.Base.Fault.t()]

The faults of a configuration a render would pass resvg: a directory that does not exist, a family that is not a string, a field of the wrong type; [] for one that can be used.

flags(fonts)

@spec flags(t()) :: [String.t()] | error()

The resvg flags of a configuration: each directory as --use-fonts-dir, the system's fonts skipped unless on, each generic's family by its flag.

iex> dir = Macaw.Draw.Render.Fonts.bundled_dir()
iex> fonts = %Macaw.Draw.Render.Fonts{dirs: [dir], system: false, families: %{serif: "DejaVu Sans"}}
iex> Macaw.Draw.Render.Fonts.flags(fonts) == ["--use-fonts-dir", dir, "--skip-system-fonts", "--serif-family", "DejaVu Sans"]
true

new(opts \\ [])

@spec new(keyword()) :: {:ok, t()} | error()

A configuration over the default: :dirs, directories added after the bundled one; :families, a keyword list or map of generics to the families they name; :system, whether the system's fonts load too; bundled: false leaves the bundled directory out. A value of the wrong type is a fault at its key; a directory's existence is checked where it is used, by the raster, when the render runs.

iex> {:ok, fonts} = Macaw.Draw.Render.Fonts.new(dirs: ["/srv/fonts"], families: [sans_serif: "Inter"])
iex> {fonts.families.sans_serif, fonts.families.serif, List.last(fonts.dirs)}
{"Inter", "DejaVu Sans", "/srv/fonts"}