feat: departure board image and vendored Roboto fonts
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
1 parent
e199600140
commit
baaaeff917
10 files changed
+425
-6
No files matched your search
@@ -0,0 +1,91 @@
|
||||
"""Draw the departure board (480x320 landscape)."""
|
||||
from datetime import datetime
|
||||
|
||||
from PIL import Image, ImageDraw, ImageFont
|
||||
|
||||
from schedule import Upcoming
|
||||
|
||||
W, H = 480, 320
|
||||
BG = (9, 11, 16)
|
||||
RING = (44, 52, 70)
|
||||
ACCENT = (255, 112, 67)
|
||||
TEXT = (245, 247, 252)
|
||||
TEXT_DIM = (140, 148, 165)
|
||||
|
||||
ROWS = 4 # departures listed under the big "next bus" block
|
||||
ROW_TOP, ROW_H = 144, 38
|
||||
HERO_DIVIDER_Y = 134
|
||||
|
||||
|
||||
def load_fonts(font_dir: str) -> dict:
|
||||
def f(name: str, size: int) -> ImageFont.FreeTypeFont:
|
||||
return ImageFont.truetype(f"{font_dir}/{name}", size)
|
||||
|
||||
return {
|
||||
"hero": f("Roboto-Light.ttf", 58),
|
||||
"hero_stop": f("Roboto-Medium.ttf", 24),
|
||||
"hero_small": f("Roboto-Regular.ttf", 20),
|
||||
"clock": f("Roboto-Regular.ttf", 22),
|
||||
"badge_big": f("Roboto-Medium.ttf", 38),
|
||||
"badge": f("Roboto-Medium.ttf", 20),
|
||||
"row": f("Roboto-Regular.ttf", 22),
|
||||
"row_time": f("Roboto-Medium.ttf", 24),
|
||||
"row_cd": f("Roboto-Regular.ttf", 20),
|
||||
}
|
||||
|
||||
|
||||
def countdown_text(u: Upcoming) -> str:
|
||||
"""Czech countdown for a list row: 'teď', 'za 7 min', 'za 1 h 16 min' or 'zítra'."""
|
||||
if u.next_day:
|
||||
return "zítra"
|
||||
if u.minutes == 0:
|
||||
return "teď"
|
||||
if u.minutes < 60:
|
||||
return f"za {u.minutes} min"
|
||||
return f"za {u.minutes // 60} h {u.minutes % 60:02d} min"
|
||||
|
||||
|
||||
def hero_text(u: Upcoming) -> str:
|
||||
"""The big countdown for the next bus."""
|
||||
if u.next_day:
|
||||
return "zítra"
|
||||
if u.minutes == 0:
|
||||
return "teď"
|
||||
if u.minutes < 60:
|
||||
return f"{u.minutes} min"
|
||||
return f"{u.minutes // 60} h {u.minutes % 60:02d}"
|
||||
|
||||
|
||||
def _badge(d: ImageDraw.ImageDraw, box, line: str, font, fill, text_fill) -> None:
|
||||
d.rounded_rectangle(box, radius=8, fill=fill)
|
||||
d.text(((box[0] + box[2]) / 2, (box[1] + box[3]) / 2), line, font=font, fill=text_fill, anchor="mm")
|
||||
|
||||
|
||||
def render_board(upcoming: list[Upcoming], now: datetime, fonts: dict, stale: bool = False) -> Image.Image:
|
||||
img = Image.new("RGB", (W, H), BG)
|
||||
d = ImageDraw.Draw(img)
|
||||
d.text((W - 16, 24), now.strftime("%H:%M"), font=fonts["clock"], fill=TEXT_DIM, anchor="rm")
|
||||
|
||||
if not upcoming:
|
||||
d.text((W // 2, H // 2), "Žádné odjezdy", font=fonts["hero_stop"], fill=TEXT_DIM, anchor="mm")
|
||||
return img
|
||||
|
||||
first = upcoming[0]
|
||||
_badge(d, (16, 20, 96, 100), first.departure.line, fonts["badge_big"], ACCENT, BG)
|
||||
d.text((112, 34), first.departure.stop, font=fonts["hero_stop"], fill=TEXT, anchor="lm")
|
||||
d.text((112, 82), hero_text(first), font=fonts["hero"], fill=TEXT, anchor="lm")
|
||||
d.text((W - 16, 100), f"odj. {first.departure.time:%H:%M}", font=fonts["hero_small"], fill=ACCENT, anchor="rm")
|
||||
|
||||
d.line([(16, HERO_DIVIDER_Y), (W - 16, HERO_DIVIDER_Y)], fill=RING, width=2)
|
||||
|
||||
for i, u in enumerate(upcoming[1 : ROWS + 1]):
|
||||
y = ROW_TOP + i * ROW_H
|
||||
mid = y + ROW_H // 2
|
||||
_badge(d, (16, y + 3, 62, y + ROW_H - 3), u.departure.line, fonts["badge"], RING, TEXT)
|
||||
d.text((76, mid), u.departure.stop, font=fonts["row"], fill=TEXT, anchor="lm")
|
||||
d.text((338, mid), f"{u.departure.time:%H:%M}", font=fonts["row_time"], fill=TEXT, anchor="rm")
|
||||
d.text((W - 16, mid), countdown_text(u), font=fonts["row_cd"], fill=TEXT_DIM, anchor="rm")
|
||||
|
||||
if stale:
|
||||
d.text((16, H - 12), "jízdní řád zastaralý", font=fonts["hero_small"], fill=ACCENT, anchor="lm")
|
||||
return img
|
||||
Reference in new issue
Block a user