【问题标题】:TTF/OTF font selection in R for windows: onscreen vs pdf()Windows R 中的 TTF/OTF 字体选择:屏幕上与 pdf()
【发布时间】:2012-09-04 21:49:30
【问题描述】:

我知道在 Linux 或 Mac 上的 R 中,字体始终被定义为参数 family="Charis SIL" 到 par()、text() 或图形设备函数之一,如 tiff()、svg()等(用您想要的任何字体名称替换“Charis SIL”)。我也知道在 Windows 上,这仅适用于 cairo_pdf() 和 svg() 设备; jpeg()、tiff()、png() 和 bmp() 等光栅图形设备要求字体首先映射到“Windows 字体数据库”中:

# this doesn't work on windows
jpeg(filename='test1.jpg', family='Charis SIL')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
dev.off()
# (gives warnings: Font family not found in Windows font database)  

# this does work on windows (assuming you have the Charis SIL font installed)
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
jpeg(filename='test2.jpg', family='myCustomWindowsFontName')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
dev.off()

pdf() 设备仍然不同:它似乎需要在 postscriptFonts() 和/或 pdfFonts() 数据库中定义的字体,这意味着只有 Type1 字体:

# this doesn't work on windows
pdf('test.pdf', family='Charis SIL')
# gives error: Unknown family "Charis SIL"

# this doesn't work either
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
pdf('test.pdf', family='myCustomWindowsFontName')
# gives error: Unknown family "myCustomWindowsFontName"

# this also won't work
pdf.options(family='Charis SIL')
pdf('test.pdf')
# gives error: Invalid font type
# also gives warning: font family "Charis SIL" not found in Postscript font database

通常这无关紧要,因为 cairo_pdf() 可以很好地替代 pdf() 设备,并且可以很好地处理 TTF 和 OTF 字体。问题是,如果用户在屏幕设备上绘图,然后使用菜单命令另存为 PDF,它似乎调用 pdf() 而不是 cairo_pdf(),然后会引发错误:

# this part works
windowsFonts(myCustomWindowsFontName=windowsFont('Charis SIL'))
par(family='myCustomWindowsFontName')
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')

# but menu command "File > Save As > PDF" gives errors:
# Error: Invalid font type
# Warning: font family "Charis SIL" not found in Postscript font database

这是一个问题,因为我正在开发的 R 包在 Windows 上一直未能通过“R CMD 检查”,显然是因为示例代码生成的屏幕输出会自动保存为 PDF,从而产生上述错误.一种解决方案是放弃 Windows 中屏幕设备的自定义字体(即,如果选择的输出是“screen”,则忽略“family”参数)。另一种选择是使用 Cairo() 包进行屏幕绘图,但如果可以的话,我更愿意坚持使用基本图形。有什么方法可以在屏幕绘图中获取自定义字体,并且在使用“另存为 PDF”菜单命令时不会引发错误?

【问题讨论】:

  • ps:如果你想在家里测试,这里是get the Charis SIL font 的位置,这里是对“text”的替代调用,它转义了非 ascii 字符:text(0, 0,labels='i\u026Ay\u028Fe\u025B\u00F8\u0153\u00E6a\u0276\u0268\u0289\u0258\u0275\u0259\u025C\u025E\u0250\u026Fu\u028A\u02524o\u025\u02524o\u025\u\u\ \u025A\u025D')
  • 这可能是一个愚蠢的问题,但如果问题是文档中的示例未通过 R CMD 检查,那么为什么不简单地指定不应运行这些示例呢?
  • 实际上,我已经解决了 R CMD 检查问题(事实证明与此处提出的问题相关但并不完全相同)。更大的问题是可用性。以我的经验,R 新手用户对 File>SaveAs 命令更满意,并希望他们以所见即所得的方式工作。

标签: windows r unicode fonts


【解决方案1】:

我最终解决这个问题的方法如下:

oldSans <- windowsFonts()$sans
windowsFonts(sans=windowsFont('Charis SIL'))
par(family='sans') # this line isn't necessary anymore
plot(0,0,type='n',ann=FALSE,frame.plot=FALSE)
text(0,0,labels='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
windowsFonts(sans=oldSans)

这样,屏幕窗口中将使用正确的字体,当用户使用菜单命令保存为 PDF 时,PDF 将被保存,但使用默认的 sans 字体而不是自定义字体.仅在 PDF 确实被导出的意义上,它是一个“解决方案”,但如果绘图具有非 ASCII 字形,则不能保证它们会出现在以这种方式生成的 PDF 中。它也可以说比原来的情况更糟,因为动作不再抛出error 甚至warning。故事的寓意:不要依赖 GUI 中的菜单命令来执行您应该知道如何在控制台中执行的操作。

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2011-07-15
    • 2021-06-08
    • 2017-02-02
    • 2019-07-22
    • 2014-04-27
    • 2016-03-04
    • 1970-01-01
    • 2014-07-23
    相关资源
    最近更新 更多