-
Notifications
You must be signed in to change notification settings - Fork 79
/
Converter.py
638 lines (438 loc) · 26.1 KB
/
Converter.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#!/usr/bin/env python
'''
A program that converts pseudocode to .png flowcharts
'''
import re
import os
from math import log, floor
import click
from PIL import Image, ImageDraw, ImageFont
from tree import newTree,newNode
__author__ = "Mugilan Ganesan"
__email__ = "[email protected]"
__status__ = "Developer"
__version__ = "1.1.0"
def read(file_name):
#get raw lines out of txt
converter = os.path.realpath(__file__)
self_path = re.compile(r'(.+)Converter.py')
text_path = re.search(self_path,converter).group(1) + file_name
text_file = open(text_path,"r")
lines = text_file.readlines()
text_file.close()
# Basic Preprocessor
processed_lines = []
for_re = re.compile(r'^FOR\s(.+)\s<-\s(.+)\sTO\s(.+)')
next_re = re.compile(r'^NEXT\s(.+)')
processed_lines.append("START")
for line in lines:
line = line.rstrip().lstrip()
if re.search(for_re,line):
var = re.search(for_re,line).group(1)
low = re.search(for_re,line).group(2)
high = re.search(for_re,line).group(3)
processed_lines.append(var + " = " + low)
processed_lines.append("WHILE " + var + " < " + high + " DO")
elif re.search(next_re,line):
var = re.search(next_re,line).group(1)
processed_lines.append(var + " = " + var + " + 1")
processed_lines.append("ENDWHILE")
elif line == '':
pass
else:
processed_lines.append(line)
processed_lines.append("STOP")
return processed_lines
def translation(lines,font_data):
# All the different fundamental types of statements
input_re = re.compile(r'^INPUT\s(.+)')
output_re = re.compile(r'^OUTPUT\s(.+)')
if_re = re.compile(r'^IF\s(.+)\sTHEN$')
else_re = re.compile(r'^ELSE$')
endif_re = re.compile(r'^ENDIF$')
while_re = re.compile(r'^WHILE\s(.+)\sDO$')
endwhile_re = re.compile(r'^ENDWHILE$')
chart_code = []
amount_per_branch = {}
beginning_of_split = {}
branch_width = {} #keeps track of the maximum width of each branch
layer_height = {} #keeps track of the maximum height of each layer
font_path = font_data['path']
font_size = font_data['size']
font = ImageFont.truetype(font_path, font_size)
img = Image.new('RGB', (10, 10), color = 'white')
draw = ImageDraw.Draw(img)
x = [1] # stack used to navigate the pseudocode as a binary tree
y = [1] # stack used to navigate the layers
# This for loop converts each line into a block
# A block will have content,role,position,type keys
# The role is what type of flow line connection it makes
# Content is its data / the line itself
# Position is a set of X and Y coords on a "grid"
# Type can be the block type like Decision or Terminator
# The fundamental operation is first to identify what type of block it is,
# with regex. Then it is drawn out on a dummy image to see what its width and height is.
# These widths are used to figure out what the maximum width of each branch is.
# Likewise the heights are used to figure out the maximum height of the layer the block is on,
# compared to the other blocks on its same layer / y-coordinate
# Then the block is added to the chart code with its relevant attributes
for i in range(0,len(lines)):
line = lines[i]
if x[-1] not in amount_per_branch:
amount_per_branch[x[-1]] = 0
beginning_of_split[x[-1]] = 0
branch_width[x[-1]] = 0
if y[-1] not in layer_height:
layer_height[y[-1]] = draw.textsize("a",font=font)[1] + font_size
if line == "START" or line == "STOP":
if line == "STOP":
chart_code.append({"type":"Terminator","content":line,"position":[x[-1],y[-1]],"role":'t'})
else:
chart_code.append({"type":"Terminator","content":line,"position":[x[-1],y[-1]],"role":'n'})
if draw.textsize(line,font=font)[0] + font_size * 2 > branch_width[x[-1]]:
branch_width[x[-1]] = draw.textsize(line,font=font)[0] + font_size * 2
if draw.textsize(line,font=font)[1] + font_size + 2*font_size > layer_height[y[-1]]:
layer_height[y[-1]] = draw.textsize(line,font=font)[1] + 2*font_size
amount_per_branch[x[-1]] += 1
y[-1] +=1
elif re.search(input_re,line) or re.search(output_re,line):
chart_code.append({"type":"IO","content":line,"position":[x[-1],y[-1]],"role":'n'})
if draw.textsize(line,font=font)[0] + font_size * 2 > branch_width[x[-1]]:
branch_width[x[-1]] = draw.textsize(line,font=font)[0] + font_size * 2
if draw.textsize(line,font=font)[1] + font_size > layer_height[y[-1]]:
layer_height[y[-1]] = draw.textsize(line,font=font)[1] + font_size
amount_per_branch[x[-1]] += 1
y[-1] +=1
elif re.search(if_re,line):
chart_code.append({"type":"Decision","content":re.search(if_re,line).group(1),"position":[x[-1],y[-1]],"role":'o'})
if len(line) > len("IF")+1:
width = 3/2*draw.textsize(line,font=font)[0]
height = 5*draw.textsize(line,font=font)[1]
else:
width = 5*font_size
height = 5*font_size
if width > branch_width[x[-1]]:
branch_width[x[-1]] = width
if height > layer_height[y[-1]]:
layer_height[y[-1]] = height
amount_per_branch[x[-1]] += 1
beginning_of_split[x[-1]] = y[-1]
x.append(2*x[-1])
y.append(y[-1]+1)
elif re.search(else_re,line):
if x[-1] % 2 == 0:
x[-1] += 1
y[-1] = y[-2] + 1
else:
x = x[:-1]
y = y[:-1]
elif re.search(endif_re,line):
if x[-1] % 2 == 0:
x[-1] += 1
y[-1] = y[-2] + 1
if x[-1] not in amount_per_branch:
amount_per_branch[x[-1]] = 0
beginning_of_split[x[-1]] = 0
branch_width[x[-1]] = 0
if y[-1] not in layer_height:
layer_height[y[-1]] = draw.textsize("a",font=font)[1] + font_size
chart_code.append({"type":"Connector","content":"c","position":[x[-1],y[-1]],"role":'e'})
amount_per_branch[x[-1]] += 1
y[-1] +=1
if amount_per_branch[x[-1]-1] > amount_per_branch[x[-1]]:
amount_per_branch[x[-2]] += amount_per_branch[x[-1]-1]
y[-2] = amount_per_branch[x[-1]-1] + beginning_of_split[x[-2]] + 1
else:
amount_per_branch[x[-2]] += amount_per_branch[x[-1]]
y[-2] = amount_per_branch[x[-1]] + beginning_of_split[x[-2]] + 1
amount_per_branch[x[-1]] = 0
amount_per_branch[x[-1]-1] = 0
y = y[:-1]
x = x[:-1]
chart_code.append({"type":"Connector","content":"cB","position":[x[-1],y[-1]],"role":'cB'})
amount_per_branch[x[-1]] += 1
y[-1] += 1
elif re.search(while_re,line):
chart_code.append({"type":"Decision","content":re.search(while_re,line).group(1),"position":[x[-1],y[-1]],"role":'o'})
if len(line) > len("IF")+1:
width = 3/2*draw.textsize(line,font=font)[0]
height = 5*draw.textsize(line,font=font)[1]
else:
width = 5*font_size
height = 5*font_size
if width > branch_width[x[-1]]:
branch_width[x[-1]] = width
if height > layer_height[y[-1]]:
layer_height[y[-1]] = height
amount_per_branch[x[-1]] += 1
beginning_of_split[x[-1]] = y[-1]
x.append(2*x[-1])
y.append(y[-1]+1)
elif re.search(endwhile_re,line):
x[-1] += 1
y[-1] = y[-2] + 1
if x[-1] not in amount_per_branch:
amount_per_branch[x[-1]] = 0
beginning_of_split[x[-1]] = 0
branch_width[x[-1]] = 0
if y[-1] not in layer_height:
layer_height[y[-1]] = draw.textsize("a",font=font)[1] + font_size
chart_code.append({"type":"Connector","content":"c","position":[x[-1],y[-1]],"role":'e'})
amount_per_branch[x[-1]] += 1
y[-1] +=1
if amount_per_branch[x[-1]-1] > amount_per_branch[x[-1]]:
amount_per_branch[x[-2]] += amount_per_branch[x[-1]-1]
y[-2] = amount_per_branch[x[-1]-1] + beginning_of_split[x[-2]] + 1
else:
amount_per_branch[x[-2]] += amount_per_branch[x[-1]]
y[-2] = amount_per_branch[x[-1]] + beginning_of_split[x[-2]] + 1
amount_per_branch[x[-1]] = 0
amount_per_branch[x[-1]-1] = 0
y = y[:-1]
x = x[:-1]
chart_code.append({"type":"Connector","content":"c","position":[x[-1],y[-1]],"role":'cW'})
amount_per_branch[x[-1]] += 1
y[-1] += 1
else:
chart_code.append({"type":"Process","content":line,"position":[x[-1],y[-1]],"role":'n'})
if draw.textsize(line,font=font)[0] + font_size * 2 > branch_width[x[-1]]:
branch_width[x[-1]] = draw.textsize(line,font=font)[0] + font_size * 2
if draw.textsize(line,font=font)[1] + font_size > layer_height[y[-1]]:
layer_height[y[-1]] = draw.textsize(line,font=font)[1] + font_size
amount_per_branch[x[-1]] += 1
y[-1] +=1
del draw,img
max_branch = 2 ** (floor(log(max(amount_per_branch),2))+1) - 1 #biggest branch
max_y = amount_per_branch[1] #the last layer in the flowchart
return chart_code,max_branch,max_y,layer_height,branch_width
def drawer(chart_code,max_branch,max_y,layer_height,branch_width,font_data):
def Terminator(text,position):
width = draw.textsize(text,font=font)[0] + font_size * 2
height = draw.textsize(text,font=font)[1] + font_size * 2
x = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2 - width/2
y = combined_heights[position[1]]
coords = [(x,y),(x+width,y+height)]
draw.ellipse(coords,fill=None,outline="black")
draw.text((x+font_size,y+height/2-scale_constant), text, fill='black',font=font)
return height
def Decision(text,position):
def diamond(coords,width,height):
draw.line([(coords[0]+width/2,coords[1]),(coords[0]+width,coords[1]+height/2)],fill='black', width=1)
draw.line([(coords[0]+width/2,coords[1]),(coords[0],coords[1]+height/2)],fill='black', width=1)
draw.line([(coords[0],coords[1]+height/2),(coords[0]+width/2,coords[1]+height)],fill='black', width=1)
draw.line([(coords[0]+width,coords[1]+height/2),(coords[0]+width/2,coords[1]+height)],fill='black', width=1)
if len(text) > len("IF")+1:
width = 3/2*draw.textsize(text,font=font)[0]
height = 5*draw.textsize(text,font=font)[1]
text_offset = draw.textsize(text,font=font)[0]/4
else:
width = 5*font_size
height = 5*font_size
text_offset = (width - draw.textsize(text,font=font)[0])/2
x = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2 - width/2
y = combined_heights[position[1]]
diamond((x,y),width,height)
draw.text((x+width/2-scale_constant,y+height/4-scale_constant), "IF", fill='black',font=font)
draw.text((x+text_offset,y+height/2-scale_constant), text, fill='black',font=font)
return height,width
def Process(text,position):
width = draw.textsize(text,font=font)[0] + font_size * 2
height = draw.textsize(text,font=font)[1] + font_size
x = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2 - width/2
y = combined_heights[position[1]]
coords = [(x,y),(x+width,y+height)]
draw.rectangle(coords,fill=None,outline='black')
draw.text((x+font_size,y+scale_constant), text, fill='black',font=font)
return height
def IO(text,position):
def parallelogram(coords,width,height,offset):
draw.line([(coords[0],coords[1]+height),(coords[0]+width,coords[1]+height)] ,fill='black', width=1)
draw.line([(coords[0]+offset,coords[1]),(coords[0]+width+offset,coords[1])] ,fill='black', width=1)
draw.line([(coords[0]+offset,coords[1]),(coords[0],coords[1]+height)] ,fill='black', width=1)
draw.line([(coords[0]+width+offset,coords[1]),(coords[0]+width,coords[1]+height)] ,fill='black', width=1)
width = draw.textsize(text,font=font)[0] + font_size * 2
height = draw.textsize(text,font=font)[1] + font_size
offset = font_size
x = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2 - width/2 - offset/2
y = combined_heights[position[1]]
parallelogram((x,y),width,height,offset)
draw.text((x+font_size + offset/2,y+scale_constant), text, fill='black',font=font)
return height
def Flowline(role,position,height,chart_code,width):
def arrow(coords, height,direction):
h = int(height)
L = int(height)
if direction == "left":
draw.line([(coords[0],coords[1]),(coords[0]+L,coords[1]+h)], fill='black', width=1)
draw.line([(coords[0],coords[1]),(coords[0]+L,coords[1]-h)], fill='black', width=1)
elif direction == "right":
draw.line([(coords[0],coords[1]),(coords[0]-L,coords[1]+h)], fill='black', width=1)
draw.line([(coords[0],coords[1]),(coords[0]-L,coords[1]-h)], fill='black', width=1)
elif direction == "down":
draw.line([(coords[0],coords[1]),(coords[0]+L,coords[1]-h)], fill='black', width=1)
draw.line([(coords[0],coords[1]),(coords[0]-L,coords[1]-h)], fill='black', width=1)
elif direction == "up":
draw.line([(coords[0],coords[1]),(coords[0]+L,coords[1]+h)], fill='black', width=1)
draw.line([(coords[0],coords[1]),(coords[0]-L,coords[1]+h)], fill='black', width=1)
# These are the various roles:
# n means that flowlines will be drawn directly after the block to the next layer
# o means that an If statement has opened two branches
# cW means that a connector object will close a while loop
# cB means that a connector will close an if statement's two branches by merging them
# t means that no flowlines will be drawn (they are terminated) after the block
# e means that it is an empty block so the lines can pass right through it
if role == 'n':
axis = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2
start = combined_heights[position[1]]+height
distance = combined_heights[position[1]+1] - start
draw.line([(axis,start),(axis,start+distance)], fill='black', width=1)
arrow([axis,start+2*distance/3],block_gap/3,"down")
elif role == 'e':
axis = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2
start = combined_heights[position[1]]
distance = combined_heights[position[1]+1] - start
draw.line([(axis,start),(axis,start+distance)], fill='black', width=1)
elif role == 'o':
if_axis = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2
start = combined_heights[position[1]]+height/2
end = combined_heights[position[1]+1]
even_axis = width_offset + combined_widths[2*position[0]] + branch_width[2*position[0]]/2
odd_axis = width_offset + combined_widths[2*position[0]+1] + branch_width[2*position[0]+1]/2
draw.line([(if_axis-width/2,start),(even_axis,start)], fill='black', width=1)
arrow([(even_axis+if_axis - width/2)/2 - block_gap/3,start],block_gap/3,"left")
draw.line([(if_axis+width/2,start),(odd_axis,start)], fill='black', width=1)
arrow([(odd_axis+if_axis + width/2)/2 + block_gap/3,start],block_gap/3,"right")
draw.line([(even_axis,start),(even_axis,end)], fill='black', width=1)
arrow([even_axis,(start+end)/2 + block_gap/3],block_gap/3,"down")
draw.line([(odd_axis,start),(odd_axis,end)], fill='black', width=1)
arrow([odd_axis,(start+end)/2 + block_gap/3],block_gap/3,"down")
draw.text(( (if_axis - width/2 + even_axis)/2 - block_gap/2,start-2*font_size), "yes", fill='black',font=font)
draw.text(( (if_axis + width/2 + odd_axis)/2 - block_gap/3,start-2*font_size), "no", fill='black',font=font)
elif role == 'cW':
axis = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2
start = combined_heights[position[1]]+height/2
distance = combined_heights[position[1]+1] - start
even_branch = [position[0]*2,0]
odd_branch = [position[0]*2+1,0]
associated_while = [position[0],0]
# This searches for the ENDWHILE's corresponding while loop
# It also looks for the branches created by that while
for obj in reversed(chart_code):
if obj['position'][0] == even_branch[0] and even_branch[1] == 0:
even_branch[1] = obj['position'][1]
if obj['position'][0] == odd_branch[0] and odd_branch[1] == 0:
odd_branch[1] = obj['position'][1]
if obj['position'][0] == associated_while[0] and associated_while[1] == 0:
associated_while[1] = obj['position'][1]
if even_branch[1] != 0 and associated_while[1] !=0 and odd_branch[1] !=0:
break
even_axis = width_offset + combined_widths[2*position[0]] + branch_width[2*position[0]]/2
draw.line([(axis,start),(even_axis,start)], fill='black', width=1)
arrow([(axis+even_axis)/2 - block_gap/3,start],block_gap/3,"right")
start = combined_heights[even_branch[1]+1]
distance = combined_heights[position[1]]+height/2 - start
draw.line([(even_axis,start),(even_axis,start+distance)], fill='black', width=1)
while_height = combined_heights[associated_while[1]+1] - block_gap
draw.line([(axis,while_height),(axis,start+distance)], fill='black', width=1)
arrow([axis,(while_height+start+distance)/2 - block_gap/3],block_gap/3,"up")
odd_axis = width_offset + combined_widths[2*position[0]+1] + branch_width[2*position[0]+1]/2
start = combined_heights[odd_branch[1]]
distance = combined_heights[position[1]]+height - start
draw.line([(odd_axis,start),(odd_axis,start+distance)], fill='black', width=1)
draw.line([(odd_axis,start+distance),(axis,start+distance)], fill='black', width=1)
arrow([(axis+odd_axis)/2 - block_gap/3,start+distance],block_gap/3,"left")
draw.line([(axis,start+distance),(axis,start+distance+block_gap)], fill='black', width=1)
arrow([axis,start+distance+block_gap*2/3],block_gap/3,"down")
elif role == 'cB':
axis = width_offset + combined_widths[position[0]] + branch_width[position[0]]/2
start = combined_heights[position[1]]+height/2
distance = combined_heights[position[1]+1] - start
draw.line([(axis,start),(axis,start+distance)], fill='black', width=1)
arrow([axis,start+2*distance/3],block_gap/3,"down")
even_branch = [position[0]*2,0]
odd_branch = [position[0]*2+1,0]
# This searches for the last objects in the two branches created by an IF statement
# This IF statement is the one that is closed by the ENDIF
for obj in reversed(chart_code):
if obj['position'][0] == odd_branch[0] and odd_branch[1] == 0:
odd_branch[1] = obj['position'][1]
elif obj['position'][0] == even_branch[0] and even_branch[1] == 0:
even_branch[1] = obj['position'][1]
if even_branch[1] != 0 and odd_branch[1] != 0:
break
even_axis = width_offset + combined_widths[2*position[0]] + branch_width[2*position[0]]/2
odd_axis = width_offset + combined_widths[2*position[0]+1] + branch_width[2*position[0]+1]/2
draw.line([(axis,start),(even_axis,start)], fill='black', width=1)
arrow([(axis+even_axis)/2 - block_gap/3,start],block_gap/3,"right")
draw.line([(axis,start),(odd_axis,start)], fill='black', width=1)
arrow([(axis+odd_axis)/2 + block_gap/3,start],block_gap/3,"left")
start = combined_heights[even_branch[1]+1]
distance = combined_heights[position[1]]+height/2 - start
draw.line([(even_axis,start),(even_axis,start+distance)], fill='black', width=1)
start = combined_heights[odd_branch[1]+1]
distance = combined_heights[position[1]]+height/2 - start
draw.line([(odd_axis,start),(odd_axis,start+distance)], fill='black', width=1)
font_path = font_data['path']
font_size = font_data['size']
font = ImageFont.truetype(font_path, font_size)
scale_constant = int(font_size/2)
block_gap = font_size * 3/2
height_offset = 2 * font_size
width_offset = 2 * font_size
tree = newTree([i for i in range(1,max_branch+1)], "levelorder") # creates a binary tree
#tree_struct is an array version of a tree which looks like this [2,1,3]
tree_struct = tree.serializeInOrder(tree.root)
combined_widths = {} # The pixel at which each branch starts at on x-axis
for i in range(0,len(tree_struct)):
branch = tree_struct[i]
if i == 0:
combined_widths[branch] = 0
else:
if tree_struct[i-1] in branch_width.keys():
combined_widths[branch] = combined_widths[tree_struct[i-1]] + branch_width[tree_struct[i-1]]
else:
branch_width[tree_struct[i-1]] = 0
combined_widths[branch] = combined_widths[tree_struct[i-1]] + 0
if tree_struct[-1] not in branch_width.keys():
branch_width[tree_struct[-1]] = 0
combined_heights = {} # The pixel at which each layer starts at on y-axis
for key in layer_height.keys():
if key == 1:
combined_heights[key] = height_offset
else:
combined_heights[key] = layer_height[key-1] + combined_heights[key-1] + block_gap
img_width = int(combined_widths[list(combined_widths.keys())[-1]] + branch_width[list(combined_widths.keys())[-1]] + width_offset*2)
img_height = int(combined_heights[max_y] + layer_height[max_y] + height_offset)
img = Image.new('RGB', (img_width, img_height), color = 'white')
draw = ImageDraw.Draw(img)
# Goes throught each line of chart code and draws it. Then it handles
# that line's flowlines depending on its role
width = 0
for i in range(0,len(chart_code)):
block = chart_code[i]
if block['type'] == 'IO':
height = IO(block['content'],block['position'])
elif block['type'] == 'Terminator':
height = Terminator(block['content'],block['position'])
elif block['type'] == 'Process':
height = Process(block['content'],block['position'])
elif block['type'] == 'Decision':
height,width = Decision(block['content'],block['position'])
elif block['type'] == 'Connector':
height = draw.textsize("c",font=font)[1] + font_size
Flowline(block['role'],block['position'],height,chart_code[0:i],width)
del draw
return img
@click.command()
@click.option('--size', default=20, help="The size of the flowchart")
@click.option('--font', default=r"./fonts/NotoSans-Regular.ttf", help="The font's path")
@click.option('--code', default="enter.txt", help="The file with pseudocode")
@click.option('--output', default="flowchart.png", help="The output image")
def main(size,code,output,font):
lines = read(code)
font_data = {"path":font,"size":size}
chart_code,max_branch,max_y,layer_height,branch_width = translation(lines,font_data)
flowchart = drawer(chart_code,max_branch,max_y,layer_height,branch_width,font_data)
flowchart.save(output)
if __name__ == '__main__':
main()