67 lines
3.6 KiB
JavaScript
67 lines
3.6 KiB
JavaScript
// 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) };
|