diff --git a/DESCRIPTION b/DESCRIPTION index df566e0..d9d4c0f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -35,7 +35,8 @@ Suggests: flextable, officer, patchwork, - ragg + ragg, + withr Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 RoxygenNote: 7.3.3 diff --git a/NEWS.md b/NEWS.md index fe11a7b..05e14db 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,9 @@ ## New features - `civilytics_logo()` and `add_logo()` gain `logo_height` (inches, default `0.5`), the height of a band reserved for the logo. +- `civilytics_load_fonts()` gains `showtext` (default `getOption("civilytics.showtext", TRUE)`), so ragg + users can register the brand fonts without turning on showtext, which draws text + at a third of its size on a 300 dpi ragg device. The default is unchanged (#29). - `patchwork` is now a suggested package, used when a patchwork composition is passed to `civilytics_logo()`. - Added comprehensive test coverage for the `prop_conf` module, including diff --git a/R/fonts.R b/R/fonts.R index b0bd14f..bab58e2 100644 --- a/R/fonts.R +++ b/R/fonts.R @@ -14,16 +14,26 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts #' Load Civilytics brand fonts #' #' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono 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). +#' Google Fonts via [sysfonts::font_add_google()], then, unless +#' `showtext = FALSE`, 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). #' #' Subsequent calls within the same session are no-ops unless `force = TRUE`. #' +#' **Using ragg.** [showtext::showtext_auto()] draws text at 96 dpi whatever the +#' device resolution, so on a `ragg::agg_png(res = 300)` device text comes out +#' at about a third of its requested size. For ragg, set +#' `options(civilytics.showtext = FALSE)` before loading the package, or call +#' `showtext::showtext_auto(FALSE)` afterwards. The brand fonts must then be +#' installed on the system, because ragg reads system fonts rather than the +#' ones registered through sysfonts. +#' #' @param force Logical. If `TRUE`, reload fonts even if they were already #' loaded in this session. Default `FALSE`. +#' @param showtext Logical. Turn on [showtext::showtext_auto()] after +#' registering the fonts. Default is `getOption("civilytics.showtext", TRUE)`. #' #' @return Invisibly returns `TRUE` if fonts were loaded, `FALSE` if skipped #' (already loaded and `force = FALSE`). @@ -32,15 +42,22 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts #' @examples #' \dontrun{ #' civilytics_load_fonts() +#' +#' # For ragg devices: register the fonts but leave showtext off +#' civilytics_load_fonts(force = TRUE, showtext = FALSE) #' } -civilytics_load_fonts <- function(force = FALSE) { +civilytics_load_fonts <- function(force = FALSE, + showtext = getOption("civilytics.showtext", TRUE)) { + if (!is.logical(showtext) || length(showtext) != 1L || is.na(showtext)) { + stop("`showtext` must be a single TRUE or FALSE.", call. = FALSE) + } if (.cv_state$fonts_loaded && !force) return(invisible(FALSE)) sysfonts::font_add_google("Inter", family = "Inter") sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin") sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4") sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono") - showtext::showtext_auto() + if (showtext) showtext::showtext_auto() .cv_state$fonts_loaded <- TRUE invisible(TRUE) diff --git a/man/civilytics_load_fonts.Rd b/man/civilytics_load_fonts.Rd index fda32e0..3e59b1c 100644 --- a/man/civilytics_load_fonts.Rd +++ b/man/civilytics_load_fonts.Rd @@ -4,28 +4,46 @@ \alias{civilytics_load_fonts} \title{Load Civilytics brand fonts} \usage{ -civilytics_load_fonts(force = FALSE) +civilytics_load_fonts( + force = FALSE, + showtext = getOption("civilytics.showtext", TRUE) +) } \arguments{ \item{force}{Logical. If \code{TRUE}, reload fonts even if they were already loaded in this session. Default \code{FALSE}.} + +\item{showtext}{Logical. Turn on \code{showtext::showtext_auto()} after +registering the fonts. Default is \code{getOption("civilytics.showtext", TRUE)}.} } \value{ Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped -(already loaded and \code{force = FALSE}). + (already loaded and \code{force = FALSE}). } \description{ Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from -Google Fonts via \code{sysfonts::font_add_google()}, then calls -\code{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). - +Google Fonts via \code{sysfonts::font_add_google()}, then, unless +\code{showtext = FALSE}, calls \code{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). +} +\details{ Subsequent calls within the same session are no-ops unless \code{force = TRUE}. + +\strong{Using ragg.} \code{showtext::showtext_auto()} draws text at 96 dpi whatever the +device resolution, so on a \code{ragg::agg_png(res = 300)} device text comes out +at about a third of its requested size. For ragg, set +\code{options(civilytics.showtext = FALSE)} before loading the package, or call +\code{showtext::showtext_auto(FALSE)} afterwards. The brand fonts must then be +installed on the system, because ragg reads system fonts rather than the +ones registered through sysfonts. } \examples{ \dontrun{ civilytics_load_fonts() + +# For ragg devices: register the fonts but leave showtext off +civilytics_load_fonts(force = TRUE, showtext = FALSE) } } diff --git a/tests/testthat/test_fonts.R b/tests/testthat/test_fonts.R new file mode 100644 index 0000000..6051868 --- /dev/null +++ b/tests/testthat/test_fonts.R @@ -0,0 +1,50 @@ +# civilytics_load_fonts() must not touch the network or global devices in tests: +# both sysfonts::font_add_google() and showtext::showtext_auto() are mocked. +with_font_mocks <- function(code, env = parent.frame()) { + calls <- new.env() + calls$showtext <- 0L + calls$google <- character() + old <- .cv_state$fonts_loaded + .cv_state$fonts_loaded <- FALSE + withr::defer(.cv_state$fonts_loaded <- old, envir = env) + testthat::local_mocked_bindings( + font_add_google = function(name, family = name, ...) { + calls$google <- c(calls$google, family) + }, + .package = "sysfonts", .env = env + ) + testthat::local_mocked_bindings( + showtext_auto = function(enable = TRUE) calls$showtext <- calls$showtext + 1L, + .package = "showtext", .env = env + ) + calls +} + +test_that("civilytics_load_fonts turns showtext on by default (#29)", { + skip_if_not_installed("withr") + calls <- with_font_mocks() + expect_true(civilytics_load_fonts()) + expect_length(calls$google, 4) + expect_equal(calls$showtext, 1L) +}) + +test_that("showtext = FALSE registers fonts but leaves showtext off (#29)", { + skip_if_not_installed("withr") + calls <- with_font_mocks() + expect_true(civilytics_load_fonts(showtext = FALSE)) + expect_length(calls$google, 4) + expect_equal(calls$showtext, 0L) +}) + +test_that("options(civilytics.showtext = FALSE) sets the default (#29)", { + skip_if_not_installed("withr") + calls <- with_font_mocks() + withr::local_options(civilytics.showtext = FALSE) + civilytics_load_fonts() + expect_equal(calls$showtext, 0L) +}) + +test_that("showtext must be a single TRUE or FALSE", { + expect_error(civilytics_load_fonts(showtext = NA), "showtext") + expect_error(civilytics_load_fonts(showtext = c(TRUE, FALSE)), "showtext") +})