From 2f8aba48c78933bc0de4432866bc3a704274d1f2 Mon Sep 17 00:00:00 2001 From: David Gohel Date: Sat, 24 Feb 2024 00:43:15 +0100 Subject: [PATCH] change: font check is done with theme settings --- DESCRIPTION | 2 +- NEWS.md | 1 + R/fonts.R | 17 +++++++++++++++++ R/girafe.R | 23 ++++++++++++++++++++--- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a162da26..47dd7a70 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: ggiraph Type: Package Title: Make 'ggplot2' Graphics Interactive Description: Create interactive 'ggplot2' graphics using 'htmlwidgets'. -Version: 0.8.9.002 +Version: 0.8.9.003 Authors@R: c( person("David", "Gohel", role = c("aut", "cre"), email = "david.gohel@ardata.fr"), diff --git a/NEWS.md b/NEWS.md index bf06dc12..91368bfb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,7 @@ - adapt guides to ggplot '3.5.0' - deprecate ggiraph +- A font check is now done with theme settings (only when argument `ggobj` is used) # ggiraph 0.8.8 diff --git a/R/fonts.R b/R/fonts.R index 248fc90b..5025696c 100644 --- a/R/fonts.R +++ b/R/fonts.R @@ -111,3 +111,20 @@ fortify_font_db <- function(){ font_db <- rbind(db_sys, db_reg) font_db } + +list_theme_fonts <- function(gg) { + element_text_set <- Filter(f = function(x) inherits(x, "element_text") && !is.null(x$family), gg[["theme"]]) + fonts <- vapply( + X = element_text_set, + FUN = function(x) { + z <- x$family + if (isTRUE(all.equal(z, ""))) "sans" + else z + }, + FUN.VALUE = NA_character_, + USE.NAMES = FALSE + ) + fonts <- setdiff(unique(fonts), c("sans", "serif", "mono", "symbol")) + fonts +} + diff --git a/R/girafe.R b/R/girafe.R index 2d93ee40..72a8912e 100644 --- a/R/girafe.R +++ b/R/girafe.R @@ -119,13 +119,30 @@ girafe <- function( args$bg <- "#fffffffd" } + if (!is.null(ggobj)) { + if (!inherits(ggobj, "ggplot")) { + cli::cli_abort(c( + "{.code ggobj} must be a {.code ggplot2} object." + )) + } + + family_list <- list_theme_fonts(ggobj) + for (font in family_list) { + ffe <- font_family_exists(font) + if (!ffe) { + cli::cli_abort(c( + sprintf("Font family '%s' has not been found on your system or is not registered.", font), + "i" = "You can use a google font with {.code gdtools::register_gfont()}.", + "i" = "You can use any font with {.code systemfonts::register_font()}." + )) + } + } + } + devlength <- length(dev.list()) do.call(dsvg, args) tryCatch({ if (!is.null(ggobj)) { - if (!inherits(ggobj, "ggplot")) { - abort("`ggobj` must be a ggplot2 plot", call = NULL) - } print(ggobj) } else code