[Sugar-devel] [PATCH] TurtleArt - SVG text wrapping #sl1856
Walter Bender
walter.bender at gmail.com
Sat Jul 3 13:08:54 EDT 2010
On Sat, Jul 3, 2010 at 7:44 AM, Tim McNamara
<paperless at timmcnamara.co.nz> wrote:
> Hi Walter, et all,
> This patch should go most of the way to fixing the bug. However, I haven't
> tested it locally & it contains a few assumptions about what the SVG files
> look like that that it's processing. I'm also not 100% sure about the
> elementtree implementation of ElementTree (am used to lxml). That said, I
> would like your feedback about whether you consider this to be an
> appropriate way to approach the problem.
I don't know much about ElementTree, but the patch itself looks OK.
What I don't understand is how the textbox width is determined. How do
we know how long to make the strings?
-walter
> diff --git a/taexporthtml.py b/taexporthtml.py
> index 5a0d163..daad983 100644
> --- a/taexporthtml.py
> +++ b/taexporthtml.py
> @@ -25,6 +25,78 @@ import os.path
> from tautils import data_to_string, save_picture, image_to_base64
> from gettext import gettext as _
>
> +try: import xml.etree.cElementTree as ET
> +except ImportError:
> + try: import cElementTree as ET # python <2.5
> + except ImportError:
> + try: import lxml.etree as ET
> + except ImportError:
> + try: import xml.etree.ElementTree as ET
> + except ImportError:
> + import elementtree.ElementTree as ET
> +
> +def split_at_len(s, max_len=50, delimiter=None):
> + """
> + Returns a list of strings, that are split from an
> + input string. Delimits on whitespace by default.
> + """
> + if delimiter is None:
> + s = s.split()
> + else:
> + s = s.split(delimiter)
> +
> + lines = []
> + current_line = ''
> + for word in s:
> + if len(current_line) >= max_len:
> + short.append(current_line)
> + current_line = word
> + else:
> + current_line = '%s %s' % (current_line, word);
> + lines[0] = lines[0][1:] #hack required by string formatting
> + lines.append(current_line)
> + return lines
> +
> +def process_svg_text_element(el, max_len=50, line_height='1.1em'):
> + """
> + Turns
> + <text>long ... content</text>
> + into
> + <text>
> + <tspan>long</tspan><tspan>...</tspan><tspan>content</tspan>
> + </text>
> + """
> + # NOTE Assumes that there are no subelements this
> + # should be safe because TurtleArt shouldn't
> + # generate any
> + data = split_at_len(el.text, max_len=max_len)
> +
> + for line in data:
> + ET.SubElement(el, 'tspan')
> +
> + i = 0
> + while i < len(data):
> + el[i].text = data[i]
> + el[i].attrib['dy'] = (i*line_height)
> + i = i+1
> + return el
> +
> +def process_text_in_svg(svg, max_len=50, line_height='1.1em'):
> + """
> + Takes an SVG as a string or file-like object
> + and returns a string with text elements split
> + into multiple lines surrounded by <tspan>.
> + """
> + tree = ET.parse(svg)
> + svg_text = tree.findall('text')
> + if len(svg_text) > 0:
> + for el in svg_text:
> + # TODO check if this actually replaces the element in tree
> + el = process_svg_text_element(el,
> + max_len=max_len,
> + line_height=line_height)
> + return ET.tostring(tree)
> +
> def save_html(self, tw, embed_flag=True):
> """ Either: Save canvas and code or pictures to HTML """
> self.embed_images = embed_flag
> @@ -85,7 +157,7 @@ def save_html(self, tw, embed_flag=True):
> imgdata = f.read()
> f.close()
> if p.endswith(('.svg')):
> - tmp = imgdata
> + tmp = process_text_in_svg(imgdata)
> else:
> pixbuf = gtk.gdk.pixbuf_new_from_file(p)
> imgdata = image_to_base64(pixbuf, tw.activity)
> @@ -97,7 +169,7 @@ def save_html(self, tw, embed_flag=True):
> f = open(p, "r")
> imgdata = f.read()
> f.close()
> - tmp = imgdata
> + tmp = process_text_in_svg(imgdata)
> else:
> tmp = self.html_glue['img3'][0]
> tmp += p
> _______________________________________________
> Sugar-devel mailing list
> Sugar-devel at lists.sugarlabs.org
> http://lists.sugarlabs.org/listinfo/sugar-devel
>
>
--
Walter Bender
Sugar Labs
http://www.sugarlabs.org
More information about the Sugar-devel
mailing list