@le-space/eigenbeleg 0.3.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/src/layout.js CHANGED
@@ -1,30 +1,70 @@
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.
11
14
 
12
15
  import { rgb } from 'pdf-lib';
13
16
  import { encode } from 'uqr';
14
- import { pdfText, wrapText } from '@le-space/pdf';
17
+ import { embedPdfImage, pdfImage, pdfText, wrapText } from '@le-space/pdf';
15
18
 
16
19
  /** @typedef {import('@le-space/pdf').Face} Face */
17
20
  /** @typedef {{ regular: Face, bold: Face, mono: Face }} Fonts */
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);
34
+
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 });
57
+
58
+ /**
59
+ * The issuer's own logo for a document: the bytes of a PNG or JPEG file,
60
+ * checked and reduced to an opaque picture (@le-space/pdf's pdfImage, whose
61
+ * ImageError says what is wrong with a file), embedded once. Null without one.
62
+ *
63
+ * @param {import('pdf-lib').PDFDocument} pdf
64
+ * @param {Uint8Array | null | undefined} file
65
+ */
66
+ export const embedIssuerLogo = async (pdf, file) =>
67
+ file ? embedPdfImage(pdf, pdfImage(file)) : null;
28
68
 
29
69
  /**
30
70
  * @typedef {object} Column
@@ -86,7 +126,7 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
86
126
  text(t, o = {}) {
87
127
  const {
88
128
  x = margin,
89
- size = 9.5,
129
+ size = TYPE.body,
90
130
  face = regular,
91
131
  color = BLACK,
92
132
  max = l.width - (x - margin),
@@ -111,27 +151,50 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
111
151
  },
112
152
 
113
153
  /**
114
- * A one-line title and, when given, an image beside it, a little higher
115
- * 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.
116
157
  *
117
158
  * @param {string} t
118
159
  * @param {{ face: Face, size: number }} o
119
160
  * @param {import('pdf-lib').PDFImage | null} [image]
161
+ * @param {number} [mark] the mark's side; the title's size when not given
120
162
  */
121
- title(t, { face, size }, image = null) {
163
+ title(t, { face, size }, image = null, mark = size) {
164
+ const lineTop = l.y;
122
165
  l.text(t, { face, size });
123
166
  if (!image) return;
124
- 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;
125
170
  l.page.drawImage(image, {
126
- x: margin + face.width(t, size) + 10,
127
- y: top - size - 2,
128
- width: size + 2,
129
- 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
130
175
  });
176
+ l.y = Math.min(l.y, bottom - 4);
177
+ },
178
+
179
+ /**
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.
184
+ *
185
+ * @param {import('pdf-lib').PDFImage | null} image
186
+ */
187
+ letterhead(image) {
188
+ if (!image) return;
189
+ const k = Math.min(ISSUER_LOGO_BOX[0] / image.width, ISSUER_LOGO_BOX[1] / image.height);
190
+ const width = image.width * k;
191
+ const height = image.height * k;
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);
131
194
  },
132
195
 
133
- /** @param {number} [thickness] @param {Color} [color] */
134
- 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) {
135
198
  l.page.drawLine({
136
199
  start: { x: margin, y: l.y },
137
200
  end: { x: A4[0] - margin, y: l.y },
@@ -140,6 +203,115 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
140
203
  });
141
204
  },
142
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
+
143
315
  /**
144
316
  * A label and its value side by side.
145
317
  *
@@ -149,11 +321,13 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
149
321
  */
150
322
  field(label, value, o = {}) {
151
323
  const lw = o.labelWidth ?? 92;
152
- const size = o.size ?? 8.5;
324
+ const size = o.size ?? TYPE.body;
153
325
  const lines = wrapText(value || '—', o.face ?? regular, size, (o.max ?? l.width) - lw);
154
326
  l.ensure(Math.min(lines.length, 3) * size * 1.35);
155
327
  const top = l.y;
156
- 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 });
157
331
  const after = l.y;
158
332
  const page = l.page;
159
333
  l.y = top;
@@ -167,26 +341,23 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
167
341
  l.y = l.page === page ? Math.min(l.y, after) : l.y;
168
342
  },
169
343
 
170
- /** A note in a tinted box. @param {string} t @param {number} [size] */
171
- box(t, size = 9) {
172
- const lines = wrapText(t, regular, size, l.width - 16);
173
- 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;
174
355
  l.ensure(h);
175
- l.page.drawRectangle({
176
- x: margin,
177
- y: l.y - h,
178
- width: l.width,
179
- height: h,
180
- color: TINT,
181
- borderColor: RULE,
182
- borderWidth: 0.6
183
- });
184
- l.y -= 7;
356
+ l.page.drawRectangle({ x: margin, y: l.y - h + 1, width: 1.5, height: h - 1, color: INK });
185
357
  for (const line of lines) {
186
- regular.draw(l.page, line, { x: margin + 8, y: l.y - size, size });
187
- 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;
188
360
  }
189
- l.y -= 7;
190
361
  },
191
362
 
192
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 */
@@ -211,6 +382,22 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
211
382
  });
212
383
  },
213
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
+
214
401
  /**
215
402
  * One row of a table, its cells wrapped; a new page (and `header` again) where it does not fit.
216
403
  *
@@ -219,7 +406,7 @@ export function pageLayout(pdf, fonts, { margin = 48, bottom = 14, leavePage, en
219
406
  * @param {{ face?: Face, color?: Color, size?: number, lead?: number, header?: () => void }} [o]
220
407
  */
221
408
  row(columns, cells, o = {}) {
222
- 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;
223
410
  const wrapped = columns.map((c, i) =>
224
411
  wrapText(cells[i] ?? '', c.mono ? fonts.mono : face, size, c.width - 6)
225
412
  );