|
6 | 6 |
|
7 | 7 | from core import report, utils, get_translation, GraphElement, ReportEnv, Plugin |
8 | 8 | from matplotlib import cm, pyplot as plt |
| 9 | +from matplotlib.font_manager import FontProperties |
9 | 10 | from matplotlib.offsetbox import OffsetImage, AnnotationBbox |
10 | 11 | from matplotlib.patches import FancyBboxPatch |
| 12 | +from matplotlib.textpath import TextPath |
11 | 13 | from plugins.userstats.filter import StatisticsFilter |
12 | 14 | from psycopg.rows import dict_row |
13 | 15 | from typing import cast |
@@ -344,18 +346,33 @@ async def render(self, server_name: str, num_rows: int, num_landings: int, squad |
344 | 346 | return |
345 | 347 |
|
346 | 348 | # 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 | + |
348 | 352 | for item in rows: |
349 | 353 | member = self.bot.get_member_by_ucid(item['player_ucid']) |
350 | 354 | if member: |
351 | 355 | item['name'] = member.display_name |
352 | | - if len(item['name']) > max_len: |
353 | | - max_len = len(item['name']) |
354 | 356 |
|
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 | + |
356 | 372 | padding = 1.0 # Padding between columns |
357 | 373 | 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 | + |
359 | 376 | legend_height = (5 if num_landings < 20 else 3) * (card_size + 0.2) |
360 | 377 | fig_height = (num_rows * row_height) + 2 + legend_height # Additional padding on the top and bottom |
361 | 378 |
|
|
0 commit comments