Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

register_fonts() work in interactive session, but not inside a package #103

Open
alesvomacka opened this issue Mar 1, 2023 · 4 comments

Comments

@alesvomacka
Copy link

Hi,
I'm trying to include a custom font inside my package and register it on load using the register_fonts() function. I'm registering the font using this code:

systemfonts::register_font(name = "Fira-Sans",
                             plain = system.file("fonts", "FiraSans-Regular.ttf", package = "avom"),
                             bold = system.file("fonts", "FiraSans-Bold.ttf", package = "avom"),
                             bolditalic = system.file("fonts", "FiraSans-BoldItalic.ttf", package = "avom"),
                             italic = system.file("fonts", "FiraSans-Italic.ttf", package = "avom"))

In an interactive session, this works flawlessly, e.g. running this works:

ggplot2::ggplot(data = mtcars,
                            mapping = ggplot2::aes(x = mpg, y = hp)) +
          ggplot2::geom_point() +
          theme(text = ggplot2::element_text(family = "Fira-Sans"))

However, when I try using the above mentioned snippet in .onLoad(), the font won't import properly:

.onLoad <- function(libname, pkgname) {
  systemfonts::register_font(name = "Fira-Sans",
                             plain = system.file("fonts", "FiraSans-Regular.ttf", package = "avom"),
                             bold = system.file("fonts", "FiraSans-Bold.ttf", package = "avom"),
                             bolditalic = system.file("fonts", "FiraSans-BoldItalic.ttf", package = "avom"),
                             italic = system.file("fonts", "FiraSans-Italic.ttf", package = "avom"))
}

This leads to the following error when trying to run devtools::check(), once the check tries to run example code:

Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) : font family 'Fira-Sans' not found in PostScript font database

I would be grateful for any advice. Thanks!

Session info:

R version 4.2.2 (2022-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.2

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] devtools_2.4.5 usethis_2.1.6  ggplot2_3.4.0 

loaded via a namespace (and not attached):
 [1] tidyselect_1.2.0  remotes_2.4.2     purrr_1.0.1       colorspace_2.1-0  vctrs_0.5.2      
 [6] generics_0.1.3    miniUI_0.1.1.1    htmltools_0.5.4   utf8_1.2.2        rlang_1.0.6      
[11] pkgbuild_1.4.0    pillar_1.8.1      later_1.3.0       urlchecker_1.0.1  glue_1.6.2       
[16] withr_2.5.0       DBI_1.1.3         sessioninfo_1.2.2 lifecycle_1.0.3   stringr_1.5.0    
[21] munsell_0.5.0     gtable_0.3.1      htmlwidgets_1.6.1 memoise_2.0.1     callr_3.7.3      
[26] fastmap_1.1.0     httpuv_1.6.8      ps_1.7.2          fansi_1.0.4       Rcpp_1.0.10      
[31] xtable_1.8-4      scales_1.2.1      promises_1.2.0.1  cachem_1.0.6      pkgload_1.3.2    
[36] mime_0.12         fs_1.6.0          digest_0.6.31     stringi_1.7.12    processx_3.8.0   
[41] dplyr_1.0.10      shiny_1.7.4       grid_4.2.2        cli_3.6.0         tools_4.2.2      
[46] magrittr_2.0.3    tibble_3.1.8      profvis_0.3.7     crayon_1.5.2      pkgconfig_2.0.3  
[51] ellipsis_0.3.2    prettyunits_1.1.1 assertthat_0.2.1  rstudioapi_0.14   R6_2.5.1         
[56] compiler_4.2.2  
@jimjam-slam
Copy link

jimjam-slam commented Mar 22, 2023

I'm having a similar problem with my themes360info package, in which I previously used register_variant to alias xisting system fonts but am now (in the feature-libreonly branch) testing using register_font to bundle a font with the package (they're stored in inst/extdata).

The .onLoad, in zzz.R, now looks like this:

onLoad <- function(libname, pkgname) {

  systemfonts::register_font(
    name = "Headline 360info",
    plain = system.file("extdata", "LibreFranklin-Black.ttf",
      package = "themes360info"))

  systemfonts::register_font(
    name = "Subhead 360info",
    plain = system.file("extdata", "LibreFranklin-Bold.ttf",
      package = "themes360info"))

  systemfonts::register_font(
    name = "Body 360info",
    plain = system.file("extdata", "LibreFranklin-Light.ttf",
      package = "themes360info"), 
    italic = system.file("extdata", "LibreFranklin-LightItalic.ttf",
      package = "themes360info"),
    bold = system.file("extdata", "LibreFranklin-Bold.ttf",
      package = "themes360info"),
    bolditalic = system.file("extdata", "LibreFranklin-BoldItalic.ttf",
      package = "themes360info"))

}

When I remotes::install_github("360-info/themes360info@feature-libreonly") on my macOS system, the package installs fine and loads.

macOS Session Info > sessionInfo() R version 4.2.2 (2022-10-31) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Ventura 13.2.1

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] themes360info_0.0.3 forcats_0.5.2 stringr_1.5.0
[4] dplyr_1.0.10 purrr_1.0.1 readr_2.1.3
[7] tidyr_1.2.1 tibble_3.2.1 ggplot2_3.4.1
[10] tidyverse_1.3.2

