When used with any of the main font settings in bs_theme() (e.g., base_font, code_font, heading_font), these font objects ensure relevant font file resources are included with the theme. A particular font object should define an single font family --- if you need multiple families, a list() of font objects may be provided to bs_theme().

font_face(
  family,
  src,
  weight = NULL,
  style = NULL,
  display = c("swap", "auto", "block", "fallback", "optional"),
  stretch = NULL,
  variant = NULL,
  unicode_range = NULL
)

font_link(family, href)

font_google(
  family,
  local = TRUE,
  cache = sass_file_cache(dir = cache_context_dir(), max_size = 100 * 1024^2),
  wght = NULL,
  ital = NULL,
  display = c("swap", "auto", "block", "fallback", "optional")
)

Arguments

family

A character string with a single font family name.

src

A character vector for the src @font-face property. Beware that is character strings are taken verbatim, so careful quoting and/or URL encoding may be required.

weight

A character (or numeric) vector for the font-weight @font-face property.

style

A character vector for the font-style @font-face property.

display

the font-display @font-face property.

stretch

A character vector for the font-stretch @font-face property.

variant

A character vector for the font-variant @font-face property.

unicode_range

A character vector for unicode-range @font-face property.

href

A URL resource pointing to the font data.

local

Whether or not download and bundle local (woff) font files.

cache

A sass::sass_file_cache() object (or, more generally, a file caching class with $get_file() and $set_file() methods). Set this argument to FALSE or NULL to disable caching.

wght

One of the following:

  • NULL, the default weight for the family.

  • A character string defining an axis range

  • A numeric vector of desired font weight(s).

ital

One of the following:

  • NULL, the default font-style for the family.

  • 0, meaning font-style: normal

  • 1, meaning font-style: italic

  • c(0, 1), meaning both normal and italic

Value

a list with a special class.

Local fonts

With local (i.e., self-hosted) fonts, clients (i.e., end users) can render fonts without an internet connection. By default, google_font() will automatically download, cache, and serve font files locally. Non-Google fonts may also be served locally, but you'll have to download and serve local file using something like shiny::addResourcePath() (or similar) and provide the relevant files to a font_face() definiton.

Remote fonts

With remotely hosted fonts, clients (i.e., end users) need an internet connection to render the fonts. Remote fonts can be implemented using font_google(..., local = FALSE) (hosted via Google), font_link() (hosted via href URL), or font_face() (hosted via src URL).

References

https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face

https://developer.mozilla.org/en-US/docs/Learn/CSS/Styling_text/Web_fonts

https://developers.google.com/fonts/docs/css2

Examples

# If you have an internet connection, running the following code # will download, cache, and import the relevant Google Font files # for local use theme <- bs_theme( base_font = font_google("Fira Sans"), code_font = font_google("Fira Code"), heading_font = font_google("Fredoka One") ) if (interactive()) { bs_theme_preview(theme) } # Three different yet equivalent ways of importing a remotely-hosted Google Font a <- font_google("Crimson Pro", wght = "200..900", local = FALSE) b <- font_link( "Crimson Pro", href = "https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@200..900" ) url <- "https://fonts.gstatic.com/s/crimsonpro/v13/q5uDsoa5M_tv7IihmnkabARboYF6CsKj.woff2" c <- font_face( family = "Crimson Pro", style = "normal", weight = "200 900", src = paste0("url(", url, ") format('woff2')") ) theme <- bs_theme(base_font = c) if (interactive()) { bs_theme_preview(theme) }