how to install macos system truetype collection fonts into R?

Viewed 121

Charter is a system font in macos. Fontbook tells me it is in the truetype collection /System/Library/Fonts/Supplemental/charter.ttc. Yet, for some reason, library extrafonts function findfonts() finds the ttf fonts in /System/Library/Fonts/Supplemental/, but seems to ignore ttc fonts. Is there an easy way to use these truetype collection fonts, too? pointers appreciated.

iaw


PS: Fortunately, in this particular case, I have a working alternative using the "Cairo" package to install the charter font from /usr/X11/lib/X11/fonts/Type1/c0648bt_.pfb. this still works with

library(Cairo)
CairoFonts(regular="Bitstream Charter")
1. family: "Bitstream Charter", style: "Regular", file: "/usr/X11/lib/X11/fonts/Type1/c0648bt_.pfb"
> CairoFonts(regular="Bitstream Charter")

and then all drawing will use the Charter fonts. However, this presumably will not work so easily with other fonts that are not in x11, too.

2 Answers

I've previously solved a similar issue by using sysfonts and showtext. See if this suits your use-case:

library(ggplot2)
#install.packages("sysfonts")
library(sysfonts)
#install.packages("showtext")
library(showtext)

font_add(family = "charter", regular = "/System/Library/Fonts/Supplemental/charter.ttc")
showtext_auto()
ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  ggtitle("Charter ttc Example") +
  theme(text = element_text(family = "charter", size = 20),
        axis.text = element_blank())

example_1.png

EDIT 1

The pdf device has restrictions on what font you can use: I believe it has to be one of the "PostScript font database" fonts (see https://www.rdocumentation.org/packages/grDevices/versions/3.6.2/topics/postscript).

To use the quartz device, you can change the font using par(), e.g.

TestChars <- function(...)
 {
   info = l10n_info()
   r <- c(32:126, 160:254)
   par(pty = "s")
   plot(c(-1,10), c(20,260), type = "n", xlab = "", ylab = "", xaxs = "i", yaxs = "i")
   grid(11, 24, lty = 1)
   mtext(paste("MBCS:", info$MBCS, "  UTF8:", info$`UTF-8`, "  Latin:", info$`Latin-1`))
   mtext(paste(LETTERS, collapse=" "), side = 3, padj = -2)
   mtext(paste(letters, collapse=" "), side = 3, padj = -4)
   for(i in r) try(points(i%%10, 10*i%/%10, pch = i, font = 5,...))
 }
par(family="Charter")
TestChars()

example_2.png

The cairo_pdf device can use other fonts, but it comes out bold (not sure why; see https://stackoverflow.com/a/38545416/12957340) and many of the special characters in the Charter font don't appear to be compatible, e.g.

cairo_pdf(file="test.pdf")
par(family="Charter")
TestChars()
dev.off()

example_3.png

I had a similar issue on Mac. I used the following code:

library(showtext)
font_add("Optima", "Optima.ttc")
showtext.auto()

Then I could use the font with:

family = "Optima"

Or this in the theme parameters to apply it to the whole graph:

base_family = "Optima" 
Related