6

I have a problem with typefaces and PDF-Output in R. On this windows machine there is no Helvetica and the Font used by the device seems to be Arial as you can see below. The simple problem is, that Arial is used (as I want it to be) but editing the PDF-File it says Helvetica is used. How can I get R to write the correct Name into the PDF-file. pdf(...,family="Arial") won't work, as this family is not known (grDevices version 2.15.1).

Or can I substitude this font in the PDF afterwards, creating a file with the font I want?

R-PDF-Output R Output: Arial Typeface

Comparision from this Article:Arial vs. Helvetica enter image description here

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
jakob-r
  • 6,824
  • 3
  • 29
  • 47
  • 1
    Concerning your final question: No, you cannot substitute fonts afterwards, at least not easily. Each font has its individual character widths, and as PDF is not a reflowing format, you would get something at least very unpretty if you simply switched fonts afterwards. That, BTW, also is the reason why you generally do need the fonts (or at least their respective metrics, i.e. char widths) in any process which generates PDF. That being said there are the standard 14 fonts for which most data does not need to be included in a generated PDF. Still, for proper layouting, the metrics are needed. – mkl Oct 24 '12 at 11:14
  • I think with substitution from Helvetica to Arial I will be ok with the ugliness, as they have quite similar character widths and especially as Plots don't have any running texts. – jakob-r Oct 24 '12 at 17:59

4 Answers4

5

Since R-3.1.0, this is much more straightforward. Now, to get an Arial font, just set family="ArialMT":

pdf("Arial.pdf", height=0.3, width=1.45, family="ArialMT")
grid::grid.text("CGJQRSafrst1237")
dev.off()

Quoting from the April 2014 R-3.1.0 release notes:

There is a new family, "ArialMT", for the pdf() and postscript() devices. This will only be rendered correctly on viewers which have access to Monotype TrueType fonts (which are sometimes requested by journals).

To ensure that the pdf will be rendered correctly wherever it's viewed, you'll need to also embed the required Arial fonts in the document. Here's one easy way to do that:

library(extrafont)
loadfonts()  ## Really only needed the first time you use extrafont
## Modify this to point to the corresponding Ghostscript executable on your own machine
Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.07/bin/gswin64c.exe")
embed_fonts("Arial.pdf")
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
2

You need to set a new font family for use with pdf(). This requires you to have Adobe Font Metric files (*.afm files) for the fonts you wish to use. You can convert the .tty files to .afm ones or find .afm files for Arial on the interweb if you don't already have them.

Arial <- Type1Font(family = "Arial",
                   metrics = c("ArialMT.afm",
                               "arial-BoldMT.afm", 
                               "Arial-ItalicMT.afm",
                               "Arial-BoldItalicMT.afm"))

where the character vector metrics contains the paths to the relevant .afm files, The files should be specified in this order:

  1. plain face
  2. bold face
  3. italic face
  4. bold-italic face

The you use the pdfFonts() function to add a mapping to these new fonts

pdfFonts(Arial = Arial)

where Arial is the object produced by Type1Font() earlier.

The final step is to use the family argument in pdf() which refers to one of the existing families as defined by pdfFonts():

pdf("testArial.pdf", family = "Arial")
plot(1:10, 1:10)
dev.off()

I haven't tried this as I don't have Arial on my system nor too many .afm files lying around, but I pieced this together from several sources:

  1. Paul Murrell and Brian Ripley (2006) Non-standard fonts in PostScript and PDF graphics. R News, 6(2):41-47. PDF
  2. A posting by David L. Carlson on the R mailing list from earlier this year.

An alternative depending on how your system is set-up is to a Cairo-based PDF device as that will use the functions of your system to identify and load fonts based simply on their name. See ?cairo_pdf and then the Cairo Fonts section of ?X11 for details.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
2

As it is stated in the one of the comments, base 14 fonts does'nt need to be embedded and Helvetica is one of these fonts.PDF consumers are supposed to provide replacements for such fonts and Arial (or Arial MT) is often used in place of Helvetica.

I'm not familiar with R but you seem to be able to embed fonts afterward

edit: This question's answer explains how to embed fonts afterward with ghostscript, just be sure to have GS map Helvetica (or Arial) to the version of the font you want. Thanks to Gavin Simpson for having me search for that :)

Community
  • 1
  • 1
user18428
  • 1,216
  • 11
  • 17
  • As I read the Question, the OP wants to use Arial directly when the PDF is generated, not embed this font afterwards. – Gavin Simpson Oct 24 '12 at 18:07
  • I read "Or can I substitude this font in the PDF afterwards, creating a file with the font I want?" ... – user18428 Oct 24 '12 at 20:08
  • Yes but embedding is not substituting - it just embeds the font so that the PDF can be viewed on a system without that font available. By substituting, the OP thought they could change the PDF to use Arial after it was created using Helvetica. I don't even think embedding works if the pdf doesn't use the font, does it? – Gavin Simpson Oct 24 '12 at 20:19
  • I agree that substitution is not embedding. As I said i'm not familiar with R but the function i pointed seems to rely on Ghostscript which will in fact embed the font (i think by doing its usual PDF->PS->PDF process). GS rely on an URW set of font for the 'base 14' and even has a configurable fontmap so one could end up embedding its prefered Helvetica font. – user18428 Oct 24 '12 at 20:43
1

R by default does not embed fonts. It just specifies that a Helvetica is to be used. You're probably best off embedding fonts. There's two ways to do that, either use the cairo_pdf device or the extrafont package with the usual pdf device. cairo_pdf is a lot simpler, but the output differs in subtle ways from pdf.

otsaw
  • 1,034
  • 8
  • 13
  • There is also native `embedFonts()`, which uses **ghostscript** to do the embedding. This embedding will only embed the fonts used though won't it? As R has not been told to use Arial how can that font be embedded. `cairo_pdf()` is a useful solution but it will *use* as well as *embed* the stated font. – Gavin Simpson Oct 24 '12 at 18:09
  • @Gavin: Not embedding fonts intended to be used is looking for trouble. I assumed OP wants to both specify to be used and embed (even though he didn't use the word), which is what both `cairo_pdf` and `extrafont` do. `extrafont` just simplifies your answer followed by embedding with ghostscript. – otsaw Oct 25 '12 at 08:41
  • +1 this is useful info - anything to simplify all that messing with `.afm` files is to be appreciated. – Gavin Simpson Oct 25 '12 at 08:50