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

+66
View File
@@ -0,0 +1,66 @@
// Country flags drawn as vector shapes in the PDF (the PDF writer draws rectangles, not SVG):
// stripes, Nordic crosses, the Swiss cross and Japan's disc cover most of the countries at
// club races. A flag not drawn here shows its country code instead.
const h = (...colors) => ({ type: "h", colors }); // horizontal stripes, top first
const v = (...colors) => ({ type: "v", colors }); // vertical stripes, left first
const nordic = (field, cross, inner) => ({ type: "nordic", field, cross, inner });
const FLAGS = {
SE: nordic("#006aa7", "#fecc00"), NO: nordic("#ba0c2f", "#ffffff", "#00205b"), DK: nordic("#c8102e", "#ffffff"),
FI: nordic("#ffffff", "#002f6c"), IS: nordic("#02529c", "#ffffff", "#dc1e35"),
DE: h("#000000", "#dd0000", "#ffce00"), AT: h("#ed2939", "#ffffff", "#ed2939"), NL: h("#ae1c28", "#ffffff", "#21468b"),
LU: h("#ed2939", "#ffffff", "#00a1de"), HU: h("#ce2939", "#ffffff", "#477050"), EE: h("#0072ce", "#000000", "#ffffff"),
LT: h("#fdb913", "#006a44", "#c1272d"), LV: { type: "h", colors: ["#9e3039", "#ffffff", "#9e3039"], weights: [2, 1, 2] },
RU: h("#ffffff", "#0039a6", "#d52b1e"), BG: h("#ffffff", "#00966e", "#d62612"), PL: h("#ffffff", "#dc143c"),
UA: h("#0057b7", "#ffd700"), ID: h("#ce1126", "#ffffff"), MC: h("#ce1126", "#ffffff"),
FR: v("#0055a4", "#ffffff", "#ef4135"), IT: v("#009246", "#ffffff", "#ce2b37"), BE: v("#000000", "#fdda24", "#ef3340"),
IE: v("#169b62", "#ffffff", "#ff883e"), RO: v("#002b7f", "#fcd116", "#ce1126"), MD: v("#0046ae", "#ffd200", "#cc092f"),
CH: { type: "swiss" }, JP: { type: "disc", field: "#ffffff", disc: "#bc002d" },
};
// Draws the flag with its lower-left corner at (x, y). Returns false when it isn't known.
function drawPdfFlag(writer, code, x, y, w, ht) {
const flag = FLAGS[String(code || "").toUpperCase()];
if (!flag) return false;
if (flag.type === "h" || flag.type === "v") {
const weights = flag.weights || flag.colors.map(() => 1);
const total = weights.reduce((sum, value) => sum + value, 0);
let offset = 0;
flag.colors.forEach((color, index) => {
const part = weights[index] / total;
if (flag.type === "h") writer.rect(x, y + ht * (1 - offset - part), w, ht * part, color);
else writer.rect(x + w * offset, y, w * part, ht, color);
offset += part;
});
} else if (flag.type === "nordic") {
// Cross a bit left of centre, like the real flags (proportions simplified).
const cx = x + w * 0.36;
const cy = y + ht / 2;
const arm = ht * 0.25;
writer.rect(x, y, w, ht, flag.field);
writer.rect(x, cy - arm / 2, w, arm, flag.cross);
writer.rect(cx - arm / 2, y, arm, ht, flag.cross);
if (flag.inner) {
const thin = arm * 0.5;
writer.rect(x, cy - thin / 2, w, thin, flag.inner);
writer.rect(cx - thin / 2, y, thin, ht, flag.inner);
}
} else if (flag.type === "swiss") {
writer.rect(x, y, w, ht, "#da291c");
const s = ht * 0.6;
const bar = s * 0.32;
writer.rect(x + w / 2 - s / 2, y + ht / 2 - bar / 2, s, bar, "#ffffff");
writer.rect(x + w / 2 - bar / 2, y + ht / 2 - s / 2, bar, s, "#ffffff");
} else if (flag.type === "disc") {
writer.rect(x, y, w, ht, flag.field);
writer.circle(x + w / 2, y + ht / 2, ht * 0.3, flag.disc);
}
// A thin edge so white flags (Finland, Japan) don't vanish on white paper.
writer.line(x, y, x + w, y, "#9aa3ad", 0.3);
writer.line(x, y + ht, x + w, y + ht, "#9aa3ad", 0.3);
writer.line(x, y, x, y + ht, "#9aa3ad", 0.3);
writer.line(x + w, y, x + w, y + ht, "#9aa3ad", 0.3);
return true;
}
module.exports = { drawPdfFlag, PDF_FLAG_CODES: Object.keys(FLAGS) };
+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);