@le-space/eigenbeleg 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -10,11 +10,11 @@ An Eigenbeleg is a receipt the payer writes for a payment that has no receipt fr
10
10
  - **The Eigenbeleg of one booking** (Belege's, `@le-space/eigenbeleg/booking`, `/booking-pdf`): `bookingPdf(doc)` for a `BookingEigenbeleg` – a bank booking or a crypto payment, with an own wallet's transaction in full (whole hash, both addresses, every movement with the gas, the explorer link as a QR code). The booking's context is the caller's to gather. `nextSelfNumber(receipts, year)` – the range EB-YYYY-NNN.
11
11
  - **A statement Eigenbeleg** (Belege's Aleph, Akash and travel statements, `@le-space/eigenbeleg/statement-layout`): `statementLayout({ title, created, continuation })` → `head`, `field`, `rule`, `heading`, `table` (the header repeats on a new page), `signature`, `save`.
12
12
 
13
- Every PDF has Eigenbeleg's look (`layout.js`): A4, grey labels beside their values, a note in a tinted box, page numbers at the foot, a line to sign. `creator` names the program in the metadata (`eigenbeleg` for the period's documents, `Belege` for a booking's and a statement's unless given).
13
+ Every PDF has Eigenbeleg's look (`layout.js`, [#951](https://github.com/Le-Space/belege/issues/951)): A4 with 48 pt margins, the title first and a strong rule under the head, small grey labels, rules in two weights and no fills or boxes, a total between two rules, colour for a warning only, page numbers at the foot, a line to sign. The sizes, colours and rule weights are tokens (`TYPE`, `INK`, `GREY`, `RULE`, `WARN`, `STROKE`); nothing is set outside them. `creator` names the program in the metadata (`eigenbeleg` for the period's documents, `Belege` for a booking's and a statement's unless given).
14
14
 
15
15
  ## The issuer's own logo
16
16
 
17
- An Eigenbeleg and a Kontoauszug are issued by the payer, so the logo that belongs on them is the payer's ([#906](https://github.com/Le-Space/belege/issues/906)). `eigenbelegPdf(doc, { issuerLogo })`, `statementPdf(st, { issuerLogo })` and `bookingPdf(doc, { issuerLogo })` take the bytes of a PNG or JPEG file and draw it in the head's top right corner, level with the title, fitted into 120 × 40 pt (`ISSUER_LOGO_BOX`) with its proportions kept. The text of the head is where it is without a logo; the chain's mark (`logo: true`) stays beside the title, and a document may carry both. The package ships no such picture and fetches none: the caller hands it in.
17
+ An Eigenbeleg is issued by the payer, so the logo that belongs on it is the payer's ([#906](https://github.com/Le-Space/belege/issues/906)). `eigenbelegPdf(doc, { issuerLogo })`, `bookingPdf(doc, { issuerLogo })` and `statementLayout({ …, issuerLogo })` take the bytes of a PNG or JPEG file and draw it large, as a letterhead: top right, fitted into about 70 × 25 mm (`ISSUER_LOGO_BOX`, 200 × 70 pt) with its proportions kept, the text below it and not above 45 mm from the top edge (`LETTERHEAD`, the head zone of DIN 5008 form B). Without a logo the page is as before. The chain's mark (`logo: true`) is the network's, not the issuer's: on an Eigenbeleg it stays very small beside the title (`MARK_SIZE.small`); on a Kontoauszug or Verbrauchsauszug, which shows what the account did and is not the issuer's own receipt, it is larger (`MARK_SIZE.large`) and the statement carries no issuer's logo. The package ships no such picture and fetches none: the caller hands it in.
18
18
 
19
19
  The file goes through [`@le-space/pdf`](../pdf)'s `pdfImage` first: at most 512 kB and 2048 pixels a side, no wider than six times its height and no taller than twice its width (`IMAGE_LIMITS`); a PNG is laid on white and written again without transparency, text chunks or a colour profile, a JPEG loses EXIF, comments and its other application segments. SVG is refused, as is a JPEG in CMYK. A file that does not pass is an `ImageError` with a sentence for the person, never a document without its logo.
