-
Notifications
You must be signed in to change notification settings - Fork 0
/
outline.py
144 lines (125 loc) · 3.25 KB
/
outline.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
import cairo
import math
import StringIO
from units import *
import defaults
class OutlineCreator:
def __init__(self, width, height, bleed=defaults.bleed, crop=defaults.crop):
self.bleed, self.crop, self.width, self.height = bleed, crop, width, height
self.print_marks = crop - bleed
self.total_width = width + self.print_marks * 2
self.total_height = height + self.print_marks * 2
self.output = StringIO.StringIO()
self.surface = cairo.PDFSurface(
self.output,
self.total_width * MM_TO_PT,
self.total_height * MM_TO_PT
)
def create_context(self):
context = cairo.Context(self.surface)
context.set_source_rgb(0, 0, 0)
context.set_line_width(0.2)
# Allow drawing on the context using human-friendly units
context.scale(MM_TO_PT, MM_TO_PT)
return context
def print_mark(self, x, y, margin):
size = margin * 3. / 4.
context = self.create_context()
context.translate(x, y)
# Circles
context.arc(0, 0, 1./3. * margin, 0, math.pi * 2)
context.arc(0, 0, 1./6. * margin, 0, math.pi * 2)
# Vertical line
context.move_to(0, -size / 2)
context.line_to(0, size / 2)
# Horizontal line
context.move_to(-size / 2, 0)
context.line_to(size / 2, 0)
context.stroke()
def print_line(self, x1, y1, x2, y2):
context = self.create_context()
context.move_to(x1, y1)
context.line_to(x2, y2)
context.stroke()
def crop_width(self):
return self.crop * 3. / 4.
def create(self):
# Top mark
self.print_mark(
self.total_width / 2,
self.print_marks / 2,
self.print_marks
)
# Bottom mark
self.print_mark(
self.total_width / 2,
self.total_height - self.print_marks / 2,
self.print_marks
)
# Left mark
self.print_mark(
self.print_marks / 2,
self.total_height / 2,
self.print_marks
)
# Right mark
self.print_mark(
self.total_width - self.print_marks/ 2,
self.total_height / 2,
self.print_marks
)
# Top cut
self.print_line(
0,
self.crop,
self.crop_width(),
self.crop
)
self.print_line(
self.total_width,
self.crop,
self.total_width - self.crop_width(),
self.crop
)
# Bottom cut
self.print_line(
0,
self.total_height - self.crop,
self.crop_width(),
self.total_height - self.crop
)
self.print_line(
self.total_width,
self.total_height - self.crop,
self.total_width - self.crop_width(),
self.total_height - self.crop
)
# Left cut
self.print_line(
self.crop,
0,
self.crop,
self.crop_width()
)
self.print_line(
self.crop,
self.total_height,
self.crop,
self.total_height - self.crop_width()
)
# Right cut
self.print_line(
self.total_width - self.crop,
0,
self.total_width - self.crop,
self.crop_width()
)
self.print_line(
self.total_width - self.crop,
self.total_height,
self.total_width - self.crop,
self.total_height - self.crop_width()
)
self.surface.flush()
self.surface.finish()
return self.output