OpenFresco.Fonts (open_fresco v0.2.0)

Copy Markdown View Source

Custom/brand font loading for measurement and rasterization.

Configure font sources once:

config :open_fresco,
  font_dirs: ["/app/priv/fonts"],
  font_files: ["/app/priv/fonts/Inter-Bold.ttf"],
  skip_system_fonts: false

or per call via the :font_dirs / :font_files / :skip_system_fonts options on OpenFresco.render/3 (per-call options win over config).

How the fonts reach each backend

  • resvg NIF — fonts load into a warm, reusable fontdb built via Resvg.init_fontdb/1 and cached in :persistent_term, so directory scanning is paid once per configuration, not per render. The same fontdb backs text measurement (OpenFresco.Measure), so wrap points are computed with the same brand fonts that paint.
  • resvg CLI — passed as --use-fonts-dir / --use-font-file flags per invocation.
  • rsvg-convert / ImageMagick — no per-call font API; they use the system fontconfig. Install brand fonts system-wide on such hosts, or prefer the NIF/CLI backends.

digest/1 folds the font configuration (paths + mtime + size) into a stable hash for render manifests, so caches key on the actual fonts that produced the pixels.

Summary

Functions

CLI flags for the resvg CLI backend (--use-fonts-dir / --use-font-file).

A stable digest of the font configuration (each file's path, mtime and size) — a render-manifest component, so cached output invalidates when the fonts that produced it change. Returns "system" when no custom fonts are configured (system-font changes are outside our visibility; the manifest documents that honestly).

A warm, reusable resvg fontdb for the given font sources — built once per configuration and cached in :persistent_term (no supervision tree required; safe under concurrency — worst case is one duplicate build).

Configured + per-call font sources, normalized.

Functions

cli_args(opts \\ [])

@spec cli_args(keyword()) :: [String.t()]

CLI flags for the resvg CLI backend (--use-fonts-dir / --use-font-file).

digest(opts \\ [])

@spec digest(keyword()) :: String.t()

A stable digest of the font configuration (each file's path, mtime and size) — a render-manifest component, so cached output invalidates when the fonts that produced it change. Returns "system" when no custom fonts are configured (system-font changes are outside our visibility; the manifest documents that honestly).

fontdb(opts \\ [])

@spec fontdb(keyword()) :: {:ok, reference()} | :none

A warm, reusable resvg fontdb for the given font sources — built once per configuration and cached in :persistent_term (no supervision tree required; safe under concurrency — worst case is one duplicate build).

Returns {:ok, fontdb_ref}, or :none when the resvg NIF is absent, the configuration lists no custom fonts (the system database is the default anyway), or the build fails.

sources(opts \\ [])

@spec sources(keyword()) :: %{
  font_dirs: [String.t()],
  font_files: [String.t()],
  skip_system_fonts: boolean()
}

Configured + per-call font sources, normalized.