wch / extrafont

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

Bug fix in type1.r #95

Closed tstenborg closed 1 year ago

tstenborg commented 1 year ago

Extrafont: Bug + Suggested Fix

Here are details of a bug in extrafont (issue #94), along with a suggested fix.

Summary: incorrect regex.

Location: lines 30-31, type1_import function, in type1.r.

Details: The statement spanning lines 30 and 31 of the type1_import function in type1.r is intended to trim .pfb and .pfa file extensions. The sub function used in that statement expects regex, but an OS command line style wildcard operator is passed instead (? acts as a quantifier, not wildcard in regex). The result is that the file extensions aren't trimmed, and the merge operation called soon after doesn't work as intended, typically (and undesirably) deleting all rows from the fontdata data frame. See below for an example:

Existing: sub("\.pf?$", "", basename("cmsyase.pfb")) Returns: "cmsyase.pfb"

Proposed: sub("\.pf.$", "", basename("cmsyase.pfb")) Returns: "cmsyase"

Fix: Changed one character. Details are given below.

Old statement: pfbdata <- data.frame(fontfile = pfbfile, base = sub("\.pf?$", "", basename(pfbfile)))

New statement: pfbdata <- data.frame(fontfile = pfbfile, base = sub("\.pf.$", "", basename(pfbfile)))

N.B. This fixes currently open issue #94.

wch commented 1 year ago

Thanks! I'm surprised this ever worked in the first place.

sysilviakim commented 1 year ago

Oh great!! This is why I kept getting the error message of

Scanning afm files in /usr/local/lib/R/site-library/fontcm/fonts/metrics

Error in $<-.data.frame(*tmp*, "package", value = "fontcm"): replacement has 1 row, data has 0 Traceback:

This is not yet reflected in the CRAN version, right?

Something similar is documented in https://pullanswer.com/questions/fontcm-font-cannot-be-installed-on-r-run-type. But I'm now wondering why it worked in the first place! Just documenting behavior (which may help someone): my fontcm installation went haywire in a renv environment and worked perfectly fine outside.