Skip to content

Commit 5e2a83b

Browse files
committed
Refactor footer image processing to improve font loading and add right-alignment for date/time display
1 parent 1c27830 commit 5e2a83b

1 file changed

Lines changed: 71 additions & 29 deletions

File tree

trmnl_server.py

Lines changed: 71 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -407,27 +407,43 @@ def add_footer_to_image(src_image, wifi_percentage, battery_percentage):
407407
d = ImageDraw.Draw(new_img)
408408
logger.debug("[image modification] adding footer to image")
409409
# Load fonts
410+
# Load icon font (FontAwesome)
411+
icon_font_path = os.path.join(base_path, "web", "fontawesome-webfont.ttf")
412+
icon_font = None
410413
try:
411-
# Use absolute paths for fonts
412-
icon_font_path = os.path.join(base_path, "web", "fontawesome-webfont.ttf")
413-
# Try system fonts for text (Arial Bold available on Windows)
414+
icon_font = ImageFont.truetype(icon_font_path, 24)
415+
logger.debug("[image modification] loaded FontAwesome from %s", icon_font_path)
416+
except Exception as e:
417+
logger.warning("[image modification] could not load FontAwesome: %s", str(e))
418+
icon_font = ImageFont.load_default()
419+
420+
# Load text font - try multiple options for cross-platform support
421+
text_font = None
422+
font_candidates = [
423+
"arialbd.ttf", # Windows
424+
"arial.ttf", # Windows
425+
"/usr/share/fonts/ttf-dejavu/DejaVuSans-Bold.ttf", # Alpine Linux
426+
"/usr/share/fonts/ttf-dejavu/DejaVuSans.ttf", # Alpine Linux
427+
"/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", # Debian/Ubuntu
428+
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", # Debian/Ubuntu
429+
]
430+
431+
for font_path in font_candidates:
414432
try:
415-
text_font = ImageFont.truetype("arialbd.ttf", 14)
433+
text_font = ImageFont.truetype(font_path, 14)
434+
logger.debug("[image modification] loaded text font: %s", font_path)
435+
break
416436
except:
417-
# Fallback to regular Arial
418-
text_font = ImageFont.truetype("arial.ttf", 14)
437+
continue
419438

420-
fonts = {
421-
"icon_font": ImageFont.truetype(icon_font_path, 24),
422-
"text_font": text_font,
423-
}
424-
logger.debug("[image modification] loading needed fonts")
425-
except IOError as e:
426-
fonts = {
427-
"icon_font": ImageFont.load_default(),
428-
"text_font": ImageFont.load_default(),
429-
}
430-
logger.error("[image modification] loading default fonts - ERROR: %s", str(e))
439+
if text_font is None:
440+
logger.warning("[image modification] no system fonts available, using default")
441+
text_font = ImageFont.load_default()
442+
443+
fonts = {
444+
"icon_font": icon_font,
445+
"text_font": text_font,
446+
}
431447

432448
# Define positions
433449
positions = {
@@ -437,7 +453,6 @@ def add_footer_to_image(src_image, wifi_percentage, battery_percentage):
437453
"wifi_text_position": (50, img.height + 7),
438454
"battery_icon_position": (104, img.height + 4),
439455
"battery_text_position": (140, img.height + 7),
440-
"date_time_position": (img.width - 155, img.height + 7),
441456
}
442457

443458
# Draw line if background is white
@@ -456,16 +471,17 @@ def add_footer_to_image(src_image, wifi_percentage, battery_percentage):
456471
fill=1,
457472
radius=5,
458473
)
459-
d.rounded_rectangle(
460-
[
461-
positions["date_time_position"][0] - 8,
462-
img.height + 3,
463-
810,
464-
img.height + FOOTER_HEIGHT + 5,
465-
],
466-
fill=1,
467-
radius=5,
468-
)
474+
# Right side pill for date/time - will be calculated after text width is known
475+
# d.rounded_rectangle(
476+
# [
477+
# positions["date_time_position"][0] - 8,
478+
# img.height + 3,
479+
# 810,
480+
# img.height + FOOTER_HEIGHT + 5,
481+
# ],
482+
# fill=1,
483+
# radius=5,
484+
# )
469485

470486
# Draw WiFi icon \uf1eb and percentage
471487
d.text(
@@ -515,8 +531,34 @@ def add_footer_to_image(src_image, wifi_percentage, battery_percentage):
515531
# Get the current time in the configured time zone
516532
time_zone = pytz.timezone(config_manager.config["time_zone"])
517533
date_time = datetime.datetime.now(time_zone).strftime("%d.%m.%Y %H:%M")
534+
535+
# Calculate text width for right alignment
536+
try:
537+
bbox = d.textbbox((0, 0), date_time, font=fonts["text_font"])
538+
text_width = bbox[2] - bbox[0]
539+
except:
540+
# Fallback for older PIL versions
541+
text_width = len(date_time) * 8 # Rough estimate
542+
543+
# Right-align with 10px margin from right edge
544+
date_time_x = img.width - text_width - 10
545+
date_time_y = img.height + 7
546+
547+
# Draw the right-side pill background (only if BACKGROUND_TYPE is black)
548+
if BACKGROUND_TYPE == 0:
549+
d.rounded_rectangle(
550+
[
551+
date_time_x - 8,
552+
img.height + 3,
553+
img.width + 10,
554+
img.height + FOOTER_HEIGHT + 5,
555+
],
556+
fill=1,
557+
radius=5,
558+
)
559+
518560
d.text(
519-
positions["date_time_position"],
561+
(date_time_x, date_time_y),
520562
date_time,
521563
fill=BACKGROUND_TYPE * -1,
522564
font=fonts["text_font"],

0 commit comments

Comments
 (0)