使用 Unicode“dingbat-like”; R 图形中的字形,跨设备和平台,尤其是 PDF
你们中的一些人可能已经看过我的 关于这个主题的博客文章,在想要帮助朋友生成半填充的圆圈作为图表上的点后,我编写了以下代码:
TestUnicode <- function(start="25a0", end="25ff", ...)
{
nstart <- as.hexmode(start)
nend <- as.hexmode(end)
r <- nstart:nend
s <- ceiling(sqrt(length(r)))
par(pty="s")
plot(c(-1,(s)), c(-1,(s)), type="n", xlab="", ylab="",
xaxs="i", yaxs="i")
grid(s+1, s+1, lty=1)
for(i in seq(r)) {
try(points(i%%s, i%/%s, pch=-1*r[i],...))
}
}
TestUnicode(9500,9900)
这有效(即生成一个几乎完整的很酷的 dingbatty 符号网格)
- : Ubuntu 10.04,在 Mandriva Linux 发行版上的 X11 或 PNG 设备中
- ,相同的设备,具有本地构建的 R,一旦安装了 pango-devel
它会不同程度地失败(即生成部分或完全填充点或空矩形的网格),或者静默或带有警告:
- 在同一台 Ubuntu 10.04 机器上的 PDF 或 PostScript 中(尝试设置 font="NimbusSan" 以使用 URW 字体,没有帮助)
- 在 MacOS X.6(quartz、X11、Cairo、PDF)上
例如,尝试所有可用的 PDF 字体系列:
flist <- c("AvantGarde", "Bookman","Courier", "Helvetica", "Helvetica-Narrow",
"NewCenturySchoolbook", "Palatino", "Times","URWGothic",
"URWBookman", "NimbusMon", "NimbusSan", "NimbusSanCond",
"CenturySch", "URWPalladio","NimbusRom")
for (f in flist) {
fn <- paste("utest_",f,".pdf",sep="")
pdf(fn,family=f)
TestUnicode()
title(main=f)
dev.off()
embedFonts(fn)
}
在 Ubuntu 上,这些文件都不包含符号。
如果让它能处理尽可能多的组合,那就太好了,尤其是在某些矢量格式中,尤其是在 PDF 中。
任何有关字体/图形设备配置的建议都将受到欢迎。
Some of you may have seen my blog post on this topic, where I wrote the following code after wanting to help a friend produce half-filled circles as points on a graph:
TestUnicode <- function(start="25a0", end="25ff", ...)
{
nstart <- as.hexmode(start)
nend <- as.hexmode(end)
r <- nstart:nend
s <- ceiling(sqrt(length(r)))
par(pty="s")
plot(c(-1,(s)), c(-1,(s)), type="n", xlab="", ylab="",
xaxs="i", yaxs="i")
grid(s+1, s+1, lty=1)
for(i in seq(r)) {
try(points(i%%s, i%/%s, pch=-1*r[i],...))
}
}
TestUnicode(9500,9900)
This works (i.e. produces a nearly-full grid of cool dingbatty symbols):
- on Ubuntu 10.04, in an X11 or PNG device
- on Mandriva Linux distribution, same devices, with locally built R, once pango-devel was installed
It fails to varying degrees (i.e. produces a grid partly or entirely filled with dots or empty rectangles), either silently or with warnings:
- on the same Ubuntu 10.04 machine in PDF or PostScript (tried setting font="NimbusSan" to use URW fonts, doesn't help)
- on MacOS X.6 (quartz, X11, Cairo, PDF)
For example, trying all the available PDF font families:
flist <- c("AvantGarde", "Bookman","Courier", "Helvetica", "Helvetica-Narrow",
"NewCenturySchoolbook", "Palatino", "Times","URWGothic",
"URWBookman", "NimbusMon", "NimbusSan", "NimbusSanCond",
"CenturySch", "URWPalladio","NimbusRom")
for (f in flist) {
fn <- paste("utest_",f,".pdf",sep="")
pdf(fn,family=f)
TestUnicode()
title(main=f)
dev.off()
embedFonts(fn)
}
on Ubuntu, none of these files contains the symbols.
It would be nice to get it to work on as many combinations as possible, but especially in some vector format and double-especially in PDF.
Any suggestions about font/graphics device configurations that would make this work would be welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你运气不好,Ben,因为根据 Paul Murrell 的一些注释,
pdf()
只能处理单字节编码。多字节编码需要转换为单字节编码,这就是问题所在;根据定义,单字节编码不能包含可以用多字节编码(例如 UTF-8)表示的所有字形。Paul 的笔记可以在此处找到,其中他建议使用基于 Cairo 的 PDF 设备的几个解决方案,在适当的 Linux 和 Mac OS 系统上使用
cairo_pdf()
,或通过 MS Windows 下的Cairo
包。I think you are out of luck Ben, as, according to some notes by Paul Murrell,
pdf()
can only handle single-byte encodings. Multi-byte encodings need to be converted to a the single-byte equivalent, and therein lies the rub; by definition, single-byte encodings cannot contain all the glyphs that can be represented in a multi-byte encoding like UTF-8, say.Paul's notes can be found here wherein he suggests a couple of solutions using Cairo-based PDF devices, using
cairo_pdf()
on suitably-endowed Linux and Mac OS systems, or via theCairo
package under MS Windows.我发现 cairo_pdf 设备完全不够:输出与 pdf 和屏幕渲染明显不同,而且它的绘图支持很粗略。
然而,在 OS X 上有一个相当简单的解决方法:使用“普通”
quartz
设备并将其type
设置为pdf
:不幸的是,在我的设备上计算机会忽略字体系列并始终使用 Helvetica(尽管文档声称默认字体是 Arial)。
至少还有两个其他问题:
pdf
将连字符转换为减号。这可能并不总是您想要的,但正确排版负数非常有用。链接的线程描述了此问题的解决方法。(我意识到 OP 简要提到了 Quartz 设备,但此线程经常被查看,我认为此解决方案需要更加突出。)
I have found the
cairo_pdf
device to be completely insufficient: the output is markedly different from bothpdf
and on-screen rendering, and its plotmath support is sketchy.However, there’s a rather simple workaround on OS X: Use the “normal”
quartz
device and set itstype
topdf
:Unfortunately, on my computer this ignores the font family and always uses Helvetica (although the documentation claims that the default is Arial).
There are at least two other gotchas:
pdf
converts hyphens to minuses. This may not even always be what you want but it’s quite useful to properly typeset negative numbers. The linked thread describes workarounds for this.(I realise that OP briefly mentions the Quartz device but this thread is frequently viewed and I think this solution needs more prominence.)
另一个解决方案可能是使用 tikzDevice ,它现在可以使用带有 Unicode 字符的 XeLaTeX。然后可以编译生成的 tex 文件以生成 pdf 文件。问题仍然是您的系统上必须有包含这些字符的字体。
第一次,这将需要很长时间。
Another solution might be to use tikzDevice which can now use XeLaTeX with Unicode characters. The resulting tex file can then be compiled to produce a pdf. The problem is still that you must have a font on your system that contains the characters.
The first time, this will take a LONG time.
您是否尝试过在 PDF 中嵌入一种字体,或者包含一种适合 Mac 用户使用的字体?
Have you tried embedding a font in the PDF, or including one for Mac users that would work?