2年前,我在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
...and当我尝试加载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文件夹中。我不确定是否可以简单地将字体拖放到此文件夹中,或者是否可以使用Font Book将这些字体写入正确的位置。我也不确定文件类型,如果我只能像帖子建议的那样使用.ttf
文件,即使我拥有的字体文件都是.otf
。
任何关于这方面的想法/帮助都是非常感谢的。我会在我可以解决的时候发布更新。
编辑:Using custom OTF fonts in ggplot2这篇文章指出,.otf
字体需要转换为.ttf
才能与extrafonts一起使用,但对于showtext
,可以使用.otf
字体。因此,我将尝试加载该库。
发布于 2020-04-11 02:18:04
在Font Book > Preferences中,通过将字体的首选项从用户切换到计算机,可确保字体安装到fonts文件夹中。这解决了我的问题。
https://stackoverflow.com/questions/61145808
复制相似问题