Skip to contents

This checks to see if a font exists. If missing it will try and install from google fonts or brick.io. If nothing can be done it will suggest alternatives from fonts_available(). In all cases this will make the font available to systemfonts (for ragg and svg devices), and extrafonts (for pdf etc). Webfonts are automatically downloaded into the users font directory and from there will be picked up by cairo devices in theory, and system pdf/svg viewers. In practice this is a bit hit and miss.

Usage

check_font(family, .legacy = FALSE)

Arguments

family

a font family name or names

.legacy

should we try and register fonts

Value

the font family name if it can be located or an alternative if not.

Examples

check_font(c("Roboto","Arial","Kings","EB Garamond"))
#>        Roboto         Arial         Kings   EB Garamond 
#>      "Roboto"       "Arial"       "Kings" "EB Garamond" 
fonts_available(c("Roboto","Arial","Kings","EB Garamond"))
#> [1] "Roboto"      "Arial"       "Kings"       "EB Garamond"

plot = ggplot2::ggplot()+
ggplot2::theme_void(base_family="Roboto")+
ggplot2::geom_point()+ggplot2::theme(margins = ggplot2::margin(14,0,14,0))+
ggplot2::annotate("label",x=0,y=0,label="Kings: Em dash: \u2014 hello world", family="Kings")+
ggplot2::annotate("text",x=0,y=1,label="Roboto: UTF-8 subscript 2: \u2082", family="Roboto")+
ggplot2::annotate("text",x=0,y=2,label="EB Garamond: UTF-8 gte: \u2265", family="EB Garamond")


if (FALSE) {


  # Does not work - "invalid font type"  & "font family 'Kings'
  # not found in PostScript font database"
  # font but no unicode support
  check_font(c("Roboto","Kings","EB Garamond"), .legacy=TRUE)
  tmp = tempfile(fileext = ".pdf")
  grDevices::pdf(tmp, width=3, height = 1, units="in")
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and unicode support
  tmp = tempfile(fileext = ".pdf")
  grDevices::cairo_pdf(tmp, width=3, height = 1, units="in")
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and unicode support
  tmp = tempfile(fileext = ".png")
  grDevices::png(tmp, width=3, height = 1, units="in", res=300)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and unicode support
  tmp = tempfile(fileext = ".png")
  ragg::agg_png(tmp, width=3, height = 1, units="in", res=300)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and unicode support
  tmp = tempfile(fileext = ".svg")
  svglite::svglite(tmp, width=3, height = 1)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # Does not work - "family 'Roboto'
  # not included in postscript() device"
  # however:  names(grDevices::postscriptFonts()) includes Roboto
  tmp = tempfile(fileext = ".eps")
  grDevices::postscript(tmp, width=3, height = 1)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and unicode support
  tmp = tempfile(fileext = ".eps")
  grDevices::cairo_ps(tmp, width=3, height = 1)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

  # font and partial unicode support
  tmp = tempfile(fileext = ".ps")
  Cairo::CairoPS(tmp, width=3, height = 1)
  plot
  grDevices::dev.off()
  utils::browseURL(tmp)

}