Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finished add spaces and dash with linefeed #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions lib/src/chord_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ChordProcessor {
required String text,
required TextStyle lyricsStyle,
required TextStyle chordStyle,
bool showChord = true,
double scaleFactor = 1.0,
int widgetPadding = 0,
int transposeIncrement = 0,
Expand All @@ -30,6 +31,11 @@ class ChordProcessor {
_textScaleFactor = scaleFactor;
chordTransposer.transpose = transposeIncrement;

String spaceChar = " ";
double minNumLeadingSpaces = 1;
double spaceWidth = textWidth(spaceChar, lyricsStyle);
double minLeadingSpace = minNumLeadingSpaces * spaceWidth;

/// List to store our updated lines without overflows
final List<String> newLines = <String>[];
String currentLine = '';
Expand All @@ -43,6 +49,58 @@ class ChordProcessor {
continue;
}

if (showChord) {
// obtain the lyrics portions in lyricsList
// and the chords in chordList and
// calculate paddingString before each chord

// Regular exp for chord
RegExp chordRe = RegExp(r'(\[[^\[]*\])');
List<String> lyricsList = currentLine.split(chordRe);
Iterable<RegExpMatch> matches = chordRe.allMatches(currentLine);

List<String> chordList = [];
for (RegExpMatch match in matches) {
String chordText = currentLine.substring(match.start, match.end);
chordList.add(chordText);
}

int lyricIdx = 0;
String paddingString = "";
String lyricsBeforeChord = lyricsList[lyricIdx++];
String correctedLine = lyricsBeforeChord;
chordList.forEach((chordText) {
lyricsBeforeChord = lyricsList[lyricIdx++];
double lyricsBetwChordsWidth =
textWidth(lyricsBeforeChord, lyricsStyle);
double chordWidth = textWidth(chordText, chordStyle);
String lastLyricChar = (lyricsBeforeChord.length > 0)
? lyricsBeforeChord.substring(lyricsBeforeChord.length - 1)
: '';
int numAddSpaces = 0;
double spaceDiff = (lyricsBetwChordsWidth - chordWidth);
if (spaceDiff < 0) {
numAddSpaces = ((minLeadingSpace - spaceDiff) / spaceWidth).round();
if (lastLyricChar != ' ') {
int numSpacesRight = ((numAddSpaces - 1) / 2).round();
int numSpacesLeft = numAddSpaces - 1 - numSpacesRight;
paddingString = (spaceChar * numSpacesLeft) +
"-" +
(spaceChar * numSpacesRight);
} else if (lastLyricChar == ' ') {
paddingString = spaceChar * numAddSpaces;
}
}
if (lyricIdx == lyricsList.length) {
paddingString = "";
}
correctedLine += chordText + lyricsBeforeChord + paddingString;
});
//String endLyrics = lyricsList[lyricIdx];
//correctedLine += endLyrics;
currentLine = correctedLine;
}

//check if we have a long line
if (textWidth(currentLine, lyricsStyle) >= media) {
_handleLongLine(
Expand Down
1 change: 1 addition & 0 deletions lib/src/lyrics_renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class _LyricsRendererState extends State<LyricsRenderer> {
widgetPadding: widget.widgetPadding,
scaleFactor: widget.scaleFactor,
transposeIncrement: widget.transposeIncrement,
showChord: widget.showChord,
);
if (chordLyricsDocument.chordLyricsLines.isEmpty) return Container();
return SingleChildScrollView(
Expand Down