0

I am using flextable, and would like to use my own defined font. Is this possible?

I looked at Is it possible to change flextable default font from arial that says it is, but am not sure what fontname are available.

I have some fonts from my computer

enter image description here

but they are not recognized when I use them as a fontname and default arial is used. Is there a way to get it recognized by R?

head(iris) %>%
  flextable() %>%
  font(j=5,fontname='Rage Italic')

enter image description here

head(iris) %>%
  flextable() %>%
  style(j=5,pr_t=fp_text(color='purple',font.size=20,
                        font.family='Rage Italic'))

enter image description here

Any suggestions how to get this set up?

frank
  • 3,036
  • 7
  • 33
  • 65

1 Answers1

0

You can use systemfonts::system_fonts() to list all available fonts on your machine (note 'systemfonts' lets you add downloaded fonts as well). The column "family" is containing the values to match with font.family.

systemfonts::system_fonts()
#> # A tibble: 679 × 9
#>    path                     index name  family style weight width italic monos…¹
#>    <chr>                    <int> <chr> <chr>  <chr> <ord>  <ord> <lgl>  <lgl>  
#>  1 /System/Library/Fonts/S…     2 Rock… Rockw… Bold  bold   norm… FALSE  FALSE  
#>  2 /System/Library/Fonts/N…     0 Note… Notew… Light normal norm… FALSE  FALSE  
#>  3 /Users/xxxxxxxxxx/Libra…     0 Fira… Fira … Ultr… light  norm… TRUE   FALSE  
#>  4 /System/Library/Fonts/S…     1 Deva… Devan… Bold  bold   norm… FALSE  FALSE  
#>  5 /Users/xxxxxxxxxx/Libra…     0 Fira… Fira … Thin  ultra… semi… FALSE  FALSE  
#>  6 /System/Library/Fonts/S…     0 Verd… Verda… Bold  bold   norm… FALSE  FALSE  
#>  7 /Users/xxxxxxxxxx/Libra…     3 Fira… Fira … Semi… semib… norm… FALSE  TRUE   
#>  8 /System/Library/Fonts/S…     0 Kann… Kanna… Regu… normal norm… FALSE  FALSE  
#>  9 /System/Library/Fonts/A…     8 Aria… Arial… Light light  norm… FALSE  FALSE  
#> 10 /System/Library/Fonts/A…    10 Appl… Apple… Thin  thin   norm… FALSE  FALSE  
#> # … with 669 more rows, and abbreviated variable name ¹​monospace

Created on 2022-10-10 with reprex v2.0.2

if( "Herculanum" %in% systemfonts::system_fonts()$family ){
  head(iris) %>%
    flextable() %>%
    style(j=5,pr_t=fp_text(color='purple',font.size=20,
                           font.family='Herculanum'))
  
}

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thanks for suggestion. I passed in the path, but it still did not work. `head(iris) %>% flextable() %>% font(j=5,fontname=systemfonts::system_fonts()[[1]][389])`, where this corresponds to `C:\\Windows\\Fonts\\RAGE.TTF` – frank Oct 11 '22 at 08:19
  • read the documentation of font(), it expects a font family (the name of the font, i.e. 'Arial'), so use family column – David Gohel Oct 11 '22 at 09:07
  • I tried all of the path, name, and family. had no success – frank Oct 11 '22 at 09:11
  • I updated the answer. I don't know why it does not for you. – David Gohel Oct 11 '22 at 11:08
  • oddly enough, it worked for `Wingdings`, but not `Rage`. I will need to examine it more – frank Oct 11 '22 at 11:38