R/lato_import.R

Defines functions lato_import

Documented in lato_import

#' Import and register Lato font
#'
#' \code{lato_import()} tests to see if Lato is imported and registered. If
#' Lato, isn't imported and registered, then \code{lato_import()} imports and
#' registers Lato with
#' \href{https://github.com/wch/extrafont}{library(extrafont)}.
#'
#' Note: Lato must be installed on your computer for \code{lato_import()} to
#' work. Lato is the Urban Institute's main font. To install, visit
#' \href{https://fonts.google.com/specimen/Lato}{Google fonts} and click
#' "Download family". Unzip and open each of the .ttf files and click install.
#'
#' Test to see if Lato is imported and registered with \code{lato_test()}.
#'
#' @md
#' @export
lato_import <- function() {

  if (sum(grepl("[Ll]ato$", extrafont::fonts())) > 0) {

    "Lato is already imported and registered."

  } else {

    # get a list of all fonts on the machine
    local_fonts <- systemfonts::system_fonts()

    # subset the list to just Lato fonts
    lato_fonts <- local_fonts[grepl(pattern = "[Ll]ato", x = local_fonts$family), ]

    # create a vector of directories where Lato fonts are located
    lato_directories <- unique(gsub("[Ll]ato.*\\.ttf", "", lato_fonts$path))

    # add a warning for unix users about Rttf2pt1 version
    if (.Platform$OS.type == "unix") {

      print(
        paste(
            "Unix (Mac) users may experience errors if the library(Rttf2pt1)",
            "version is >= 1.3.9. Restart R. Run",
            "remotes::install_version('Rttf2pt1', version = '1.3.8').",
            "Restart R. Then try lato_import() again."
        )
      )

    }

    # import the Lato fonts
    extrafont::font_import(paths = lato_directories, pattern = "[Ll]ato")

    # register the fonts
    extrafont::loadfonts()

    # test to confirm that Lato is imported and registered
    lato_test()

  }

}
UrbanInstitute/urbnthemes documentation built on Jan. 26, 2022, 8:34 p.m.