wch / extrafont

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

Font loads without problem but does not render #66

Open jimjam-slam opened 4 years ago

jimjam-slam commented 4 years ago

I'm having a recurring problem where I'm able to register and load a TrueType font, but the resulting plot falls back to Arial without any error or warning appearing.

I first had this problem with Montserrat (retrieved from google fonts). The problem persisted for several days, during which I:

Nothing appeared to fix the problem, but when I went back a couple of days later, Montserrat was suddenly working perfectly (unfortunately, I never worked out why Montserrat suddenly started working).

Now I've acquired Gotham Bold and Gotham Black, and I'm having the same problem. There are no warnings or errors; when I font_import() and loadfonts() it appears to register on all devices:

font_import(system.file('assets', 'fonts', package = 'climatecomms'), prompt = FALSE)
# Scanning ttf files in C:/Users/rensa/Code/climate-comms/inst/assets/fonts ...
# Extracting .afm files from .ttf files...
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Gotham-Black.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Gotham-Black
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Gotham-BlackItalic.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Gotham-BlackItalic
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Gotham-Bold.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Gotham-Bold
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Gotham-BoldItalic.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Gotham-BoldItalic
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\helvetica-neue-ltpro-bdcn.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/helvetica-neue-ltpro-bdcn
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\helvetica-neue-ltpro-ltcn.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/helvetica-neue-ltpro-ltcn
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Montserrat-Bold.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Montserrat-Bold
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Montserrat-BoldItalic.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Montserrat-BoldItalic
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Montserrat-Regular.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Montserrat-Regular
# C:\Users\rensa\Code\climate-comms\inst\assets\fonts\Montserrat-RegularItalic.ttf => C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics/Montserrat-RegularItalic
# Found FontName for 10 fonts.
# Scanning afm files in C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/metrics
# Writing font table in C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/fontmap/fonttable.csv
# Writing Fontmap to C:/Users/rensa/Documents/R/win-library/3.6/extrafontdb/fontmap/Fontmap...

loadfonts()
# Registering font with R using windowsFonts(): Gotham Black
# Registering font with R using windowsFonts(): Gotham Bold
# Registering font with R using windowsFonts(): HelveticaNeueLT Pro 57 Cn
# Registering font with R using windowsFonts(): HelveticaNeueLT Pro 47 LtCn
# Registering font with R using windowsFonts(): Montserrat
# Registering font with R using pdfFont(): Gotham Black
# Registering font with R using pdfFont(): Gotham Bold
# Registering font with R using pdfFont(): HelveticaNeueLT Pro 47 LtCn
# No regular (non-bold, non-italic) version of HelveticaNeueLT Pro 57 Cn. Skipping setup for this font.
# Registering font with R using pdfFont(): Montserrat
# Registering font with R using postscriptFont(): Gotham Black
# Registering font with R using postscriptFont(): Gotham Bold
# Registering font with R using postscriptFont(): HelveticaNeueLT Pro 47 LtCn
# No regular (non-bold, non-italic) version of HelveticaNeueLT Pro 57 Cn. Skipping setup for this font.
# Registering font with R using postscriptFont(): Montserrat

fonts()
# [1] "Gotham Black"                "Gotham Bold"                 "HelveticaNeueLT Pro 57 Cn"   "HelveticaNeueLT Pro 47 LtCn"
[5] "Montserrat" 

Everything looks good in fonttable() too. But I've tried test plots with both ggplto2 and base graphics using png(), pdf() and cairo_pdf() devices. Montserrat and my HelveticaNeue variants work perfectly, while Gotham Black and Gotham Bold default to Arial silently (note that they are read as separate font families but correctly show an Italic and a non-Italic variant for each):

ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_point() +
  labs(title = 'Oh hello there') +
  theme_grey(base_family = 'Montserrat')

image

ggplot(mtcars, aes(x = mpg, y = disp)) +
  geom_point() +
  labs(title = 'Oh hello there') +
  theme_grey(base_family = 'Gotham Black')

image

This is with extrafont 0.17.0.9000 (GitHub: wch/extrafont@227e6e3) and extrafontdb (1.0) on R 3.6.0 on Windows 10 1903. (Should I not be using the CRAN version of extrafontdb with the dev reelase of extrafont?)

ChrisWaloszek commented 4 years ago

I experienced a potentially similar problem with Lato fonts, also a common TTF, in a thesis written in R markdown.

Any included ggplots with had empty labels (no numbers/letters). Yet, compiling a ggplot with Lato worked well in R studio interface: Rstudio_Lato When saving the plot with ggsave, the font was not embedded and the graph had empty labels. Lato Light Plot

However, running embed_fonts("Lato Light Plot.pdf", outfile="Lato Light Plot - embed.pdf") worked well to embed the fonts afterward. Lato Light Plot - embed

Just as in the single ggplot graph, my thesis had empty labels. Yet, executing the embedding command (with the complete compiled thesis) again brought all the labels back, i.e. embed_fonts("thesis.pdf", outfile="thesis_embeded.pdf").

(FYI: The error message in the Rmd file was "`Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : font width unknown for character 0x20à"