-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
766 lines (714 loc) · 24.5 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
#!/usr/bin/env node
import {CMYK, cmyk, PageSizes, PDFDocument, PDFFont, PDFPage} from 'pdf-lib'
import fontkit from '@pdf-lib/fontkit'
import * as fs from "fs";
interface Options {
topTitle: string
title: string
titleEN: string
outerPadding: number
itemHeight: number
columnPadding: number
listRowPadding: number
columns: number
dashedLineWidth: number
circleRadius: number
circleWidth: number
numberSize: number
nameSize: number
subjectSize: number
}
const defaultOptions = {
topTitle: "SP-Stimmzettel / SP Ballot",
title: "Wahl zum 44. Studierendenparlament der RFWU Bonn — 20. Januar 2022",
titleEN: "Election of the 44th Student Parliament of the University of Bonn — 20th January 2022",
outerPadding: 5,
itemHeight: 6.5,
columnPadding: 3,
listRowPadding: 8,
columns: 5,
dashedLineWidth: 2,
circleRadius: 2,
circleWidth: 0.25,
numberSize: 8,
nameSize: 7,
subjectSize: 6,
}
class GlobalConfig {
options: Options
pageSize: [number, number]
outerPadding: number
headerHeight: number
itemHeight: number
columnPadding: number
listRowPadding: number
columns: number
regularFont: PDFFont
italicFont: PDFFont
boldFont: PDFFont
dottedLineWidth: number
dashedLineWidth: number
azb: boolean
constructor(regularFont: PDFFont, italicFont: PDFFont, boldFont: PDFFont, options: Options, azb: boolean) {
this.options = options
this.pageSize = PageSizes.A3
this.outerPadding = mm(options.outerPadding)
this.headerHeight = mm(18)
this.itemHeight = mm(options.itemHeight)
this.columnPadding = mm(options.columnPadding)
this.listRowPadding = mm(options.listRowPadding)
this.columns = options.columns
this.regularFont = regularFont
this.italicFont = italicFont
this.boldFont = boldFont
this.dottedLineWidth = 0.25
this.dashedLineWidth = mm(options.dashedLineWidth)
this.azb = azb;
if(this.azb){
this.headerHeight = mm(60);
this.dashedLineWidth = 1;
}
}
get listSpaceHeight() {
return this.pageSize[1] - 2 * this.outerPadding - this.headerHeight
}
}
class ItemConfig {
globalConfig: GlobalConfig
height: number
width: number
circleSpaceWidth: number
circleRadius: number
circleWidth: number
numberSpaceWidth: number
numberSize: number
numberFont: PDFFont
nameSize: number
nameY: number
nameFont: PDFFont
subjectSize: number
subjectY: number
subjectFont: PDFFont
get nameSpaceWidth(): number {
return this.width - this.circleSpaceWidth - this.numberSpaceWidth
}
constructor(globalConfig: GlobalConfig) {
this.globalConfig = globalConfig
this.height = globalConfig.itemHeight
this.width = (globalConfig.pageSize[0] - (2 * this.globalConfig.outerPadding) - ((this.globalConfig.columns - 1) * this.globalConfig.columnPadding)) / this.globalConfig.columns
this.circleRadius = mm(globalConfig.options.circleRadius)
this.circleWidth = mm(globalConfig.options.circleWidth)
this.circleSpaceWidth = this.circleRadius * 2 + mm(0.25)
this.numberSpaceWidth = mm(4)
this.numberSize = globalConfig.options.numberSize
this.numberFont = globalConfig.regularFont
this.nameSize = globalConfig.options.nameSize
this.nameY = 3.5 / 6.5 * this.height
this.nameFont = globalConfig.regularFont
this.subjectSize = globalConfig.options.subjectSize
this.subjectY = 1 / 6.5 * this.height
this.subjectFont = globalConfig.italicFont
if(globalConfig.azb){
this.circleSpaceWidth = mm(15);
}
}
}
class Data {
lists: List[]
constructor(lists) {
this.lists = lists
}
static fromJson(obj: any) {
return new Data(obj.map(l => List.fromJson(l)))
}
getListsHeight(c: GlobalConfig){
let sum = 0
for (let list of this.lists) {
sum += list.getHeight(c)
}
return sum
}
}
class Person {
number: string
name: string
subjects: string
constructor(number, name, subjects) {
this.number = number
this.name = name
this.subjects = subjects
}
static fromJson(obj: any): Person {
return new Person(obj.number, obj.name, obj.subjects)
}
}
class List {
listname: string[]
people: Person[]
constructor(listname, people) {
this.listname = listname
this.people = people
}
static fromJson(obj: any): List {
return new List(obj.listname, obj.people.map(p => Person.fromJson(p)))
}
getRowsPerColumn(c: GlobalConfig) {
return Math.ceil((this.people.length + 1) / c.columns)
}
getHeight(c: GlobalConfig) {
let rowsPerColumn = this.getRowsPerColumn(c);
if (c.azb && (((this.people.length + 1) % c.columns) == 0)) {
rowsPerColumn++;
}
return c.itemHeight * rowsPerColumn
}
}
const mm = (value: number): number => {
return value * 72 / 25.4
}
const drawPerson = (page: PDFPage, person: Person, x: number, y: number, c: ItemConfig, topLine: boolean = false, debug: boolean = true) => {
if (debug) {
page.drawRectangle({
x: x,
y: y,
width: c.width,
height: c.height,
color: cmyk(0, 1, 0, 0),
opacity: 0.5,
})
}
let start_x = x + (c.circleWidth / 2)
if (debug) {
page.drawRectangle({
x: x,
y: y,
width: c.circleSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.3, 0),
opacity: 0.5,
})
}
if(c.globalConfig.azb){
page.drawRectangle({
x: start_x,
y: y,
width: c.circleSpaceWidth,
height: c.height,
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 1,
});
} else {
page.drawCircle({
x: start_x + c.circleRadius,
y: y + (c.height / 2),
size: c.circleRadius,
borderColor: cmyk(0, 0, 0, 1),
borderWidth: c.circleWidth,
})
}
start_x = x + c.circleSpaceWidth
const referenceHeight = c.numberFont.heightAtSize(c.numberSize, {descender: false})
if (debug) {
page.drawRectangle({
x: start_x,
y: y,
width: c.numberSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.6, 0),
opacity: 0.5,
})
}
drawCentredText(page, person.number, start_x, y + (c.height - referenceHeight) / 2, c.numberSpaceWidth, c.numberSize, c.numberFont, debug)
start_x += c.numberSpaceWidth
if (debug) {
page.drawRectangle({
x: start_x,
y: y,
width: c.nameSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.9, 0),
opacity: 0.5,
})
}
const textWidth = c.nameFont.widthOfTextAtSize(person.name, c.nameSize)
const textHeight = c.nameFont.heightAtSize(c.nameSize, {descender: false})
let nameScale = 1
if (textWidth > c.nameSpaceWidth) {
nameScale = c.nameSpaceWidth / textWidth
if (debug) {
page.drawRectangle({
x: start_x,
y: y + c.nameY,
width: textWidth,
height: textHeight,
color: cmyk(0, 1, 1, 0),
opacity: 0.5,
})
}
}
page.drawText(person.name, {
x: start_x,
y: y + c.nameY,
size: c.nameSize,
font: c.nameFont,
color: cmyk(0, 0, 0, 1),
matrix: [nameScale, 0, 0, 1, 0, 0],
})
const subjectsWidth = c.subjectFont.widthOfTextAtSize(person.subjects, c.subjectSize)
const subjectsHeight = c.subjectFont.heightAtSize(c.subjectSize, {descender: false})
let subjectsScale = 1
if (subjectsWidth > c.nameSpaceWidth) {
subjectsScale = c.nameSpaceWidth / subjectsWidth
if (debug) {
page.drawRectangle({
x: start_x,
y: y + c.subjectY,
width: subjectsWidth,
height: subjectsHeight,
color: cmyk(0, 1, 1, 0),
opacity: 0.5,
})
}
}
page.drawText(person.subjects, {
x: start_x,
y: y + c.subjectY,
size: c.subjectSize,
font: c.subjectFont,
color: cmyk(0, 0, 0, 0.7),
matrix: [subjectsScale, 0, 0, 1, 0, 0],
})
drawSmallDottedLine(page, x, y, c.width, c.globalConfig)
if (topLine) {
drawSmallDottedLine(page, x, y + c.height, c.width, c.globalConfig)
}
}
const drawListField = (page: PDFPage, list: List, x: number, y: number, c: ItemConfig, debug: boolean = true) => {
if (debug) {
page.drawRectangle({
x: x,
y: y,
width: c.width,
height: c.height,
color: cmyk(0, 1, 0, 0),
opacity: 0.5,
})
}
let start_x = x + (c.circleWidth / 2)
if (debug) {
page.drawRectangle({
x: x,
y: y,
width: c.circleSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.3, 0),
opacity: 0.5,
})
}
if(c.globalConfig.azb){
page.drawRectangle({
x: start_x,
y: y,
width: c.circleSpaceWidth,
height: c.height,
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 1,
});
start_x = x + c.circleSpaceWidth;
const referenceHeight = c.numberFont.heightAtSize(c.numberSize, { descender: false });
if (debug) {
page.drawRectangle({
x: start_x,
y: y,
width: c.numberSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.6, 0),
opacity: 0.5,
});
}
drawCentredText(page, 'L', start_x, y + (c.height - referenceHeight) / 2, c.numberSpaceWidth, c.numberSize, c.numberFont, debug);
start_x += c.numberSpaceWidth;
} else {
page.drawCircle({
x: start_x + c.circleRadius,
y: y + (c.height / 2),
size: c.circleRadius,
borderColor: cmyk(0, 0, 0, 1),
borderWidth: c.circleWidth,
})
start_x = x + c.circleSpaceWidth + c.numberSpaceWidth
}
if (debug) {
page.drawRectangle({
x: start_x,
y: y,
width: c.nameSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0.9, 0),
opacity: 0.5,
})
}
page.drawRectangle({
x: start_x,
y: y,
width: c.nameSpaceWidth,
height: c.height,
color: cmyk(0, 0, 0, 0.3),
opacity: 1,
})
const height = c.nameFont.heightAtSize(c.nameSize, {descender: false})
if (list.listname.length == 1) {
drawCentredText(page, list.listname[0], start_x, y + (c.height - height) / 2, c.nameSpaceWidth, c.nameSize, c.nameFont, debug)
} else {
let topLineY = y + (c.height - 2 * height) / 5 * 3 + height;
let bottomLineY = y + (c.height - height) / 5;
drawCentredText(page, list.listname[0], start_x, topLineY, c.nameSpaceWidth, c.nameSize, c.nameFont, debug)
drawCentredText(page, list.listname[1], start_x, bottomLineY, c.nameSpaceWidth, c.nameSize, c.nameFont, debug)
}
drawSmallDottedLine(page, x, y, c.width, c.globalConfig)
drawSmallDottedLine(page, x, y + c.height, c.width, c.globalConfig)
}
const drawCentredText = (page, text: string, x: number, y: number, width: number, size: number, font: PDFFont, debug: boolean = true, color: CMYK = cmyk(0, 0, 0, 1)) => {
const textWidth = font.widthOfTextAtSize(text, size)
let start_x = x + (width - textWidth) / 2
if (debug) {
const textHeight = font.heightAtSize(size, {descender: false})
page.drawRectangle({
x: start_x,
y: y,
width: textWidth,
height: textHeight,
color: cmyk(1, 1, 0, 0),
opacity: 0.2,
})
}
let nameScale = 1
if (textWidth > width) {
nameScale = width / textWidth
start_x = x
if (debug) {
const textHeight = font.heightAtSize(size, {descender: false})
page.drawRectangle({
x: start_x,
y: y,
width: textWidth,
height: textHeight,
color: cmyk(0, 1, 1, 0),
opacity: 0.5,
})
}
}
page.drawText(text, {
x: start_x,
y: y,
size: size,
font: font,
color: color,
matrix: [nameScale, 0, 0, 1, 0, 0],
})
}
const drawSmallDottedLine = (page: PDFPage, x: number, y: number, width: number, c: GlobalConfig) => {
page.drawLine({
start: {x: x, y: y},
end: {x: x + width, y: y},
thickness: c.dottedLineWidth,
color: cmyk(0, 0, 0, 0.5),
dashArray: [0, 0.5],
dashPhase: c.dottedLineWidth / 4,
lineCap: 1,
})
}
const drawList = (page: PDFPage, list: List, y: number, c: GlobalConfig, debug: boolean = true) => {
const listItemConfig = new ItemConfig(c);
listItemConfig.nameFont = c.boldFont
drawListField(page, list, c.outerPadding, y, listItemConfig, debug)
let index = 1
const rowsPerColumn = list.getRowsPerColumn(c)
for (let person of list.people) {
const column = Math.floor(index / rowsPerColumn)
const row = index % rowsPerColumn
const itemConfig = new ItemConfig(c);
const x = c.outerPadding + column * itemConfig.width + column * c.columnPadding
const pos_y = y - row * c.itemHeight
drawPerson(page, person, x, pos_y, itemConfig, row == 0, debug)
index++;
}
if(c.azb){
let column = Math.floor(index / rowsPerColumn);
let row = index % rowsPerColumn;
if (column > (c.columns - 1)) {
column = c.columns - 1;
row = rowsPerColumn;
}
const itemConfig = new ItemConfig(c);
itemConfig.nameFont = c.boldFont;
const x = c.outerPadding + column * itemConfig.width + column * c.columnPadding;
const pos_y = y - row * c.itemHeight;
const person = new Person('G', 'Gesamtstimmen', list.listname.join(' '));
drawPerson(page, person, x, pos_y, itemConfig, row == 0, debug);
}
}
const drawLists = (page: PDFPage, data: Data, c: GlobalConfig, debug: boolean = true) => {
let current_y = page.getHeight() - c.outerPadding - c.headerHeight - c.dashedLineWidth / 2
drawDashedLine(page, current_y, c)
current_y -= (0.5 * c.listRowPadding + c.itemHeight)
for (let list of data.lists) {
const listtodraw = List.fromJson(list);
drawList(page, listtodraw, current_y, c, debug)
if(c.azb){
current_y = current_y - list.getHeight(c) - c.listRowPadding;
} else {
const rowsPerColumn = list.getRowsPerColumn(c)
current_y = current_y - rowsPerColumn * c.itemHeight - c.listRowPadding
}
const line_y = current_y + c.itemHeight + 0.5 * c.listRowPadding
drawDashedLine(page, line_y, c)
}
}
const drawDashedLine = (page: PDFPage, y: number, c: GlobalConfig) => {
const dashLength = (page.getWidth() - c.outerPadding * 2) / 61
page.drawLine({
start: {x: c.outerPadding, y: y},
end: {x: page.getWidth() - c.outerPadding, y: y},
thickness: c.dashedLineWidth,
color: cmyk(0, 0, 0, 0.2),
dashArray: [dashLength, dashLength],
dashPhase: 0,
})
}
function drawX(page: PDFPage, start_x: number, start_y: number, c: GlobalConfig) {
page.drawLine({
start: {x: start_x, y: start_y + 2},
end: {x: start_x + c.itemHeight - 5, y: start_y + c.itemHeight - 2},
thickness: 2,
color: cmyk(0, 0, 0, 1),
lineCap: 1,
})
page.drawLine({
start: {x: start_x + 1, y: start_y + c.itemHeight - 2},
end: {x: start_x + c.itemHeight - 5, y: start_y + 2},
thickness: 2,
color: cmyk(0, 0, 0, 1),
lineCap: 1,
})
}
const drawAltHeader = (page, c, debug = true) => {
const { width, height } = page.getSize();
let x, y, w;
x = c.outerPadding;
y = height - c.outerPadding - mm(33);
w = mm(55);
page.drawRectangle({
x: x,
y: y,
width: w,
height: mm(33),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
drawCentredText(page, 'Urne Nr.', x, y + mm(1), w, 8, c.regularFont, debug);
x += w + c.columnPadding;
w = mm(112);
page.drawRectangle({
x: x,
y: y,
width: w,
height: mm(33),
borderColor: cmyk(0, 0, 0, .5),
borderWidth: 2,
});
drawCentredText(page, 'Kommentare + anders interpretierte Stimmzettel', x, y + mm(1), w, 8, c.regularFont, debug);
x = c.outerPadding;
y -= mm(18);
w = mm(35);
page.drawRectangle({
x: x,
y: y,
width: w,
height: mm(15),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
drawCentredText(page, 'Nr. auf Laufzettel', x, y + mm(1), w, 8, c.regularFont, debug);
x += w + c.columnPadding;
page.drawRectangle({
x: x,
y: y,
width: w,
height: mm(15),
borderColor: cmyk(0, 0, 0, .5),
borderWidth: 2,
});
drawCentredText(page, 'Team', x, y + mm(1), w, 8, c.regularFont, debug);
x += w + c.columnPadding;
w = mm(94);
page.drawRectangle({
x: x,
y: y,
width: w,
height: mm(15),
borderColor: cmyk(0, 0, 0, .5),
borderWidth: 2,
});
drawCentredText(page, 'Unterschrift Tischleitung', x, y + mm(1), w, 8, c.regularFont, debug);
x += w + c.columnPadding;
w = mm(55);
drawCentredText(page, 'Gültige Stimmen', x, y + mm(3), w, 20, c.regularFont, debug);
page.drawRectangle({
x: x + w,
y: y,
width: w,
height: mm(10),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
y += mm(13);
drawCentredText(page, 'Unültige Stimmen', x, y + mm(3), w, 20, c.regularFont, debug);
page.drawRectangle({
x: x + w,
y: y,
width: w,
height: mm(10),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
y += mm(13);
drawCentredText(page, 'Stimmen Gesamt', x, y + mm(3), w, 20, c.boldFont, debug);
page.drawRectangle({
x: x + w,
y: y,
width: w,
height: mm(10),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
y += mm(13);
drawCentredText(page, 'Vorab ungültig, weil', x, y + mm(6), w, 8, c.regularFont, debug);
drawCentredText(page, 'Briefwahlvorgaben nicht eingehalten', x, y + mm(3), w, 8, c.regularFont, debug);
page.drawRectangle({
x: x + w,
y: y,
width: w,
height: mm(10),
borderColor: cmyk(0, 0, 0, 1),
borderWidth: 2,
});
};
const drawHeader = (page: PDFPage, c: GlobalConfig, debug: boolean = true) => {
const {width, height} = page.getSize()
if (debug) {
page.drawRectangle({
x: c.outerPadding,
y: height - c.outerPadding - c.headerHeight,
width: width - 2 * c.outerPadding,
height: c.headerHeight,
color: cmyk(0, 1, 1, 0),
opacity: 0.3,
})
}
const circleRadius = mm(7)
page.drawCircle({
x: c.outerPadding + circleRadius,
y: height - c.outerPadding - circleRadius,
size: circleRadius,
color: cmyk(0, 0, 0, 0.9),
})
drawCentredText(page, "?", c.outerPadding, height - c.outerPadding - circleRadius * 1.6, 2 * circleRadius, 32, c.boldFont, debug, cmyk(0, 0, 0, 0))
drawTexts(page, [{text: "Du hast ", font: c.regularFont}, {
text: "eine Stimme.",
font: c.boldFont
}], 12, mm(22), height - mm(11))
drawTexts(page, [{text: "You have ", font: c.regularFont}, {
text: "one vote.",
font: c.boldFont
}], 12, mm(22), height - mm(16))
const fakeList = new List(["List Name"], [])
const fakeConfig = new ItemConfig(c)
let sectionWidth = mm(30)
fakeConfig.width = sectionWidth
fakeConfig.nameFont = c.boldFont
let start_x = mm(70)
let start_y = height - c.outerPadding - c.itemHeight
drawListField(page, fakeList, start_x, start_y, fakeConfig, debug)
drawX(page, start_x, start_y, c);
start_y -= mm(4)
drawCentredText(page, "Wähle eine Liste", start_x, start_y, sectionWidth, 9, c.regularFont, debug)
start_y -= mm(4)
drawCentredText(page, "Vote for one list", start_x, start_y, sectionWidth, 9, c.italicFont, debug)
start_x += mm(25)
start_y = height - c.outerPadding - c.itemHeight - mm(1)
drawCentredText(page, "ODER", start_x, start_y, sectionWidth, 9, c.regularFont, debug)
start_y -= mm(4)
drawCentredText(page, "OR", start_x, start_y, sectionWidth, 9, c.italicFont, debug)
const fakePerson = new Person('99', 'Vorname Nachname', 'Studienfach')
const fakeConfig2 = new ItemConfig(c)
fakeConfig2.width = sectionWidth
start_x += mm(25)
start_y = height - c.outerPadding - c.itemHeight
drawPerson(page, fakePerson, start_x, start_y, fakeConfig2, true, debug)
drawX(page, start_x, start_y, c);
start_y -= mm(4)
drawCentredText(page, "Wähle eine Person", start_x, start_y, sectionWidth, 9, c.regularFont, debug)
start_y -= mm(4)
drawCentredText(page, "Vote for one person", start_x, start_y, sectionWidth, 9, c.italicFont, debug)
start_x = width / 2
sectionWidth = width / 2 - c.outerPadding
start_y = height - c.outerPadding - mm(4)
drawCentredText(page, c.options.topTitle, start_x, start_y, sectionWidth, 12, c.boldFont, debug)
start_y -= mm(5)
drawCentredText(page, c.options.title, start_x, start_y, sectionWidth, 12, c.regularFont, debug)
start_y -= mm(4)
drawCentredText(page, c.options.titleEN, start_x, start_y, sectionWidth, 10, c.italicFont, debug)
}
interface TextCfg {
text: string
font: PDFFont
}
const drawTexts = (page: PDFPage, texts: TextCfg[], size: number, x: number, y: number) => {
let start_x = x
for (let text of texts) {
const width = text.font.widthOfTextAtSize(text.text, size)
page.drawText(text.text, {
x: start_x,
y: y,
size: size,
font: text.font,
color: cmyk(0, 0, 0, 1),
})
start_x += width
}
}
const main = async (inputfile: string, outputfile: string, debug: boolean, azb: boolean) => {
const pdfDoc = await PDFDocument.create()
pdfDoc.registerFontkit(fontkit)
const italicttf = fs.readFileSync('fonts/RobotoCondensed-Italic.ttf')
const regularttf = fs.readFileSync('fonts/RobotoCondensed-Regular.ttf')
const boldttf = fs.readFileSync('fonts/RobotoCondensed-Bold.ttf')
const italicFont = await pdfDoc.embedFont(italicttf)
const regularFont = await pdfDoc.embedFont(regularttf)
const boldFont = await pdfDoc.embedFont(boldttf)
const globalConfig = new GlobalConfig(regularFont, italicFont, boldFont, defaultOptions, azb)
const page = pdfDoc.addPage(globalConfig.pageSize)
const rawData = JSON.parse(fs.readFileSync(inputfile).toString())
const data = Data.fromJson(rawData)
const rowPaddingHeight = globalConfig.listSpaceHeight - data.getListsHeight(globalConfig) - globalConfig.dashedLineWidth
globalConfig.listRowPadding = rowPaddingHeight / data.lists.length
if (debug) {
page.drawRectangle({
x: globalConfig.outerPadding,
y: globalConfig.outerPadding,
width: page.getWidth() - 2 * globalConfig.outerPadding,
height: page.getHeight() - 2 * globalConfig.outerPadding,
color: cmyk(1, 0, 0, 0),
opacity: 0.1,
})
}
drawHeader(page, globalConfig, debug)
drawLists(page, data, globalConfig, debug)
const pdfBytes = await pdfDoc.save()
fs.writeFileSync(outputfile, pdfBytes)
}
await main('2022.json', 'stimmzettel.pdf', false, false)
await main('2022.json', 'stimmzettel-debug.pdf', true, false)
await main('2022.json', 'auszaehlbogen.pdf', false, true)
await main('2022.json', 'auszaehlbogen-debug.pdf', true, true)