Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TTF/OTF font selection in R for windows: onscreen vs pdf()

I know that in R on Linux or Mac, fonts are consistently defined as an argument family="Charis SIL" to par(), text(), or one of the graphic device functions like tiff(), svg(), etc (substitute "Charis SIL" with whatever font name you want). I also know that on Windows, that only works for the cairo_pdf() and svg() devices; raster graphic devices like jpeg(), tiff(), png(), and bmp() require that the font be mapped in the "Windows font database" first:

# this doesn't work on windows
jpeg(filename='test1.jpg', family='Charis SIL')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
dev.off()
# (gives warnings: Font family not found in Windows font database)  

# this does work on windows (assuming you have the Charis SIL font installed)
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
jpeg(filename='test2.jpg', family='myCustomWindowsFontName')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
dev.off()

The pdf() device is different still: it seems to need fonts defined in either the postscriptFonts() and/or pdfFonts() database, which means only Type1 fonts:

# this doesn't work on windows
pdf('test.pdf', family='Charis SIL')
# gives error: Unknown family "Charis SIL"

# this doesn't work either
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
pdf('test.pdf', family='myCustomWindowsFontName')
# gives error: Unknown family "myCustomWindowsFontName"

# this also won't work
pdf.options(family='Charis SIL')
pdf('test.pdf')
# gives error: Invalid font type
# also gives warning: font family "Charis SIL" not found in Postscript font database

Ordinarily this would not matter, because cairo_pdf() is a fine substitute for the pdf() device and handles TTF and OTF fonts just fine. The problem is that if a user plots to the onscreen device and then uses the menu commands to save as PDF, it appears to call pdf() instead of cairo_pdf(), which then throws errors:

# this part works
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
par(family='myCustomWindowsFontName')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')

# but menu command "File > Save As > PDF" gives errors:
# Error: Invalid font type
# Warning: font family "Charis SIL" not found in Postscript font database

This is a problem because the R package I'm developing keeps failing "R CMD check" on Windows, apparently because the example code generates on-screen output that gets saved out as PDF automatically, which generates the above-mentioned errors. One solution is to give up custom fonts for the on-screen devices in Windows (i.e., just ignore the "family" argument if the chosen output is "screen"). Another option is to use the Cairo() package for onscreen plotting, but I'd prefer to stick with base graphics if I can. Is there any way I can get custom fonts in an onscreen plot and not have it throw errors when using the "save as PDF" menu commands?

like image 239
drammock Avatar asked Nov 03 '22 16:11

drammock


1 Answers

The way I eventually solved this was as follows:

oldSans <- windowsFonts()$sans
windowsFonts(sans=windowsFont('Charis SIL'))
par(family='sans') # this line isn't necessary anymore
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
windowsFonts(sans=oldSans)

In this way, the correct font will be used in the on-screen window, and when the user uses the menu commands to save as PDF, the PDF will get saved out but with the default sans font instead of the custom one. It is a "solution" only in the sense that the PDF does get exported, but if the plot has non-ASCII glyphs there is no guarantee that they'll show up in a PDF generated in this way. It is also arguably WORSE than the original situation, because the action no longer throws an error or even a warning. The moral of the story: don't rely on the menu commands in your GUI to do things that you should know how to do in the console.

like image 80
drammock Avatar answered Nov 10 '22 06:11

drammock