1 | #!/usr/bin/python3
|
2 |
|
3 | import svgwrite #muss man installieren
|
4 | import string
|
5 |
|
6 | infilename = 'lesbar.txt'
|
7 | outfilename = 'bild.svg'
|
8 |
|
9 | # hides the ugliness of svgwrite (inverted y)
|
10 | class SvgSheet:
|
11 | def __init__(self, filename):
|
12 | self.xmin=-100
|
13 | self.xmax=1800
|
14 | self.ymin=-100
|
15 | self.ymax=800
|
16 | worldwidth=self.xmax-self.xmin
|
17 | worldheight=self.ymax-self.ymin
|
18 | paperwidth=17 #cm
|
19 | paperheight=10 #cm
|
20 | paperdims=("%fcm" % paperwidth, "%fcm" % paperheight)
|
21 | self.dwg = svgwrite.Drawing(filename=filename, debug=True, size = paperdims)
|
22 | self.dwg.viewbox(minx=self.xmin-1, miny=self.ymin-1, width=worldwidth+2, height=worldheight+2)
|
23 |
|
24 | def w2g(self, xy):
|
25 | return (xy[0], self.ymax -xy[1])
|
26 |
|
27 | def save_image(self):
|
28 | self.dwg.save()
|
29 |
|
30 | def line(self, a, b, color='gray', lw='0.5mm'):
|
31 | self.dwg.add(self.dwg.line(self.w2g(a), self.w2g(b), stroke=color, fill='none', stroke_width=lw, stroke_linecap="round") )
|
32 |
|
33 | def draw_text(self, p, text, color='orange'):
|
34 | self.dwg.add(self.dwg.text(text, insert=self.w2g(p), fill=color))
|
35 |
|
36 | #helper to save typing
|
37 | def coords(params):
|
38 | return int(params[0]), int(params[1])
|
39 |
|
40 | class PlotParser:
|
41 | def __init__(self, painter):
|
42 | self.painter = painter
|
43 | # internal state:
|
44 | self.cursor = (0, 0)
|
45 | self.color = 'black'
|
46 | # map command names to functions:
|
47 | self.commands = dict()
|
48 | self.commands['SP'] = self.select_pen
|
49 | self.commands['DA'] = self.draw_absolute
|
50 | self.commands['MA'] = self.move_absolute
|
51 | self.commands['MR'] = self.move_relative
|
52 | self.commands['DR'] = self.draw_relative
|
53 | self.commands['LA'] = self.label
|
54 |
|
55 | def unknown(self, command, params):
|
56 | print("unknown command %s(%s)" %(command, ','.join(params)))
|
57 |
|
58 | def move_relative(self, params):
|
59 | dx, dy = coords(params)
|
60 | self.cursor = (self.cursor[0] + dx, self.cursor[1] + dy)
|
61 |
|
62 | def label(self, params):
|
63 | text = params[0]
|
64 | self.painter.draw_text(self.cursor, text, self.color)
|
65 |
|
66 | def move_absolute(self, params):
|
67 | x, y = coords(params)
|
68 | self.cursor =(x, y)
|
69 |
|
70 | def draw_absolute(self, params):
|
71 | x, y = coords(params)
|
72 | self.painter.line(self.cursor, (x, y), self.color)
|
73 | self.cursor = (x, y)
|
74 |
|
75 | def draw_relative(self, params):
|
76 | dx, dy = coords(params)
|
77 | x = self.cursor[0] + dx
|
78 | y = self.cursor[1] + dy
|
79 | self.painter.line(self.cursor, (x, y), self.color)
|
80 | self.cursor =(x, y)
|
81 |
|
82 | def select_pen(self, params):
|
83 | pen_num = int(params[0])
|
84 | colors = {1: 'blue', 2: 'red', 3: 'black', 4: 'green'}
|
85 | self.color = colors[pen_num]
|
86 |
|
87 | def interpret(self, s):
|
88 | if len(s) < 2:
|
89 | print('ignoring "%s"' % s)
|
90 | return
|
91 | command = s[0:2]
|
92 | params = s[2:].split(',')
|
93 | if command in self.commands:
|
94 | self.commands[command](params)
|
95 | else:
|
96 | self.unknown(command, params)
|
97 |
|
98 | def finish(self):
|
99 | self.painter.save_image()
|
100 |
|
101 |
|
102 | if __name__ == '__main__':
|
103 | paper = SvgSheet(outfilename)
|
104 | printer = PlotParser(paper)
|
105 |
|
106 | infile = open(infilename, 'r')
|
107 | graphics_begun = False
|
108 | for li in infile.readlines():
|
109 | line = ''.join(filter(lambda x: x in string.printable, li.strip())) # ugly: remove nonprintable chars
|
110 | if not graphics_begun:
|
111 | if 'GRC' in line:
|
112 | graphics_begun = True
|
113 | if graphics_begun:
|
114 | printer.interpret(line)
|
115 | printer.finish()
|