R代表Windows的TTF / OTF字体选择:屏幕与pdf()
我知道在
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,type='n',ann=FALSE,frame.plot=FALSE) text(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,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,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中屏幕设备的自定义字体(即,如果所选输出是“屏幕”,则忽略“家庭”参数).另一个选择是使用Cairo()包进行屏幕绘图,但如果可以,我更愿意坚持使用基本图形.有没有什么办法可以在屏幕上绘制自定义字体而不会在使用“另存为PDF”菜单命令时抛出错误? 解决方法
我最终解决这个问题的方式如下:
oldSans <- windowsFonts()$sans windowsFonts(sans=windowsFont('Charis SIL')) par(family='sans') # this line isn't necessary anymore plot(0,labels='iye???a????ɑ????o?u???????????') windowsFonts(sans=oldSans) 这样,将在屏幕窗口中使用正确的字体,当用户使用菜单命令保存为PDF时,PDF将被保存,但使用默认的sans字体而不是自定义字体.它只是在PDF导出的意义上的“解决方案”,但如果绘图具有非ASCII字形,则无法保证它们将以这种方式显示在PDF中.它也可以说比原始情况更糟糕,因为行动不再引发错误甚至是警告.故事的寓意:不要依赖GUI中的菜单命令来执行您应该知道如何在控制台中执行的操作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows – 有没有推荐的方法从外部程序重复调用matlab?
- 无法将64位Windows 8.1 Pro Client加入Windows 2003域. Win
- 避免在Windows Azure上支付超额费用?
- windows-server-2008 – IIS可以从GPU核心创建线程吗?
- windows-7 – 检查InnoSetup配置文件的[Run]段中的Windows版
- unity3d – 如何从构建中排除不必要的DLL?
- windows – MSTSC:远程桌面上的黑色区域
- 当我在Windows上使用脚本名称运行Python脚本时,不能重定向输
- Windows安装程序 – 最简单的解决方案来替代MSI中的一个小文
- vbscript – 为什么WScript.StdOut.Write方法在VBS和JScrip