register_font: Register font collections as families

View source: R/register_font.R

register_fontR Documentation

Register font collections as families

Description

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.

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.

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.

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()

# Reset
clear_registry()


systemfonts documentation built on May 29, 2024, 9:20 a.m.