loaded via a namespace (and not attached):
[1] httr_1.4.4 pkgload_1.3.1 jsonlite_1.8.3
[4] modelr_0.1.9 shiny_1.7.3 assertthat_0.2.1
[7] googlesheets4_1.0.1 cellranger_1.1.0 remotes_2.4.2
[10] sessioninfo_1.2.2 pillar_1.8.1 backports_1.4.1
[13] glue_1.6.2 digest_0.6.30 promises_1.2.0.1
[16] gridtext_0.1.5 rvest_1.0.3 colorspace_2.1-0
[19] htmltools_0.5.3 httpuv_1.6.6 pkgconfig_2.0.3
[22] devtools_2.4.5 broom_1.0.1 haven_2.5.1
[25] magick_2.7.4 xtable_1.8-4 patchwork_1.1.2
[28] scales_1.2.1 processx_3.8.0 svglite_2.1.1
[31] later_1.3.0 tzdb_0.3.0 googledrive_2.0.0
[34] generics_0.1.3 farver_2.1.1 usethis_2.1.6
[37] ellipsis_0.3.2 cachem_1.0.6 withr_2.5.0
[40] cli_3.6.0 magrittr_2.0.3 crayon_1.5.2
[43] readxl_1.4.1 mime_0.12 memoise_2.0.1
[46] ggtext_0.1.2 ps_1.7.2 fs_1.5.2
[49] fansi_1.0.4 xml2_1.3.3 pkgbuild_1.3.1
[52] profvis_0.3.7 tools_4.2.2 prettyunits_1.1.1
[55] hms_1.1.2 gargle_1.2.1 lifecycle_1.0.3
[58] munsell_0.5.0 reprex_2.0.2 callr_3.7.3
[61] compiler_4.2.2 systemfonts_1.0.4 rlang_1.1.0
[64] grid_4.2.2 htmlwidgets_1.5.4 miniUI_0.1.1.1
[67] labeling_0.4.2 gtable_0.3.3 DBI_1.1.3
[70] R6_2.5.1 lubridate_1.8.0 fastmap_1.1.0
[73] utf8_1.2.3 stringi_1.7.12 Rcpp_1.0.10
[76] vctrs_0.6.0 urlchecker_1.0.1 dbplyr_2.2.1
[79] tidyselect_1.2.0

But plots give me:

> ggplot(mtcars) + aes(mpg, hp) + geom_point() + labs(title = "Blah") + theme_360()

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found
In addition: There were 16 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  no font could be found for family "Headline 360info"
2: In grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) :
  no font could be found for family "Headline 360info"

Or the same if I use ggplot(mtcars) + aes(mpg, hp) + geom_point() + labs(title = "Blah") + theme_minimal(18, "Body 360info"). I can verify that the fonts are present in systemfonts::registry_fonts() when the package is loaded:

> systemfonts::registry_fonts()
# A tibble: 12 × 7
   path                                 index family style weight italic featu…¹
   <chr>                                <int> <chr>  <chr> <ord>  <lgl>  <list> 
 1 /Library/Frameworks/R.framework/Ver…     0 Body … Regu… normal FALSE  <int>  
 2 /Library/Frameworks/R.framework/Ver…     0 Body … Bold  bold   FALSE  <int>  
 3 /Library/Frameworks/R.framework/Ver…     0 Body … Ital… normal TRUE   <int>  
 4 /Library/Frameworks/R.framework/Ver…     0 Body … Bold… bold   TRUE   <int>  
 5 /Library/Frameworks/R.framework/Ver…     0 Subhe… Regu… normal FALSE  <int>  
 6 /Library/Frameworks/R.framework/Ver…     0 Subhe… Bold  bold   FALSE  <int>  
 7 /Library/Frameworks/R.framework/Ver…     0 Subhe… Ital… normal TRUE   <int>  
 8 /Library/Frameworks/R.framework/Ver…     0 Subhe… Bold… bold   TRUE   <int>  
 9 /Library/Frameworks/R.framework/Ver…     0 Headl… Regu… normal FALSE  <int>  
10 /Library/Frameworks/R.framework/Ver…     0 Headl… Bold  bold   FALSE  <int>  
11 /Library/Frameworks/R.framework/Ver…     0 Headl… Ital… normal TRUE   <int>  
12 /Library/Frameworks/R.framework/Ver…     0 Headl… Bold… bold   TRUE   <int>  
# … with abbreviated variable name ¹​features

> systemfonts::registry_fonts()$family
 [1] "Body 360info"     "Body 360info"     "Body 360info"     "Body 360info"    
 [5] "Subhead 360info"  "Subhead 360info"  "Subhead 360info"  "Subhead 360info" 
 [9] "Headline 360info" "Headline 360info" "Headline 360info" "Headline 360info"

I've also tested in a rocker container based on r-ver:4.2. There, I don't get errors, but I do get a fallback font that is not Libre Franklin:

ggplot(mtcars) + aes(mpg, hp) + geom_point() + theme_minimal(18, "Headline 360info")

image

@thomasp85
Copy link
Member

@alesvomacka It seems your issue is not with the font registry as such but with the check functionality not using ragg/svglite and thus not being able to find the registered fonts (if you inspect the error message you'll see that it mentions postscript font database)

I'm afraid there is not much I can do about how postscript looks for fonts

@thomasp85
Copy link
Member

@jimjam-slam I think this is a different issue. Am I understanding it correctly that register_variant() works fine but not register_font(). If so, that is surprising because the former ends up calling the latter...

@jimjam-slam
Copy link

jimjam-slam commented Aug 17, 2023

@thomasp85 Apologies for leaving that last comment up—at some point it started working and I could no longer reproduce the problem 😅 All good now!

Oops, I was wrong—it works fine on a Linux dev container but doesn't work on macOS. I might need to do some more work to isolate the problem!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants