Skip to content

Commit

Permalink
change: font check is done with theme settings
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgohel committed Feb 23, 2024
1 parent 00fd9c9 commit 2f8aba4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]"),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions R/fonts.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

23 changes: 20 additions & 3 deletions R/girafe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f8aba4

Please sign in to comment.