3
votes

I'm using cairo_pdf() to generate a plot in R with nice fonts. It works fine if the font family I specify has basic weights named sensibly, e.g. "Arial" contains Arial Regular, Arial Bold, Arial Italic, Arial Bold Italic.

But is there a way to arbitrarily set the font for a given style? For example, in Hoefler Text I have Hoefler Text Black not Bold. Can I get cairo_pdf to use this weight for bold text?

1
At the moment the call: names(X11Fonts()) does not bring up 'Hoefler Text' on my machine, but it's certainly possible that your machine is different. Why don't you review ?cairo_pdf and ?X11Fonts and then provide sufficient specifics so we can offer advice? I will hold off voting to close as "unclear" but only for a few hours. After that I will both downvote and vote to close.IRTFM
I do have Hoefler Text installed on my machine and I have read the documentation. cairo_pdf takes the single name of a family and then infers which weight is 'bold'; the documentation doesn't say how to explicitly set the preferred font for 'bold' in the same way that CairoFonts does for example.jkeirstead

1 Answers

3
votes

I'm probably using a different OS than you but I'm wondering if there's a chance that this will succeed on a Linux box, although it doesn't succeed on my Mac. The font styles were determined with Mac-specific application, FontBook.app, but I imagine that you have fontforge or similar access to a way to determine needed names of styles.:

 Cairo::CairoFonts(    # for some reason I seem to need the `pkgname::` format
    regular="Hoefler Text:style=Regular",
    bold="Hoefler Text:style=Black",
    italic="Hoefler Text:style=Italic",
    bolditalic="Hoefler Text:style=Black Italic",
    symbol="Symbol"
 )
Cairo::CairoFontMatch("Hoefler Text:style=Black",verbose=TRUE)
cairo_pdf("test_bold.pdf", family="Hoefler Text") 
     plot(1,1, main=bquote(bold(test~fl~ligature)) )
dev.off()

The output on my machine appears to be in the 'Hoefler Text' family, but the style does not appear to be any different that the non-bold call. I thinking a Linux machine might have better luck.

Update: I used one of the "online font converters" found on an SO search to extract the 'Hoefler Text.ttc' (a TrueType collection) fonts into its components. I moved them into the /Library/Fonts/ directory. I looked at the output from Cairo::CairoFontMatch("HoeflerText:style=Black",verbose=TRUE) and saw that there it was seeing the ttc file so I moved the original ttc file to a non-Font directory and restarted R. Now when I run:

cairo_pdf("test_bold.pdf", family="HoeflerText")
  plot(1,1, main=bquote(bold(test~fl~ligature)) ); dev.off() 

... and view in my PDF viewer I see a bold font as title. I am not sure if this is actually a result of a) remapping the fonts by R or if b) the viewer is now doing the proper lookup and had not been given the correct name by the cairo_pdf device. At any rate, I am now able to do the correct lookup:

> Cairo::CairoFontMatch("HoeflerText:style=Black",verbose=TRUE)
1. family: "Hoefler Text", style: "Black", file: "/Library/Fonts/HoeflerText-Black.ttf"
   "Hoefler Text-12:familylang=en:style=Black,黑體,Sort,Sehr fett,Lihava,Noir,Nero,ブラック,두껍게,Zwart,Svart,Preto,Насыщенный жирный,Extrafet,黑体,Negra:stylelang=en,zh-tw,da,de,fi,fr,it,ja,ko,nl,no,pt,ru,sv,zh-cn,es:fullname=Hoefler Text Black:fullnamelang=en:slant=0:weight=0:width=100:pixelsize=12.5:foundry=unknown:hintstyle=3:hinting=True:verticallayout=False:autohint=False:globaladvance=True:file=/Library/Fonts/HoeflerText-Black.ttf:index=0:outline=True:scalable=True:dpi=75:scale=1:charset=  |>^1!|>^1!P0oWQ U>w0A|>^1!|>^1!!!!%#lm6Uq{tu}/lr9j#{}!CB!!OoV!/9m> !%J<G!!!)$&olY4 !^^7$   9;*f! !!!.%!!#&|!!!&S    !!!!# !!!1&|>^1!|>^1!|>^1!!)Ib~9R6#i(){NjxJemz#%jg%!!#0GM>RAd#y#fx!!!!5s<..R!!%U3!!![t  !!#3H !!!!n      !!#6I;sG44!!!?+!!!%#!!!!X    !!#AL      !!!1& !!+lx       P0oWR!!+u{!!!#/       !!+~~  !9PX<!!!!#    :lang=aa|av|ay|az-az|ba|be|bg|bi|br|bs|bua|ca|ce|ch|chm|co|cs|cv|da|de|en|es|et|eu|fi|fj|fo|fr|fur|fy|gd|gl|gv|ho|hr|hu|ia|id|ie|ik|io|is|it|kaa|kk|kum|kv|ky|lb|lez|lt|lv|mg|mh|mk|mo|mt|nb|nds|nl|nn|no|nr|nso|oc|om|os|pl|pt|rm|ro|ru|sel|sh|sk|sl|sma|smj|so|sq|sr|ss|st|sv|sw|tg|tk|tl|tn|tr|ts|tt|tyv|uk|uz|vo|vot|wa|wen|xh|yap|zu|an|crh|csb|fil|hsb|ht|jv|kj|ku-tr|kwm|li|mn-mn|ms|ng|pap-an|pap-aw|rn|rw|sc|sg|sn|su|za:fontversion=65536:capability=otlayout\:DFLT otlayout\:cyrl otlayout\:grek otlayout\:latn:fontformat=TrueType:embeddedbitmap=True:decorative=False:namelang=en:prgname=R:hash=sha256\:6ad00ff0335de002e10e0dc0aca8b6282ba3425c3b2f8ce3f0a6fea668d0a711:postscriptname=HoeflerText-Black"