Package 'AddFonts'

Title: Download and Register Fonts from GDPR-Compliant Providers For Use In R graphics
Description: Download and register fonts from GDPR-compliant providers for use in R graphics. Currently supports Bunny Fonts (<https://fonts.bunny.net/>/). Architecture allows for easy additions of font providers.
Authors: Guillaume Noblet [aut, cre, cph]
Maintainer: Guillaume Noblet <[email protected]>
License: GPL (>= 3)
Version: 0.2.0
Built: 2026-05-13 09:05:07 UTC
Source: https://codeberg.org/gnoblet/AddFonts

Help Index


Add a font to the local cache and register it for use

Description

Ensure a font is available locally: try the cache first, otherwise download/convert and register the font so it can be used by plotting devices. Returns (invisibly) the list of local file paths.

Usage

add_font(
  name,
  provider = "bunny",
  family = NULL,
  regular.wt = 400,
  bold.wt = 700,
  subset = "latin"
)

Arguments

name

(character(1)) Name of the font as known to the provider.

provider

(character(1)) Provider id/name (default: "bunny").

family

(character | NULL) Optional family name to register the font under (default: NULL).

regular.wt

(numeric(1)) Regular weight to request (default: 400).

bold.wt

(numeric(1)) Bold weight to request (default: 700).

subset

(character(1)) Glyph subset to request (default: "latin").

Value

(list) Invisibly returns a list with paths for regular, italic, bold and bolditalic variants, or throws an error on failure.


Read from list

Description

Read from list

Usage

as_CacheEntryList(l)

Arguments

l

(list) The list to convert to a CacheEntryList.

Value

(CacheEntryList) The CacheEntryList object created from the list.


Construct a FontProvider from a named list

Description

Construct a FontProvider from a named list

Usage

as_FontProvider(x)

Arguments

x

(list) Named list (e.g. from JSON) with provider details.

Value

(FontProvider) The corresponding FontProvider object.


As list

Description

As list

Usage

as_list(x)

Arguments

x

(CacheMeta | CacheEntry | CacheEntryList) The object to convert to a list.

Value

(list) The list representation of the CacheEntryList.


Clean cache entries

Description

Remove entries from the cache, optionally unlinking referenced files.

Usage

cache_clean(cache_dir = NULL, families = NULL, reset = FALSE, ...)

Arguments

cache_dir

(NULL | character(1)) Cache directory to use (default: NULL)

families

(character | NULL) Character vector of family names to remove, or NULL to clear the whole cache (default: NULL)

reset

(logical(1)) If TRUE, completely reset and clear the cache (default: FALSE).

...

(anyD) Additional arguments (currently unused).

Value

(character | NULL) Invisibly returns character vector of removed family names when deleting specific entries, or NULL when nothing changed. Remove files by default.

See Also

Other cache: cache_get(), cache_get_weights(), cache_read(), cache_remove(), cache_write()


Get certain families from CacheEntryList

Description

Get certain families from CacheEntryList

Usage

cache_get(x, families = NULL, quiet = TRUE)

Arguments

x

(CacheEntryList) The CacheEntryList object to query.

families

(⁠character vector⁠) The family names to retrieve.

quiet

(logical(1)) If TRUE, suppress informational messages (default: TRUE).

Value

(list) A list of CacheEntry objects matching the specified families. If no families

See Also

Other cache: cache_clean(), cache_get_weights(), cache_read(), cache_remove(), cache_write()


Check which weights are available in a cache entry

Description

Check which weights are available in a cache entry

Usage

cache_get_weights(entry, weights)

Arguments

entry

(CacheEntry) The CacheEntry object to check.

weights

(numeric) Vector of weights to check for availability.

Value

(lgl) Logical vector indicating which weights are cached).

See Also

Other cache: cache_clean(), cache_get(), cache_read(), cache_remove(), cache_write()


Read cache entry from disk

Description

Read cache entry from disk

Usage

cache_read(cache_dir)

Arguments

cache_dir

(character | NULL) Cache directory to use. Use get_cache_dir() to get the default cache directory.

Value

(CacheEntryList) The cache index as a if found and valid.

See Also

Other cache: cache_clean(), cache_get(), cache_get_weights(), cache_remove(), cache_write()


Delete entry from cache

Description

Delete entry from cache

Usage

cache_remove(x, families = NULL, remove_files = TRUE, cache_dir = NULL)

Arguments

x

(CacheEntryList) The CacheEntryList object to modify.

families

(character | NULL) The font families to delete. If NULL, all entries are deleted.

remove_files

(logical(1)) If TRUE attempt to delete files referenced by removed entries (default: TRUE).

