Skip to content

Commit 1c9a9c6

Browse files
committed
feat: reafactor config without tackling any custom section type overrides
1 parent 2f438b5 commit 1c9a9c6

File tree

4 files changed

+232
-132
lines changed

4 files changed

+232
-132
lines changed

src/formatter/pdf_formatter.ts

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
PDFConfiguration,
3737
PdfConstructor,
3838
PdfDoc,
39+
Margins,
3940
} from './pdf_formatter/types';
4041
import {
4142
NimbusSansLBolBold,
@@ -77,6 +78,8 @@ class PdfFormatter extends Formatter {
7778

7879
fontConfiguration: FontConfiguration = defaultConfiguration.fonts.text;
7980

81+
margins: Margins = defaultConfiguration.layout.global.margins;
82+
8083
// Main function to format and save the song as a PDF
8184
format(
8285
song: Song,
@@ -89,8 +92,9 @@ class PdfFormatter extends Formatter {
8992
this.configuration = configuration;
9093
this.pdfConfiguration = pdfConfiguration;
9194
this.doc = this.setupDoc(docConstructor);
92-
this.y = this.pdfConfiguration.margintop + this.pdfConfiguration.layout.header.height;
93-
this.x = this.pdfConfiguration.marginleft;
95+
this.margins = this.pdfConfiguration.layout.global.margins;
96+
this.y = this.margins.top + this.pdfConfiguration.layout.header.height;
97+
this.x = this.margins.left;
9498
this.currentColumn = 1;
9599
this.formatParagraphs();
96100
this.recordFormattingTime();
@@ -144,17 +148,13 @@ class PdfFormatter extends Formatter {
144148
doc.addFileToVFS('NimbusSanL-BolIta-bolditalic.ttf', NimbusSansLBolItaBoldItalic);
145149
doc.addFont('NimbusSanL-BolIta-bolditalic.ttf', 'NimbusSansL-BolIta', 'bolditalic');
146150

151+
// Calculate column width using new config structure
152+
const { columnCount, columnSpacing } = this.pdfConfiguration.layout.sections.global;
147153
const pageWidth = doc.internal.pageSize.getWidth();
148154

149-
const {
150-
marginleft,
151-
marginright,
152-
columnCount,
153-
columnSpacing,
154-
} = this.pdfConfiguration;
155+
this.columnWidth = (pageWidth - this.margins.left - this.margins.right -
156+
(columnCount - 1) * columnSpacing) / columnCount;
155157

156-
this.columnWidth =
157-
(pageWidth - marginleft - marginright - (columnCount - 1) * columnSpacing) / columnCount;
158158
return doc;
159159
}
160160

@@ -197,9 +197,8 @@ class PdfFormatter extends Formatter {
197197
// Renders the layout for header and footer
198198
private renderLayout(layoutConfig: LayoutItem, section: LayoutSection) {
199199
const { height } = layoutConfig;
200-
const { margintop, marginbottom } = this.pdfConfiguration;
201200
const pageHeight = this.doc.internal.pageSize.getHeight();
202-
const sectionY = section === 'header' ? margintop : pageHeight - height - marginbottom;
201+
const sectionY = section === 'header' ? this.margins.top : pageHeight - height - this.margins.bottom;
203202

204203
layoutConfig.content.forEach((contentItem) => {
205204
const item = contentItem as LayoutContentItem;
@@ -308,7 +307,7 @@ class PdfFormatter extends Formatter {
308307
this.setFontStyle(style);
309308
const pageWidth = this.doc.internal.pageSize.getWidth();
310309
const availableWidth = position.width ||
311-
(pageWidth - this.pdfConfiguration.marginleft - this.pdfConfiguration.marginright);
310+
(pageWidth - this.margins.left - this.margins.right);
312311
let y = sectionY + position.y;
313312

314313
if (position.clip) {
@@ -381,11 +380,11 @@ class PdfFormatter extends Formatter {
381380
this.doc.setLineDash([]);
382381
}
383382

384-
const x = this.pdfConfiguration.marginleft + (position.x || 0);
383+
const x = this.margins.left + (position.x || 0);
385384
const y = sectionY + position.y;
386385

387386
const pageWidth = this.doc.internal.pageSize.getWidth();
388-
const availableWidth = pageWidth - this.pdfConfiguration.marginleft - this.pdfConfiguration.marginright;
387+
const availableWidth = pageWidth - this.margins.left - this.margins.right;
389388
const lineWidth = position.width === 'auto' ? availableWidth : position.width;
390389

391390
this.doc.line(x, y, x + lineWidth, y + (position.height || 0));
@@ -450,13 +449,13 @@ class PdfFormatter extends Formatter {
450449
case 'center':
451450
return this.doc.internal.pageSize.getWidth() / 2 - width / 2;
452451
case 'right':
453-
return this.doc.internal.pageSize.getWidth() - this.pdfConfiguration.marginright - width;
452+
return this.doc.internal.pageSize.getWidth() - this.margins.right - width;
454453
case 'left':
455454
default:
456455
if (typeof alignment === 'number') {
457-
return this.pdfConfiguration.marginleft + alignment;
456+
return this.margins.left + alignment;
458457
}
459-
return this.pdfConfiguration.marginleft;
458+
return this.margins.left;
460459
}
461460
}
462461

@@ -505,7 +504,7 @@ class PdfFormatter extends Formatter {
505504
if (
506505
paragraphSummary.countNonLyricLines === 1 &&
507506
paragraphSummary.countChordLyricPairLines === 0 &&
508-
this.pdfConfiguration.lyricsOnly
507+
this.pdfConfiguration.layout.sections.base.display?.lyricsOnly
509508
) {
510509
return;
511510
}
@@ -514,7 +513,7 @@ class PdfFormatter extends Formatter {
514513
this.renderLines(lines);
515514
});
516515

517-
this.y += this.pdfConfiguration.paragraphSpacing || 0;
516+
this.y += this.pdfConfiguration.layout.sections.global.paragraphSpacing || 0;
518517
}
519518

520519
private measureAndComputeLineLayouts(line: Line): LineLayout[] {
@@ -525,7 +524,7 @@ class PdfFormatter extends Formatter {
525524
// Find the next item with lyrics after the current index and get its index
526525
let nextItemWithLyrics: ChordLyricsPair | null = null;
527526
let nextItemWithLyricsIndex: number | null = null;
528-
if (this.pdfConfiguration.lyricsOnly && index === 0) {
527+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly && index === 0) {
529528
for (let i = index + 1; i < line.items.length; i += 1) {
530529
if (isChordLyricsPair(line.items[i]) && (line.items[i] as ChordLyricsPair).lyrics?.trim() !== '') {
531530
nextItemWithLyrics = line.items[i] as ChordLyricsPair;
@@ -544,7 +543,8 @@ class PdfFormatter extends Formatter {
544543
}
545544
}
546545
}
547-
if (this.pdfConfiguration.lyricsOnly && index === 0 && (item as ChordLyricsPair).lyrics?.trim() === '') {
546+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly &&
547+
index === 0 && (item as ChordLyricsPair).lyrics?.trim() === '') {
548548
const chordLyricsPairItem = item as ChordLyricsPair;
549549
chordLyricsPairItem.lyrics = '';
550550
}
@@ -816,7 +816,7 @@ class PdfFormatter extends Formatter {
816816
type = 'Empty';
817817
}
818818

819-
if (this.pdfConfiguration.lyricsOnly && type === 'ChordLyricsPair') {
819+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly && type === 'ChordLyricsPair') {
820820
const indexOfFirstItemContainingLyrics = items.findIndex(
821821
({ item }) => (
822822
item instanceof ChordLyricsPair &&
@@ -864,7 +864,7 @@ class PdfFormatter extends Formatter {
864864
({ item }) => item instanceof ChordLyricsPair && item.lyrics && item.lyrics.trim() !== '',
865865
);
866866

867-
const { chordLyricSpacing } = this.pdfConfiguration;
867+
const { chordLyricSpacing } = this.pdfConfiguration.layout.sections.global;
868868
let chordsYOffset = yOffset;
869869
let lyricsYOffset = yOffset;
870870

@@ -920,7 +920,7 @@ class PdfFormatter extends Formatter {
920920
);
921921

922922
// Render chords only if `lyricsOnly` is false
923-
if (!this.pdfConfiguration.lyricsOnly && chords) {
923+
if (!this.pdfConfiguration.layout.sections.base.display?.lyricsOnly && chords) {
924924
const chordDimensions = this.getTextDimensions(chords, chordFont);
925925
const chordBaseline = chordsYOffset + this.maxChordHeight(items) - chordDimensions.h;
926926
this.renderText(chords, x, chordBaseline, chordFont);
@@ -966,7 +966,7 @@ class PdfFormatter extends Formatter {
966966
const { lineLayouts, totalHeight, countChordLyricPairLines } = paragraphSummary;
967967
let newLineLayouts: LineLayout[][] = [];
968968
const cumulativeHeight = this.y;
969-
const columnStartY = this.pdfConfiguration.margintop + this.pdfConfiguration.layout.header.height;
969+
const columnStartY = this.margins.top + this.pdfConfiguration.layout.header.height;
970970
const columnBottomY = this.getColumnBottomY();
971971

972972
// Check if the entire paragraph fits in the current column
@@ -1017,7 +1017,7 @@ class PdfFormatter extends Formatter {
10171017
cumulativeHeight: number,
10181018
): LineLayout[][] {
10191019
let newLineLayouts: LineLayout[][] = [];
1020-
const columnStartY = this.pdfConfiguration.margintop + this.pdfConfiguration.layout.header.height;
1020+
const columnStartY = this.margins.bottom + this.pdfConfiguration.layout.header.height;
10211021
const columnBottomY = this.getColumnBottomY();
10221022

10231023
let chordLyricPairLinesSeen = 0;
@@ -1068,7 +1068,7 @@ class PdfFormatter extends Formatter {
10681068
): LineLayout[][] {
10691069
let newLineLayouts: LineLayout[][] = [];
10701070
const columnStartY =
1071-
this.pdfConfiguration.margintop + this.pdfConfiguration.layout.header.height;
1071+
this.margins.top + this.pdfConfiguration.layout.header.height;
10721072
const columnBottomY = this.getColumnBottomY();
10731073

10741074
// Flatten lineLayouts into a flat array of LineLayout
@@ -1192,7 +1192,7 @@ class PdfFormatter extends Formatter {
11921192
// Estimate the line height
11931193
private estimateLineHeight(items: MeasuredItem[]): number {
11941194
const maxChordHeight = this.maxChordHeight(items);
1195-
const { chordLyricSpacing, linePadding } = this.pdfConfiguration;
1195+
const { chordLyricSpacing, linePadding } = this.pdfConfiguration.layout.sections.global;
11961196

11971197
const hasChords = items.some(({ item }) => item instanceof ChordLyricsPair && item.chords);
11981198
const hasLyrics = items.some(
@@ -1244,7 +1244,7 @@ class PdfFormatter extends Formatter {
12441244

12451245
const {
12461246
columnCount,
1247-
} = this.pdfConfiguration;
1247+
} = this.pdfConfiguration.layout.sections.global;
12481248

12491249
if (this.currentColumn > columnCount) {
12501250
this.doc.addPage();
@@ -1254,15 +1254,15 @@ class PdfFormatter extends Formatter {
12541254
}
12551255

12561256
this.carriageReturn();
1257-
this.y = this.pdfConfiguration.margintop + this.pdfConfiguration.layout.header.height;
1257+
this.y = this.margins.top + this.pdfConfiguration.layout.header.height;
12581258
}
12591259

12601260
// Get the bottom Y coordinate of the column
12611261
private getColumnBottomY(): number {
12621262
const pageHeight = this.doc.internal.pageSize.getHeight();
1263-
const { marginbottom, layout } = this.pdfConfiguration;
1263+
const { layout } = this.pdfConfiguration;
12641264
const footerHeight = layout.footer.height;
1265-
return pageHeight - marginbottom - footerHeight;
1265+
return pageHeight - this.margins.bottom - footerHeight;
12661266
}
12671267

12681268
// Helper methods for layout calculations
@@ -1271,8 +1271,8 @@ class PdfFormatter extends Formatter {
12711271
}
12721272

12731273
private columnStartX(): number {
1274-
const { columnSpacing, marginleft } = this.pdfConfiguration;
1275-
return marginleft + (this.currentColumn - 1) * (this.columnWidth + columnSpacing);
1274+
const { columnSpacing } = this.pdfConfiguration.layout.sections.global;
1275+
return this.margins.left + (this.currentColumn - 1) * (this.columnWidth + columnSpacing);
12761276
}
12771277

12781278
private getSpaceWidth(): number {
@@ -1331,15 +1331,15 @@ class PdfFormatter extends Formatter {
13311331
}
13321332

13331333
// Check if the next item has a hyphen
1334-
if (this.pdfConfiguration.lyricsOnly) {
1334+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly) {
13351335
if (nextLyrics.startsWith(' -') || nextLyrics.startsWith('-')) {
13361336
lyrics = lyrics.trimEnd();
13371337
// eslint-disable-next-line no-param-reassign
13381338
nextItem.lyrics = this.removeHyphens(nextLyrics);
13391339
}
13401340
}
13411341
}
1342-
if (this.pdfConfiguration.lyricsOnly) {
1342+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly) {
13431343
// clean next lyrics and this lyrics
13441344
// eslint-disable-next-line no-param-reassign
13451345
item.lyrics = this.removeHyphens(lyrics);
@@ -1401,7 +1401,7 @@ class PdfFormatter extends Formatter {
14011401
const chordWidth = chords ? this.getTextDimensions(chords, chordFont).w : 0;
14021402
const lyricsWidth = lyrics ? this.getTextDimensions(lyrics, lyricsFont).w : 0;
14031403

1404-
if (this.pdfConfiguration.lyricsOnly) {
1404+
if (this.pdfConfiguration.layout.sections.base.display?.lyricsOnly) {
14051405
if (lyrics === '') {
14061406
return [
14071407
{
@@ -1510,7 +1510,7 @@ class PdfFormatter extends Formatter {
15101510
// Get chord spacing
15111511
private get chordSpacingAsSpaces(): string {
15121512
let str = '';
1513-
for (let i = 0; i < this.pdfConfiguration.chordSpacing; i += 1) {
1513+
for (let i = 0; i < this.pdfConfiguration.layout.sections.global.chordSpacing; i += 1) {
15141514
str += ' ';
15151515
}
15161516
return str;
@@ -1527,8 +1527,8 @@ class PdfFormatter extends Formatter {
15271527

15281528
const pageWidth = this.doc.internal.pageSize.getWidth();
15291529
const timeTextWidth = this.getTextDimensions(`${timeTaken}s`).w;
1530-
const timeTextX = pageWidth - timeTextWidth - this.pdfConfiguration.marginright;
1531-
const timeTextY = this.pdfConfiguration.margintop / 2;
1530+
const timeTextX = pageWidth - timeTextWidth - this.margins.right;
1531+
const timeTextY = this.margins.top / 2;
15321532

15331533
this.doc.text(`${timeTaken}s`, timeTextX, timeTextY);
15341534
}

src/formatter/pdf_formatter/default_configuration.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@ const defaultConfiguration: PDFConfiguration = {
2828
name: 'NimbusSansL-Reg', style: 'normal', size: 10, color: 'black',
2929
},
3030
},
31-
// Layout settings
32-
margintop: 35,
33-
marginbottom: 10,
34-
marginleft: 45,
35-
marginright: 45,
36-
paragraphSpacing: 10,
37-
linePadding: 4,
38-
chordLyricSpacing: 2,
39-
chordSpacing: 2,
40-
columnCount: 2,
41-
columnWidth: 0,
42-
columnSpacing: 25,
43-
lyricsOnly: false,
4431
layout: {
32+
global: {
33+
margins: {
34+
top: 35,
35+
bottom: 10,
36+
left: 45,
37+
right: 45,
38+
},
39+
},
4540
header: {
4641
height: 60,
4742
content: [
@@ -84,6 +79,22 @@ const defaultConfiguration: PDFConfiguration = {
8479
},
8580
],
8681
},
82+
sections: {
83+
global: {
84+
paragraphSpacing: 10,
85+
linePadding: 4,
86+
chordLyricSpacing: 2,
87+
chordSpacing: 2,
88+
columnCount: 2,
89+
columnWidth: 0,
90+
columnSpacing: 25,
91+
},
92+
base: {
93+
display: {
94+
lyricsOnly: false,
95+
},
96+
},
97+
},
8798
},
8899
};
89100

0 commit comments

Comments
 (0)