20
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@le-space/eigenbeleg",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "description": "The Eigenbeleg and the Kontoauszug of Le Space's tools: a payer's own receipt for payments without one from the other side (a period's network fees, one booking in any script), a blockchain account's statement (Aleph credits as a Verbrauchsauszug), as PDF/A-2b and CSV. Browser and Node; the rates handed in.",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "type": "module",
@@ -36,16 +36,16 @@
36
36
  "dependencies": {
37
37
  "pdf-lib": "1.17.1",
38
38
  "uqr": "0.1.3",
39
+ "@le-space/charts": "^0.2.0",
39
40
  "@le-space/ledger": "^0.3.0",
40
- "@le-space/pdf": "^0.2.0",
41
- "@le-space/charts": "^0.2.0"
41
+ "@le-space/pdf": "^0.2.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@noble/hashes": "2.4.0",
45
45
  "prettier": "3.6.2",
46
46
  "unpdf": "1.8.1",
47
47
  "@le-space/browser-test": "0.1.0",
48
- "@le-space/chains": "^0.6.0"
48
+ "@le-space/chains": "^0.6.1"
49
49
  },
50
50
  "scripts": {
51
51
  "test": "node --test --test-reporter=spec \"test/*.test.js\"",
@@ -13,29 +13,24 @@ import { fromUnits } from '@le-space/ledger';
13
13
 
14
14
  import { bookingNote, bookingNotInvoice } from './booking.js';
15
15
  import { deDate, deEuro, deNumber } from './document.js';
16
- import { A4, BLACK, GREY, embedIssuerLogo, pageLayout } from './layout.js';
16
+ import { A4, BLACK, GREY, TYPE, embedIssuerLogo, pageLayout } from './layout.js';
17
17
 
18
18
  /**
19
19
  * @param {import('./booking.js').BookingEigenbeleg} doc
20
20
  * @param {{ creator?: string, producer?: string, issuerLogo?: Uint8Array | null }} [o] what made it: `Belege` when not given. issuerLogo: the issuer's own
21
- * logo, a PNG or JPEG file's bytes, in the head's top right corner (none unless given; an
21
+ * logo, a PNG or JPEG file's bytes, large as a letterhead: top right, the text below it (none unless given; an
22
22
  * ImageError of @le-space/pdf when the file is no small PNG or JPEG)
23
23
  * @returns {Promise<Uint8Array>}
24
24
  */
25
25
  export async function bookingPdf(doc, { creator = 'Belege', producer, issuerLogo = null } = {}) {
26
26
  const created = new Date(doc.createdAt);
27
27
  const { pdf, fonts } = await createPdfA();
28
- const { regular, bold, mono } = fonts;
28
+ const { bold, mono } = fonts;
29
29
  const own = await embedIssuerLogo(pdf, issuerLogo);
30
30
  const M = 48;
31
31
  let pageNo = 1;
32
32
  const footer = (/** @type {import('./layout.js').Layout} */ l) =>
33
- regular.draw(l.page, `${doc.number} · Seite ${pageNo}`, {
34
- x: M,
35
- y: M / 2,
36
- size: 7,
37
- color: GREY
38
- });
33
+ l.foot(l.page, `${doc.number} · Seite ${pageNo}`);
39
34
  const l = pageLayout(pdf, fonts, {
40
35
  margin: M,
41
36
  leavePage: footer,
@@ -43,37 +38,36 @@ export async function bookingPdf(doc, { creator = 'Belege', producer, issuerLogo
43
38
  pageNo++;
44
39
  }
45
40
  });
46
- const { text, gap, field } = l;
41
+ const { text, gap, field, label } = l;
47
42
  const width = l.width;
48
43
  const out = doc.amountCents < 0;
49
44
 
50
45
  // ---- head
51
- l.issuerLogo(own, l.y);
52
- text('Eigenbeleg', { face: bold, size: 20 });
53
- text(`Nr. ${doc.number}`, { face: bold, size: 12 });
54
- gap(2);
55
- text('Beleg für eine Zahlung, zu der es keinen Beleg der Gegenseite gibt.', { size: 10 });
56
- gap(10);
46
+ l.letterhead(own);
47
+ l.head('Eigenbeleg', {
48
+ number: doc.number,
49
+ subtitle: 'Beleg für eine Zahlung, zu der es keinen Beleg der Gegenseite gibt.'
50
+ });
57
51
  const top = l.y;
58
52
  const half = width / 2 - 10;
59
- text('Aussteller', { face: bold, size: 8.5, color: GREY, max: half });
60
- text(doc.issuer || '—', { face: bold, size: 10, max: half });
61
- if (doc.issuerAddress) text(doc.issuerAddress, { size: 9.5, max: half });
53
+ label('Aussteller', { max: half });
54
+ text(doc.issuer || '—', { face: bold, size: TYPE.lead, max: half });
55
+ if (doc.issuerAddress) text(doc.issuerAddress, { max: half });
62
56
  const leftEnd = l.y;
63
57
  l.y = top;
64
58
  const rx = M + width / 2 + 10;
65
- text('Ausgestellt am', { x: rx, face: bold, size: 8.5, color: GREY });
59
+ label('Ausgestellt am', { x: rx });
66
60
  text(`${deDate(doc.createdAt)}, ${doc.createdAt.slice(11, 16)} UTC`, { x: rx });
67
- gap(3);
68
- text(out ? 'Empfänger' : 'Zahlende Seite', { x: rx, face: bold, size: 8.5, color: GREY });
61
+ gap(5);
62
+ label(out ? 'Empfänger' : 'Zahlende Seite', { x: rx });
69
63
  text(doc.counterparty || '—', { x: rx });
70
64
  l.y = Math.min(l.y, leftEnd);
71
65
  gap(8);
72
- text('Konto', { face: bold, size: 8.5, color: GREY });
73
- text(doc.account || '—', { size: 9.5 });
74
- gap(10);
75
- l.box(bookingNote(Boolean(doc.chain)));
66
+ label('Konto');
67
+ text(doc.account || '—');
76
68
  gap(12);
69
+ l.note(bookingNote(Boolean(doc.chain)));
70
+ gap(16);
77
71
 
78
72
  // ---- the payment
79
73
  field('Datum der Zahlung', deDate(doc.date));
@@ -90,78 +84,62 @@ export async function bookingPdf(doc, { creator = 'Belege', producer, issuerLogo
90
84
 
91
85
  if (doc.chain) {
92
86
  const c = doc.chain;
93
- const side = 96;
94
- const textMax = width - side - 12;
87
+ const side = 72;
88
+ const textMax = width - side - 16;
95
89
  l.ensure(150);
96
90
  l.rule();
97
- gap(8);
91
+ gap(10);
98
92
  const qrTop = l.y;
99
93
  text(`Zahlung auf ${c.chain}${c.at ? ` · ${deDate(c.at)} ${c.at.slice(11, 19)} UTC` : ''}`, {
100
94
  face: bold,
101
- size: 9.5,
95
+ size: TYPE.lead,
102
96
  max: textMax
103
97
  });
104
- gap(2);
98
+ gap(3);
105
99
  /** @param {import('./booking.js').Party} p */
106
100
  const party = (p) => {
107
101
  const note = [p.label, p.own ? 'eigene Wallet' : ''].filter(Boolean).join(', ');
108
102
  if (!p.address) return note || '—';
109
103
  return note && p.address !== p.label ? `${p.address} (${note})` : p.address;
110
104
  };
111
- field('Transaktion', c.hash, { face: mono, size: 7.5, max: textMax });
112
- field('Von', party(c.from), { face: mono, size: 7.5, max: textMax });
113
- field('An', party(c.to), { face: mono, size: 7.5, max: textMax });
105
+ const hash = { face: mono, size: TYPE.small, max: textMax, labelWidth: 76 };
106
+ field('Transaktion', c.hash, hash);
107
+ field('Von', party(c.from), hash);
108
+ field('An', party(c.to), hash);
114
109
  if (c.explorerUrl) {
115
- field('Block-Explorer', c.explorerUrl, { face: mono, size: 6.5, max: textMax });
110
+ field('Block-Explorer', c.explorerUrl, { ...hash, size: TYPE.label });
116
111
  l.qr(c.explorerUrl, A4[0] - M - side, qrTop, side);
117
- l.y = Math.min(l.y, qrTop - side - 4);
112
+ l.y = Math.min(l.y, qrTop - side);
118
113
  }
119
- gap(3);
114
+ gap(10);
120
115
  const columns = [
121
116
  { title: 'Bewegung', width: 170 },
122
117
  { title: 'Menge', width: 150, right: true },
123
118
  { title: 'Wert EUR', width: 90, right: true },
124
119
  { title: 'Gebucht', width: width - 410 }
125
120
  ];
126
- const header = () =>
127
- l.row(
128
- columns,
129
- columns.map((x) => x.title),
130
- { face: bold, color: GREY }
131
- );
121
+ const header = () => l.tableHead(columns);
132
122
  header();
133
123
  for (const m of c.movements)
134
124
  l.row(columns, [m.what, m.quantity || '—', m.euro || '—', m.booked ? 'ja' : 'nein'], {
135
125
  color: m.booked ? BLACK : GREY,
136
126
  header
137
127
  });
138
- gap(6);
139
- } else if (doc.txRef) field('Referenz', doc.txRef, { face: mono, size: 7.5 });
128
+ gap(12);
129
+ } else if (doc.txRef) field('Referenz', doc.txRef, { face: mono, size: TYPE.small });
140
130
 
141
131
  // ---- the amount, what and why
142
- l.ensure(40);
143
- l.rule();
144
- gap(8);
145
- l.at('Betrag', M, 10, bold);
146
- l.right(deEuro(fromUnits(String(doc.amountCents), 2), 2), A4[0] - M, 11, bold);
147
- l.y -= 18;
132
+ l.total('Betrag', deEuro(fromUnits(String(doc.amountCents), 2), 2));
148
133
  field('Was wurde bezahlt', doc.description);
134
+ gap(2);
149
135
  field('Warum kein Fremdbeleg', doc.reason);
136
+ gap(2);
150
137
  field('Erstellt', `${deDate(doc.createdAt)}${doc.createdBy ? ` von ${doc.createdBy}` : ''}`);
151
138
 
152
139
  // ---- what it is not, and the line to sign
153
140
  gap(8);
154
- text(bookingNotInvoice(creator), { size: 8.5, color: GREY });
155
- gap(24);
156
- l.ensure(40);
157
- l.page.drawLine({
158
- start: { x: M, y: l.y },
159
- end: { x: M + 230, y: l.y },
160
- thickness: 0.6,
161
- color: BLACK
162
- });
163
- l.y -= 4;
164
- text('Ort, Datum, Unterschrift', { size: 8, color: GREY });
141
+ text(bookingNotInvoice(creator), { size: TYPE.table, color: GREY });
142
+ l.signature('Ort, Datum, Unterschrift');
165
143
  footer(l);
166
144
 
167
145
  return savePdfA(pdf, {
package/src/chart-pdf.js CHANGED
@@ -18,9 +18,28 @@ const color = (hex) =>
18
18
  parseInt(hex.slice(5, 7), 16) / 255
19
19
  );
20
20
 
21
- /** Paper is light. */
21
+ /**
22
+ * A document's own tones (#951): the diagram is drawn in the ink of the page,
23
+ * its series a ramp from ink to light that is told apart by lightness alone,
24
+ * so it reads on a black-and-white print and with any colour vision. The
25
+ * roles are the chart's; a role this map does not name keeps the chart's
26
+ * light colour.
27
+ */
28
+ const DOCUMENT = /** @type {Partial<Record<Role, string>>} */ ({
29
+ text: '#17191f',
30
+ muted: '#666b75',
31
+ grid: '#e3e5e9',
32
+ axis: '#9a9fa8',
33
+ s1: '#1f3350',
34
+ s2: '#6d87a8',
35
+ s3: '#b3c1d3',
36
+ s4: '#3f5f8a',
37
+ other: '#9a9fa8'
38
+ });
22
39
  const COLORS = /** @type {Record<Role, import('pdf-lib').Color>} */ (
23
- Object.fromEntries(Object.entries(PALETTE.light).map(([role, hex]) => [role, color(hex)]))
40
+ Object.fromEntries(
41
+ Object.entries({ ...PALETTE.light, ...DOCUMENT }).map(([role, hex]) => [role, color(hex)])
42
+ )
24
43
  );
25
44
 
26
45
  /**
package/src/document.js CHANGED
@@ -108,8 +108,6 @@ export const currencyPlaces = (currency) => DOCUMENT_CURRENCIES[documentCurrency
108
108
  export function conversionText(currency) {
109
109
  const ecb = 'zum Euro-Referenzkurs der EZB des Tages oder des letzten davor';
110
110
  if (documentCurrency(currency) === 'EUR') return '';
111
- if (currency === 'USD')
112
- return `von CoinGecko dessen USD-Kurs desselben Zeitpunkts, sonst über den Euro in USD umgerechnet ${ecb}`;
113
111
  if (DOCUMENT_CURRENCIES[currency].via === 'cbr')
114
112
  return `über den Euro in ${currency} umgerechnet zum amtlichen Kurs der Bank von Russland, gültig am Tag (bis 01.03.2022 ${ecb})`;
115
113
  return `über den Euro in ${currency} umgerechnet ${ecb}`;
@@ -427,6 +425,23 @@ export function deNumber(amount) {
427
425
  return `${neg ? '−' : ''}${grouped}${frac ? ',' + frac : ''}`;
428
426
  }
429
427
 
428
+ /**
429
+ * A rate for the table, rounded to `digits` significant digits: `0.012345678901234567` → `0,0123457`,
430
+ * `0.00000072500003` → `0,000000725`. Read, not recomputed: the values stay at the full rate,
431
+ * which the CSV keeps; a rate with every decimal does not fit its column.
432
+ *
433
+ * @param {string} rate @param {number} [digits]
434
+ */
435
+ export function deRate(rate, digits = 6) {
436
+ const s = normalize(String(rate)).replace(/^-/, '');
437
+ const [int, frac = ''] = s.split('.');
438
+ const places =
439
+ int !== '0'
440
+ ? Math.max(0, digits - int.length)
441
+ : frac.length - frac.replace(/^0+/, '').length + digits;
442
+ return deNumber(normalize(round(String(rate), places)));
443
+ }
444
+
430
445
  /** An amount with fixed decimals and no unit: `0.0017` → `0,0017`. @param {string} amount @param {number} places */
431
446
  export function deFixed(amount, places = 2) {
432
447
  const r = round(String(amount), places);
package/src/index.js CHANGED
@@ -15,6 +15,7 @@ export {
15
15
  CHAIN_NAMES,
16
16
  DATA_SOURCES,
17
17
  deNumber,
18
+ deRate,
18
19
  deEuro,
19
20
  deFixed,
20
21
  deMoney,
@@ -45,7 +46,7 @@ export { valueEntries, fixedRates } from './value.js';
45
46
  export { nextSelfNumber, bookingNote, bookingNotInvoice } from './booking.js';
46
47
  export { bookingPdf } from './booking-pdf.js';
47
48
  export { statementLayout } from './statement-layout.js';
48
- export { ISSUER_LOGO_BOX } from './layout.js';
49
+ export { ISSUER_LOGO_BOX, LETTERHEAD, MARK_SIZE } from './layout.js';
49
50
  // What an issuer's logo may be, and the error a file that is not gets (issuerLogo of the PDFs).
50
51
  export { ImageError, IMAGE_LIMITS } from '@le-space/pdf';
51
52
  /** @typedef {import('./value.js').Rate} Rate */
package/src/layout.js CHANGED
@@ -1,10 +1,13 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-or-later
2
2
  // Eigenbeleg's look, shared by every document this package draws (the
3
3
  // Eigenbeleg of a period, the Kontoauszug, the Eigenbeleg of one booking, a
4
- // statement Eigenbeleg): A4, a 48 pt margin, grey labels beside their values,
5
- // thin rules, a tinted box for the note, tables whose header repeats on a
6
- // new page, a QR code of the explorer link. Text in any script the fonts
7
- // have (@le-space/pdf); pages as many as the content needs.
4
+ // statement Eigenbeleg), one style for all of them (#951): A4, a 48 pt margin,
5
+ // the title first and a strong rule under the head, labels small and grey
6
+ // beside or above their values, rules in two weights and nothing else to
7
+ // separate (no fills, no boxes), ink and one grey, red for a warning only, a
8
+ // total between two rules, tables whose header repeats on a new page, a QR
9
+ // code of the explorer link. Text in any script the fonts have
10
+ // (@le-space/pdf); pages as many as the content needs.
8
11
  //
9
12
  // The drawing helpers of Eigenbeleg's pdf.js and statement-pdf.js (from
10
13
  // Le-Space/eigenbeleg-agent at e6cde90), on faces instead of pdf-lib fonts.
@@ -18,16 +21,39 @@ import { embedPdfImage, pdfImage, pdfText, wrapText } from '@le-space/pdf';
18
21
  /** @typedef {import('pdf-lib').Color} Color */
19
22
 
20
23
  export const A4 = /** @type {[number, number]} */ ([595.28, 841.89]);
21
- export const BLACK = rgb(0, 0, 0);
22
- export const GREY = rgb(0.38, 0.38, 0.38);
23
- export const RULE = rgb(0.78, 0.78, 0.78);
24
- export const TINT = rgb(0.95, 0.95, 0.93);
25
- export const BAND = rgb(0.93, 0.94, 0.95);
26
- export const WARN = rgb(0.6, 0.12, 0.08);
27
- export const OK = rgb(0.1, 0.42, 0.2);
24
+ /** Ink: the text, the strong rules, the QR code. A near-black, softer on paper and screen than pure black. */
25
+ export const INK = rgb(0.09, 0.1, 0.13);
26
+ /** What every text is drawn in unless it says otherwise: ink. */
27
+ export const BLACK = INK;
28
+ /** Secondary text: labels, table heads, fine print. */
29
+ export const GREY = rgb(0.4, 0.42, 0.46);
30
+ /** Hairlines. */
31
+ export const RULE = rgb(0.78, 0.8, 0.83);
32
+ /** The one colour: a warning (the test network, a balance that does not reconcile). */
33
+ export const WARN = rgb(0.62, 0.13, 0.1);
28
34
 
29
- /** The room the issuer's logo is fitted into, in points: wide enough for a word mark, as high as a title and its line below. */
30
- export const ISSUER_LOGO_BOX = /** @type {readonly [number, number]} */ ([120, 40]);
35
+ /** The sizes every document is set in, in points. Nothing is set in a size between them. */
36
+ export const TYPE = /** @type {const} */ ({
37
+ title: 22,
38
+ lead: 10,
39
+ body: 9,
40
+ table: 8,
41
+ small: 7.5,
42
+ label: 7
43
+ });
44
+ /** The two rules: a strong one for structure (under the head, around a total), a hairline between rows. */
45
+ export const STROKE = /** @type {const} */ ({ strong: 0.8, hair: 0.3 });
46
+
47
+ /**
48
+ * The room the issuer's logo is fitted into, in points: about 70 × 25 mm, a letterhead's logo.
49
+ * It stands top right, in the head zone of a German business letter (DIN 5008, form B: the
50
+ * first 45 mm of the page); the text of the document starts below it.
51
+ */
52
+ export const ISSUER_LOGO_BOX = /** @type {readonly [number, number]} */ ([200, 70]);
53
+ /** Where the text starts below a letterhead: 45 mm from the top edge (DIN 5008, form B). */
54
+ export const LETTERHEAD = 45 * (72 / 25.4);
55
+ /** The chain's mark beside the title, in points: small on an Eigenbeleg, larger on a statement. */
56
+ export const MARK_SIZE = /** @type {const} */ ({ small: 11, large: 34 });
31
57
 
32
58
  /**
33
59
  * The issuer's own logo for a document: the bytes of a PNG or JPEG file,
@@ -100,7 +126,7 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
100
126
  text(t, o = {}) {
101
127
  const {
102
128
  x = margin,
103
- size = 9.5,
129
+ size = TYPE.body,
104
130
  face = regular,
105
131
  color = BLACK,
106
132
  max = l.width - (x - margin),
@@ -125,43 +151,50 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
125
151
  },
126
152
 
127
153
  /**
128
- * A one-line title and, when given, an image beside it, a little higher
129
- * than the letters: the chain's mark (`logo`).
154
+ * A one-line title and, when given, the chain's mark (`logo`) beside it:
155
+ * `mark` points square, its middle level with the middle of the capitals.
156
+ * A mark larger than the line makes room below it.
130
157
  *
131
158
  * @param {string} t
132
159
  * @param {{ face: Face, size: number }} o
133
160
  * @param {import('pdf-lib').PDFImage | null} [image]
161
+ * @param {number} [mark] the mark's side; the title's size when not given
134
162
  */
135
- title(t, { face, size }, image = null) {
163
+ title(t, { face, size }, image = null, mark = size) {
164
+ const lineTop = l.y;
136
165
  l.text(t, { face, size });
137
166
  if (!image) return;
138
- const top = l.y + size * 1.35;
167
+ // The capitals' middle: a little above the line's baseline.
168
+ const middle = lineTop - size + size * 0.36;
169
+ const bottom = middle - mark / 2;
139
170
  l.page.drawImage(image, {
140
- x: margin + face.width(t, size) + 10,
141
- y: top - size - 2,
142
- width: size + 2,
143
- height: size + 2
171
+ x: margin + face.width(t, size) + Math.max(6, mark * 0.3),
172
+ y: bottom,
173
+ width: mark,
174
+ height: mark
144
175
  });
176
+ l.y = Math.min(l.y, bottom - 4);
145
177
  },
146
178
 
147
179
  /**
148
- * The issuer's logo in the head's top right corner: fitted into
149
- * ISSUER_LOGO_BOX keeping its proportions, its top at `top`, its right
150
- * edge at the margin. The text of the head is where it is without one.
180
+ * The issuer's logo as a letterhead: fitted into ISSUER_LOGO_BOX keeping
181
+ * its proportions, top right with its top at the current line and its
182
+ * right edge at the margin. The text goes on below it, and not above the
183
+ * head zone of DIN 5008 (LETTERHEAD). Without a logo nothing moves.
151
184
  *
152
185
  * @param {import('pdf-lib').PDFImage | null} image
153
- * @param {number} top
154
186
  */
155
- issuerLogo(image, top) {
187
+ letterhead(image) {
156
188
  if (!image) return;
157
189
  const k = Math.min(ISSUER_LOGO_BOX[0] / image.width, ISSUER_LOGO_BOX[1] / image.height);
158
190
  const width = image.width * k;
159
191
  const height = image.height * k;
160
- l.page.drawImage(image, { x: A4[0] - margin - width, y: top - height, width, height });
192
+ l.page.drawImage(image, { x: A4[0] - margin - width, y: l.y - height, width, height });
193
+ l.y = Math.min(l.y - height - 14, A4[1] - LETTERHEAD);
161
194
  },
162
195
 
163
- /** @param {number} [thickness] @param {Color} [color] */
164
- rule(thickness = 0.6, color = RULE) {
196
+ /** A hairline across the page; `strong()` for the other one. @param {number} [thickness] @param {Color} [color] */
197
+ rule(thickness = STROKE.hair, color = RULE) {
165
198
  l.page.drawLine({
166
199
  start: { x: margin, y: l.y },
167
200
  end: { x: A4[0] - margin, y: l.y },
@@ -170,6 +203,115 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
170
203
  });
171
204
  },
172
205
 
206
+ /** The strong rule: under a head, around a total. */
207
+ strong() {
208
+ l.rule(STROKE.strong, INK);
209
+ },
210
+
211
+ /**
212
+ * A label: what the value below or beside it is. Small, grey, never bold.
213
+ *
214
+ * @param {string} t
215
+ * @param {{ x?: number, max?: number }} [o]
216
+ */
217
+ label(t, o = {}) {
218
+ l.text(t, { ...o, size: TYPE.label, color: GREY, lead: 1.55 });
219
+ },
220
+
221
+ /**
222
+ * The head of a document: its kind as the title (with the chain's mark when
223
+ * given), its number, what it is in one line, and the strong rule under them.
224
+ *
225
+ * @param {string} kind
226
+ * @param {{ number?: string, subtitle?: string, image?: import('pdf-lib').PDFImage | null, mark?: number }} [o]
227
+ */
228
+ head(kind, { number = '', subtitle = '', image = null, mark } = {}) {
229
+ l.title(kind, { face: fonts.bold, size: TYPE.title }, image, mark);
230
+ if (number) l.text(`Nr. ${number}`, { size: TYPE.lead });
231
+ if (subtitle) {
232
+ l.gap(1);
233
+ l.text(subtitle, { size: TYPE.body, color: GREY });
234
+ }
235
+ l.gap(8);
236
+ l.strong();
237
+ l.gap(12);
238
+ },
239
+
240
+ /**
241
+ * A total, alone between two strong rules: its label on the left, the
242
+ * amount large on the right, both on one baseline.
243
+ *
244
+ * @param {string} label
245
+ * @param {string} total
246
+ */
247
+ total(label, total) {
248
+ const size = 14;
249
+ l.ensure(size + 30);
250
+ l.strong();
251
+ l.y -= 8;
252
+ const amount = pdfText(total);
253
+ const room = l.width - fonts.bold.width(amount, size) - 16;
254
+ const lines = wrapText(label, fonts.bold, TYPE.lead, room);
255
+ // The first line of the label shares the amount's baseline.
256
+ lines.forEach((line, i) =>
257
+ fonts.bold.draw(l.page, line, {
258
+ x: margin,
259
+ y: l.y - size - i * TYPE.lead * 1.35,
260
+ size: TYPE.lead,
261
+ color: INK
262
+ })
263
+ );
264
+ l.right(amount, A4[0] - margin, size, fonts.bold, INK);
265
+ l.y -= size + (lines.length - 1) * TYPE.lead * 1.35 + 8;
266
+ l.strong();
267
+ l.y -= 10;
268
+ },
269
+
270
+ /**
271
+ * The line to sign, with room above it for a hand.
272
+ *
273
+ * @param {string} caption
274
+ */
275
+ signature(caption) {
276
+ l.ensure(70);
277
+ l.gap(40);
278
+ l.page.drawLine({
279
+ start: { x: margin, y: l.y },
280
+ end: { x: margin + 230, y: l.y },
281
+ thickness: 0.5,
282
+ color: INK
283
+ });
284
+ l.y -= 4;
285
+ l.label(caption);
286
+ },
287
+
288
+ /**
289
+ * The foot of a page: a hairline, the document and page on the left, where
290
+ * the data is from on the right.
291
+ *
292
+ * @param {import('pdf-lib').PDFPage} page
293
+ * @param {string} left
294
+ * @param {string} [right]
295
+ */
296
+ foot(page, left, right = '') {
297
+ page.drawLine({
298
+ start: { x: margin, y: margin - 12 },
299
+ end: { x: A4[0] - margin, y: margin - 12 },
300
+ thickness: STROKE.hair,
301
+ color: RULE
302
+ });
303
+ const size = TYPE.label;
304
+ regular.draw(page, pdfText(left), { x: margin, y: margin / 2, size, color: GREY });
305
+ const r = pdfText(right);
306
+ if (r)
307
+ regular.draw(page, r, {
308
+ x: A4[0] - margin - regular.width(r, size),
309
+ y: margin / 2,
310
+ size,
311
+ color: GREY
312
+ });
313
+ },
314
+
173
315
  /**
174
316
  * A label and its value side by side.
175
317
  *
@@ -179,11 +321,13 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
179
321
  */
180
322
  field(label, value, o = {}) {
181
323
  const lw = o.labelWidth ?? 92;
182
- const size = o.size ?? 8.5;
324
+ const size = o.size ?? TYPE.body;
183
325
  const lines = wrapText(value || '—', o.face ?? regular, size, (o.max ?? l.width) - lw);
184
326
  l.ensure(Math.min(lines.length, 3) * size * 1.35);
185
327
  const top = l.y;
186
- l.text(label, { face: fonts.bold, size: 8, color: GREY, max: lw - 6 });
328
+ // The label on the value's baseline, though it is smaller.
329
+ l.y = top - (size - TYPE.small) * 0.9;
330
+ l.text(label, { size: TYPE.small, color: GREY, max: lw - 6 });
187
331
  const after = l.y;
188
332
  const page = l.page;
189
333
  l.y = top;
@@ -197,26 +341,23 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
197
341
  l.y = l.page === page ? Math.min(l.y, after) : l.y;
198
342
  },
199
343
 
200
- /** A note in a tinted box. @param {string} t @param {number} [size] */
201
- box(t, size = 9) {
202
- const lines = wrapText(t, regular, size, l.width - 16);
203
- const h = lines.length * size * 1.35 + 14;
344
+ /**
345
+ * A note: what the document is, said once. A paragraph with an ink bar at
346
+ * its left edge; no box, no fill.
347
+ *
348
+ * @param {string} t
349
+ * @param {number} [size]
350
+ */
351
+ note(t, size = TYPE.body) {
352
+ const lead = size * 1.4;
353
+ const lines = wrapText(t, regular, size, l.width - 12);
354
+ const h = lines.length * lead;
204
355
  l.ensure(h);
205
- l.page.drawRectangle({
206
- x: margin,
207
- y: l.y - h,
208
- width: l.width,
209
- height: h,
210
- color: TINT,
211
- borderColor: RULE,
212
- borderWidth: 0.6
213
- });
214
- l.y -= 7;
356
+ l.page.drawRectangle({ x: margin, y: l.y - h + 1, width: 1.5, height: h - 1, color: INK });
215
357
  for (const line of lines) {
216
- regular.draw(l.page, line, { x: margin + 8, y: l.y - size, size });
217
- l.y -= size * 1.35;
358
+ regular.draw(l.page, line, { x: margin + 12, y: l.y - size, size, color: INK });
359
+ l.y -= lead;
218
360
  }
219
- l.y -= 7;
220
361
  },
221
362
 
222
363
  /** A QR code of `value`, `side` points wide, its top left at (x, top). @param {string} value @param {number} x @param {number} top @param {number} side */
@@ -241,6 +382,22 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
241
382
  });
242
383
  },
243
384
 
385
+ /**
386
+ * The head of a table: the columns' titles as labels over a strong rule.
387
+ *
388
+ * @param {Column[]} columns
389
+ */
390
+ tableHead(columns) {
391
+ l.row(
392
+ columns.map((c) => ({ ...c, mono: false })),
393
+ columns.map((c) => c.title),
394
+ { color: GREY, size: TYPE.label, lead: 9 }
395
+ );
396
+ l.y -= 1;
397
+ l.strong();
398
+ l.y -= 3;
399
+ },
400
+
244
401
  /**
245
402
  * One row of a table, its cells wrapped; a new page (and `header` again) where it does not fit.
246
403
  *
@@ -249,7 +406,7 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
249
406
  * @param {{ face?: Face, color?: Color, size?: number, lead?: number, header?: () => void }} [o]
250
407
  */
251
408
  row(columns, cells, o = {}) {
252
- const { face = regular, color = BLACK, size = 7.5, lead = 9.5, header } = o;
409
+ const { face = regular, color = BLACK, size = TYPE.small, lead = 10.5, header } = o;
253
410
  const wrapped = columns.map((c, i) =>
254
411
  wrapText(cells[i] ?? '', c.mono ? fonts.mono : face, size, c.width - 6)
255
412
  );