-
Notifications
You must be signed in to change notification settings - Fork 0
/
chalk_core.py
176 lines (128 loc) · 4.58 KB
/
chalk_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
try:
import svgwrite
except:
print 'Please install svgwrite (http://pypi.python.org/pypi/svgwrite/)'
exit(1)
import re
import os
from settings import *
def draw(datastructure, filename='chalk.svg', style_sheet=None):
"""
Creates SVG drawing of the given datastructure, and saves it.
If a filename is given then the resulting drawing is saved in that file,
otherwise the default chalk.svg is written.
"""
try:
dwg = svgwrite.Drawing(filename, profile='full')
style_sheet_content = ''
if style_sheet is None:
print 'css not given'
style_sheet = os.path.dirname(os.path.abspath(__file__)) + '/' + STYLE_SHEET
print 'style_sheet', style_sheet
with open(style_sheet, 'r') as css_file:
style_sheet_content = ''.join([ re.sub('\s', ' ', line) for line in css_file])
dwg.defs.add( dwg.style(style_sheet_content))
w,h = draw_ds(datastructure, dwg)
dwg.attribs['width'] = w
dwg.attribs['height'] = h
dwg.save()
print "final image dimensions %s X %s" % (dwg.attribs['width'], dwg.attribs['height'])
except:
import traceback
traceback.print_exc()
def draw_ds(ds, dwg, x=0.0, y=0.0):
if type(ds) is list:
w,h = draw_iterable(ds, dwg, x, y)
elif type(ds) is tuple:
w,h = draw_iterable(ds, dwg, x, y)
elif type(ds) is dict:
w,h = draw_iterable(ds, dwg, x, y)
elif type(ds) is int:
w,h = draw_int(ds, dwg, x, y)
elif type(ds) is str:
w,h = draw_str(ds, dwg, x, y)
elif hasattr(ds, '__dict__'):
w,h = draw_iterable(ds.__dict__, dwg, x,y)
return (w, h)
def draw_iterable(ds, dwg, x, y):
g = dwg.g(class_=LIST_CLASS)
w = 0 #GLYPH_WIDTH*len(title)
h = PADDING #GLYPH_HEIGHT + 2*PADDING
ix = x+PADDING
iy = y+PADDING #+GLYPH_HEIGHT+MARGIN
index_digits = len(str(len(ds)-1))
for i,item in enumerate(ds):
index = '%'+str(index_digits)+'d'
index = index % i
if type(ds) is dict:
iw, ih = draw_list_item(item, ds[item], ds, dwg, ix, iy)
else:
iw, ih = draw_list_item(index, item, ds, dwg, ix, iy)
ix += 0 # no horizontal shift
iy += ih + MARGIN
w = max(w, iw)
h += ih+MARGIN
if len(ds) > 0:
h -= MARGIN
# the rectangle around the contents of the list
h += PADDING
w += 2*PADDING
r = None
if type(ds) is tuple:
r = dwg.rect(insert=(x, y), size=(w, h), rx=PADDING, class_='container')
else:
r = dwg.rect(insert=(x, y), size=(w, h), class_='container')
g.add(r)
dwg.add(g)
return (w,h)
def draw_list_item(index, ds, parent_ds, dwg, x, y):
print('drawing [%s]%s' % (index,ds))
w = 0
h = GLYPH_HEIGHT+2*PADDING
g = dwg.g(class_=LIST_ITEM_CLASS)
type_tag_margin = GLYPH_HEIGHT
# draw the index
gi = dwg.g(class_=LIST_INDEX_CLASS)
index_width = GLYPH_WIDTH*len(index) + PADDING + type_tag_margin
index_height = GLYPH_HEIGHT + 2*PADDING
if type(parent_ds) is dict:
points = []
points.append((x+type_tag_margin,y))
points.append((x+index_width,y))
points.append((x+index_width,y+index_height))
points.append((x+type_tag_margin,y+index_height))
points.append((x,y+index_height/2))
gi.add(dwg.polygon(points=points))
else:
gi.add(dwg.rect(insert=(x, y), size=(index_width, index_height)))
gi.add(dwg.text(index, insert=(x+type_tag_margin, y+PADDING+GLYPH_HEIGHT)))
g.add(gi)
w += index_width
# ww, _ = draw_int(ds, dwg, '', x+w, y)
ww, hh = draw_ds(ds, dwg, x+w, y)
w = w + ww
h = max(h, hh)
dwg.add(g)
return (w, h)
def draw_int(ds, dwg, x, y):
print('drawing %s' % (ds))
w = 0
h = GLYPH_HEIGHT+2*PADDING
# draw the int
g = dwg.g(class_=INT_CLASS)
g.add(dwg.rect(insert=(x+w, y), size=(GLYPH_WIDTH*len(str(ds))+2*PADDING, GLYPH_HEIGHT+2*PADDING)))
g.add(dwg.text(str(ds), insert=(x+w+PADDING, y+PADDING+GLYPH_HEIGHT)))
w += GLYPH_WIDTH*len(str(ds))+2*PADDING
dwg.add(g)
return (w, GLYPH_HEIGHT+2*PADDING)
def draw_str(ds, dwg, x, y):
print('str:%s' % (ds))
w = 0
h = GLYPH_HEIGHT+2*PADDING
# draw the int
g = dwg.g(class_=STR_CLASS)
g.add(dwg.rect(insert=(x+w, y), size=(GLYPH_WIDTH*len(str(ds))+2*PADDING, GLYPH_HEIGHT+2*PADDING)))
g.add(dwg.text(str(ds), insert=(x+w+PADDING, y+PADDING+GLYPH_HEIGHT)))
w += GLYPH_WIDTH*len(str(ds))+2*PADDING
dwg.add(g)
return (w, GLYPH_HEIGHT+2*PADDING)