Skip to content

Commit a0dd8f5

Browse files
author
Artus Kolanowski
committed
Add transparent background
1 parent a1e652c commit a0dd8f5

File tree

4 files changed

+43
-72
lines changed

4 files changed

+43
-72
lines changed

suml/common.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,10 @@ def crop(fin, fout):
9393
diff = ImageChops.difference(img, bg)
9494
area = img.crop(diff.getbbox())
9595
area.save(fout, 'png')
96+
97+
def clear(root):
98+
g = root.findall('.//{http://www.w3.org/2000/svg}g[@id=\'graph0\']')[0]
99+
polygons = root.findall('.//{http://www.w3.org/2000/svg}g[@id=\'graph0\']/{http://www.w3.org/2000/svg}polygon')
100+
101+
for polygon in polygons:
102+
g.remove(polygon)

suml/scruffy.py

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ def transformText(elem, font):
123123
elem.attrib['font-family'] = font
124124

125125
def transformAddShade(root, elem):
126-
if elem.get('fill', '') == 'white' and elem.get('stroke', '') == 'white':
127-
# Graphviz puts everything in one big polygon. Skip it!
128-
return
129-
130126
# Need to prepend element of the same shape
131127
shade = root.makeelement(elem.tag, elem.attrib)
132128
for i, child in enumerate(root):
@@ -143,10 +139,6 @@ def transformAddShade(root, elem):
143139
#shade.attrib['style'] = 'opacity:0.75;filter:url(#filterBlur)'
144140

145141
def transformAddGradient(elem):
146-
if elem.get('fill', '') == 'white' and elem.get('stroke', '') == 'white':
147-
# Graphviz puts everything in one big polygon. Skip it!
148-
return
149-
150142
fill = elem.get('fill', '')
151143
if fill == 'none':
152144
elem.attrib['fill'] = 'white'
@@ -156,6 +148,7 @@ def transformAddGradient(elem):
156148

157149
def _transform(root, options, level=0):
158150
for child in root[:]:
151+
159152
if child.tag == ns('rect'):
160153
transformRect2Polygon(child)
161154
elif child.tag == ns('line'):
@@ -191,56 +184,10 @@ def _transform(root, options, level=0):
191184
etree.SubElement(gradient, ns('stop'), {'offset':'0%', 'style':'stop-color:white;stop-opacity:1'})
192185
etree.SubElement(gradient, ns('stop'), {'offset':'50%', 'style':'stop-color:%s;stop-opacity:1' % name})
193186

194-
def transform(fin, fout, options):
195-
'''
196-
Read svg from file object fin, write output to file object fout
197-
198-
options.png (boolean) Try to produce PNG output
199-
options.font (string) Font family to use (Ubuntu: Purisa)
200-
'''
201-
etree.register_namespace('', 'http://www.w3.org/2000/svg')
202-
root = etree.parse(fin).getroot()
203-
204-
187+
def transform(root, options):
205188
w, h = root.attrib.get('width', ''), root.attrib.get('height', '')
206189
if w.endswith('in') or h.endswith('in'):
207190
global gCoordinates
208191
gCoordinates = 'in'
209192

