showtext icon indicating copy to clipboard operation
showtext copied to clipboard

Will font_install() support loading in local font files?

Open trangdata opened this issue 2 years ago • 1 comments

Hi @yixuan thank you for this beautiful package! 🚀 🌻

I have a quick question regarding showtextdb::font_install(). Right now, it seems that it only supports installing fonts from an online URL. I wonder if, in the future, I will be able to use font_install() to install fonts from a local file.

Ultimately, my goal is to include private font files in a package. Here's a related SO post, but I much prefer showtext. I'm hoping I could include all the font install/registration/import in the .onload() function (e.g., with sysfonts::font_add()).

Thanks so much for your help! 🌵

trangdata avatar Nov 16 '21 19:11 trangdata

So I found a (temporary?) solution for my problem, but please let me know if this approach is not advisable. In my R/zzz.R file:

.onLoad <- function(libname, pkgname){
  font_dir <- system.file("fonts", package = "my_pkg")
  sysfonts::font_paths(file.path(font_dir, "NW"))

  sysfonts::font_add(
    "NW",
    regular = "NW-Regular.ttf",
    italic = "NW-Italic.ttf",
    bold = "NW-Bold.ttf"
  )

  showtext::showtext_auto()
}

.onUnload <- function(libpath) {
  showtext::showtext_auto(FALSE)
}


trangdata avatar Nov 16 '21 23:11 trangdata

Hi @trangdata, your solution is a good one. font_install() does nothing but put files into the fonts folder of the showtextdb package, and let showtextdb automatically load these fonts via font_add(). So in some sense font_add() is the ultimate solution.

But if you want to make use of the existing loading mechanism, there is one (undocumented) way. You can create a folder, e.g., myfont, under the fonts directory of the showtextdb package, and then put files regular.ttf/otf, bold.ttf/otf etc. under myfont. Then the next time you load showtext, it will register the "myfont" family using the files you provide. This is how font_install() actually works.

yixuan avatar Oct 10 '22 02:10 yixuan

Thank you for this explanation @yixuan. That makes sense. Because I wanted to distribute the font files within my package's inst, my user would have to be the one to create the folder under the fonts directory of the showtextdb package, so I think sticking with my previous solution would be easiest so that the user doesn't have to perform these manual steps.

trangdata avatar Oct 10 '22 14:10 trangdata