R中用于windows的TTF / OTF字体选择:onscreen vs pdf() [英] TTF/OTF font selection in R for windows: onscreen vs pdf()

查看:170
本文介绍了R中用于windows的TTF / OTF字体选择:onscreen vs pdf()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,在Linux或Mac上的R中,字体一直定义为par(),text()或像tiff(),svg()这样的图形设备函数之一的参数family =Charis SIL等等(用你想要的字体名称替换Charis SIL)。我也知道,在Windows上,只能用于cairo_pdf()和svg()设备;光栅图形设备(如jpeg(),tiff(),png()和bmp())要求首先在Windows字体数据库中映射字体:

#这不适用于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()
#(给出警告:Font family在Windows的字体数据库中找不到)

# (0,0,type ='n',ann = FALSE,frame.plot = FALSE)
text(0) ,0,labels ='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
dev.off()

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

 #这不适用于windows 
pdf('test.pdf',family ='Charis SIL')
#给出错误:未知的系列Charis SIL

#这不起作用
windowsFonts (myCustomWindowsFontName = windowsFont('Charis SIL'))
pdf('test.pdf',family ='myCustomWindowsFontName')
#给出错误:未知族myCustomWindowsFontName

#这也是行不通的
pdf.options(family ='Charis SIL')
pdf('test.pdf')
#给出错误:字体类型无效
#也给出警告:字体系列Charis SIL在Postscript字体数据库中找不到

通常这不会有问题,因为cairo_pdf()是pdf()设备的很好的替代品,并且处理TTF和OTF字体就好了。问题是,如果用户对屏幕上的设备进行绘图,然后使用菜单命令将其另存为PDF,则会调用pdf()而不是cairo_pdf(),然后引发错误:

$ pre $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b图(0,0,type ='n',ann = FALSE,frame.plot = FALSE)
text(0,0,labels ='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')

#menu命令文件>另存为> PDF给出错误:
#错误:字体类型无效
警告:在Postscript字体数据库中找不到字体系列Charis SIL
解决方案我最终解决这个问题的方式如下:
$ b $ pre $ old $ { $ b windowsFonts(sans = windowsFont('Charis SIL'))
par(family ='sans')#这行不再是必须的
plot(0,0,type ='n', ann = FALSE,frame.plot = FALSE)
text(0,0,labels ='iyeøɛœaɶɪʏæɑɒʌɔɤoɯuʊɨʉɘɵəɜɞɐɚɝ')
windowsFonts(sans = oldSans)

通过这种方式,在屏幕窗口中将使用正确的字体,并且当用户使用菜单命令保存为PDF时,PDF将被保存但用默认的无字体而不是自定义的字体。这只是一个解决方案,只是在PDF文件被导出的意义上说,但是如果这个图是非ASCII字形的,那么就不能保证它们会以这种方式显示在PDF中。这也可能是WORSE比原来的情况,因为行动不再抛出一个错误甚至一个警告。故事的寓意:不要依赖GUI中的菜单命令来执行你应该知道如何在控制台中执行的操作。

I know that in R on Linux or Mac, fonts are consistently defined as an argument family="Charis SIL" to par(), text(), or one of the graphic device functions like tiff(), svg(), etc (substitute "Charis SIL" with whatever font name you want). I also know that on Windows, that only works for the cairo_pdf() and svg() devices; raster graphic devices like jpeg(), tiff(), png(), and bmp() require that the font be mapped in the "Windows font database" first:

# 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()

The pdf() device is different still: it seems to need fonts defined in either the postscriptFonts() and/or pdfFonts() database, which means only Type1 fonts:

# 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

Ordinarily this would not matter, because cairo_pdf() is a fine substitute for the pdf() device and handles TTF and OTF fonts just fine. The problem is that if a user plots to the onscreen device and then uses the menu commands to save as PDF, it appears to call pdf() instead of cairo_pdf(), which then throws errors:

# 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

This is a problem because the R package I'm developing keeps failing "R CMD check" on Windows, apparently because the example code generates on-screen output that gets saved out as PDF automatically, which generates the above-mentioned errors. One solution is to give up custom fonts for the on-screen devices in Windows (i.e., just ignore the "family" argument if the chosen output is "screen"). Another option is to use the Cairo() package for onscreen plotting, but I'd prefer to stick with base graphics if I can. Is there any way I can get custom fonts in an onscreen plot and not have it throw errors when using the "save as PDF" menu commands?

解决方案

The way I eventually solved this was as follows:

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)

In this way, the correct font will be used in the on-screen window, and when the user uses the menu commands to save as PDF, the PDF will get saved out but with the default sans font instead of the custom one. It is a "solution" only in the sense that the PDF does get exported, but if the plot has non-ASCII glyphs there is no guarantee that they'll show up in a PDF generated in this way. It is also arguably WORSE than the original situation, because the action no longer throws an error or even a warning. The moral of the story: don't rely on the menu commands in your GUI to do things that you should know how to do in the console.

这篇关于R中用于windows的TTF / OTF字体选择:onscreen vs pdf()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