Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions suml/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
57 changes: 2 additions & 55 deletions suml/scruffy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
Expand All @@ -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'):
Expand Down Expand Up @@ -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()
27 changes: 16 additions & 11 deletions suml/suml2pic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\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)
24 changes: 18 additions & 6 deletions suml/yuml2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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:]]
Expand Down Expand Up @@ -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 = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\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)