diff --git a/DESCRIPTION b/DESCRIPTION index 6deb3a0..2a74d6b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,6 @@ Imports: magrittr (>= 2.0.3), rlang (>= 1.1.0), shiny (>= 1.14.0), - systemfonts (>= 1.3.1), ROvis.utils Suggests: covr (>= 3.6.4), diff --git a/NAMESPACE b/NAMESPACE index f45bc22..64f66a5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -35,4 +35,3 @@ importFrom(shiny, HTML, tags ) -importFrom(systemfonts,system_fonts) diff --git a/R/ROvis.table-package.R b/R/ROvis.table-package.R index b26e89d..3d66aeb 100644 --- a/R/ROvis.table-package.R +++ b/R/ROvis.table-package.R @@ -7,7 +7,6 @@ #' @importFrom cli cli_abort #' @importFrom dplyr filter #' @importFrom magrittr %>% -#' @importFrom systemfonts system_fonts #' @importFrom DT datatable #' @importFrom gt gt #' @importFrom ROvis.utils ro_check_if_font_available diff --git a/R/ro_dt_theme.R b/R/ro_dt_theme.R index a06a9fa..7f41116 100644 --- a/R/ro_dt_theme.R +++ b/R/ro_dt_theme.R @@ -8,7 +8,9 @@ #' @importFrom shiny tags HTML #' @importFrom ROvis.utils ro_check_if_font_available #' -#' @param base_family Character. Font family to use for table text. Default is "Arial". +#' @param base_family Character. Font family to use for table text. Default is +#' "RijksoverheidSansWebText". If not installed, Verdana is used, then Arial, +#' then the first font found on your device, whichever is installed first. #' @param sorting Logical. If \code{TRUE}, column headers are left-aligned; #' if \code{FALSE}, right-aligned. Default is \code{FALSE}. #' @@ -19,7 +21,7 @@ #' @examples #' if (interactive()) { #' ui <- fluidPage( -#' ro_dt_theme(base_family = "Arial", sorting = FALSE), +#' ro_dt_theme(sorting = FALSE), #' DTOutput("mytable") #' ) #' server <- function(input, output, session) { @@ -33,7 +35,7 @@ ro_dt_theme <- function( sorting = FALSE ) { check_string(base_family) - ro_check_if_font_available(base_family = base_family) + base_family <- ro_check_if_font_available(target_font_family = base_family) if (sorting) { header_align <- "left" diff --git a/R/ro_gt_theme.R b/R/ro_gt_theme.R index ab8eff7..6f5fa3b 100644 --- a/R/ro_gt_theme.R +++ b/R/ro_gt_theme.R @@ -7,12 +7,13 @@ #' @importFrom gt gt tab_options cols_align sub_missing opt_row_striping #' tab_style cells_title cell_text cells_column_labels cells_row_groups #' cells_body everything where px -#' @importFrom systemfonts system_fonts +#' @importFrom ROvis.utils ro_check_if_font_available #' @importFrom ROvis.utils ro_color #' #' @param df Dataframe. #' @param ROfont Default is FALSE, will change to TRUE for next major release (1.0.0). -#' If Rijksoverheid font is not available on your device, Verdana will be used. +#' If Rijksoverheid font is not available on your device, Verdana is used, then Arial, +#' then the first font found on your device, whichever is installed first. #' @param ... For internal use. Leave empty. #' @family gt #' @return gt table object. @@ -27,13 +28,15 @@ ro_gt_theme <- function(df, ROfont = FALSE, ...) { check_data_frame(df) check_bool(ROfont) - if ( - ROfont && - "RijksoverheidSansWebText" %in% system_fonts()$family - ) { - font <- "RijksoverheidSansWebText" + # RO font usage is opt-in until it becomes the default in the next major + # release (1.0.0), so don't surface RO-font-specific messaging until then. + if (ROfont) { + font <- ro_check_if_font_available("RijksoverheidSansWebText") } else { - font <- "Verdana" + font <- suppressMessages(ro_check_if_font_available("RijksoverheidSansWebText")) + if (identical(font, "RijksoverheidSansWebText")) { + font <- "Verdana" + } } gt(df, ...) |> diff --git a/man/ro_dt_theme.Rd b/man/ro_dt_theme.Rd index 2b96c91..32f4d49 100644 --- a/man/ro_dt_theme.Rd +++ b/man/ro_dt_theme.Rd @@ -7,7 +7,9 @@ ro_dt_theme(base_family = "RijksoverheidSansWebText", sorting = FALSE) } \arguments{ -\item{base_family}{Character. Font family to use for table text. Default is "Arial".} +\item{base_family}{Character. Font family to use for table text. Default is +"RijksoverheidSansWebText". If not installed, Verdana is used, then Arial, +then the first font found on your device, whichever is installed first.} \item{sorting}{Logical. If \code{TRUE}, column headers are left-aligned; if \code{FALSE}, right-aligned. Default is \code{FALSE}.} @@ -24,7 +26,7 @@ following preliminary Rijksoverheid huisstijl. \examples{ if (interactive()) { ui <- fluidPage( - ro_dt_theme(base_family = "Arial", sorting = FALSE), + ro_dt_theme(sorting = FALSE), DTOutput("mytable") ) server <- function(input, output, session) { diff --git a/man/ro_gt_theme.Rd b/man/ro_gt_theme.Rd index c60826f..904755c 100644 --- a/man/ro_gt_theme.Rd +++ b/man/ro_gt_theme.Rd @@ -10,7 +10,8 @@ ro_gt_theme(df, ROfont = FALSE, ...) \item{df}{Dataframe.} \item{ROfont}{Default is FALSE, will change to TRUE for next major release (1.0.0). -If Rijksoverheid font is not available on your device, Verdana will be used.} +If Rijksoverheid font is not available on your device, Verdana is used, then Arial, +then the first font found on your device, whichever is installed first.} \item{...}{For internal use. Leave empty.} } diff --git a/tests/testthat/test-ro_dt_theme.R b/tests/testthat/test-ro_dt_theme.R new file mode 100644 index 0000000..a2fb769 --- /dev/null +++ b/tests/testthat/test-ro_dt_theme.R @@ -0,0 +1,14 @@ +test_that("ro_dt_theme applies the font resolved by ro_check_if_font_available", { + local_mocked_bindings( + ro_check_if_font_available = function(target_font_family) "Verdana" + ) + css <- ro_dt_theme()$children[[1]]$children[[1]] + expect_true(grepl("Verdana", css, fixed = TRUE)) +}) + +test_that("ro_dt_theme errors for an unavailable custom font", { + expect_error( + ro_dt_theme(base_family = "Not an installed font"), + "Can't find" + ) +}) diff --git a/tests/testthat/test-ro_gt_theme.R b/tests/testthat/test-ro_gt_theme.R index 1a7bfcc..8d38dc2 100644 --- a/tests/testthat/test-ro_gt_theme.R +++ b/tests/testthat/test-ro_gt_theme.R @@ -15,3 +15,32 @@ gt_object_test <- mtcars |> test_that("output is a gt object", { expect_identical(class(gt_object_test)[1], "gt_tbl") }) + +test_that("ro_gt_theme stays quiet about font resolution by default", { + local_mocked_bindings( + ro_check_if_font_available = function(base_family) { + message("mocked font message") + "Verdana" + } + ) + expect_no_message(mtcars |> ro_gt_theme()) +}) + +test_that("ro_gt_theme surfaces the font message when ROfont = TRUE", { + local_mocked_bindings( + ro_check_if_font_available = function(base_family) { + message("mocked font message") + "Verdana" + } + ) + expect_message(mtcars |> ro_gt_theme(ROfont = TRUE), "mocked font message") +}) + +test_that("ro_gt_theme does not use the RO font when ROfont = FALSE, even if installed", { + local_mocked_bindings( + ro_check_if_font_available = function(base_family) "RijksoverheidSansWebText" + ) + html <- mtcars |> ro_gt_theme() |> gt::as_raw_html() + expect_true(grepl("Verdana", html, fixed = TRUE)) + expect_false(grepl("RijksoverheidSansWebText", html, fixed = TRUE)) +})