Check font availability: detect missing fonts before rendering, to avoid silent fallbacks
font_family_exists(), sys_fonts()
How font metric calculation works
gdtools was originally designed to compute font metrics for R graphic
devices via C-level interfaces (Cairo and FreeType). With the arrival of
the ‘systemfonts’ package, font discovery and matching became much
simpler and the C interface was no longer needed by other packages.
The package has since been refocused. Font metric calculation follows
two steps:
Font lookup: ‘systemfonts’ resolves the family name to a font file.
Any font registered via systemfonts::register_font(),
register_gfont(), or font_set() is found.
Metric computation: Cairo reads the font file and computes width,
ascent, and descent.
The metrics are independent of any graphic device. Whether they match
the final rendering depends on how the device resolves fonts on its
side, this is a device concern.
Installation
You can install the released version of gdtools from CRAN with:
The main function for text measurement is strings_sizes(). It returns
width, ascent and descent in inches using Cairo. The measurements are
accurate for devices that use Cairo or ‘systemfonts’ for font resolution
(ragg, svglite, ggiraph, cairo_pdf(), …); for devices with their own
font engine (pdf(), png(), …) the metrics may not match the
rendering.
font_set_auto() detects the best available system fonts and falls back
to Liberation fonts when needed. It returns a font_set object ready to
use with ggplot2, ggiraph, or any ‘systemfonts’-based device:
library(gdtools)
fonts <- font_set_auto()
fonts
#> Font set
#> sans: Arial [system]
#> serif: Times New Roman [system]
#> mono: Courier New [system]
#> symbol: Symbol [system]
#> 4 HTML dependencies
For explicit control, font_set() lets you pick each role with
font_google(), font_liberation(), or a plain family name:
fonts <- font_set(
sans = font_google("Open Sans"),
mono = font_liberation("mono")
)
fonts$sans # "Open Sans"
fonts$dsvg_fonts # named list for dsvg() / girafe()
fonts$dependencies # list of htmlDependency objects
For an offline-only setup, font_set_liberation() uses Liberation fonts
for all four roles:
fonts <- font_set_liberation()
fonts
Going further
Google Fonts
A set of lower-level functions is provided to download and cache fonts
from ‘Google Fonts’. They can be used in ‘R Markdown’ documents and
‘Shiny’ applications, as well as with graphic outputs generated by
‘ggiraph’, ‘ragg’ and ‘svglite’, or tabular outputs from ‘flextable’.
# Download and register with systemfonts (cached for future use)
register_gfont(family = "Open Sans")
Use addGFontHtmlDependency() to embed a Google Font in an HTML
document, and gfontHtmlDependency() to create an htmltools
dependency object.
To install a Google Font at the system level (e.g. in a Dockerfile), use
install_gfont_script():
The package bundles ‘Liberation Sans’, ‘Liberation Serif’ and
‘Liberation Mono’ fonts (SIL Open Font License). They are useful as
cross-platform fallback fonts, for visual tests, or when no internet
connection is available.
font_set_liberation() registers all three families with ‘systemfonts’
and produces the matching HTML dependencies in a single call:
The individual functions register_liberationsans(),
register_liberationserif() and register_liberationmono() remain
available when only a specific variant is needed.
gdtools
gdtoolsaddresses four practical problems when working with fonts in R:strings_sizes()via Cairofont_set_liberation(),font_set_auto(),font_set()htmlDependencyobjects so the browser loads the right fontsfont_set()$dependencies,gfontHtmlDependency(),liberation*HtmlDependency()font_family_exists(),sys_fonts()How font metric calculation works
gdtoolswas originally designed to compute font metrics for R graphic devices via C-level interfaces (Cairo and FreeType). With the arrival of the ‘systemfonts’ package, font discovery and matching became much simpler and the C interface was no longer needed by other packages.The package has since been refocused. Font metric calculation follows two steps:
systemfonts::register_font(),register_gfont(), orfont_set()is found.The metrics are independent of any graphic device. Whether they match the final rendering depends on how the device resolves fonts on its side, this is a device concern.
Installation
You can install the released version of gdtools from CRAN with:
And the development version from GitHub with:
Font metrics
The main function for text measurement is
strings_sizes(). It returns width, ascent and descent in inches using Cairo. The measurements are accurate for devices that use Cairo or ‘systemfonts’ for font resolution (ragg, svglite, ggiraph,cairo_pdf(), …); for devices with their own font engine (pdf(),png(), …) the metrics may not match the rendering.All arguments are vectorized:
Font management
font_set_auto()detects the best available system fonts and falls back to Liberation fonts when needed. It returns afont_setobject ready to use with ggplot2, ggiraph, or any ‘systemfonts’-based device:For explicit control,
font_set()lets you pick each role withfont_google(),font_liberation(), or a plain family name:For an offline-only setup,
font_set_liberation()uses Liberation fonts for all four roles:Going further
Google Fonts
A set of lower-level functions is provided to download and cache fonts from ‘Google Fonts’. They can be used in ‘R Markdown’ documents and ‘Shiny’ applications, as well as with graphic outputs generated by ‘ggiraph’, ‘ragg’ and ‘svglite’, or tabular outputs from ‘flextable’.
Use
addGFontHtmlDependency()to embed a Google Font in an HTML document, andgfontHtmlDependency()to create anhtmltoolsdependency object.To install a Google Font at the system level (e.g. in a Dockerfile), use
install_gfont_script():Liberation fonts
The package bundles ‘Liberation Sans’, ‘Liberation Serif’ and ‘Liberation Mono’ fonts (SIL Open Font License). They are useful as cross-platform fallback fonts, for visual tests, or when no internet connection is available.
font_set_liberation()registers all three families with ‘systemfonts’ and produces the matching HTML dependencies in a single call:The individual functions
register_liberationsans(),register_liberationserif()andregister_liberationmono()remain available when only a specific variant is needed.