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

Better rendering warnings #80

Closed nstrayer closed 3 years ago

nstrayer commented 3 years ago

Previously if you tried to use thematic to render a plot with a google font but were not using a ragg device and didnt have showtext installed, the plot would fail for ggplot2 and simply have no text for base graphics.

This is because the logic to warn about not having a package installed only was triggered if both ragg and showtext were not installed. If the user has ragg installed but isnt using it, e.g. plotting to the viewer pane or running shiny without setting options("shiny.useragg" = TRUE), the warning gave no hint as to why it was not working.

Additionally, now the font is just set back to default instead of failing in these scenarios. This means fewer catastrophic failures and the new warning message doesn't get buried by a bunch of grid warnings from trying to render the non-existent font.

Example

You can recreate this behavior by running

remove.packages('showtext')
library(thematic)
library(ggplot2)
thematic_on(font = "Righteous")
ggplot() + labs(title = "Custom font title")

Which will fail with the error

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found
In addition: There were 50 or more warnings (use warnings() to see the first 50)

But work when showtext is reinstalled.

With this PR the result is:

Warning message:
It seems the current graphics device 'RStudioGD' is unable to render the requested font family 'Righteous'. To render custom fonts, either use a ragg device or install the showtext package 

With the resulting plot of image

Unit tests

Based on conversations with @wch , we don't think it's possible to write unit tests that fit in the normal workflow for this so there are none.

Relevant issues

This is related to https://github.com/rstudio/thematic/issues/78 and https://github.com/rstudio/thematic/issues/77