-
Notifications
You must be signed in to change notification settings - Fork 0
/
Annotation.py
191 lines (150 loc) · 5.46 KB
/
Annotation.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
from __future__ import annotations
import enum
import math
import numpy as np
from reportlab.pdfgen import canvas
_ALIGN_MARK = '\u3000'
_BLACK = (0, 0, 0)
_WHITE = (0, 0, 0)
def reset_colors(canv: canvas.Canvas):
canv.setFillColorRGB(*_BLACK)
canv.setStrokeColorRGB(*_BLACK)
def grey(percent: float):
return (int(percent*255) for _ in range(3))
class Annotation:
'''Base Annotation Class'''
pos = None
text = None
alt_text = None
leader_pos = None
def __init__(self, pos, text, alt_text='', leader_pos=None) -> None:
self.pos = pos
self.text = text
self.alt_text = alt_text
self.leader_pos = leader_pos
def annotate(self, canv: canvas.Canvas, params: AnnotationParameters):
pos = params.get_snapped_coordinates(self, canv._pagesize)
canv.setFontSize(params.smallFontSize)
canv.drawString(*pos, self.text)
def _polygon(self, canv: canvas.Canvas, points, fill=None, stroke=None):
poly = canv.beginPath()
poly.moveTo(*points[0])
for i, point in enumerate(points[1:]):
poly.lineTo(*points[i+1])
poly.lineTo(*points[0])
if fill is not None:
canv.setFillColorRGB(*fill)
if stroke is not None:
canv.setStrokeColorRGB(*stroke)
canv.drawPath(poly, stroke=stroke is not None, fill=fill is not None)
reset_colors(canv)
class ActorMovement(Annotation):
actor = None
def __init__(self, pos, actor) -> None:
# TODO Define actor class
self.actor = actor
super().__init__(pos, actor.number, actor.name)
def _draw_triangle(self, canv: canvas.Canvas, pos,
params: AnnotationParameters, rotation=0, size=1):
base = params.itemSize*size
height = base*math.sqrt(3)/2
points = [
np.array([0, -base/2]),
np.array([0, base/2]),
np.array([height, 0])
]
vtrans = [
np.array(pos),
np.array(pos),
np.array(pos) + np.array([height,0]),
np.array(pos)
]
rotations = [
np.array([[1, 0], [0, 1]]),
np.array([[0, -1], [1, 0]]),
np.array([[-1, 0], [0, -1]]),
np.array([[0, 1], [-1, 0]])
]
transform = lambda p, r: rotations[r]@p.transpose() + vtrans[r].transpose()
t_points = [transform(p, rotation) for p in points]
self._polygon(canv, t_points, (255, 255, 0),(255, 0, 0))
def annotate(self, canv: canvas.Canvas, params: AnnotationParameters, rot=0, size=1):
pos = np.array(params.get_snapped_coordinates(self, canv._pagesize))
canv.setFontSize(params.bigFontSize)
text_pos = (pos+np.array([0, -params.bigFontSize/2/1.2]))
canv.drawRightString(*text_pos, f"{self.actor.number} ")
self._draw_triangle(canv, pos, params, rot, size)
# self._polygon(canv, [[0, pos[1]], [200, pos[1]]], stroke=(0, 0, 0))
class ActorEntrance(ActorMovement):
def annotate(self, canv: canvas.Canvas, params: AnnotationParameters):
return super().annotate(canv, params, 0)
class ActorExit(ActorMovement):
def annotate(self, canv: canvas.Canvas, params: AnnotationParameters):
return super().annotate(canv, params, 2, 0.5)
class WarnActorEntrance(ActorMovement):
def annotate(self, canv: canvas.Canvas, params: AnnotationParameters):
pos = np.array(params.get_snapped_coordinates(self, canv._pagesize))
canv.setFontSize(params.bigFontSize)
text_pos = (pos+np.array([0, params.bigFontSize/5]))
self._draw_triangle(canv, pos, params, 3, 1)
canv.drawCentredString(*text_pos, self.actor.number)
class ActorLine(Annotation):
pass
class SoundCue(Annotation):
pass
class SoundSnap(SoundCue):
pass
class WarningNote(Annotation):
pass
class Note(Annotation):
pass
class AnnotationParameters:
margins = None
positions = {
Annotation: [None, None],
ActorEntrance: ["left", None],
ActorExit: ["left", None],
WarnActorEntrance: ["right", None],
ActorLine: ["left", None],
SoundCue: ["right", None],
WarningNote: [None, None],
Note: [None, None]
}
margin_map = None
itemSize = 24
bigFontSize = 20
smallFontSize = 15
def __init__(self, margins, positions = None) -> None:
self.margins = margins
if positions is not None:
self.positions = positions
self.margin_map = {
"left": margins[0]/2,
"right": margins[1]/2,
"top": margins[2]/2,
"bottom": margins[3]/2
}
def get_snapped_coordinates(self, obj: Annotation, page_size):
prototypes = self.positions[type(obj)]
if prototypes is None:
return obj.pos
new_pos = []
for elem, prototype, size in zip(obj.pos, prototypes, page_size):
if prototype is None:
new_pos.append(elem)
else:
new_pos.append(self._margin_pos(prototype, size))
return new_pos
def _margin_pos(self, margin_str, page_length):
margin = self.margin_map[margin_str]
if margin_str in ("left", "top"):
return margin
return page_length - margin
class Actor:
name = None
number = None
color = None
def __init__(self, number: str, name: str, color=None) -> None:
self.name = name
self.number = number
self.color = color