Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

feat(pdf): draw the trip's route on the cover, keep its figures on one line

Maurice590b174dd162
diff --git a/client/src/components/PDF/TripPDF.tsx b/client/src/components/PDF/TripPDF.tsx
index cc101a813..087d11ba2 100644
--- a/client/src/components/PDF/TripPDF.tsx
+++ b/client/src/components/PDF/TripPDF.tsx
@@ -429,6 +429,11 @@ ${coverHtml({
image: coverImg,
logo: absUrl('/logo-light.svg'),
stats,
+ route: (tripRoute?.days || []).filter(d => d.lines.length).map(d => ({ colour: hexColour(d.color.line, '#ffffff'), lines: d.lines })),
+ ribbon: sorted.map(dayColourOf),
+ ribbonEnds: sorted.length > 1 && sorted[0].date && sorted[sorted.length - 1].date
+ ? [shortDate(sorted[0].date, loc), shortDate(sorted[sorted.length - 1].date, loc)]
+ : null,
tr,
})}
${overviewHtml}
diff --git a/client/src/components/PDF/tripPdfOverview.test.ts b/client/src/components/PDF/tripPdfOverview.test.ts
index b3c5e84be..f9c803a8c 100644
--- a/client/src/components/PDF/tripPdfOverview.test.ts
+++ b/client/src/components/PDF/tripPdfOverview.test.ts
@@ -4,7 +4,7 @@
*/
import { describe, expect, it } from 'vitest';
-import { coverHtml, glanceHtml, ledgerHtml, type GlanceRow, type LedgerRow } from './tripPdfOverview';
+import { coverHtml, coverRouteSvg, glanceHtml, ledgerHtml, type GlanceRow, type LedgerRow } from './tripPdfOverview';
const tr = (key: string) => key;
const parse = (html: string) => new DOMParser().parseFromString(html, 'text/html');
@@ -21,6 +21,9 @@ describe('coverHtml', () => {
{ value: '22', label: 'days' },
{ value: '1,234 km', label: 'pdf.distanceLabel' },
],
+ route: [],
+ ribbon: [],
+ ribbonEnds: null,
tr,
};
@@ -52,6 +55,82 @@ describe('coverHtml', () => {
});
});
+describe('the cover route and day band', () => {
+ const base = { title: 'Japan', description: null, range: null, image: null, logo: null, stats: [], tr };
+
+ it('FE-PDFOVERVIEW-006: the route is drawn day by day in its colours, the band has a segment per day', () => {
+ const doc = parse(
+ coverHtml({
+ ...base,
+ route: [
+ {
+ colour: '#ff0000',
+ lines: [
+ [
+ [35.68, 139.76],
+ [35.0, 135.77],
+ ],
+ ],
+ },
+ {
+ colour: '#00ff00',
+ lines: [
+ [
+ [35.0, 135.77],
+ [34.69, 135.5],
+ ],
+ ],
+ },
+ ],
+ ribbon: ['#ff0000', '#00ff00', '#0000ff'],
+ ribbonEnds: ['Sun, Nov 1', 'Tue, Nov 3'],
+ })
+ );
+
+ const coloured = [...doc.querySelectorAll('.cover-route-svg path[stroke]')].map((p) => p.getAttribute('stroke'));
+ expect(coloured).toEqual(['#ff0000', '#00ff00']);
+ expect(doc.querySelectorAll('.cover-route-glow path')).toHaveLength(2);
+ expect(texts(doc, '.cover-route-lbl')).toEqual(['pdf.mapTitle']);
+ expect(doc.querySelectorAll('.cover-ribbon span')).toHaveLength(3);
+ expect(texts(doc, '.cover-ribbon-ends span')).toEqual(['Sun, Nov 1', 'Tue, Nov 3']);
+ // Every point lands inside the drawing.
+ const numbers = [...doc.querySelectorAll('.cover-route-svg path[stroke]')].flatMap((p) =>
+ (p.getAttribute('d') ?? '').split(/[ML ]/).filter(Boolean).map(Number)
+ );
+ for (const n of numbers) expect(n).toBeGreaterThanOrEqual(0);
+ for (const n of numbers) expect(n).toBeLessThanOrEqual(240);
+ });
+
+ it('FE-PDFOVERVIEW-007: nothing routed, one point or one spot draws no card, one day no band', () => {
+ expect(coverRouteSvg([])).toBe('');
+ expect(coverRouteSvg([{ colour: '#ff0000', lines: [[[1, 1]]] }])).toBe('');
+ expect(
+ coverRouteSvg([
+ {
+ colour: '#ff0000',
+ lines: [
+ [
+ [1, 1],
+ [1, 1],
+ ],
+ ],
+ },
+ ])
+ ).toBe('');
+
+ const doc = parse(coverHtml({ ...base, route: [], ribbon: ['#ff0000'], ribbonEnds: null }));
+ expect(doc.querySelector('.cover-route')).toBeNull();
+ expect(doc.querySelector('.cover-ribbon')).toBeNull();
+ });
+
+ it('FE-PDFOVERVIEW-008: a long road is thinned to a drawable number of points', () => {
+ const road = Array.from({ length: 20000 }, (_, i) => [45 + i / 20000, 7 + i / 10000] as [number, number]);
+ const svg = coverRouteSvg([{ colour: '#ff0000', lines: [road] }]);
+ const d = /<path d="([^"]+)" stroke=/.exec(svg)?.[1] ?? '';
+ expect(d.split('L').length).toBeLessThanOrEqual(1700);
+ });
+});
+
describe('glanceHtml', () => {
const row = (over: Partial<GlanceRow> = {}): GlanceRow => ({
number: 1,
diff --git a/client/src/components/PDF/tripPdfOverview.ts b/client/src/components/PDF/tripPdfOverview.ts
index 2d4546aec..3073725e4 100644
--- a/client/src/components/PDF/tripPdfOverview.ts
+++ b/client/src/components/PDF/tripPdfOverview.ts
@@ -13,6 +13,12 @@ export interface CoverStat {
label: string;
}
+/** One day of the route as the cover draws it: its colour and its roads, `[lat, lng]`. */
+export interface CoverRouteDay {
+ colour: string;
+ lines: readonly (readonly [number, number])[][];
+}
+
export interface CoverInput {
title: string;
description: string | null;
@@ -22,11 +28,98 @@ export interface CoverInput {
/** The light TREK logo, absolute. */
logo: string | null;
stats: readonly CoverStat[];
+ /** The routed days, drawn as the trip's shape; empty when nothing was routed. */
+ route: readonly CoverRouteDay[];
+ /** Every day's colour in trip order, for the band under the title. */
+ ribbon: readonly string[];
+ /** The first and the last day's short date, under the band's ends. */
+ ribbonEnds: readonly [string, string] | null;
tr: Translate;
}
+const ROUTE_W = 240;
+const ROUTE_H = 170;
+const ROUTE_PAD = 14;
+/** Points drawn at most: a road trip routes to tens of thousands, the shape needs far fewer. */
+const ROUTE_MAX_POINTS = 1600;
+
+type LatLng = readonly [number, number];
+
+/**
+ * The trip's roads as one small drawing, each day in its colour on a soft glow, with a
+ * ring where the trip starts and a dot where it ends. Projected flat with the longitude
+ * scaled to the middle latitude, which is true enough at the size of a trip. Empty when
+ * there is no shape to draw.
+ */
+export function coverRouteSvg(days: readonly CoverRouteDay[]): string {
+ const lines = days.flatMap((d) => d.lines.filter((l) => l.length > 1).map((l) => ({ colour: d.colour, points: l })));
+ const points = lines.flatMap((l) => l.points);
+ if (!lines.length) return '';
+ const lats = points.map((p) => p[0]);
+ const midLat = (Math.min(...lats) + Math.max(...lats)) / 2;
+ const k = Math.cos((midLat * Math.PI) / 180);
+ const xs = points.map((p) => p[1] * k);
+ const ys = points.map((p) => -p[0]);
+ const minX = Math.min(...xs);
+ const minY = Math.min(...ys);
+ const w = Math.max(...xs) - minX;
+ const h = Math.max(...ys) - minY;
+ const scale = Math.min(
+ w > 0 ? (ROUTE_W - 2 * ROUTE_PAD) / w : Infinity,
+ h > 0 ? (ROUTE_H - 2 * ROUTE_PAD) / h : Infinity
+ );
+ if (!Number.isFinite(scale)) return '';
+ const offX = (ROUTE_W - w * scale) / 2;
+ const offY = (ROUTE_H - h * scale) / 2;
+ const x = (p: LatLng) => (offX + (p[1] * k - minX) * scale).toFixed(1);
+ const y = (p: LatLng) => (offY + (-p[0] - minY) * scale).toFixed(1);
+ const step = Math.max(1, Math.ceil(points.length / ROUTE_MAX_POINTS));
+ const path = (line: readonly LatLng[]) =>
+ line
+ .filter((_, i) => i % step === 0 || i === line.length - 1)
+ .map((p, i) => `${i ? 'L' : 'M'}${x(p)} ${y(p)}`)
+ .join('');
+ const drawn = lines.map((l) => ({ colour: l.colour, d: path(l.points) }));
+ const first = lines[0];
+ const last = lines[lines.length - 1];
+ const start = first.points[0];
+ const end = last.points[last.points.length - 1];
+ return `<svg class="cover-route-svg" viewBox="0 0 ${ROUTE_W} ${ROUTE_H}" preserveAspectRatio="xMidYMid meet" aria-hidden="true">
+ <g class="cover-route-glow" fill="none" stroke="currentColor" stroke-width="5.5" stroke-linecap="round" stroke-linejoin="round">${drawn
+ .map((l) => `<path d="${l.d}"/>`)
+ .join('')}</g>
+ <g fill="none" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">${drawn
+ .map((l) => `<path d="${l.d}" stroke="${l.colour}"/>`)
+ .join('')}</g>
+ <circle class="cover-route-start" cx="${x(start)}" cy="${y(start)}" r="4.2" stroke="${first.colour}" stroke-width="2.2"/>
+ <circle class="cover-route-end" cx="${x(end)}" cy="${y(end)}" r="4.2" fill="${last.colour}" stroke-width="1.6"/>
+ </svg>`;
+}
+
/** The cover: the trip's photo left bright, the title, the dates and its figures. */
-export function coverHtml({ title, description, range, image, logo, stats, tr }: CoverInput): string {
+export function coverHtml({
+ title,
+ description,
+ range,
+ image,
+ logo,
+ stats,
+ route,
+ ribbon,
+ ribbonEnds,
+ tr,
+}: CoverInput): string {
+ const routeSvg = coverRouteSvg(route);
+ const routeCard = routeSvg
+ ? `<div class="cover-route"><div class="cover-route-lbl">${escHtml(tr('pdf.mapTitle'))}</div>${routeSvg}</div>`
+ : '';
+ const ends = ribbonEnds
+ ? `<div class="cover-ribbon-ends"><span>${escHtml(ribbonEnds[0])}</span><span>${escHtml(ribbonEnds[1])}</span></div>`
+ : '';
+ const band =
+ ribbon.length > 1
+ ? `<div class="cover-ribbon">${ribbon.map((c) => `<span style="background:${c}"></span>`).join('')}</div>${ends}`
+ : '';
return `<section class="cover${image ? '' : ' cover-plain'}">
<div class="cover-bg"${image ? ` style="background-image:url('${escHtml(cssUrl(image))}')"` : ''}></div>
<div class="cover-shade"></div>
@@ -35,9 +128,15 @@ export function coverHtml({ title, description, range, image, logo, stats, tr }:
<span class="eyebrow">${escHtml(tr('pdf.travelPlan'))}</span>
</div>
<div class="cover-body">
- ${range ? `<div class="cover-dates">${escHtml(range)}</div>` : ''}
- <h1 class="cover-title">${escHtml(title)}</h1>
- ${description ? `<p class="cover-desc">${escHtml(description)}</p>` : ''}
+ <div class="cover-main">
+ <div class="cover-head">
+ ${range ? `<div class="cover-dates">${escHtml(range)}</div>` : ''}
+ <h1 class="cover-title">${escHtml(title)}</h1>
+ ${description ? `<p class="cover-desc">${escHtml(description)}</p>` : ''}
+ </div>
+ ${routeCard}
+ </div>
+ ${band}
<div class="cover-stats">${stats
.map(
(s) =>
@@ -77,7 +176,7 @@ export function glanceHtml(rows: readonly GlanceRow[], tr: Translate): string {
.map(
(r) => `<tr style="--day:${r.colour}">
<td><span class="glance-day">${r.number}</span></td>
- <td class="num">${escHtml(r.date)}</td>
+ <td class="num glance-date">${escHtml(r.date)}</td>
<td class="glance-title">${escHtml(r.title)}</td>
<td>${escHtml(r.stay)}</td>
<td class="num glance-stops">${r.stops || ''}</td>
diff --git a/client/src/components/PDF/tripPdfStyles.ts b/client/src/components/PDF/tripPdfStyles.ts
index a875c6f47..1c58bee2d 100644
--- a/client/src/components/PDF/tripPdfStyles.ts
+++ b/client/src/components/PDF/tripPdfStyles.ts
@@ -47,6 +47,9 @@ export function tripPdfCss(): string {
--glass: rgba(255, 255, 255, 0.13);
--glass-edge: rgba(255, 255, 255, 0.22);
--glass-dark: rgba(14, 14, 18, 0.42);
+ --glass-tile: rgba(18, 18, 24, 0.34);
+ --glass-deep: rgba(12, 12, 16, 0.5);
+ --route-glow: rgba(255, 255, 255, 0.5);
--on-night: rgba(255, 255, 255, 0.78);
--on-night-2: rgba(255, 255, 255, 0.56);
--gold: #b7791f;
@@ -93,7 +96,7 @@ export function tripPdfCss(): string {
}
.cover-shade {
position: absolute; inset: 0;
- background: linear-gradient(180deg, var(--shade-mid) 0%, var(--shade-none) 24%, var(--shade-none) 40%, var(--shade) 100%);
+ background: linear-gradient(180deg, var(--shade-mid) 0%, var(--shade-none) 22%, var(--shade-none) 36%, var(--shade) 92%);
}
.cover-top {
position: absolute; inset-block-start: 14mm; inset-inline: var(--gutter);
@@ -101,21 +104,56 @@ export function tripPdfCss(): string {
}
.cover-top img { height: 22px; }
.cover-top .eyebrow { color: var(--on-night); }
- .cover-body { position: absolute; inset-inline: var(--gutter); inset-block-end: 16mm; }
- .cover-dates { font-family: var(--num); font-size: 11px; font-weight: 500; letter-spacing: 0.04em; color: var(--on-night); }
- .cover-title { font-size: 46px; font-weight: 700; line-height: 1.04; letter-spacing: -0.02em; margin: 6px 0 10px; }
- .cover-desc { max-width: 135mm; font-size: 12px; line-height: 1.6; color: var(--on-night); }
- .cover-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(30mm, 1fr)); gap: 8px; margin-block-start: 10mm; }
+ .cover-body { position: absolute; inset-inline: var(--gutter); inset-block-end: 15mm; }
+ .cover-main { display: flex; align-items: flex-end; gap: 8mm; }
+ .cover-head { flex: 1; min-width: 0; }
+ .cover-dates {
+ display: inline-block; font-family: var(--num); font-size: 9px; font-weight: 600; letter-spacing: 0.16em;
+ text-transform: uppercase; color: var(--paper); padding: 5px 11px; border-radius: 99px;
+ background: var(--glass-tile); border: 1px solid var(--glass-edge);
+ }
+ .cover-title { font-size: 54px; font-weight: 700; line-height: 1; letter-spacing: -0.025em; margin: 5mm 0 3.5mm; }
+ .cover-desc { max-width: 120mm; font-size: 12px; line-height: 1.6; color: var(--on-night); }
+ .cover-route {
+ flex: 0 0 66mm; padding: 9px 10px 6px; border-radius: 18px;
+ background: var(--glass-deep); border: 1px solid var(--glass-edge);
+ }
+ .cover-route-lbl {
+ font-family: var(--num); font-size: 7px; font-weight: 600; letter-spacing: 0.16em;
+ text-transform: uppercase; color: var(--on-night-2);
+ }
+ .cover-route-svg { display: block; width: 100%; height: auto; }
+ .cover-route-glow { color: var(--route-glow); }
+ .cover-route-start { fill: var(--paper); }
+ .cover-route-end { stroke: var(--paper); }
+ .cover-ribbon { display: flex; gap: 2px; height: 5px; margin-block-start: 8mm; }
+ .cover-ribbon span { flex: 1; border-radius: 3px; }
+ .cover-ribbon-ends {
+ display: flex; justify-content: space-between; margin-block-start: 5px;
+ font-family: var(--num); font-size: 7.5px; font-weight: 600; letter-spacing: 0.12em;
+ text-transform: uppercase; color: var(--on-night-2);
+ }
+ .cover-stats { display: flex; flex-wrap: wrap; gap: 8px; margin-block-start: 6mm; }
.cover-stat {
- background: var(--glass); border: 1px solid var(--glass-edge); border-radius: 16px;
- padding: 12px 14px 11px; -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
+ flex: 1 1 0; min-width: max-content;
+ background: var(--glass-tile); border: 1px solid var(--glass-edge); border-radius: 16px;
+ padding: 12px 14px 11px;
+ }
+ .cover-stat-num {
+ font-family: var(--num); font-size: 19px; font-weight: 600; line-height: 1.1;
+ white-space: nowrap; font-variant-numeric: tabular-nums;
}
- .cover-stat-num { font-family: var(--num); font-size: 20px; font-weight: 600; line-height: 1.1; font-variant-numeric: tabular-nums; }
.cover-stat-lbl {
font-family: var(--num); font-size: 7.5px; font-weight: 500; letter-spacing: 0.14em;
text-transform: uppercase; color: var(--on-night-2); margin-block-start: 5px;
}
+ /* The blur behind the glass shows in the preview; print engines leave it out. */
+ @media screen {
+ .cover-dates, .cover-stat, .glass-label { -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); }
+ .cover-route { -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px); }
+ }
+
/* ── Overview: route map, the days at a glance, bookings and stays ─────── */
.overview { padding: 2mm var(--gutter) 0; }
.pdf-no-overview .overview { display: none; }
@@ -150,6 +188,7 @@ export function tripPdfCss(): string {
.glance-title { color: var(--ink); font-weight: 500; }
.glance .num, .glance-end { text-align: end; white-space: nowrap; }
.glance .glance-stops { text-align: center; }
+ .glance .glance-date { text-align: start; }
.glance th.glance-stops svg { display: inline-block; vertical-align: -1px; }
.ledger { display: grid; gap: 5px; }
@@ -223,7 +262,6 @@ export function tripPdfCss(): string {
padding: 4px 10px; border-radius: 99px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
font-size: calc(8px * var(--t)); font-weight: 500; color: var(--paper);
background: var(--glass-dark); border: 1px solid var(--glass-edge);
- -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
}
.pdf-no-photos .day-strip { display: none; }