Skip to content

Commit 124ea43

Browse files
wip attempt to split lines by commas
1 parent ce8a562 commit 124ea43

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

src/formatter/pdf_formatter.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type LayoutItem = {
6767
};
6868

6969
type MeasuredItem = {
70-
item: ChordLyricsPair | Comment | SoftLineBreak,
70+
item: ChordLyricsPair | Comment | SoftLineBreak | Tag | Item,
7171
width: number,
7272
chordHeight?: number,
7373
};
@@ -356,43 +356,73 @@ class PdfFormatter extends Formatter {
356356
});
357357
}
358358

359+
splitString(str: string, regex: RegExp): string[] {
360+
const position = str.search(regex);
361+
362+
if (position === -1) {
363+
return [str];
364+
}
365+
366+
const first = str.substring(0, position);
367+
const rest = str.substring(position + 1);
368+
369+
return [first, ...this.splitString(rest, regex)];
370+
}
371+
359372
formatLine(line: Line) {
360373
const chordFont = this.getFontConfiguration('chord');
361374
const lyricsFont: FontConfiguration = this.getFontConfiguration('text');
362375
const commentFont: FontConfiguration = this.getFontConfiguration('comment');
363376

364-
const renderedLine = line.items.map((item) => {
377+
const renderedLine = line.items.flatMap((item): MeasuredItem[] => {
365378
if (isChordLyricsPair(item)) {
366-
const chordLyricsPair = item as ChordLyricsPair;
367-
const { chords, lyrics } = chordLyricsPair;
368-
const chordWidth = this.getTextDimensions(chords, chordFont).w;
369-
const lyricWidth = this.getTextDimensions(lyrics, lyricsFont).w;
370-
371-
const pairWidth = (chordWidth > lyricWidth)
372-
? this.getTextDimensions(`${chords}${this.spaces}`, chordFont).w
373-
: lyricWidth;
374-
375-
return {
376-
item: chordLyricsPair,
377-
width: pairWidth,
378-
chordHeight: this.getTextDimensions(chords, chordFont).h,
379-
};
379+
return this.prepareAndMeasureChordLyricsItem(item as ChordLyricsPair, chordFont, lyricsFont);
380380
} else if (isTag(item) && isComment(item as Tag)) {
381381
const comment = item as Tag;
382382
const commentWidth = this.getTextDimensions(comment.value, commentFont).w;
383383

384-
return {
384+
return [{
385385
item: comment,
386386
width: commentWidth,
387-
};
387+
}];
388388
} else {
389-
return { item, width: 0 };
389+
return [{ item, width: 0 }];
390390
}
391391
});
392392

393393
this.renderLineItems(renderedLine);
394394
}
395395

396+
private prepareAndMeasureChordLyricsItem(item: ChordLyricsPair, chordFont: FontConfiguration, lyricsFont: FontConfiguration): MeasuredItem[] {
397+
const chordLyricsPair = item as ChordLyricsPair;
398+
const { chords, lyrics } = chordLyricsPair;
399+
const lyricParts = this.splitString(lyrics || '', /,\s*/);
400+
401+
return lyricParts.flatMap((itemLyrics, index): MeasuredItem[] => {
402+
const itemChords = (index === 0) ? chords : '';
403+
const { w: chordWidth, h: chordHeight } = this.getTextDimensions(itemChords, chordFont);
404+
const lyricWidth = this.getTextDimensions(itemLyrics, lyricsFont).w;
405+
406+
const pairWidth = (chordWidth > lyricWidth)
407+
? this.getTextDimensions(`${itemChords}${this.spaces}`, chordFont).w
408+
: lyricWidth;
409+
410+
const ret: MeasuredItem[] = [];
411+
412+
if (index === 0) {
413+
ret.push({ item: new SoftLineBreak(), width: 0 });
414+
}
415+
416+
ret.push({
417+
item: new ChordLyricsPair(itemChords, itemLyrics),
418+
width: pairWidth,
419+
chordHeight,
420+
});
421+
422+
return ret;
423+
});
424+
}
425+
396426
get spaces() {
397427
let str = '';
398428

test/formatter/pdf/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/javascript/javascript.min.js"></script>
1111
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.2/mode/json/json.min.js"></script>
1212
<style>
13-
#editors { height: 98vh; width: 49%; float: left;}
14-
#pdfViewer { height: 98vh; width: 49%; float: right; }
13+
#editors { height: 98vh; width: 49%; float: left; display: none; }
14+
#pdfViewer { height: 98vh; width: 100%; float: right; }
1515
</style>
1616
</head>
1717
<body style="height: 100%;">

0 commit comments

Comments
 (0)