From 4096ad51b2c7efb6b370861ce4f0a814f4428fdb Mon Sep 17 00:00:00 2001 From: teejt Date: Sun, 29 May 2022 14:29:23 +0800 Subject: [PATCH 1/2] Finished add spaces and dash with linefeed --- lib/src/chord_parser.dart | 55 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/lib/src/chord_parser.dart b/lib/src/chord_parser.dart index c52d064..71f9558 100644 --- a/lib/src/chord_parser.dart +++ b/lib/src/chord_parser.dart @@ -30,6 +30,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 newLines = []; String currentLine = ''; @@ -43,6 +48,56 @@ class ChordProcessor { continue; } + // 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 lyricsList = currentLine.split(chordRe); + Iterable matches = chordRe.allMatches(currentLine); + + List 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( From 37fb3330ffeb343c21c6c0f8590b74ccbc0f286e Mon Sep 17 00:00:00 2001 From: teejt Date: Sun, 29 May 2022 14:45:21 +0800 Subject: [PATCH 2/2] Added showChord - so that paddingSpaces are not calculated when not showChord --- lib/src/chord_parser.dart | 93 +++++++++++++++++++----------------- lib/src/lyrics_renderer.dart | 1 + 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/lib/src/chord_parser.dart b/lib/src/chord_parser.dart index 71f9558..4eb81ac 100644 --- a/lib/src/chord_parser.dart +++ b/lib/src/chord_parser.dart @@ -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, @@ -48,55 +49,57 @@ class ChordProcessor { continue; } - // obtain the lyrics portions in lyricsList - // and the chords in chordList and - // calculate paddingString before each chord + 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 lyricsList = currentLine.split(chordRe); - Iterable matches = chordRe.allMatches(currentLine); + // Regular exp for chord + RegExp chordRe = RegExp(r'(\[[^\[]*\])'); + List lyricsList = currentLine.split(chordRe); + Iterable matches = chordRe.allMatches(currentLine); - List chordList = []; - for (RegExpMatch match in matches) { - String chordText = currentLine.substring(match.start, match.end); - chordList.add(chordText); - } + List 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; + 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; + 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) { diff --git a/lib/src/lyrics_renderer.dart b/lib/src/lyrics_renderer.dart index e037f60..99ba403 100644 --- a/lib/src/lyrics_renderer.dart +++ b/lib/src/lyrics_renderer.dart @@ -110,6 +110,7 @@ class _LyricsRendererState extends State { widgetPadding: widget.widgetPadding, scaleFactor: widget.scaleFactor, transposeIncrement: widget.transposeIncrement, + showChord: widget.showChord, ); if (chordLyricsDocument.chordLyricsLines.isEmpty) return Container(); return SingleChildScrollView(