# Font family name constants used as defaults in theme functions. # These match the --cv-font-* CSS custom properties on www.civilytics.com. CV_FONT_DISPLAY <- "Libre Franklin" # headings / display text CV_FONT_SANS <- "Inter" # axis text, legends, UI elements CV_FONT_SERIF <- "Source Serif 4" # body prose (not used in default theme) # Internal flag so civilytics_load_fonts() is idempotent within a session. .cv_fonts_loaded <- FALSE #' Load Civilytics brand fonts #' #' Downloads Inter and Libre Franklin from Google Fonts via #' [sysfonts::font_add_google()], then calls [showtext::showtext_auto()] so #' that all graphics devices render text with those fonts. This is called #' automatically when the package loads; use this function to retry if the #' initial load failed (e.g., the machine was offline at load time). #' #' @return Invisibly returns `NULL`. #' @export #' #' @examples #' \dontrun{ #' civilytics_load_fonts() #' } civilytics_load_fonts <- function() { sysfonts::font_add_google("Inter", family = "Inter") sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin") showtext::showtext_auto() .cv_fonts_loaded <<- TRUE invisible(NULL) } .onLoad <- function(libname, pkgname) { tryCatch( civilytics_load_fonts(), error = function(e) { # Store the failure so .onAttach can surface a message to the user. options(.civilytics_fonts_failed = TRUE) } ) } .onAttach <- function(libname, pkgname) { if (isTRUE(getOption(".civilytics_fonts_failed"))) { packageStartupMessage( "[civilytics] Brand fonts (Inter, Libre Franklin) could not be loaded ", "from Google Fonts. Charts will fall back to system fonts. ", "Call civilytics_load_fonts() once you have an internet connection." ) } }