简体   繁体   English

在SVG中操作/翻译<text>(添加边界框)

[英]Manipulating/translating <text> in SVG (adding a bounding box)

Problem : We need to replicate SVG text that was created in RaphaelJS with variable font-face, font-size, position, scale, rotation in SVG. 问题 :我们需要复制在RaphaelJS中创建的SVG文本,其中SVG中的字体,字体,位置,比例,旋转可变。

BUT... Raphael uses a bounding box for text which has a height that is independent of the actual height of the string. 但是...... Raphael使用了一个文本边界框,其高度与字符串的实际高度无关。 The bounding box's height is based on font-size and font-family, but is independent of the actual string. 边界框的高度基于font-size和font-family,但与实际字符串无关。 So a string of “Y” and “,” (with the same font-face and font-size) have the same height. 因此,一串“Y”和“,”(具有相同的font-face和font-size)具有相同的高度。

When we recreate the text in SVG (by generating it in PHP) we can get the font-size correct, and the font-family correct. 当我们在SVG中重新创建文本时(通过在PHP中生成它),我们可以得到正确的字体大小,并且字体系列正确。 However, SVG 's height is NOT independent of the actual string. 但是,SVG的高度并不独立于实际的字符串。 So a “,” would have a much lower height then a “Y”. 因此,“,”将具有比“Y”低得多的高度。 This difference in height breaks rotation and positioning. 这种高度差异打破了旋转和定位。

QUESTION : How do we create svg with a 'bounding box' that replicates RaphaelJS's getBBox (which essentially just sizes a box that would fit any character in it) so we can mimic the rotation and positioning from RaphaelJS? 问题 :我们如何使用复制RaphaelJS的getBBox的'边界框'创建svg(它基本上只是一个适合其中任何字符的框的尺寸),所以我们可以模仿RaphaelJS的旋转和定位? NOTE: We can convert text->path for the SVG, if that helps. 注意:如果有帮助,我们可以转换SVG的text-> path。 We also have access to the font files. 我们还可以访问字体文件。

在此输入图像描述

EDIT : The issue was solved by using the matrix() command and directly translating the transformations over, as opposed to applying positioning, then scaling, then rotation. 编辑 :通过使用matrix()命令并直接转换转换来解决问题,而不是应用定位,然后缩放,然后旋转。

If you're looking to manipulate character positions inside words:sentences and the SVG you are generating on the server then you should be able to make use of SVG TSPAN and orientate character position. 如果你想操纵单词中的字符位置:句子和你在服务器上生成的SVG那么你应该能够使用SVG TSPAN并定位字符位置。 If you can change letters to path then I think that algorithm would be possible. 如果您可以将字母更改为路径,那么我认为该算法是可行的。 There's nothing to stop you creating an array of relative positions for your alphanumeric font set(s). 没有什么可以阻止你为字母数字字体集创建一个相对位置数组。 I hope some of the above is useful... 我希望上面的一些有用......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM