diff --git a/suml/common.py b/suml/common.py index 45c39af..80b4890 100644 --- a/suml/common.py +++ b/suml/common.py @@ -93,3 +93,10 @@ def crop(fin, fout): diff = ImageChops.difference(img, bg) area = img.crop(diff.getbbox()) area.save(fout, 'png') + +def clear(root): + g = root.findall('.//{http://www.w3.org/2000/svg}g[@id=\'graph0\']')[0] + polygons = root.findall('.//{http://www.w3.org/2000/svg}g[@id=\'graph0\']/{http://www.w3.org/2000/svg}polygon') + + for polygon in polygons: + g.remove(polygon) diff --git a/suml/scruffy.py b/suml/scruffy.py index 350f2e1..5bba0a9 100755 --- a/suml/scruffy.py +++ b/suml/scruffy.py @@ -123,10 +123,6 @@ def transformText(elem, font): elem.attrib['font-family'] = font def transformAddShade(root, elem): - if elem.get('fill', '') == 'white' and elem.get('stroke', '') == 'white': - # Graphviz puts everything in one big polygon. Skip it! - return - # Need to prepend element of the same shape shade = root.makeelement(elem.tag, elem.attrib) for i, child in enumerate(root): @@ -143,10 +139,6 @@ def transformAddShade(root, elem): #shade.attrib['style'] = 'opacity:0.75;filter:url(#filterBlur)' def transformAddGradient(elem): - if elem.get('fill', '') == 'white' and elem.get('stroke', '') == 'white': - # Graphviz puts everything in one big polygon. Skip it! - return - fill = elem.get('fill', '') if fill == 'none': elem.attrib['fill'] = 'white' @@ -156,6 +148,7 @@ def transformAddGradient(elem): def _transform(root, options, level=0): for child in root[:]: + if child.tag == ns('rect'): transformRect2Polygon(child) elif child.tag == ns('line'): @@ -191,56 +184,10 @@ def _transform(root, options, level=0): etree.SubElement(gradient, ns('stop'), {'offset':'0%', 'style':'stop-color:white;stop-opacity:1'}) etree.SubElement(gradient, ns('stop'), {'offset':'50%', 'style':'stop-color:%s;stop-opacity:1' % name}) -def transform(fin, fout, options): - ''' - Read svg from file object fin, write output to file object fout - - options.png (boolean) Try to produce PNG output - options.font (string) Font family to use (Ubuntu: Purisa) - ''' - etree.register_namespace('', 'http://www.w3.org/2000/svg') - root = etree.parse(fin).getroot() - - +def transform(root, options): w, h = root.attrib.get('width', ''), root.attrib.get('height', '') if w.endswith('in') or h.endswith('in'): global gCoordinates gCoordinates = 'in' _transform(root, options) - - scruffySvg = etree.tostring(root) + '\n' - - if options.png: - import subprocess - png = subprocess.Popen(['rsvg-convert', '-f', 'png'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=scruffySvg)[0] - fout.write(png) - else: - fout.write(scruffySvg) - -def main(): - import optparse - - parser = optparse.OptionParser(usage='usage: %prog [options] [input file]') - parser.add_option('-p', '--png', action='store_true', dest='png', - help='create a png file (requires rsvg-convert)') - parser.add_option('-o', '--output', action='store', dest='output', - help='output file name') - parser.add_option('--font-family', action='store', dest='font', - help='set output font family') - (options, args) = parser.parse_args() - - if len(args) > 1: - parser.error('Too many arguments') - - fin, fout = sys.stdin, sys.stdout - if options.output: - fout = open(options.output, 'wb') - - if len(args) > 0: - fin = open(args[0]) - - transform(fin, fout, options) - -if __name__ == '__main__': - main() diff --git a/suml/suml2pic.py b/suml/suml2pic.py index a1ebae2..b0b3602 100755 --- a/suml/suml2pic.py +++ b/suml/suml2pic.py @@ -26,6 +26,7 @@ ''' import os +import xml.etree.ElementTree as etree from . import common sequence_pic = os.path.join(os.path.dirname(__file__), 'sequence.pic') @@ -129,20 +130,24 @@ def transform(expr, fout, options): import subprocess import StringIO + svg = subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0] + + etree.register_namespace('', 'http://www.w3.org/2000/svg') + root = etree.parse(StringIO.StringIO(svg)).getroot() + + common.clear(root) + if options.scruffy: import scruffy - svg = subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0] - if options.png: - tocrop = StringIO.StringIO() - scruffy.transform(StringIO.StringIO(svg), tocrop, options) - common.crop(StringIO.StringIO(tocrop.getvalue()), fout) - else: - scruffy.transform(StringIO.StringIO(svg), fout, options) - elif options.png: - png = subprocess.Popen(['pic2plot', '-Tpng'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0] - common.crop(StringIO.StringIO(png), fout) + scruffy.transform(root, options) + + svg = '\n' + etree.tostring(root) + '\n' + + if options.png: + png = subprocess.Popen(['convert', '-', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=svg)[0] + fout.write(png) elif options.svg: - subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=fout).communicate(input=pic) + fout.write(svg) else: fout.write(pic) diff --git a/suml/yuml2dot.py b/suml/yuml2dot.py index ce0ec98..03f15d3 100755 --- a/suml/yuml2dot.py +++ b/suml/yuml2dot.py @@ -49,6 +49,7 @@ import textwrap import common +import xml.etree.ElementTree as etree def escape_token_escapes(spec): return spec.replace('\\[', '\\u005b').replace('\\]', '\\u005d') @@ -95,6 +96,7 @@ def yumlExpr(spec): if part.startswith('note:'): expr.append(('note', unescape_token_escapes(part[5:].strip()), bg)) elif '[' in part and ']' in part: + p = part.split('[') part = p[0] nodes = [node.replace(']', '').strip() for node in p[1:]] @@ -255,16 +257,26 @@ def transform(expr, fout, options): if options.png or options.svg: import subprocess + import StringIO + + svg = subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=dot)[0] + + etree.register_namespace('', 'http://www.w3.org/2000/svg') + root = etree.parse(StringIO.StringIO(svg)).getroot() + + common.clear(root) if options.scruffy: - import StringIO import scruffy - svg = subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=dot)[0] - scruffy.transform(StringIO.StringIO(svg), fout, options) - elif options.png: - subprocess.Popen(['dot', '-Tpng'], stdin=subprocess.PIPE, stdout=fout).communicate(input=dot) + scruffy.transform(root, options) + + svg = '\n' + etree.tostring(root) + '\n' + + if options.png: + png = subprocess.Popen(['convert', '-', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=svg)[0] + fout.write(png) elif options.svg: - subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=fout).communicate(input=dot) + fout.write(svg) else: fout.write(dot)