feat(theme): add a showtext switch to civilytics_load_fonts() for ragg users (#29)
R-CMD-check / R CMD check (push) Successful in 5m7s
R-CMD-check / R CMD check (push) Successful in 5m7s
civilytics_load_fonts(showtext = getOption("civilytics.showtext", TRUE))
registers the brand fonts and only then turns on showtext, so ragg users can
opt out of its 96 dpi text sizing. The default is unchanged, so existing
reports render as before. The help page documents the ragg path.
Closes #29
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f3ec052144
commit
119aa7aa0f
5 files changed
+105
-16
No files matched your search
+2
-1
@@ -35,7 +35,8 @@ Suggests:
|
|||||||
flextable,
|
flextable,
|
||||||
officer,
|
officer,
|
||||||
patchwork,
|
patchwork,
|
||||||
ragg
|
ragg,
|
||||||
|
withr
|
||||||
Config/testthat/edition: 3
|
Config/testthat/edition: 3
|
||||||
Config/roxygen2/version: 8.0.0
|
Config/roxygen2/version: 8.0.0
|
||||||
RoxygenNote: 7.3.3
|
RoxygenNote: 7.3.3
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
## New features
|
## New features
|
||||||
- `civilytics_logo()` and `add_logo()` gain `logo_height` (inches, default `0.5`),
|
- `civilytics_logo()` and `add_logo()` gain `logo_height` (inches, default `0.5`),
|
||||||
the height of a band reserved for the logo.
|
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
|
- `patchwork` is now a suggested package, used when a patchwork composition is
|
||||||
passed to `civilytics_logo()`.
|
passed to `civilytics_logo()`.
|
||||||
- Added comprehensive test coverage for the `prop_conf` module, including
|
- Added comprehensive test coverage for the `prop_conf` module, including
|
||||||
|
|||||||
@@ -14,16 +14,26 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
|||||||
#' Load Civilytics brand fonts
|
#' Load Civilytics brand fonts
|
||||||
#'
|
#'
|
||||||
#' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
#' Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||||
#' Google Fonts via [sysfonts::font_add_google()], then calls
|
#' Google Fonts via [sysfonts::font_add_google()], then, unless
|
||||||
#' [showtext::showtext_auto()] so that all graphics devices render text with
|
#' `showtext = FALSE`, calls [showtext::showtext_auto()] so that all graphics
|
||||||
#' those fonts. This is called automatically when the package loads; use this
|
#' devices render text with those fonts. This is called automatically when the
|
||||||
#' function to retry if the initial load failed (e.g., the machine was offline
|
#' package loads; use this function to retry if the initial load failed (e.g.,
|
||||||
#' at load time).
|
#' the machine was offline at load time).
|
||||||
#'
|
#'
|
||||||
#' Subsequent calls within the same session are no-ops unless `force = TRUE`.
|
#' 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
|
#' @param force Logical. If `TRUE`, reload fonts even if they were already
|
||||||
#' loaded in this session. Default `FALSE`.
|
#' 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
|
#' @return Invisibly returns `TRUE` if fonts were loaded, `FALSE` if skipped
|
||||||
#' (already loaded and `force = FALSE`).
|
#' (already loaded and `force = FALSE`).
|
||||||
@@ -32,15 +42,22 @@ CV_FONT_MONO <- "JetBrains Mono" # code, data tables, numeric callouts
|
|||||||
#' @examples
|
#' @examples
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' civilytics_load_fonts()
|
#' 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))
|
if (.cv_state$fonts_loaded && !force) return(invisible(FALSE))
|
||||||
|
|
||||||
sysfonts::font_add_google("Inter", family = "Inter")
|
sysfonts::font_add_google("Inter", family = "Inter")
|
||||||
sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin")
|
sysfonts::font_add_google("Libre Franklin", family = "Libre Franklin")
|
||||||
sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4")
|
sysfonts::font_add_google("Source Serif 4", family = "Source Serif 4")
|
||||||
sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono")
|
sysfonts::font_add_google("JetBrains Mono", family = "JetBrains Mono")
|
||||||
showtext::showtext_auto()
|
if (showtext) showtext::showtext_auto()
|
||||||
|
|
||||||
.cv_state$fonts_loaded <- TRUE
|
.cv_state$fonts_loaded <- TRUE
|
||||||
invisible(TRUE)
|
invisible(TRUE)
|
||||||
|
|||||||
@@ -4,28 +4,46 @@
|
|||||||
\alias{civilytics_load_fonts}
|
\alias{civilytics_load_fonts}
|
||||||
\title{Load Civilytics brand fonts}
|
\title{Load Civilytics brand fonts}
|
||||||
\usage{
|
\usage{
|
||||||
civilytics_load_fonts(force = FALSE)
|
civilytics_load_fonts(
|
||||||
|
force = FALSE,
|
||||||
|
showtext = getOption("civilytics.showtext", TRUE)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{force}{Logical. If \code{TRUE}, reload fonts even if they were already
|
\item{force}{Logical. If \code{TRUE}, reload fonts even if they were already
|
||||||
loaded in this session. Default \code{FALSE}.}
|
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{
|
\value{
|
||||||
Invisibly returns \code{TRUE} if fonts were loaded, \code{FALSE} if skipped
|
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{
|
\description{
|
||||||
Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
Downloads Inter, Libre Franklin, Source Serif 4 and JetBrains Mono from
|
||||||
Google Fonts via \code{sysfonts::font_add_google()}, then calls
|
Google Fonts via \code{sysfonts::font_add_google()}, then, unless
|
||||||
\code{showtext::showtext_auto()} so that all graphics devices render text with
|
\code{showtext = FALSE}, calls \code{showtext::showtext_auto()} so that all graphics
|
||||||
those fonts. This is called automatically when the package loads; use this
|
devices render text with those fonts. This is called automatically when the
|
||||||
function to retry if the initial load failed (e.g., the machine was offline
|
package loads; use this function to retry if the initial load failed (e.g.,
|
||||||
at load time).
|
the machine was offline at load time).
|
||||||
|
}
|
||||||
|
\details{
|
||||||
Subsequent calls within the same session are no-ops unless \code{force = TRUE}.
|
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{
|
\examples{
|
||||||
\dontrun{
|
\dontrun{
|
||||||
civilytics_load_fonts()
|
civilytics_load_fonts()
|
||||||
|
|
||||||
|
# For ragg devices: register the fonts but leave showtext off
|
||||||
|
civilytics_load_fonts(force = TRUE, showtext = FALSE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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")
|
||||||
|
})
|
||||||
Reference in new issue
Block a user