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

问题描述 投票:0回答:1

[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

...并且当我尝试加载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/文件夹中不可用,如您在下图中所看到的:

enter image description hereenter image description here

我已经测试了该字体在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文件。

非常感谢任何想法/帮助。我可以解决后便会发布更新。

编辑:Using custom OTF fonts in ggplot2此帖子表明,.otf字体需要转换为.ttf才能与Extrafonts一起使用,但是对于showtext,可以使用.otf字体。因此,我将尝试加载该库。

2年前,我发布了一篇相关文章-向R中添加字体,该字体不在extrafonts库中。通过更好地了解字体的工作原理,我现在有了一套新的字体,需要将它们上载到R中才能使用...

r fonts true-type-fonts extrafont showtext
1个回答
0
投票

enter image description here在“字体书”>“首选项”中,通过将字体的首选项从“用户”切换到“计算机”,可以确保将字体安装到字体文件夹中。这解决了我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.