是否可以在使用脚本创建 SVG 之前确定文本字符串的(像素)宽度
我打算使用(可能)perl 脚本创建一堆 SVG 图形。这些 SVG 图形将包含文本块。由于我想用线条“连接”此类文本块(不同宽度),我想知道文本的宽度,以便我可以相应地绘制连接线的长度。
我在 SVG get text element width 中看到,这可以通过java脚本实现。但这可能不是我想要的,因为我不打算在浏览器中托管 SVG。
所以,我想也许有一种方法可以在脚本运行时找出所需的宽度。如果有人能给我指出一个解决方案(也在 Perl 领域之外,但在 Windows 上),我将非常感激。
I am about the create a bunch of SVG graphics with (probabably) a perl script. These SVG graphics will contain text blocks. Since I want to "connect" such text blocks (of varying widths) with lines I'd like to know what width a text will be so that I can draw the connecting lines' length accordingly.
I have seen in SVG get text element width that it could be possible with java script. But that's probably not what I am after since I don't intend to host the SVG in a browser.
So, I thought that maybe there's a way to find out the desired width at the script's runtime. If someone can point me to a solution (also outside the realm of perl but on windows), I'd be very gratefu.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大约一年前,我使用 PDF::API2 和 advancewidth 函数做到了这一点: https://metacpan.org/module/PDF::API2::Content#width-txt-advancewidth-string-text_state-
请注意,您需要将 PDF 的 DPI 与SVG:它们可能不同(实际上我只是将值除以 1.25,你可以做得更好)。
PDF::API2 为您提供非常准确的值,非常适合 Inkscape(就我而言)。
I did that exactly that about a year ago using PDF::API2 and advancewidth function: https://metacpan.org/module/PDF::API2::Content#width-txt-advancewidth-string-text_state-
Note that you need to correlate DPI of PDF and SVG: they may be different (I actually did that just dividing values by 1.25, you can be better).
PDF::API2 gives you very accurate values that works for Inkscape (in my case) well.