wch / extrafont

Tools for using fonts in R graphics
312 stars 49 forks source link

R 4.4.0 breaks some user-installed fonts in PDFs #101

Open gitrman opened 4 weeks ago

gitrman commented 4 weeks ago

Hi

I have found that upgrading from R 4.3.3 to R 4.4.0 causes certain fonts to no longer display properly in PDFs generated by converting R markdown code to PDF to using knitr. I have replicated this problem on Windows 10 and 11 machines.

Here's a working example:

A) Download and install the Open Sans collection of fonts from here:

https://fonts.google.com/specimen/Open+Sans

There are 36 ttf files in the collection.

B ) R markdown code

---
output:
  pdf_document:
    latex_engine: xelatex
    dev: "cairo_pdf"
---

```{r}
library(extrafont)
extrafont::font_import() 
library(ggplot2)

# Example plot
p <- ggplot(mtcars, aes(x=mpg, y = cyl)) +
  geom_point() +
  labs(title = "Main title test", subtitle = "Subtitle testing font")

# Use Open Sans Semi Condensed Light Font
p + theme_bw(base_family = "Open Sans SemiCondensed Light")

Figure Under R 4.3.3

When I Knit that code to PDF in R Studio, with the version of R set to R-4.3.3 under my Global Settings, the resulting image in the PDF looks like this

R433

The font in the title / subtitle / axis labels is correct (Open Sans SemiCondensed Light).

Figure Under R 4.4.0

Running exactly the same code with the version of R set to 4.4.0 in R studio produces the following plot:

R440

The font under 4.4.0 is the default sans serif for Windows (Arial), rather than Open Sans SemiCondensed Light.

Session Info

sessInfo

Other Info

p + theme_bw(base_family = "Calibri")

calibiri

In contrast, a different font I installed on the system myself (Econ Sans Condensed Bold Italic) looks like this under R 4.3.3:

p + theme_bw(base_family = "EconSansCndBolIta")

econsans 4 3 3

... but under R 4.4.0 the font face defaults back to Arial.

econsans 4 4 0

Interestingly, if the font is set to "Opens Sans", then the graph looks like this under both R 4.3.3 and R 4.4.0

p + theme_bw(base_family = "Open Sans")

Open Sans

So, even though I installed "Open Sans" myself graphs generated using that font look fine under both R 4.3.3 and R 4.4.0, whereas other fonts I installed myself ("Open Sans SemiCondensed Light" and "EconSansCndBolIta") work fine under 4.3.3 but fail under 4.4.0.