Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to put newlines in pysvg?

Tags:

python

svg

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')

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>

How can I get a newline to display in the SVG?

like image 469
merlin2011 Avatar asked May 07 '14 08:05

merlin2011


1 Answers

Section 10.1 of the SVG spec gives three options:

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.)
like image 89
Janne Karila Avatar answered Oct 11 '22 17:10

Janne Karila