feat(drivers): country flag before the driver's name in timing, overlay, results, printouts and PDF (1.89.20)

This commit is contained in:
larssand committed 2026-11-13 16:23:19 +01:00
1 parent da20a7e978
commit 6e45918e74
101 files changed
+2898 -45

No files matched your search

+35 -12
View File
@@ -1,6 +1,7 @@
// Styled A4 PDF for result lists, start lists and standings: a branded header, real tables
// (coloured header row, striped rows, podium marks), page numbers. Uses the PDF standard fonts
// Helvetica / Helvetica-Bold with WinAnsiEncoding so Swedish å/ä/ö print without embedding fonts.
const { drawPdfFlag } = require("./pdf_flags");
const THEMES = {
classic: { band: "#14213d", accent: "#2f6fde", headFill: "#14213d", headText: "#ffffff", zebra: "#f2f5fa" },
@@ -94,10 +95,31 @@ const PORTRAIT_WIDTH = 595 - MARGIN * 2;
const NUMERIC = /^[-+()0-9.:,\s%/]+$/;
const POS_HEADER = /^(pos|plac|placering|place|rank|#|nr)\.?$/i;
// Which columns hold numbers or places, and which one is the driver name (the first that is neither).
function analyzeColumns(section) {
const numeric = section.headers.map((_h, index) => {
const values = section.rows.map((row) => String(row[index] || "").trim()).filter(Boolean);
return values.length > 0 && values.every((value) => NUMERIC.test(value));
});
const placeColumn = section.headers.map((_h, index) => {
const values = section.rows.map((row) => String(row[index] || "").trim()).filter(Boolean);
return values.length > 0 && values.every((value) => /^(TQ|[A-Z]?\d+|-)$/.test(value));
});
const first = section.headers.findIndex((_h, index) => index > 0 && !numeric[index] && !placeColumn[index]);
// A table whose first column is the driver (channel analysis) has no place column before it.
const nameIndex = first >= 0 ? first : (!numeric[0] && !placeColumn[0] ? 0 : -1);
return { numeric, placeColumn, nameIndex };
}
// Room for the small country flag before the driver's name.
const FLAG_SPACE = 15;
const hasFlags = (section) => Array.isArray(section.flags) && section.flags.some(Boolean);
function naturalWidths(section) {
return section.headers.map((header, index) => Math.min(230, Math.max(
const flagColumn = hasFlags(section) ? analyzeColumns(section).nameIndex : -1;
return section.headers.map((header, index) => Math.min(230 + (index === flagColumn ? FLAG_SPACE : 0), Math.max(
textWidth(header, HEAD_SIZE, true),
...section.rows.map((row) => textWidth(row[index], CELL_SIZE, false)),
...section.rows.map((row) => textWidth(row[index], CELL_SIZE, false) + (index === flagColumn ? FLAG_SPACE / 1.08 : 0)),
) * 1.08 + PAD * 2 + 2));
}
@@ -226,17 +248,10 @@ function drawSectionTitle(writer, title, theme) {
function drawTable(writer, section, theme) {
const widths = columnWidths(section, writer.width);
const numeric = section.headers.map((_h, index) => {
const values = section.rows.map((row) => String(row[index] || "").trim()).filter(Boolean);
return values.length > 0 && values.every((value) => NUMERIC.test(value));
});
const hasPos = POS_HEADER.test(String(section.headers[0] || "").trim());
const placeColumn = section.headers.map((_h, index) => {
const values = section.rows.map((row) => String(row[index] || "").trim()).filter(Boolean);
return values.length > 0 && values.every((value) => /^(TQ|[A-Z]?\d+|-)$/.test(value));
});
// The driver name is the first column that is not a place or a number; podium rows make it bold.
const nameIndex = section.headers.findIndex((_h, index) => index > 0 && !numeric[index] && !placeColumn[index]);
const { numeric, placeColumn, nameIndex } = analyzeColumns(section);
const flags = hasFlags(section) ? section.flags : [];
const align = (index) => (placeColumn[index] && index < 3 ? "center" : numeric[index] ? "right" : "left");
const headHeight = 19;
const rowHeight = 16.5;
@@ -277,7 +292,15 @@ function drawTable(writer, section, theme) {
return;
}
const tq = String(row[index] || "").trim() === "TQ";
writer.text(x + PAD, top - 11.5, fitText(row[index], CELL_SIZE, bold || tq, widths[index] - PAD * 2), CELL_SIZE, { bold: bold || tq, color: tq ? "#b07d00" : TEXT, align: align(index), width: widths[index] - PAD * 2 });
let shift = 0;
if (index === nameIndex && flags[rowIndex]) {
// The country flag before the name; a country without a drawn flag shows its code.
if (!drawPdfFlag(writer, flags[rowIndex], x + PAD, top - 12.6, 12, 8.4)) {
writer.text(x + PAD, top - 11.2, String(flags[rowIndex]).slice(0, 3), 6, { bold: true, color: MUTED });
}
shift = FLAG_SPACE;
}
writer.text(x + PAD + shift, top - 11.5, fitText(row[index], CELL_SIZE, bold || tq, widths[index] - PAD * 2 - shift), CELL_SIZE, { bold: bold || tq, color: tq ? "#b07d00" : TEXT, align: align(index), width: widths[index] - PAD * 2 - shift });
x += widths[index];
});
writer.line(MARGIN, top - rowHeight, MARGIN + writer.width, top - rowHeight, RULE, 0.4);