wch / extrafont

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

Seek replacement for `ttf2pt1` for extracting font metrics #92

Open jpgoldberg opened 2 years ago

jpgoldberg commented 2 years ago

I do not know how R handles fonts, but it appears that it only deals with Type1 postscript fonts for which we need a separate font metrics file, fontname.afm.

extrafont relies on ttf2pt1 to extract the afm. But it appears that ttf2pt1 is abandonware. (It certainly gives me a bunch of compiler warnings when built).

Possible options

From the extrafont source in truetype.r we find the comment

Previously, I tried using ttf2afm to do the afm extraction, but the afm files created by ttf2afm didn't work with R. The command for ttf2afm was: ttf2afm Impact.ttf -o Impact.afm

I am assuming that that is ttf2afm from pdftex (source) but I am assuming that that was about the one that comes in common TeX installations.

There is a Rust crate that uses https://docs.rs/ttf-parser/0.14.0/ttf_parser/index.html I do not know enough about the structure of ttf and or afm to see how to use that to generate an afm from a ttf.

sjewo commented 2 years ago

I did a quick test with ttf2afm from pdftex and on first sight it might work out. At the moment pdf export seems to function properly on linux systems (I tested this also on Mac OS, but I did not install ghostscript).

I patched truetype.r to use ttf2afm in my fork: https://github.com/sjewo/extrafont

You might need to install the binary first:

library(tinytex)
tlmgr_install("ttfutils")

Update:

pooranis commented 2 years ago

On a mac, ttf2pt1 installed with Homebrew (using the bottled version compiled by them) works. It is a drop-in replacement for the one in Rttf2pt1 - as in I copied the homebrew binary to the Rttf2pt1/exec/ R library folder, and then all the TTF files in my system were able to be imported (i.e. no segmentation fault and the Font Name was returned) except for a few. (With Rttf2pt1, only about half worked).