【问题标题】:Is it possible to access the glyph outline coordinates for a plotted string in R?是否可以访问 R 中绘制的字符串的字形轮廓坐标?
【发布时间】:2019-12-30 21:48:45
【问题描述】:

我正在从事一个在 R 中开发 wordcloud 类型图形的个人项目。我有一些基本的代码可以工作,但它仅限于使用限制每个单词的最小矩形来检测单词之间的冲突。

我想知道是否可以访问单个字母(字形?)轮廓的实际坐标,以便我可以将单词打包得更接近彼此 - 甚至可以做一些事情,例如绘制一个单词例如,在字符“o”中间的孔内。

我所有的搜索都被证明是徒劳的。此链接似乎相关,但对于 TeX https://tex.stackexchange.com/questions/180510/how-to-get-intersection-points-of-two-glyphs

【问题讨论】:

标签: r collision-detection glyph


【解决方案1】:

这个问题没有什么吸引力,但经过大量挖掘后,我设法共同破解了一些适合我的目的的解决方案,并分享以供参考。

我无法访问绘制的字符坐标,但我能够制作自己的字符字形并绘制它们。

看到https://stackoverflow.com/a/7745776/5825522 对一个无关问题的回答让我开始使用 grImport 包来跟踪 postscript 文件。而且我能够一起破解一个最终有效的解决方案(下面的示例) - 尽管我不能承认理解 postscript 代码和 XML 解析的来龙去脉(因为这是我第一次遇到这两个主题!)

注意,在我的研究过程中,我还发现了这个包https://github.com/yixuan/fontr,它可能对提取实际绘制的字符坐标更有用,但它超出了我的理解和使用能力。

library(grImport)
library(magrittr)
library(xml2)

# Create postscript file for the letter 'S' in times new roman
cat("%!PS 
    /Times-Roman findfont
    10 scalefont 
    setfont 
    newpath 0 0 moveto (S) show",
    file="example.ps")

# Trace the postscript file to an XML file
grImport::PostScriptTrace("example.ps", "example.xml")

# Read the XML
xml <- xml2::read_xml("example.xml")

# Extract the XML node associated with the letter paths
letter_paths <- xml_find_all(xml, "text/path")

# Extract coordinates
x <- letter_paths %>% xml_find_all("move|line") %>% xml_attr("x") %>% as.numeric()
y <- letter_paths %>% xml_find_all("move|line") %>% xml_attr("y") %>% as.numeric()

# Remove last pair of coordinates as the post script tracing leaves undesired xy coordinates. 
# These corrections are tricky for glyphs that have 'holes' in them like 'O' and 'B'
x <- x[-length(x)]
y <- y[-length(y)]

# plot
plot(x,y, asp=1, type="l")
points(x,y)

经过一番摆弄,我能够制作出适合我的字形集

【讨论】:

    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 2017-07-03
    • 2022-01-13
    • 2021-11-03
    • 1970-01-01
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多