cache_dir

(character(1) | NULL) The cache directory to delete from. If NULL, the default cache directory is used.

Value

(CacheEntryList) The modified CacheEntryList with the specified entries removed.

See Also

Other cache: cache_clean(), cache_get(), cache_get_weights(), cache_read(), cache_write()


Set cache entries

Description

Set cache entries

Usage

cache_set(x, family, meta)

Arguments

x

(CacheEntryList) The CacheEntryList object to modify.

family

(character(1)) The font family to modify.

meta

(CacheMeta) The new metadata to set.

Value

(CacheEntryList) The modified CacheEntryList with the updated entry.


Compose canonical filename for a cached TTF

Description

Compose canonical filename for a cached TTF

Usage

cache_ttf_filename(source, font_id, subset, weight, style)

Arguments

source

(character(1)) Provider source identifier.

font_id

(character(1)) Font id used to create a filesystem-safe filename.

subset

(character(1)) Glyph subset identifier.

weight

(integer(1)) Font weight.

style

(character(1)) Style string (e.g. "normal", "italic").

Value

(character(1)) Filename (not including the cache directory) for the cached TTF.


Compute canonical cache path for a TTF file

Description

Compute canonical cache path for a TTF file

Usage

cache_ttf_path(source, font_id, subset, weight, style, cache_dir = NULL)

Arguments

source

(character(1)) Provider source identifier.

font_id

(character(1)) Font id used for filenames.

subset

(character(1)) Glyph subset identifier.

weight

(integer(1)) Font weight.

style

(character(1)) Style string (e.g. "normal", "italic").

cache_dir

(character | NULL) Cache directory to use (default: NULL)

Value

(character(1)) Path to the cached .ttf file.


Compute paths used for caching provider artifacts and any conversion intermediate files.

Description

Compute paths used for caching provider artifacts and any conversion intermediate files.

Usage

cache_variant_paths(provider, family, weight, style, subset, cache_dir = NULL)

Arguments

provider

(FontProvider) Provider object with source and optional conversion info.

family

(character(1)) Family identifier.

weight

(integer(1)) Font weight.

style

(character(1)) Style string (e.g. "normal", "italic").

subset

(character(1)) Glyph subset identifier.

cache_dir

(character | NULL) Cache directory to use (default: NULL)

Value

(list) A list with elements to_convert (path or NULL) and ttf (path).


Write CacheEntryList to disk as JSON

Description

Write CacheEntryList to disk as JSON

Usage

cache_write(x, cache_dir = NULL, quiet = TRUE)

Arguments

x

(CacheEntryList) The CacheEntryList object to write to disk.

cache_dir

(character(1)) The cache directory to write to (default: NULL).

quiet

(logical(1)) Whether to suppress output messages (default: TRUE).

Value

(NULL) Invisibly returns NULL.

See Also

Other cache: cache_clean(), cache_get(), cache_get_weights(), cache_read(), cache_remove()


S7-backed cache entry (CacheEntry)

Description

S7-backed cache entry (CacheEntry)

Usage

CacheEntry(family = character(0), meta = CacheMeta())

Arguments

family

(character(1)) Family name for this cache entry (safe identifier containing only letters, digits, and hyphens).

meta

(CacheMeta) A CacheMeta object describing the cached files and origin for this family.

Value

(S7_object) A validated S7 CacheEntry object.


S7 list of cache entries (CacheEntryList)

Description

S7 list of cache entries (CacheEntryList)

Usage

CacheEntryList(entries = list())

Arguments

entries

(⁠list(1+)⁠) A non-empty list of CacheEntry objects.

Value

(S7_object) A validated S7 CacheEntryList object.


S7-backed cache metadata (CacheMeta)

Description

S7-backed cache metadata (CacheMeta)

Usage

CacheMeta(family_id = character(0), source = character(0), files = list())

Arguments

family_id

(character(1)) A non-empty safe identifier for the font family. Allowed characters: letters, digits and hyphen (no spaces, slashes, dots, colons, tildes, etc.).

source

(character(1)) Name of the provider or source that produced the cached font files.

files

(⁠list(1+)⁠) A non-empty named list of file paths. Names are weight identifiers (e.g., "400" for normal weight 400, "400italic" for italic weight 400). Each element is a character(1) path to the local font files.

Details

The added property is a getter-only field that returns the current timestamp as a character string when accessed. It cannot be set during construction.

Weights are not explicitly stored - they are derived from the names of the files list. Registration functions are responsible for selecting appropriate weights for regular/bold variants.

Value

(S7_object) A validated S7 CacheMeta object.


Resolve a conversion name to the converter function used by the package (currently only woff2_to_ttf).

