-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_driver.py
98 lines (86 loc) · 2.69 KB
/
output_driver.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
"""
This module contains the code the actually produces graphics output.
The idea is that other parts of the code can make calls in here to draw
simple shapes and this will handle the job of actually converting that
to output in whatever format we've requested
"""
from colour import Colour
# Define some useful colour constants (we may move these later)
BACKGROUND_COLOUR = Colour("#FE8F80")
class OutputDriver:
"""
This class can be used to do draw some simple shapes
Each drawing operation will add the shape to a buffer.
This will only be written to the file when output() is
called. The idea of this is so that we don't
leave a file open for a while as we're putting in each
shape.
"""
def __init__(self, filename, width=200, height=150):
# Remember the canvas size in case we want to scale
self.filename = filename
self.width = width
self.height = height
self.buffer = """%!PS
% Function to draw centred text
/cshow { dup stringwidth pop -0.5 mul 0 rmoveto show } def
% Make the background white
newpath
0 0 moveto
0 500 lineto
500 500 lineto
500 0 lineto
closepath
1 1 1 setrgbcolor
fill
"""
def draw_box(
self, start_x: int, start_y: int, end_x: int, end_y: int, colour: Colour
):
"""Draw a box
Args:
start_x (int): The x position of the left side of the box
start_y (int): The y position of the bottom
end_x (int): The x position of the right side
end_y (int): The y position of the top
colour (Colour): The colour of the background of the box
Return: None
"""
red, blue, green = colour.rgb()
self.buffer += f"""
newpath
{start_x} {start_y} moveto
{start_x} {end_y} lineto
{end_x} {end_y} lineto
{end_x} {start_y} lineto
closepath
gsave
{red} {blue} {green} setrgbcolor
fill
grestore
0 setgray
2 setlinewidth
0 0 0 setrgbcolor
stroke
"""
def draw_text(self, text: str, x_pos, y_pos):
"""
Draw the text, centred on x_pos, y_pos
Args:
text: The text to draw
x_pos; The x coordinate of the centre of the text
y_pos; The y coordinate of the centre of the text
"""
self.buffer += f"""
/Times-Roman findfont 10 scalefont setfont
{x_pos} {y_pos} moveto 0 0 0 setrgbcolor ({text}) cshow
"""
def output(self) -> None:
"""
Write out the diagram to the filename specified.
NOTE: all "drawing" should already have been done by calling other
methods first, to build up an image. This is the final step to output
the image to a file.
"""
with open(self.filename, "w") as file:
file.write(self.buffer)