210193
_transform(root, options)
211-
212-
scruffySvg = etree.tostring(root) + '\n'
213-
214-
if options.png:
215-
import subprocess
216-
png = subprocess.Popen(['rsvg-convert', '-f', 'png'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=scruffySvg)[0]
217-
fout.write(png)
218-
else:
219-
fout.write(scruffySvg)
220-
221-
def main():
222-
import optparse
223-
224-
parser = optparse.OptionParser(usage='usage: %prog [options] [input file]')
225-
parser.add_option('-p', '--png', action='store_true', dest='png',
226-
help='create a png file (requires rsvg-convert)')
227-
parser.add_option('-o', '--output', action='store', dest='output',
228-
help='output file name')
229-
parser.add_option('--font-family', action='store', dest='font',
230-
help='set output font family')
231-
(options, args) = parser.parse_args()
232-
233-
if len(args) > 1:
234-
parser.error('Too many arguments')
235-
236-
fin, fout = sys.stdin, sys.stdout
237-
if options.output:
238-
fout = open(options.output, 'wb')
239-
240-
if len(args) > 0:
241-
fin = open(args[0])
242-
243-
transform(fin, fout, options)
244-
245-
if __name__ == '__main__':
246-
main()

suml/suml2pic.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'''
2727

2828
import os
29+
import xml.etree.ElementTree as etree
2930
from . import common
3031

3132
sequence_pic = os.path.join(os.path.dirname(__file__), 'sequence.pic')
@@ -129,20 +130,24 @@ def transform(expr, fout, options):
129130
import subprocess
130131
import StringIO
131132

133+
svg = subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0]
134+
135+
etree.register_namespace('', 'http://www.w3.org/2000/svg')
136+
root = etree.parse(StringIO.StringIO(svg)).getroot()
137+
138+
common.clear(root)
139+
132140
if options.scruffy:
133141
import scruffy
134142

135-
svg = subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0]
136-
if options.png:
137-
tocrop = StringIO.StringIO()
138-
scruffy.transform(StringIO.StringIO(svg), tocrop, options)
139-
common.crop(StringIO.StringIO(tocrop.getvalue()), fout)
140-
else:
141-
scruffy.transform(StringIO.StringIO(svg), fout, options)
142-
elif options.png:
143-
png = subprocess.Popen(['pic2plot', '-Tpng'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=pic)[0]
144-
common.crop(StringIO.StringIO(png), fout)
143+
scruffy.transform(root, options)
144+
145+
svg = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n' + etree.tostring(root) + '\n'
146+
147+
if options.png:
148+
png = subprocess.Popen(['convert', '-', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=svg)[0]
149+
fout.write(png)
145150
elif options.svg:
146-
subprocess.Popen(['pic2plot', '-Tsvg'], stdin=subprocess.PIPE, stdout=fout).communicate(input=pic)
151+
fout.write(svg)
147152
else:
148153
fout.write(pic)

suml/yuml2dot.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
import textwrap
5151
import common
52+
import xml.etree.ElementTree as etree
5253

5354
def escape_token_escapes(spec):
5455
return spec.replace('\\[', '\\u005b').replace('\\]', '\\u005d')
@@ -95,6 +96,7 @@ def yumlExpr(spec):
9596
if part.startswith('note:'):
9697
expr.append(('note', unescape_token_escapes(part[5:].strip()), bg))
9798
elif '[' in part and ']' in part:
99+
98100
p = part.split('[')
99101
part = p[0]
100102
nodes = [node.replace(']', '').strip() for node in p[1:]]
@@ -255,16 +257,26 @@ def transform(expr, fout, options):
255257

256258
if options.png or options.svg:
257259
import subprocess
260+
import StringIO
261+
262+
svg = subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=dot)[0]
263+
264+
etree.register_namespace('', 'http://www.w3.org/2000/svg')
265+
root = etree.parse(StringIO.StringIO(svg)).getroot()
266+
267+
common.clear(root)
258268

259269
if options.scruffy:
260-
import StringIO
261270
import scruffy
262271

263-
svg = subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=dot)[0]
264-
scruffy.transform(StringIO.StringIO(svg), fout, options)
265-
elif options.png:
266-
subprocess.Popen(['dot', '-Tpng'], stdin=subprocess.PIPE, stdout=fout).communicate(input=dot)
272+
scruffy.transform(root, options)
273+
274+
svg = '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>\n' + etree.tostring(root) + '\n'
275+
276+
if options.png:
277+
png = subprocess.Popen(['convert', '-', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate(input=svg)[0]
278+
fout.write(png)
267279
elif options.svg:
268-
subprocess.Popen(['dot', '-Tsvg'], stdin=subprocess.PIPE, stdout=fout).communicate(input=dot)
280+
fout.write(svg)
269281
else:
270282
fout.write(dot)

0 commit comments

Comments
 (0)