Learn R Programming

sysfonts (version 0.8.9)

font_add_google: Load Google Fonts into 'sysfonts'

Description

The two versions of this function are equivalent, but the "underscore" naming is preferred.

This function will search the Google Fonts repository (https://fonts.google.com/) for a specified family name, download the proper font files, and then add them to sysfonts. This function requires the jsonlite and curl packages.

Usage

font_add_google(
  name,
  family = name,
  regular.wt = 400,
  bold.wt = 700,
  repo = "http://fonts.gstatic.com/",
  db_cache = TRUE,
  handle = curl::new_handle()
)

font.add.google( name, family = name, regular.wt = 400, bold.wt = 700, repo = "http://fonts.gstatic.com/", handle = curl::new_handle() )

Arguments

name

name of the font that will be searched in Google Fonts

family

specifies the family name of this font in R. This can be any string, not necessarily the same as name. The value of this parameter will be used in R plotting functions. See the example code below.

regular.wt

font weight for the regular font face, usually 400

bold.wt

font weight for the bold font face, usually 700

repo

the site that hosts the font files. Default is the official repository http://fonts.gstatic.com/ provided by Google Fonts.

db_cache

whether to obtain font metadata from a cache site. Using cache is typically faster, but not as update-to-date as using the official API. If db_cache is set to FALSE, then metadata are retrieved from the Google Fonts API.

handle

a curl handle object passed to curl::curl_download().

Author

Yixuan Qiu <https://statr.me/>

Details

There are thousands of open source fonts in the Google Fonts repository (https://fonts.google.com/). This function will try to search the font family specified by the name argument, and then automatically download the font files for all possible font faces ("regular", "bold", "italic" and "bold italic", but no"symbol"). If fonts are found and downloaded successfully, they will be also added to sysfonts with the given family name.

See Also

font_families_google()

Examples

Run this code
if (FALSE) {
font_add_google("Alegreya Sans", "aleg")

if(require(showtext))
{
    wd = setwd(tempdir())
    pdf("google-fonts-ex.pdf")
    showtext_begin()
    
    par(family = "aleg")
    plot(0:5,0:5, type="n")
    text(1:4, 1:4, "Alegreya Sans", font=1:4, cex = 2)
    
    showtext_end()
    dev.off()
    setwd(wd)
}

}

Run the code above in your browser using DataLab