有没有办法在pysvg中添加换行符? [英] Is there a way to put newlines in pysvg?

查看:151
本文介绍了有没有办法在pysvg中添加换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下最小工作示例:

Consider the following minimum working example:

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

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

以图形方式查看生成的svg 时,由于XML不遵守换行符,因此换行符已转换为一个空格.

When the resulting svg is viewed graphically, the newline has been transformed into a single space, because the XML does not respect the newline.

<?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中?

推荐答案

第10.1节SVG规范提供了三种选择:

每个文本"元素都会导致呈现单个文本字符串. SVG 不执行自动换行或自动换行.为了实现 多行文本的效果,请使用以下方法之一:

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:

  • 作者或创作包需要预先计算换行符,并使用多个文本"元素(每行文本一个).
  • 作者或创作包需要预先计算换行符,并将单个"text"元素与一个或多个"tspan"子元素一起使用 为属性"x","y","dx"和 "dy"为那些以新开头的字符设置新的开头位置 线. (此方法允许跨多行选择用户文本 文本-​​请参阅文本选择和剪贴板操作.)
  • 表示要在另一个XML名称空间中呈现的文本,例如在'foreignObject'元素中内联的XHTML嵌入式内联. (笔记: 这种方法的确切语义尚未在以下位置完全定义 这次.)
  • 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.)

这篇关于有没有办法在pysvg中添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