我目前正在尝试在我的所有情节中使用 Arial.ttf。我在网上找到了几本关于字体的手册,但是当我尝试通过 ggsave 和默认设备保存花盆时遇到了问题。
最小的工作示例
我正在运行以下最小工作示例来重现此错误:
Sys.setenv(R_GSCMD="D:/gs/bin/gswin64c.exe")
library(ggplot2)
require(extrafont)
font_import(prompt = F, pattern = "arial.ttf")
font_import(prompt = F, pattern = "JOKERMAN.TTF")
loadfonts(device="win", quiet = T)
loadfonts(device="pdf", quiet = T)
plot <- ggplot(mtcars, aes(wt, mpg, colour = hp)) + geom_point()
jokerman_plot <- plot + theme_bw(base_family = "Jokerman")
arial_plot <- plot + theme_bw(base_family = "Arial")
ggsave("arial_plot.png", arial_plot)
ggsave("arial_plot.pdf", arial_plot)
embed_fonts("arial_plot.pdf")
ggsave("jokerman_plot.png", jokerman_plot)
ggsave("jokerman_plot.pdf", jokerman_plot)
embed_fonts("jokerman_plot.pdf")
这会产生以下(正确的)PNG 图像:
然而,Arial PDF 将所有字符堆叠在一起(Jokerman PDF 呈现正确):
警告输出
此脚本会生成以下警告消息。
Warning messages:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x68
2: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x70
3: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font metrics unknown for character 0x4d
4: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x68
5: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x70
6: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x4e
7: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x41
8: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font metrics unknown for character 0x4d
9: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x31
10: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
11: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
12: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x31
13: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x35
14: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
15: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x32
16: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
17: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
18: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x32
19: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x35
20: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
21: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x33
22: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
23: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
24: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x4e
25: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x41
26: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x4e
27: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x41
28: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x31
29: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
30: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
31: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x31
32: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x35
33: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
34: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x32
35: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
36: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
37: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x32
38: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x35
39: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
40: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x33
41: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
42: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x30
43: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x4e
44: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x41
45: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x6d
46: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x70
47: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x67
48: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font metrics unknown for character 0x4d
49: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x6d
50: In grid.Call(L_textBounds, as.graphicsAnnot(x$label), ... :
font width unknown for character 0x70
会话信息
我现在的sessionInfo()
R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] extrafont_0.17 ggplot2_2.1.0 RevoUtilsMath_3.2.5
loaded via a namespace (and not attached):
[1] labeling_0.3 colorspace_1.2-6 scales_0.4.0 assertthat_0.1 plyr_1.8.3 tools_3.3.0
[7] gtable_0.2.0 ggthemes_3.0.3 Rcpp_0.12.5 extrafontdb_1.0 grid_3.3.0 digest_0.6.9
[13] Rttf2pt1_1.3.4 munsell_0.4.3
尝试的解决方案
- 我尝试了几种重命名生成的 AMF 文件的方法,但均无济于事
- 我认为也许我的源文件的编码可能很重要,所以我将其更改为 UTF-8(没有更改)
- 多次重置 extrafontdb
- 多次重新安装ghostscript
解决方法
我目前有两种解决方法,但是,出于便携性考虑,我更喜欢使用 R 或 ggplot 中配置的“本机”PDF设备
- 将 cairo_pdf 指定为设备(这可能会导致输出出现次要问题或某些绘图的渲染略有不同)。PDF 很好,那么这是否指向默认 PDF 设备中的错误?
- 使用 PNG/位图输出(我想在打印中使用绘图,所以首选矢量输出)
- 默认使用 ArialMT 依赖 R。我不确定 ArialMT 是否与我的(Latex)文档中使用的字体完全相同,所以我想明确指定字体。