Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a specific font family for use in an R package?

I need to ship a package with graphs and the client insists on using its own licensed font. The graphs have to be rendered as EPS and sent to the layout people for the reports.

I have the .ttf files for the font, with names like this :

  • FontNameSans-Bold.ttf
  • FontNameSans-Medium.ttf
  • FontNameSerif-Bold.ttf
  • FontNameSerif-Regular.ttf
  • ...

I also have .eot, .svg and .woff versions of these files.

So I tried the extrafont package :

ttf_import('Path/To/ttfFiles')
fonts()
[1] "FontNameSans-Bold"     "FontNameSans-Light"   
[3] "FontNameSans-Medium"   "FontNameSans-Regular" 
[5] "FontNameSerif-Bold"    "FontNameSerif-Light"  
[7] "FontNameSerif-Medium"  "FontNameSerif-Regular"

I expected to see two families, but that obviously didn't work.

In the end, the ttf files should be included in a subfolder of the inst folder and I should have something like:

.onLoad({
  font_import(file.path(system.file(package = 'mypackage'),
                        "fontdir")
  )
  # something else to create a family FontName for 
  # use with postscript() or cairo_ps()
  loadfonts()
})

So I can then later do:

setEPS()
postscript('somefile.eps', family = 'FontName Sans')
plot(mydata)
dev.off()
embed_fonts('somefile.eps', outfile = 'somefile-embedded.eps')

Question : What extra steps do I have to take in order to make the families available just like any other family?

EDIT: I am aware that I have to load the fonts before I can use them. But I can't load the family if it's registered wrong. The question is about registering the font family correctly so the rest works as it should.

like image 849
Joris Meys Avatar asked Nov 08 '22 01:11

Joris Meys


1 Answers

This answer is a bit late and probably you don't need it anymore. But this might be of interest for other users on the search.

Depending on your operating system you need to perform different steps. I have a package that includes several fonts in inst/extdata/fonts/some-fonts.ttf.

  • On Linux systems one can copy those files when loading the pacakge to the fonts directory.
  • On Windows one needs to copy the .ttf files manually to C:/Windows/Fonts.

To load the fonts when the package is loaded I adapted the .onLoad function:

.onLoad <- function(libname, pkgname) {
  if (Sys.info()[1] == "Linux") {
    dir.create('~/.fonts')
    file.copy("inst/extdata/fonts/some-fonts.ttf", "~/.fonts")
    system('fc-cache -f ~/.fonts')
  }
  if (Sys.info()[1] == "Windows") {
    windowsFonts()
    extrafont::font_import(pattern = "some-fonts", prompt = FALSE)
    extrafont::loadfonts(device = "win")
    windowsFonts()
  }
  print(extrafont::fonts())
}
  • On Linux I only had to put the custom fonts in the fonts directory. Afterwards they were available to R.
  • On Windows you need to import them using extrafont::font_import and to register them using extrafont::loadfonts(device = "win").

Hope this helps you, others or my future self. ;-)

like image 122
symbolrush Avatar answered Nov 15 '22 07:11

symbolrush