-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscoreboard.py
370 lines (336 loc) · 12.5 KB
/
scoreboard.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# Wahoo! Results - https://github.com/JohnStrunk/wahoo-results
# Copyright (C) 2022 - John D. Strunk
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Generate an image of the scoreboard from a RaceTimes object."""
from typing import Optional, Tuple
import sentry_sdk
from matplotlib import font_manager # type: ignore
from PIL import Image, ImageDraw, ImageFont, UnidentifiedImageError
from PIL.ImageEnhance import Brightness
from model import Model
from raceinfo import (
INCONSISTENT,
NO_SHOW,
HeatData,
NameMode,
NumericTime,
format_name,
is_special_time,
)
def waiting_screen(size: Tuple[int, int], model: Model) -> Image.Image:
"""Generate a "waiting" image to display on the scoreboard.
:param size: The size of the image in pixels
:param model: The application model state
:returns: An image to display on the scoreboard
"""
img = Image.new(mode="RGBA", size=size, color=model.color_bg.get())
center = (int(size[0] * 0.5), int(size[1] * 0.8))
normal = fontname_to_file(model.font_normal.get())
font_size = 72
fnt = ImageFont.truetype(normal, font_size)
draw = ImageDraw.Draw(img)
color = model.color_event.get()
draw.text(center, "Waiting for results...", font=fnt, fill=color, anchor="ms")
return img
class ScoreboardImage:
"""Generate a scoreboard image from a RaceTimes object."""
_BORDER_FRACTION = 0.05 # Fraction of image left as a border around all sides
_EVENT_SIZE = "E:MMM"
_HEAT_SIZE = "H:MM"
_img: Image.Image # The rendered image
_lanes: int # The number of lanes to display
_line_height: int # Height of a line of text (baseline to baseline), in px
_text_height: int # Height of actual text, in px
_normal_font: ImageFont.FreeTypeFont # Font for normal text
_time_font: ImageFont.FreeTypeFont # Font for printing times
def __init__(
self,
size: Tuple[int, int],
race: HeatData,
model: Model,
background: bool = True,
):
"""
Generate a scoreboard image from a RaceTimes object.
:param size: A tuple representing the size of the image in pixels
:param race: The RaceTimes object containing the race result (and optionally the swimmer names/teams)
:param model: The model state that contains the rendering preferences
"""
with sentry_sdk.start_span(op="render_image", description="Render image"):
self._race = race
self._model = model
# We save the lane count once because it's used multiple times, and we
# want to ensure the value doesn't change while we're building the
# scoreboard image
self._lanes = model.num_lanes.get()
bg_color = model.color_bg.get()
if not background:
bg_color = "#00000000" # transparent
self._img = Image.new(mode="RGBA", size=size, color=bg_color)
if background:
self._add_bg_image()
self._load_fonts()
self._draw_header()
self._draw_lanes()
@property
def image(self) -> Image.Image:
"""The image of the scoreboard."""
return self._img
@property
def size(self):
"""Get the size of the image."""
return self._img.size
def _add_bg_image(self) -> None:
bg_image_filename = self._model.image_bg.get()
if bg_image_filename == "":
return # bg image not defined
try:
bg_image = Image.open(bg_image_filename)
# Ensure the size matches
bg_image = bg_image.resize(self.size, Image.Resampling.BICUBIC)
# Make sure the image modes match
bg_image = bg_image.convert("RGBA")
# Adjust the image brightness
bg_image = Brightness(bg_image).enhance(
float(self._model.brightness_bg.get()) / 100.0
)
# Overlay it, respecting the alpha channel
self._img.alpha_composite(bg_image)
except FileNotFoundError:
return
except UnidentifiedImageError:
return
except ValueError:
return
def _load_fonts(self) -> None:
usable_height = self.size[1] * (1 - (2 * self._BORDER_FRACTION))
lines = self._lanes
lines += 1 # Event num + Header
lines += 1 # Heat num + Event descr
lines += 1 # Name, team, time header
self._line_height = int(usable_height / lines)
scaled_height = self._line_height / self._model.text_spacing.get()
normal_f_file = fontname_to_file(self._model.font_normal.get())
time_f_file = fontname_to_file(self._model.font_time.get())
self._normal_font = ImageFont.truetype(normal_f_file, int(scaled_height))
self._time_font = ImageFont.truetype(time_f_file, int(scaled_height))
draw = ImageDraw.Draw(self._img)
self._text_height = draw.textbbox((0, 0), self._EVENT_SIZE, self._normal_font)[
3
]
def _draw_header(self) -> None:
draw = ImageDraw.Draw(self._img)
edge_l = int(self.size[0] * self._BORDER_FRACTION)
edge_r = int(self.size[0] * (1 - self._BORDER_FRACTION))
width = edge_r - edge_l
# Line1 - E: 999 Heading text
draw.text(
(edge_l, self._baseline(1)),
f"E:{self._race.event}",
font=self._time_font,
anchor="ls",
fill=self._model.color_event.get(),
)
hstart = edge_l + draw.textlength(self._EVENT_SIZE, self._normal_font)
hwidth = width - hstart
head_txt = self._model.title.get()
while draw.textlength(head_txt, self._normal_font) > hwidth:
head_txt = head_txt[:-1]
draw.text(
(edge_r, self._baseline(1)),
head_txt,
font=self._normal_font,
anchor="rs",
fill=self._model.color_title.get(),
)
# Line2 - H: 99 Event description
draw.text(
(edge_l, self._baseline(2)),
f"H:{self._race.heat}",
font=self._time_font,
anchor="ls",
fill=self._model.color_event.get(),
)
dstart = edge_l + draw.textlength(self._HEAT_SIZE, self._normal_font)
dwidth = width - dstart
desc_txt = self._race.description
while draw.textlength(desc_txt, self._normal_font) > dwidth:
desc_txt = desc_txt[:-1]
draw.text(
(edge_r, self._baseline(2)),
desc_txt,
font=self._normal_font,
anchor="rs",
fill=self._model.color_event.get(),
)
def _draw_lanes(self) -> None:
draw = ImageDraw.Draw(self._img)
edge_l = int(self.size[0] * self._BORDER_FRACTION)
edge_r = int(self.size[0] * (1 - self._BORDER_FRACTION))
width = edge_r - edge_l
time_width = int(draw.textlength("00:00.00", self._time_font) * 1.1)
idx_width = draw.textlength("L", self._normal_font)
pl_width = draw.textlength("MMM", self._normal_font)
name_width = width - time_width - idx_width - pl_width
# Lane title
baseline = self._baseline(3)
title_color = self._model.color_event.get()
draw.text(
(edge_l, baseline),
"L",
font=self._normal_font,
anchor="ls",
fill=title_color,
)
draw.text(
(edge_l + idx_width + pl_width, baseline),
"Name",
font=self._normal_font,
anchor="ls",
fill=title_color,
)
draw.text(
(edge_r, baseline),
"Time",
font=self._normal_font,
anchor="rs",
fill=title_color,
)
# Lane data
for i in range(1, self._lanes + 1):
color = (
self._model.color_odd.get() if i % 2 else self._model.color_even.get()
)
line_num = 3 + i
# Lane
draw.text(
(edge_l + idx_width / 2, self._baseline(line_num)),
f"{i}",
font=self._normal_font,
anchor="ms",
fill=color,
)
# Place
pl_num = self._race.place(i)
pl_color = color
if pl_num == 1:
pl_color = self._model.color_first.get()
if pl_num == 2: # noqa: PLR2004
pl_color = self._model.color_second.get()
if pl_num == 3: # noqa: PLR2004
pl_color = self._model.color_third.get()
ptxt = format_place(pl_num)
draw.text(
(edge_l + idx_width + pl_width / 2, self._baseline(line_num)),
ptxt,
font=self._normal_font,
anchor="ms",
fill=pl_color,
)
# Name
name_variants = format_name(NameMode.NONE, self._race.lane(i).name)
while draw.textlength(name_variants[0], self._normal_font) > name_width:
name_variants.pop(0)
name = name_variants[0]
draw.text(
(edge_l + idx_width + pl_width, self._baseline(line_num)),
f"{name}",
font=self._normal_font,
anchor="ls",
fill=color,
)
# Time
draw.text(
(edge_r, self._baseline(line_num)),
self._time_text(i),
font=self._time_font,
anchor="rs",
fill=color,
)
def _time_text(self, lane_num: int) -> str:
lane = self._race.lane(lane_num)
final_time = lane.time()
# Only print NS if someone was supposed to be there
if lane.is_empty or final_time == NO_SHOW:
if lane.name == "":
return ""
return "NS"
if final_time == INCONSISTENT:
return "--:--.--"
if not is_special_time(final_time):
assert isinstance(final_time, NumericTime)
return format_time(final_time)
return ""
def _baseline(self, line: int) -> int:
"""Return the y-coordinate for the baseline of the n-th line of text from the top."""
return int(
self._img.size[1] * self._BORDER_FRACTION # skip top border
+ line * self._line_height # move down to proper line
- (self._line_height - self._text_height) / 2
) # up 1/2 the inter-line space
def format_time(seconds: NumericTime) -> str:
"""Format a time in minutes, seconds, and hundredths.
:param seconds: The time in seconds
:returns: A string representation of the time
>>> format_time(NumericTime("1.2"))
'01.20'
>>> format_time(NumericTime("9.87"))
'09.87'
>>> format_time(NumericTime("50"))
'50.00'
>>> format_time(NumericTime("120.0"))
'2:00.00'
"""
sixty = NumericTime("60")
minutes = seconds // sixty
seconds = seconds % sixty
if minutes == 0:
return f"{seconds:05.2f}"
return f"{minutes}:{seconds:05.2f}"
def fontname_to_file(name: str) -> str:
"""Convert a font name (Roboto) to its corresponding filename.
:param name: The name of the font
:returns: The filename of the font
"""
properties = font_manager.FontProperties(family=name, weight="bold")
filename = font_manager.findfont(properties)
return filename
def format_place(place: Optional[int]) -> str:
"""
Turn a numerical place into the printable string representation.
>>> format_place(None)
''
>>> format_place(0)
''
>>> format_place(1)
'1st'
>>> format_place(2)
'2nd'
>>> format_place(3)
'3rd'
>>> format_place(6)
'6th'
"""
if place is None:
return ""
if place == 0:
return ""
if place == 1:
return "1st"
if place == 2: # noqa: PLR2004
return "2nd"
if place == 3: # noqa: PLR2004
return "3rd"
return f"{place}th"