Description

Resolve a conversion name to the converter function used by the package (currently only woff2_to_ttf).

Usage

conv_fun(conversion)

Arguments

conversion

(character(1)) Name of the conversion to resolve.

Value

(function) The conversion function if known; otherwise the helper aborts.


Delete files

Description

Attempt to delete a set of files (character vector or list of paths).

Usage

delete_files(entries, quiet = c("full", "success", "fail", "none"))

Arguments

entries

(character) Character vector of file paths to remove.

quiet

(full" | "success" | "fail" | "none) If "full", capture all output and suppress console messages; if "success", only show success messages; if "fail", only show error messages; if "none", show all messages (default: "none").

Value

(list) A list with the following elements:

  • deleted: character() — paths successfully deleted

  • failed: character() — paths that existed but could not be deleted

  • not_found: character() — paths that were not found on disk


Download font variants and add to cache

Description

Downloads font files for requested weights, creates a cache entry, and writes to cache. Does NOT register the font - caller should use register_from_cache() for that.

Usage

download_and_cache(
  provider,
  name,
  font_id,
  family_name,
  regular.wt = 400,
  bold.wt = 700,
  subset = "latin",
  cache_dir = NULL
)

Arguments

provider

(FontProvider) Provider object used for downloads.

name

(character(1)) Font name at the provider.

font_id

(character(1)) Filesystem-safe font id.

family_name

(character(1)) Family name for the font.

regular.wt

(integer(1)) Regular weight to fetch (default: 400)

bold.wt

(integer(1)) Bold weight to fetch (default: 700)

subset

(character(1)) Glyph subset to request (default: "latin")

cache_dir

(character | NULL) Cache directory to use (default: NULL)

Value

(CacheEntry | NULL) Cache entry with downloaded fonts, or NULL on failure.


Download and (if needed) convert a provider artifact to a local TTF file for a given family/weight/style and return the local path.

Description

Download and (if needed) convert a provider artifact to a local TTF file for a given family/weight/style and return the local path.

Usage

download_variant_generic(
  provider,
  family,
  weight,
  style,
  subset = "latin",
  cache_dir = NULL,
  quiet = FALSE
)

Arguments

provider

(FontProvider) Provider object with url_template and source.

family

(character(1)) Family identifier.

weight

(integer(1)) Font weight to fetch (100-900).

style

(character(1)) Style (e.g. "normal", "italic").

subset

(character(1)) Glyph subset to request (default: "latin")

cache_dir

(character | NULL) Cache directory to use (default: NULL)

quiet

(logical(1)) Suppress warnings/messages (default: FALSE)

Value

(character | NULL) Path to the local .ttf file on success, or NULL on failure.


Download font files for specified weights

Description

Downloads normal and italic variants for each weight and returns a named list with weight-based keys.

Usage

download_weights(provider, name, weights, subset, cache_dir, quiet = TRUE)

Arguments

provider

(FontProvider) Provider object used for downloads.

name

(character(1)) Font name at the provider.

weights

(numeric) Vector of weights to download.

subset

(character(1)) Glyph subset to request.

cache_dir

(character(1)) Cache directory to use.

quiet

(logical(1)) Whether to suppress download messages (default: TRUE).

Value

(list) Named list where names are weight identifiers (e.g., "400", "700italic") and values are file paths.


Font provider specification (FontProvider)

Description

Font provider specification (FontProvider)

Usage

FontProvider(
  source = character(0),
  url_template = character(0),
  conversion = NULL,
  conversion_ext = NULL,
  aliases = list()
)

Arguments

source

(character(1)) Provider id/name (e.g. "bunny").

url_template

(character(1)) URL template used to construct download URLs.

conversion

(character(1) | NULL) Optional conversion function name (as string) or NULL.

conversion_ext

(character(1) | NULL) Original extension handled by the provider (e.g. "woff2").

aliases

(list | NULL) Optional list of alias names to match (e.g. "fonts.bunny.net").

Value

(FontProviders) S7 class representing a font provider specification.


Get the package cache directory for fonts

Description

Determine a platform-appropriate cache directory for AddFonts and ensure it exists, creating it when necessary.

Usage

get_cache_dir()

Value

(character(1)) Absolute path to the cache directory.


Get provider details from internal data

Description

Load and return a FontProvider object for the specified provider. The providers data is stored in the package's internal sysdata.rda.

Usage

get_provider_details(provider)

Arguments

provider

(character(1)) Provider id/name (e.g. "bunny").

Value

(FontProvider) A validated FontProvider object.


Preview a font by ensuring it's installed and drawing a sample string

Description

