gpt4 book ai didi

python - 有没有办法在 pysvg 中添加换行符?

转载 作者:太空狗 更新时间:2023-10-30 02:45:44 24 4
gpt4 key购买 nike

考虑以下最小工作示例:

from pysvg.text import *
from pysvg.builders import *

doc = svg()
doc.addElement(text("hello\nWorld", 150, 50))
doc.save('HelloWorld2.svg')

当生成的 svg 以图形方式查看时,换行符已转换为单个空格,因为 XML 不遵守换行符。

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink"  >
<text font-size="12" y="50" x="150" >
hello
World</text>
</svg>

如何让换行符显示在 SVG 中?

最佳答案

Section 10.1 of the SVG spec给出三个选项:

Each ‘text’ element causes a single string of text to be rendered. SVG performs no automatic line breaking or word wrapping. To achieve the effect of multiple lines of text, use one of the following methods:

  • The author or authoring package needs to pre-compute the line breaks and use multiple ‘text’ elements (one for each line of text).
  • The author or authoring package needs to pre-compute the line breaks and use a single ‘text’ element with one or more ‘tspan’ child elements with appropriate values for attributes ‘x’, ‘y’, ‘dx’ and ‘dy’ to set new start positions for those characters which start new lines. (This approach allows user text selection across multiple lines of text -- see Text selection and clipboard operations.)
  • Express the text to be rendered in another XML namespace such as XHTML embedded inline within a ‘foreignObject’ element. (Note: the exact semantics of this approach are not completely defined at this time.)

关于python - 有没有办法在 pysvg 中添加换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23512469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com