processText method

ChordLyricsDocument processText({
  1. required String text,
  2. required TextStyle lyricsStyle,
  3. required dynamic chordStyle,
  4. dynamic widgetPadding = 0,
  5. int transposeIncrement = 0,
})

Process the text to get the parsed ChordLyricsDocument

Implementation

ChordLyricsDocument processText({
  required String text,
  required TextStyle lyricsStyle,
  required chordStyle,
  widgetPadding = 0,
  int transposeIncrement = 0,
}) {
  final lines = text.split('\n');

  bool _chordHasStartedOuter = false;
  String _currentCharacters = '';
  int _lastSpace = 0;
  String _currentLine = '';
  String _character = '';
  double _media = MediaQuery.of(context).size.width;
  int _characterIndex = 0;

  //list to store our updated lines without overflows
  final _newLines = <String>[];

  //loop through the lines
  for (var i = 0; i < lines.length; i++) {
    _characterIndex = 0;
    _currentCharacters = '';
    _currentLine = lines[i];

    //check if we have a long line
    if (textWidth(_currentLine, lyricsStyle) >= _media) {
      //work our way through the line and split when we need to
      for (var j = 0; j < _currentLine.length; j++) {
        _character = _currentLine[j];
        if (_character == '[')
          _chordHasStartedOuter = true;
        else if (_character == ']')
          _chordHasStartedOuter = false;
        else if (!_chordHasStartedOuter) {
          _currentCharacters += _character;
          if (_character == ' ') {
            //use this marker to only split where there are spaces. We can trim later.
            _lastSpace = j;
          }

          //This is the point where we need to split
          //widgetPadding has been added as a parameter to be passed from the build function
          //It is intended to allow for padding in the widget when comparing it to screen width
          //An additional buffer of around 10 might be needed to definitely stop overflow (ie. padding + 10).
          if (textWidth(_currentCharacters, lyricsStyle) + widgetPadding >=
              _media) {
            _newLines.add(
                _currentLine.substring(_characterIndex, _lastSpace).trim());
            _currentCharacters = '';
            _characterIndex = _lastSpace;
          }
        }
      }
      //add the rest of the long line
      _newLines.add(_currentLine
          .substring(_characterIndex, _currentLine.length)
          .trim());
    } else {
      //otherwise just add the regular line
      _newLines.add(_currentLine.trim());
    }
  }

  List<ChordLyricsLine> _chordLyricsLines =
      _newLines.map<ChordLyricsLine>((line) {
    ChordLyricsLine _chordLyricsLine = ChordLyricsLine([], '');
    String _lyricsSoFar = '';
    String _chordsSoFar = '';
    bool _chordHasStarted = false;
    line.split('').forEach((character) {
      if (character == ']') {
        final sizeOfLeadingLyrics = textWidth(_lyricsSoFar, lyricsStyle);

        final lastChordText = _chordLyricsLine.chords.isNotEmpty
            ? _chordLyricsLine.chords.last.chordText
            : '';

        final lastChordWidth = textWidth(lastChordText, chordStyle);
        // final sizeOfThisChord = textWidth(_chordsSoFar, chordStyle);

        double leadingSpace = 0;
        leadingSpace = (sizeOfLeadingLyrics - lastChordWidth);

        leadingSpace = max(0, leadingSpace);

        final transposedChord = transposeChord(
          _chordsSoFar,
          transposeIncrement,
        );

        _chordLyricsLine.chords.add(Chord(leadingSpace, transposedChord));
        _chordLyricsLine.lyrics += _lyricsSoFar;
        _lyricsSoFar = '';
        _chordsSoFar = '';
        _chordHasStarted = false;
      } else if (character == '[') {
        _chordHasStarted = true;
      } else {
        if (_chordHasStarted) {
          _chordsSoFar += character;
        } else {
          _lyricsSoFar += character;
        }
      }
    });

    _chordLyricsLine.lyrics += _lyricsSoFar;

    return _chordLyricsLine;
  }).toList();

  return ChordLyricsDocument(_chordLyricsLines);
}