By design, systemfonts searches the fonts installed natively on the system.
It is possible, however, to register other fonts from e.g. font packages or
local font files, that will get searched before searching any installed
fonts. You can always get an overview over all registered fonts with the
registry_fonts()
function that works as a registry focused analogue to
system_fonts()
. If you wish to clear out the registry, you can either
restart the R session or call clear_registry()
.
Usage
register_font(
name,
plain,
bold = plain,
italic = plain,
bolditalic = plain,
features = font_feature()
)
registry_fonts()
clear_registry()
Arguments
- name
The name the collection will be known under (i.e. family)
- plain, bold, italic, bolditalic
Fontfiles for the different faces of the collection. can either be a filepath or a list containing a filepath and an index (only for font files containing multiple fonts). If not given it will default to the
plain
specification.- features
A
font_feature
object describing the specific OpenType font features to turn on for the registered font.
Value
register_font()
and clear_registry()
returns NULL
invisibly.
registry_fonts()
returns a data table in the same style as system_fonts()
though less detailed and not based on information in the font file.
Details
register_font
also makes it possible to use system fonts with traits that
is not covered by the graphic engine in R. In plotting operations it is only
possible to specify a family name and whether or not the font should be bold
and/or italic. There are numerous fonts that will never get matched to this,
especially because bold is only one of many weights.
Apart from granting a way to use new varieties of fonts, font registration
also allows you to override the default sans
, serif
, and mono
mappings,
simply by registering a collection to the relevant default name. As
registered fonts are searched first it will take precedence over the default.
Examples
# Create a random font collection
fonts <- system_fonts()
plain <- sample(which(!fonts$italic & fonts$weight <= 'normal'), 1)
bold <- sample(which(!fonts$italic & fonts$weight > 'normal'), 1)
italic <- sample(which(fonts$italic & fonts$weight <= 'normal'), 1)
bolditalic <- sample(which(fonts$italic & fonts$weight > 'normal'), 1)
register_font(
'random',
plain = list(fonts$path[plain], fonts$index[plain]),
bold = list(fonts$path[bold], fonts$index[bold]),
italic = list(fonts$path[italic], fonts$index[italic]),
bolditalic = list(fonts$path[bolditalic], fonts$index[bolditalic])
)
# Look at your creation
registry_fonts()
#> # A tibble: 4 × 7
#> path index family style weight italic features
#> <chr> <int> <chr> <chr> <ord> <lgl> <list>
#> 1 /usr/share/fonts/opentype/urw… 0 random Regu… normal FALSE <int>
#> 2 /usr/share/fonts/truetype/dej… 0 random Bold bold FALSE <int>
#> 3 /usr/share/fonts/type1/urw-ba… 0 random Ital… normal TRUE <int>
#> 4 /usr/share/fonts/opentype/urw… 0 random Bold… bold TRUE <int>
# Reset
clear_registry()