-
Notifications
You must be signed in to change notification settings - Fork 2
/
gui.py
118 lines (92 loc) · 3.08 KB
/
gui.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
#!/usr/bin/env python
#
# Copyright (c) 2013 Florian Festi
# Copyright (c) 2013 Sebastian Reichel
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import cairo
class GraphicalOutput:
def __init__(self, output="output.svg"):
# output
self.output = output
# init origin
self.origin_x = 0
self.origin_y = 0
# init bounding box
self.bb1_x = 0
self.bb1_y = 0
self.bb2_x = 0
self.bb2_y = 0
self._init_surface()
def _init_surface(self):
width = abs(self.bb1_x-self.bb2_x)
height = abs(self.bb1_y-self.bb2_y)
self.surface = cairo.SVGSurface(self.output, width, height)
self.ctx = ctx = cairo.Context(self.surface)
ctx.scale(1, 1)
ctx.set_source_rgb(1.0, 1.0, 1.0)
ctx.rectangle(0, 0, width, height)
ctx.fill()
ctx.set_source_rgb(0.0, 0.0, 0.0)
ctx.set_line_width(0.1)
self._calc_real_origin()
def _calc_real_origin(self):
self.real_origin_x = self.origin_x - (self.origin_x + self.bb1_x)
self.real_origin_y = self.origin_y - (self.origin_y + self.bb1_y)
def _custom_stroke(self, r, g, b):
self.ctx.set_source_rgb(r, g, b)
x, y = self.ctx.get_current_point()
self.ctx.stroke ()
self.ctx.move_to(x, y)
def command(self, name, params):
print "%s:" % name, ', '.join((str(i) for i in params))
method = getattr(self, name, None)
if method:
method(*params)
def move_origin(self, x, y):
self.origin_x = x
self.origin_y = y
self._calc_real_origin()
def bounding_box_top_left(self, x, y):
self.bb1_x = x
self.bb1_y = y
self._init_surface()
def bounding_box_bottom_right(self, x, y):
self.bb2_x = x
self.bb2_y = y
self._init_surface()
def move_to(self, x, y):
self.ctx.line_to(self.real_origin_x+x, self.real_origin_y+y)
self._custom_stroke(0, 0, 1)
def line_to(self, x, y):
self.ctx.line_to(self.real_origin_x+x, self.real_origin_y+y)
self._custom_stroke(0, 0, 0)
def line_rel(self, inc_x=0, inc_y=0):
x,y = self.ctx.get_current_point()
self.ctx.line_to(x+inc_x, y+inc_y)
self._custom_stroke(1, 0, 0)
def line_rel_vert(self, inc_y):
self.line_rel(0, inc_y)
def line_rel_hor(self, inc_x):
self.line_rel(inc_x, 0)
def move_rel(self, inc_x=0, inc_y=0):
x,y = self.ctx.get_current_point()
self.ctx.line_to(x+inc_x, y+inc_y)
self._custom_stroke(0, 0, 1)
def move_rel_vert(self, inc_y):
self.move_rel(0, inc_y)
def move_rel_hor(self, inc_x):
self.move_rel(inc_x, 0)
def end_of_file(self):
#surface.write_to_png("output.png")
self.surface.flush()