使用 extrafont 或 showtext 库将字体添加到 R(在 Mac 上通过 FontBook)

Adding Fonts to R using extrafont or showtext libraries (on Mac via FontBook)

两年前我post编辑了一个相关的post: 。通过更好地了解字体的工作原理,我现在有了一组新字体,我需要将它们上传到 R 以用于 ggplot。尽管在之前 post 中提供了有用的答案,但我 运行 正在处理一组新问题:

首先,我无法加载 showtext 库。重新安装并加载库后,我收到此错误消息:

>   library(showtext)
Loading required package: sysfonts
Error: package or namespace load failed for ‘sysfonts’:
 .onLoad failed in loadNamespace() for 'sysfonts', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so, 6): Library not loaded: /opt/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so
  Reason: image not found
Error: package ‘sysfonts’ could not be loaded

...当我尝试加载 sysfonts 库时,我收到以下信息:

> library(sysfonts)
Error: package or namespace load failed for ‘sysfonts’:
 .onLoad failed in loadNamespace() for 'sysfonts', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so, 6): Library not loaded: /opt/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so
  Reason: image not found

我不确定如何修复这个 unable to load shared object 错误,因此我几乎放弃了使用 showtext,因为我无法将它加载到 R.

转到 extrafont 然后,我 运行 将字体从我的 Mac 导入 R:

> library(extrafont)
> font_import() # import all fonts on my system
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y
Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
Extracting .afm files from .ttf files...
/Library/Fonts/Andale Mono.ttf => /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics/Andale Mono
/Library/Fonts/Apple Chancery.ttf : No FontName. Skipping.
/Library/Fonts/AppleGothic.ttf : No FontName. Skipping.
/Library/Fonts/Arial Black.ttf => /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics/Arial Black
...
...
Found FontName for 63 fonts.
Scanning afm files in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics
Writing font table in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/fontmap/Fontmap...
There were 50 or more warnings (use warnings() to see the first 50)

我注意到某些字体由于 No FontName 错误而被跳过,而其他字体 (Arial Black) 似乎加载正常。在 运行 设置 font_import 之后,我查看可用的字体:

>   fonts()
 [1] ".Keyboard"             "System Font"           ".SF NS Rounded"        "Andale Mono"          
 [5] "Apple Braille"         "AppleMyungjo"          "Arial Black"           "Arial"                
 [9] "Arial Narrow"          "Arial Rounded MT Bold" "Arial Unicode MS"      "Bodoni Ornaments"     
[13] "Bodoni 72 Smallcaps"   ""                      "Brush Script MT"       "Comic Sans MS"        
[17] "Courier New"           "DIN Alternate"         "DIN Condensed"         "Georgia"              
[21] "Impact"                "Khmer Sangam MN"       "Lao Sangam MN"         "Luminari"             
[25] "Microsoft Sans Serif"  "Tahoma"                "Times New Roman"       "Trattatello"          
[29] "Trebuchet MS"          "Verdana"               "Webdings"              "Wingdings"            
[33] "Wingdings 2"           "Wingdings 3"    

对于我的自定义字体,我获得了字体的 zip 文件,我已将这些字体上传到我的 Mac*。字体名称是 FreightDisp Pro,我可以在这里看到该字体在 Mac 的 Font Book 应用程序中。但是,我会注意到该字体在我的 library/Fonts/ 文件夹中不可用,如下图所示:

我测试过该字体在 Microsoft Word 中可用,确实如此。

最后,当我尝试将字体导入 R 时,我收到以下错误:

> font_import(pattern="FreightDisp Pro")
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y
Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
Extracting .afm files from .ttf files...
Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 0, 1

我在将模式更改为“National2”时也收到同样的错误。

所以当我总结这个 post 时,似乎我也找到了问题的答案,即字体不能简单地安装在 fontbook 中,还必须在 /Library/Fonts 文件夹。我不确定我是否可以简单地将字体拖放到此文件夹中,或者我是否可以使用 Font Book 将这些字体写入正确的位置。我也不确定文件类型,如果我只能按照 post 的建议使用 .ttf 文件,即使我拥有的字体文件都是 .otf.

编辑

这个 post 表示 .otf 字体需要转换为 .ttf 才能与 extrafonts 一起使用,但是 showtext, .otf字体可以使用。所以我会尝试加载那个库。

在 Font Book > Preferences 中,通过将字体的首选项从 User 切换到 Computer,它确保字体安装到 fonts 文件夹中。这解决了我的问题。