0

两年前,我发布了一篇相关文章: Add font to R that is not in extrafonts library。随着对字体的工作原理有了更好的了解,我现在有了一组新的字体,我需要将它们上传到 R 中以与ggplot. 尽管在上一篇文章中给出了有用的答案,但我遇到了一系列新问题:

首先,我无法加载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”时,我也会收到同样的错误。

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

编辑

在 ggplot2 中使用自定义 OTF 字体这篇文章表明.otf需要将字体转换为与额外字体.ttf一起使用,但可以使用showtext,.otf字体。所以我会尝试加载那个库。

4

1 回答 1

1

在此处输入图像描述 在 Font Book > Preferences 中,通过将字体的首选项从 User 切换到 Computer,它可以确保字体安装到 fonts 文件夹中。这解决了我的问题。

于 2020-04-10T18:18:03.617 回答