Skip to content

Commit f2c931a

Browse files
Fix spacing for words and paragraphs
1 parent 9bfb965 commit f2c931a

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

src/formatter/pdf_formatter.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class PdfFormatter extends Formatter {
195195
format(song: Song, configuration: PDFConfiguration = defaultConfiguration): void {
196196
this.startTime = performance.now();
197197
this.song = song;
198+
console.log(song.lines);
198199
this.pdfConfiguration = configuration;
199200
this.doc = this.setupDoc();
200201
this.renderLayout(this.pdfConfiguration.layout.header, 'header');
@@ -338,6 +339,7 @@ class PdfFormatter extends Formatter {
338339

339340
bodyParagraphs.forEach((paragraph) => {
340341
this.formatParagraph(paragraph, columnHeight);
342+
this.y += this.pdfConfiguration.lineHeight;
341343
});
342344
}
343345

@@ -350,7 +352,6 @@ class PdfFormatter extends Formatter {
350352
this.moveToNextColumn(columnHeight);
351353
}
352354
this.formatLine(line);
353-
this.y += this.pdfConfiguration.lineHeight;
354355
}
355356
});
356357
}
@@ -366,7 +367,10 @@ class PdfFormatter extends Formatter {
366367
const { chords, lyrics } = chordLyricsPair;
367368
const chordWidth = this.getTextDimensions(chords, chordFont).w;
368369
const lyricWidth = this.getTextDimensions(lyrics, lyricsFont).w;
369-
const pairWidth = Math.max(chordWidth, lyricWidth);
370+
371+
const pairWidth = (chordWidth > lyricWidth)
372+
? this.getTextDimensions(`${chords}${this.spaces}`, chordFont).w
373+
: lyricWidth;
370374

371375
return {
372376
item: chordLyricsPair,
@@ -389,17 +393,26 @@ class PdfFormatter extends Formatter {
389393
this.renderLineItems(renderedLine);
390394
}
391395

396+
get spaces() {
397+
let str = '';
398+
399+
for (let i = 0; i < this.pdfConfiguration.numberOfSpacesToAdd; i++) {
400+
str += ' ';
401+
}
402+
403+
return str;
404+
}
405+
392406
renderLineItems(items: MeasuredItem[]) {
393407
const chordFont = this.getFontConfiguration('chord');
394408
const lyricsFont: FontConfiguration = this.getFontConfiguration('text');
395-
const spaceWidth = this.getSpaceWidth();
396409
const maxChordHeight = items.reduce((maxHeight, { chordHeight }) => Math.max(maxHeight, chordHeight || 0), 0);
397410
const [first, ...rest] = items;
398-
const { chordLyricSpacing, numberOfSpacesToAdd } = this.pdfConfiguration;
411+
const { chordLyricSpacing } = this.pdfConfiguration;
399412

400413
if (!first) {
401414
this.carriageReturn();
402-
this.lineFeed(maxChordHeight);
415+
this.lineFeed(Math.max(maxChordHeight, this.pdfConfiguration.lineHeight));
403416
return;
404417
}
405418

@@ -423,7 +436,7 @@ class PdfFormatter extends Formatter {
423436
this.renderText(lyrics, this.x, lyricsY, lyricsFont);
424437
}
425438

426-
this.x += width + (numberOfSpacesToAdd || 0) * spaceWidth;
439+
this.x += width;
427440
} else if (item instanceof Tag) {
428441
this.formatComment((item as Tag).value);
429442
} else if (item instanceof SoftLineBreak) {
@@ -460,8 +473,8 @@ class PdfFormatter extends Formatter {
460473

461474
get maxX() {
462475
const { columnWidth, currentColumn } = this;
463-
const { columnSpacing } = this.pdfConfiguration;
464-
return (currentColumn * columnWidth ) + ((currentColumn - 1) * columnSpacing);
476+
const { columnSpacing, marginleft } = this.pdfConfiguration;
477+
return (currentColumn * columnWidth ) + ((currentColumn - 1) * columnSpacing) + marginleft;
465478
}
466479

467480
renderText(text: string, x: number, y: number, style: FontConfiguration | null = null): void {
@@ -470,23 +483,13 @@ class PdfFormatter extends Formatter {
470483

471484
formatComment(commentText: string): void {
472485
const style = this.getFontConfiguration('comment');
473-
this.setFontStyle(style);
474-
const textY = this.y;
475-
476-
// Print comment text
477-
this.doc.text(commentText, this.x, textY);
478-
479-
// Underline the comment
480-
const textWidth = this.getTextDimensions(commentText).w;
486+
this.withFontConfiguration(style, () => this.doc.text(commentText, this.x, this.y));
487+
const { w: textWidth } = this.getTextDimensions(commentText, style);
481488
this.doc.setDrawColor(0);
482489
this.doc.setLineWidth(0.5);
483-
this.doc.line(this.x, textY + 1, this.x + textWidth, textY + 1);
484-
485-
// Update y for next element
486-
this.y += this.getTextDimensions(commentText).h;
490+
this.doc.line(this.x, this.y + 1, this.x + textWidth, this.y + 1);
487491
}
488492

489-
// Utility functions
490493
spacer(size: number) {
491494
this.y += size;
492495
}

0 commit comments

Comments
 (0)