Ensure the requested font is installed via add_font() and draw a brief sample using showtext for proper font rendering.

Usage

preview_font(
  name,
  provider = "bunny",
  family = NULL,
  sample = "The quick brown fox jumps over the lazy dog",
  size = 28,
  subset = "latin",
  regular.wt = 400,
  bold.wt = 700
)

Arguments

name

(character(1)) Font name as used by the provider (e.g. "oswald").

provider

(character(1)) Provider name to use (default: "bunny")

family

(character | NULL) Optional family name to register the font under (default: NULL)

sample

(character(1)) Sample text to display (default: "The quick brown fox jumps over the lazy dog")

size

(numeric(1)) Font size in points for the preview (default: 28)

subset

(character(1)) Glyph subset to request (default: "latin")

regular.wt

(integer(1)) Regular weight to display (default: 400)

bold.wt

(integer(1)) Bold weight to display (default: 700)

Value

(list) Invisibly returns the list of paths produced by add_font().


Validate a cache entry and register the font with sysfonts if the required files exist. Returns the prepared files list or NULL when registration cannot proceed.

Description

This is the ONLY function that calls sysfonts::font_add(). It does not print success messages - callers should handle user feedback.

Usage

register_from_cache(entry, regular.wt = 400, bold.wt = 700)

Arguments

entry

(CacheEntry) Cache entry object with family and metadata.

regular.wt

(numeric(1)) Regular weight to use for regular and italic variants (default: 400).

bold.wt

(numeric(1)) Bold weight to use for bold and bolditalic variants (default: 700).

Value

(list | NULL) Prepared files list (with regular, italic, bold, bolditalic) or NULL.


Download necessary variants for a font, write a cache entry and register the font with sysfonts. Returns the prepared files list on success, or NULL if a regular font could not be obtained.

Description

This function downloads fonts, creates a cache entry, and then calls register_from_cache() to perform the actual registration with sysfonts.

Usage

register_from_download(
  provider,
  name,
  font_id,
  family_name,
  regular.wt = 400,
  bold.wt = 700,
  subset = "latin",
  cache_dir = NULL
)

Arguments

provider

(FontProvider) Provider object used for downloads.

name

(character(1)) Font name at the provider.

font_id

(character(1)) Filesystem-safe font id.

family_name

(character(1)) Family name to register the font under.

regular.wt

(integer(1)) Regular weight to fetch (default: 400)

bold.wt

(integer(1)) Bold weight to fetch (default: 700)

subset

(character(1)) Glyph subset to request (default: "latin")

cache_dir

(character | NULL) Cache directory to use (default: NULL)

Value

(list | NULL) List of local file paths for variants, or NULL on failure.


Create a filesystem-safe id from a name

Description

Replace disallowed characters with - and convert to lower-case so the resulting id is safe to use in filenames.

Usage

safe_id(name)

Arguments

name

(character(1)) Input name to sanitise.

Value

(character(1)) Sanitised identifier.


Download missing weights and update an existing cache entry

Description

Downloads missing font weights and updates the cache entry. Does NOT register the font - use register_from_cache() after this.

Usage

update_download_and_cache(
  entry,
  provider,
  name,
  family_name,
  missing_weights,
  subset = "latin",
  cache_dir = NULL,
  cel = NULL
)

Arguments

entry

(CacheEntry) Existing cache entry to update.

provider

(FontProvider) Provider object used for downloads.

name

(character(1)) Font name at the provider.

family_name

(character(1)) Family name for the font.

missing_weights

(numeric) Vector of weights to download and add to cache.

subset

(character(1)) Glyph subset to request (default: "latin").

cache_dir

(character | NULL) Cache directory to use.

cel

(CacheEntryList) Current cache entry list to update.

Value

(CacheEntry | NULL) Updated cache entry with new weights (NOT registered), or NULL on failure.


Convert a .woff2 font to .ttf using the system 'woff2_decompress' tool

Description

Internal helper. Uses the system woff2_decompress tool to convert a .woff2 file into a .ttf file. Documented with roxytypes annotations for clarity.

Usage

woff2_to_ttf(font_file, overwrite = FALSE, remove_old = TRUE, quiet = "fail")

Arguments

font_file

(character(1)) Path to the .woff2 file to convert.

overwrite

(logical(1)) If TRUE, overwrite an existing .ttf conversion.

remove_old

(logical(1)) If TRUE, remove the original .woff2 file after conversion try.

quiet

(full" | "success" | "fail" | "none) If "full", capture all output and suppress console messages; if "success", only show success messages; if "fail", only show error messages; if "none", show all messages (default: "none").

Value

(character(1)) Invisibly returns the path to the .ttf file on success.