| 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. Supports weight-based providers (e.g. Bunny Fonts, <https://fonts.bunny.net/>) and file-based providers (e.g. Bye Bye Binary, <https://gitlab.com/bye-bye-binary>). Architecture allows for easy additions of font providers. |
| Authors: | Guillaume Noblet [aut, cre, cph] |
| Maintainer: | Guillaume Noblet <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 1.0.0 |
| Built: | 2026-07-08 10:44:25 UTC |
| Source: | https://codeberg.org/gnoblet/AddFonts |
Handles the cache-check → download → register cycle when the user supplies provider = "url". Uses source key "url" in the cache index.
.add_font_direct_url(name, family_name, variants, cache_dir).add_font_direct_url(name, family_name, variants, cache_dir)
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(list) Invisibly, a named list of local file paths for all registered variants.
Handles the cache-check → download → register cycle for FontProviderFile providers using symbolic variant keys.
.add_font_file(provider_obj, name, family_name, variants, cache_dir).add_font_file(provider_obj, name, family_name, variants, cache_dir)
provider_obj |
( |
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(list) Invisibly, a named list of local file paths for all registered variants.
Handles the cache-check → copy → register cycle when the user supplies provider = "file". Uses source key "file" in the cache index.
.add_font_local(name, family_name, variants, cache_dir).add_font_local(name, family_name, variants, cache_dir)
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(list) Invisibly, a named list of local file paths for all registered variants.
Handles the full cache-check → optional partial update-download-register cycle for FontProviderWeight providers.
.add_font_weight( provider_obj, name, family_name, regular.wt, bold.wt, subset, cache_dir ).add_font_weight( provider_obj, name, family_name, regular.wt, bold.wt, subset, cache_dir )
provider_obj |
( |
name |
( |
family_name |
( |
regular.wt |
( |
bold.wt |
( |
subset |
( |
cache_dir |
( |
(list) Invisibly, a named list of local file paths for all registered variants.
Issues an HTTP GET for url, writing the response body to local_path. On failure (httr2 error or missing output file) warns (unless quiet) and returns NULL.
.fetch_url_to_cache(url, local_path, family, variant, quiet).fetch_url_to_cache(url, local_path, family, variant, quiet)
url |
( |
local_path |
( |
family |
( |
variant |
( |
quiet |
( |
(character | NULL) local_path on success, or NULL on failure.
Constructs a CacheMeta() and a CacheEntry(), upserts the entry into the on-disk cache index via cache_read_safe(), cache_set(), and cache_write(), then returns the new entry. This is the shared persist tail used by all download/copy orchestrators.
.persist_cache_entry( source, family_name, files_entry, cache_dir, failed_keys = character(0) ).persist_cache_entry( source, family_name, files_entry, cache_dir, failed_keys = character(0) )
source |
( |
family_name |
( |
files_entry |
( |
cache_dir |
( |
failed_keys |
( |
(CacheEntry) The newly created cache entry.
Checks that variants is a non-NULL named list whose names are a subset of the recognised symbolic keys and contains at least "regular". Aborts with an informative error on the first violation found.
.validate_variants(variants).validate_variants(variants)
variants |
( |
(NULL) Returns invisible(NULL) on success; called for its side-effect.
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.
add_font( name, provider = "bunny", family = NULL, variants = NULL, regular.wt = 400, bold.wt = 700, subset = "latin" )add_font( name, provider = "bunny", family = NULL, variants = NULL, regular.wt = 400, bold.wt = 700, subset = "latin" )
name |
( |
provider |
( |
family |
( |
variants |
( |
regular.wt |
( |
bold.wt |
( |
subset |
( |
For weight-based providers (e.g. Bunny Fonts), supply regular.wt,bold.wt, and subset. For file-based providers (e.g. Bye Bye Binary), supply variants instead.
(list) Invisibly returns a list with paths for regular, italic, bold and
bolditalic variants, or throws an error on failure.
Read from list
as_CacheEntryList(l) ## S7 method for class <list> as_CacheEntryList(l)as_CacheEntryList(l) ## S7 method for class <list> as_CacheEntryList(l)
l |
( |
(CacheEntryList) The CacheEntryList object created from the list.
Reads the type field ("weight" or "file") and constructs the appropriate subclass. Missing type defaults to "weight" for backward compatibility with existing providers.json entries.
as_FontProvider(x) ## S7 method for class <list> as_FontProvider(x)as_FontProvider(x) ## S7 method for class <list> as_FontProvider(x)
x |
( |
(FontProviderWeight | FontProviderFile) The corresponding provider object.
As list
as_list(x) ## S7 method for class <AddFonts::CacheMeta> as_list(x) ## S7 method for class <AddFonts::CacheEntry> as_list(x) ## S7 method for class <AddFonts::CacheEntryList> as_list(x)as_list(x) ## S7 method for class <AddFonts::CacheMeta> as_list(x) ## S7 method for class <AddFonts::CacheEntry> as_list(x) ## S7 method for class <AddFonts::CacheEntryList> as_list(x)
x |
( |
(list) The list representation of the CacheEntryList.
Remove entries from the cache, optionally unlinking referenced files.
cache_clean(cache_dir = NULL, families = NULL, reset = FALSE)cache_clean(cache_dir = NULL, families = NULL, reset = FALSE)
cache_dir |
( |
families |
( |
reset |
( |
(character | NULL) Invisibly returns character vector of removed family names when deleting specific entries, or NULL when nothing changed. Remove files by default.
Other cache:
cache_get(),
cache_get_variants(),
cache_get_weights(),
cache_read(),
cache_remove(),
cache_write()
Used by file-based providers (e.g. Bye Bye Binary) where each variant is
identified by a symbolic key ("regular", "italic", "bold",
"bolditalic") rather than a numeric weight.
cache_file_path(source, family, variant, file_ext, cache_dir = NULL)cache_file_path(source, family, variant, file_ext, cache_dir = NULL)
source |
( |
family |
( |
variant |
( |
file_ext |
( |
cache_dir |
( |
(character(1)) Full path to the locally cached font file.
Get certain families from CacheEntryList
cache_get(x, families = NULL, source = NULL, quiet = TRUE) ## S7 method for class <AddFonts::CacheEntryList> cache_get(x, families = NULL, source = NULL, quiet = TRUE)cache_get(x, families = NULL, source = NULL, quiet = TRUE) ## S7 method for class <AddFonts::CacheEntryList> cache_get(x, families = NULL, source = NULL, quiet = TRUE)
x |
( |
families |
( |
source |
( |
quiet |
( |
(list) A list of CacheEntry objects matching the specified families, or NULL.
Other cache:
cache_clean(),
cache_get_variants(),
cache_get_weights(),
cache_read(),
cache_remove(),
cache_write()
Used for file-based providers whose CacheMeta@files uses the key "regular", "italic", "bold", "bolditalic" instead of numeric weight strings.
cache_get_variants(entry, variants) ## S7 method for class <AddFonts::CacheEntry> cache_get_variants(entry, variants)cache_get_variants(entry, variants) ## S7 method for class <AddFonts::CacheEntry> cache_get_variants(entry, variants)
entry |
( |
variants |
( |
(lgl) Named logical vector indicating which variants are cached.
Other cache:
cache_clean(),
cache_get(),
cache_get_weights(),
cache_read(),
cache_remove(),
cache_write()
Check which weights are available in a cache entry
cache_get_weights(entry, weights) ## S7 method for class <AddFonts::CacheEntry> cache_get_weights(entry, weights)cache_get_weights(entry, weights) ## S7 method for class <AddFonts::CacheEntry> cache_get_weights(entry, weights)
entry |
( |
weights |
( |
(lgl) Logical vector indicating which weights are cached).
Other cache:
cache_clean(),
cache_get(),
cache_get_variants(),
cache_read(),
cache_remove(),
cache_write()
Read cache entry from disk
cache_read(cache_dir) ## S7 method for class <character> cache_read(cache_dir)cache_read(cache_dir) ## S7 method for class <character> cache_read(cache_dir)
cache_dir |
( |
(CacheEntryList) The cache index as a CacheEntryList if found and valid.
Other cache:
cache_clean(),
cache_get(),
cache_get_variants(),
cache_get_weights(),
cache_remove(),
cache_write()
Delete entry from cache
cache_remove( x, families = NULL, source = NULL, remove_files = TRUE, cache_dir = NULL ) ## S7 method for class <AddFonts::CacheEntryList> cache_remove( x, families = NULL, source = NULL, remove_files = TRUE, cache_dir = NULL )cache_remove( x, families = NULL, source = NULL, remove_files = TRUE, cache_dir = NULL ) ## S7 method for class <AddFonts::CacheEntryList> cache_remove( x, families = NULL, source = NULL, remove_files = TRUE, cache_dir = NULL )
x |
( |
families |
( |
source |
( |
remove_files |
( |
cache_dir |
( |
(CacheEntryList) The modified CacheEntryList with the specified entries removed.
Other cache:
cache_clean(),
cache_get(),
cache_get_variants(),
cache_get_weights(),
cache_read(),
cache_write()
Set cache entries
cache_set(x, family, meta) ## S7 method for class <AddFonts::CacheEntryList> cache_set(x, family, meta)cache_set(x, family, meta) ## S7 method for class <AddFonts::CacheEntryList> cache_set(x, family, meta)
x |
( |
family |
( |
meta |
( |
(CacheEntryList) The modified CacheEntryList with the updated entry.
Compose canonical filename for a cached TTF
cache_ttf_filename(source, font_id, subset, weight, style)cache_ttf_filename(source, font_id, subset, weight, style)
source |
( |
font_id |
( |
subset |
( |
weight |
( |
style |
( |
(character(1)) Filename (not including the cache directory) for the cached TTF.
Compute canonical cache path for a TTF file
cache_ttf_path(source, font_id, subset, weight, style, cache_dir = NULL)cache_ttf_path(source, font_id, subset, weight, style, cache_dir = NULL)
source |
( |
font_id |
( |
subset |
( |
weight |
( |
style |
( |
cache_dir |
( |
(character(1)) Path to the cached .ttf file.
Compute paths used for caching provider artifacts and any conversion intermediate files.
cache_variant_paths(provider, family, weight, style, subset, cache_dir = NULL)cache_variant_paths(provider, family, weight, style, subset, cache_dir = NULL)
provider |
( |
family |
( |
weight |
( |
style |
( |
subset |
( |
cache_dir |
( |
(list) A list with elements to_convert (path or NULL) and ttf (path).
Write CacheEntryList to disk as JSON
cache_write(x, cache_dir = NULL, quiet = TRUE) ## S7 method for class <AddFonts::CacheEntryList> cache_write(x, cache_dir = NULL, quiet = TRUE)cache_write(x, cache_dir = NULL, quiet = TRUE) ## S7 method for class <AddFonts::CacheEntryList> cache_write(x, cache_dir = NULL, quiet = TRUE)
x |
( |
cache_dir |
( |
quiet |
( |
(NULL) Invisibly returns NULL.
Other cache:
cache_clean(),
cache_get(),
cache_get_variants(),
cache_get_weights(),
cache_read(),
cache_remove()
S7-backed cache entry (CacheEntry)
CacheEntry(family = character(0), meta = CacheMeta())CacheEntry(family = character(0), meta = CacheMeta())
family |
( |
meta |
( |
(S7_object) A validated S7 CacheEntry object.
S7 list of cache entries (CacheEntryList)
CacheEntryList(entries = list())CacheEntryList(entries = list())
entries |
( |
(S7_object) A validated S7 CacheEntryList object.
S7-backed cache metadata (CacheMeta)
CacheMeta(source, files, key_scheme = NULL, failed_keys = character(0))CacheMeta(source, files, key_scheme = NULL, failed_keys = character(0))
source |
( |
files |
( |
key_scheme |
( |
failed_keys |
( |
(S7_object) A validated S7 CacheMeta object.
woff2_to_ttf).Resolve a conversion name to the converter function used by the package (currently only woff2_to_ttf).
conv_fun(conversion)conv_fun(conversion)
conversion |
( |
(function) The conversion function if known; otherwise the helper aborts.
Copies each local font file into the cache directory, creates a
CacheEntry() with symbolic keys and source "file", writes the entry to
the cache, and returns it.
copy_and_cache_local(name, family_name, variants, cache_dir = NULL)copy_and_cache_local(name, family_name, variants, cache_dir = NULL)
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(CacheEntry | NULL) Cache entry with copied variants, or NULL if the regular variant
could not be copied.
Copies a local font file into the AddFonts cache directory, naming it according to the standard "file-{family}-{variant}.{ext}" convention.
copy_variant_to_cache( src_path, family, variant, cache_dir = NULL, quiet = FALSE )copy_variant_to_cache( src_path, family, variant, cache_dir = NULL, quiet = FALSE )
src_path |
( |
family |
( |
variant |
( |
cache_dir |
( |
quiet |
( |
(character | NULL) Path to the cached file on success, or NULL on failure.
Attempt to delete a set of files (character vector or list of paths).
delete_files(entries, quiet = FALSE)delete_files(entries, quiet = FALSE)
entries |
( |
quiet |
( |
(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
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.
download_and_cache( provider, name, family_name, regular.wt = 400, bold.wt = 700, subset = "latin", cache_dir = NULL )download_and_cache( provider, name, family_name, regular.wt = 400, bold.wt = 700, subset = "latin", cache_dir = NULL )
provider |
( |
name |
( |
family_name |
( |
regular.wt |
( |
bold.wt |
( |
subset |
( |
cache_dir |
( |
(CacheEntry | NULL) Cache entry with downloaded fonts, or NULL on failure.
Downloads each named variant from a FontProviderFile provider, creates a CacheEntry() with symbolic keys ("regular", "italic", "bold", "bolditalic"), writes the entry to the cache, and returns it.
download_and_cache_file( provider, name, family_name, variants, cache_dir = NULL )download_and_cache_file( provider, name, family_name, variants, cache_dir = NULL )
provider |
( |
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(CacheEntry | NULL) Cache entry with downloaded variants, or NULL if the regular variant could not be downloaded.
Downloads each variant from a direct URL, creates a CacheEntry() with
symbolic keys and source "url", writes the entry to the cache, and
returns it.
download_and_cache_url(name, family_name, variants, cache_dir = NULL)download_and_cache_url(name, family_name, variants, cache_dir = NULL)
name |
( |
family_name |
( |
variants |
( |
cache_dir |
( |
(CacheEntry | NULL) Cache entry with downloaded variants, or NULL if the regular variant
could not be downloaded.
Downloads a single font variant directly from a FontProviderFile using its base_url template. No conversion is performed — the file is stored as received.
download_variant_file( provider, family, filename, variant, cache_dir = NULL, quiet = FALSE )download_variant_file( provider, family, filename, variant, cache_dir = NULL, quiet = FALSE )
provider |
( |
family |
( |
filename |
( |
variant |
( |
cache_dir |
( |
quiet |
( |
(character | NULL) Path to the locally cached font file on success, 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.
download_variant_generic( provider, family, weight, style, subset = "latin", cache_dir = NULL, quiet = FALSE )download_variant_generic( provider, family, weight, style, subset = "latin", cache_dir = NULL, quiet = FALSE )
provider |
( |
family |
( |
weight |
( |
style |
( |
subset |
( |
cache_dir |
( |
quiet |
( |
(character | NULL) Path to the local .ttf file on success, or NULL on failure.
Downloads normal and italic variants for each weight and returns a named list with weight-based keys.
download_weights(provider, name, weights, subset, cache_dir, quiet = TRUE)download_weights(provider, name, weights, subset, cache_dir, quiet = TRUE)
provider |
( |
name |
( |
weights |
( |
subset |
( |
cache_dir |
( |
quiet |
( |
(list) Named list where names are weight identifiers (e.g., "400", "700italic") and values are file paths.
Abstract base class shared by all provider types. Do not construct this directly. Use FontProviderWeight() or FontProviderFile() instead.
FontProvider( source = character(0), aliases = list(), first_use_message = character(0), first_use_url = character(0) )FontProvider( source = character(0), aliases = list(), first_use_message = character(0), first_use_url = character(0) )
source |
( |
aliases |
( |
first_use_message |
( |
first_use_url |
( |
(FontProvider) S7 base class. Use a subclass constructor in practice.
Returned internally when add_font(provider = "url") is used. Carries no extra properties — its type alone signals the direct-URL dispatch path.
FontProviderDirectURL()FontProviderDirectURL()
Provider that downloads font files directly by filename from a base URL, with no weight/style/subset parameterisation. Covers Git-hosted collections like Bye Bye Binary where each variant has a fixed filename.
FontProviderFile( source, base_url, file_ext = "ttf", aliases = list(), first_use_message = NULL, first_use_url = NULL )FontProviderFile( source, base_url, file_ext = "ttf", aliases = list(), first_use_message = NULL, first_use_url = NULL )
source |
( |
base_url |
( |
file_ext |
( |
aliases |
( |
first_use_message |
( |
first_use_url |
( |
(FontProviderFile) A validated S7 FontProviderFile object.
Returned internally when add_font(provider = "file") is used. Carries no extra properties — its type alone signals the local-copy dispatch path.
FontProviderLocal()FontProviderLocal()
Provider that resolves font variants by numeric weight and style via a glue-style URL template. This covers APIs like Bunny Fonts.
FontProviderWeight( source, url_template, conversion = NULL, conversion_ext = NULL, aliases = list(), first_use_message = NULL, first_use_url = NULL )FontProviderWeight( source, url_template, conversion = NULL, conversion_ext = NULL, aliases = list(), first_use_message = NULL, first_use_url = NULL )
source |
( |
url_template |
( |
conversion |
( |
conversion_ext |
( |
aliases |
( |
first_use_message |
( |
first_use_url |
( |
(FontProviderWeight) A validated S7 FontProviderWeight object.
Determine a platform-appropriate cache directory for AddFonts and ensure it exists, creating it when necessary.
get_cache_dir()get_cache_dir()
(character(1)) Absolute path to the cache directory.
Load and return a FontProvider object for the specified provider. The providers data is stored in the package's internal sysdata.rda.
get_provider_details(provider)get_provider_details(provider)
provider |
( |
(FontProvider) A validated FontProvider object.
Returns a named list of all FontProvider objects: built-in providers first, then any providers registered in the current session. Session providers with the same source name as a built-in take precedence.
list_providers()list_providers()
(list) Named list of FontProvider objects keyed by their source name.
If provider@first_use_message is non-NULL and no message has been shown for this provider's source in the current R session, emits the message via cli::cli_inform() and records that it has been shown. Subsequent calls for the same provider are silently ignored.
maybe_show_first_use(provider)maybe_show_first_use(provider)
provider |
( |
(invisible(NULL)) Called for its side-effect only.
Ensure the requested font is installed via add_font() and draw a brief sample using showtext for proper font rendering.
preview_font( name, provider = "bunny", family = NULL, variants = NULL, sample = "The quick brown fox jumps over the lazy dog", size = 28, subset = "latin", regular.wt = 400, bold.wt = 700 )preview_font( name, provider = "bunny", family = NULL, variants = NULL, sample = "The quick brown fox jumps over the lazy dog", size = 28, subset = "latin", regular.wt = 400, bold.wt = 700 )
name |
( |
provider |
( |
family |
( |
variants |
( |
sample |
( |
size |
( |
subset |
( |
regular.wt |
( |
bold.wt |
( |
(list) Invisibly returns the list of paths produced by add_font().
files list or NULL when registration cannot proceed.This is the ONLY function that calls sysfonts::font_add(). It does not print success messages - callers should handle user feedback.
register_from_cache(entry, regular.wt = 400, bold.wt = 700)register_from_cache(entry, regular.wt = 400, bold.wt = 700)
entry |
( |
regular.wt |
( |
bold.wt |
( |
(list | NULL) Prepared files list (with regular, italic, bold, bolditalic) or NULL.
Adds a FontProvider object to the session-level registry so it can be referenced by name in add_font(). The registry is cleared when the R session ends.
register_provider(provider, overwrite = FALSE)register_provider(provider, overwrite = FALSE)
provider |
( |
overwrite |
( |
(NULL) Called for its side-effect; returns NULL invisibly.
Replace disallowed characters with - and convert to lower-case so
the resulting id is safe to use in filenames.
safe_id(name)safe_id(name)
name |
( |
(character(1)) Sanitised identifier.
Dispatches on a source name (character) or a FontProvider object.
unregister_provider(x)unregister_provider(x)
x |
( |
(NULL) Called for its side-effect; returns NULL invisibly.
Downloads missing font weights and updates the cache entry. Does NOT register the font - use register_from_cache() after this.
update_download_and_cache( entry, provider, name, family_name, missing_weights, subset = "latin", cache_dir = NULL, cel = NULL )update_download_and_cache( entry, provider, name, family_name, missing_weights, subset = "latin", cache_dir = NULL, cel = NULL )
entry |
( |
provider |
( |
name |
( |
family_name |
( |
missing_weights |
( |
subset |
( |
cache_dir |
( |
cel |
( |
(CacheEntry | NULL) Updated cache entry with new weights (NOT registered), or NULL on failure.
Internal helper. Uses the system woff2_decompress tool to convert a .woff2 file into a .ttf file.
woff2_to_ttf(font_file, overwrite = FALSE, remove_old = TRUE, quiet = FALSE)woff2_to_ttf(font_file, overwrite = FALSE, remove_old = TRUE, quiet = FALSE)
font_file |
( |
overwrite |
( |
remove_old |
( |
quiet |
( |
(character(1)) Invisibly returns the path to the .ttf file on success.