rstudio / thematic

Theme ggplot2, lattice, and base graphics based on a few simple settings.
https://rstudio.github.io/thematic/
Other
244 stars 10 forks source link

Font availability check sometimes does not work #78

Closed wch closed 3 years ago

wch commented 3 years ago

This is the code that checks whether a font is available: https://github.com/rstudio/thematic/blob/21256e257aff9d435dd1ad3606bea75a81c22574/R/hooks.R#L161-L180

However, it may not give an accurate answer, because R can render graphics asynchronously. For example, in a new R session in the terminal, run this code two times:

tryCatch({
    plot(1, family = "Righteous")
  },
  warning = function(e) {
    message("Caught a warning")
  }
)

This is the result:

> tryCatch({
+     plot(1, family = "Righteous")
+   },
+   warning = function(e) {
+     message("Caught a warning")
+   }
+ )
> 
There were 36 warnings (use warnings() to see them)
> tryCatch({
+     plot(1, family = "Righteous")
+   },
+   warning = function(e) {
+     message("Caught a warning")
+   }
+ )
Caught a warning

The first time, the warnings are not caught, but the second time, they are caught. (The logic here is roughly the same as the logic used in thematic.)

For testing, here's an example using grid which does the same thing:

library(grid)
tryCatch({
    grid.newpage()
    grid.text("hello world", 0.5, 0.5, gp=gpar(fontsize=40, fontfamily = "Righteous"))
  },
  warning = function(e) {
    message("got warning")
  }
)
cpsievert commented 3 years ago

I'm fairly sure this is only ever a problem if that code is run at the console when a graphics device isn't already open, and since this code is wrapped in attempt_with_device(dev_fun, code), that should never happen...

thematic:::attempt_with_device(
  grDevices::png,
  tryCatch({
    plot(1, family = "Righteous")
  },
  warning = function(e) {
    message("Caught a warning")
  }
  )
)
#> Caught a warning