Skip to content

Commit 0f5f837

Browse files
committed
Recalculate the pilot name columns based on the pixel count.
1 parent d9f3b59 commit 0f5f837

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

plugins/greenieboard/reports.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66

77
from core import report, utils, get_translation, GraphElement, ReportEnv, Plugin
88
from matplotlib import cm, pyplot as plt
9+
from matplotlib.font_manager import FontProperties
910
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
1011
from matplotlib.patches import FancyBboxPatch
12+
from matplotlib.textpath import TextPath
1113
from plugins.userstats.filter import StatisticsFilter
1214
from psycopg.rows import dict_row
1315
from typing import cast
@@ -344,18 +346,33 @@ async def render(self, server_name: str, num_rows: int, num_landings: int, squad
344346
return
345347

346348
# Calculate dynamic figure size based on rows and columns
347-
max_len = -1
349+
max_name_width_points = 0
350+
font_props = FontProperties(fname=font_name, size=text_size, weight='bold')
351+
348352
for item in rows:
349353
member = self.bot.get_member_by_ucid(item['player_ucid'])
350354
if member:
351355
item['name'] = member.display_name
352-
if len(item['name']) > max_len:
353-
max_len = len(item['name'])
354356

355-
pilot_column_width = max_len * 0.20
357+
# TextPath calculates the bounding box from the font geometry itself
358+
tp = TextPath((0, 0), item['name'], size=text_size, prop=font_props)
359+
bbox = tp.get_extents()
360+
width_points = bbox.width
361+
362+
if width_points > max_name_width_points:
363+
max_name_width_points = width_points
364+
365+
# Check "Pilot" header too
366+
tp_header = TextPath((0, 0), "Pilot", size=text_size, prop=font_props)
367+
header_width_points = tp_header.get_extents().width
368+
369+
# Convert points (1/72 inch) to inches
370+
pilot_column_width = max(header_width_points, max_name_width_points) / 50.0
371+
356372
padding = 1.0 # Padding between columns
357373
fig_width = pilot_column_width + padding + (
358-
num_columns * column_width) + 2 # Additional padding on the sides
374+
num_columns * column_width) + 2 # Additional padding on the sides
375+
359376
legend_height = (5 if num_landings < 20 else 3) * (card_size + 0.2)
360377
fig_height = (num_rows * row_height) + 2 + legend_height # Additional padding on the top and bottom
361378

0 commit comments

Comments
 (0)