LCOV - code coverage report
Current view: top level - src/engraving/editing - editlyrics.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 75.6 % 356 269
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2026 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program. If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : 
      23              : #include "editlyrics.h"
      24              : 
      25              : #include "containers.h"
      26              : #include "log.h"
      27              : 
      28              : #include "../dom/chord.h"
      29              : #include "../dom/factory.h"
      30              : #include "../dom/lyrics.h"
      31              : #include "../dom/measure.h"
      32              : #include "../dom/score.h"
      33              : #include "../dom/segment.h"
      34              : #include "../dom/utils.h"
      35              : 
      36              : using namespace mu::engraving;
      37              : 
      38           25 : EditLyrics::InitialState EditLyrics::initialState(Lyrics* lyrics)
      39              : {
      40           25 :     InitialState state;
      41           25 :     state.lyrics = lyrics;
      42           25 :     state.chordRest = lyrics->chordRest();
      43           25 :     state.segment = lyrics->segment();
      44           25 :     state.track = lyrics->track();
      45           25 :     state.verse = lyrics->verse();
      46           25 :     state.placement = lyrics->placement();
      47           25 :     state.placementFlags = lyrics->propertyFlags(Pid::PLACEMENT);
      48           25 :     state.fontStyle = lyrics->fontStyle();
      49           25 :     state.fontStyleFlags = lyrics->propertyFlags(Pid::FONT_STYLE);
      50           25 :     state.centering = lyrics->centerBetweenStaves();
      51           25 :     state.styleType = lyrics->textStyleType();
      52           25 :     state.yOffset = lyrics->offset().y();
      53           25 :     state.hasPrecedingRepeat = state.chordRest->hasPrecedingJumpItem();
      54           25 :     state.hasFollowingRepeat = state.chordRest->hasFollowingJumpItem();
      55           25 :     state.prevPartialDash = findPrevPartialLyricsLineDash(lyrics);
      56           25 :     return state;
      57              : }
      58              : 
      59            3 : bool EditLyrics::canNavigateToNextSyllable(const InitialState& state)
      60              : {
      61            3 :     track_idx_t toLyricTrack = state.track;
      62            6 :     return findNextSyllableSegment(state, toLyricTrack) || state.hasFollowingRepeat || state.hasPrecedingRepeat;
      63              : }
      64              : 
      65            7 : Lyrics* EditLyrics::navigateToNextSyllable(Transaction&, Score* score, const InitialState& state)
      66              : {
      67            7 :     ChordRest* initialCR = state.chordRest;
      68            7 :     const track_idx_t track = state.track;
      69            7 :     const int verse = state.verse;
      70            7 :     const PlacementV placement = state.placement;
      71            7 :     PartialLyricsLine* prevPartialLyricsLine = state.prevPartialDash;
      72              : 
      73            7 :     track_idx_t toLyricTrack = track;
      74            7 :     Segment* nextSegment = findNextSyllableSegment(state, toLyricTrack);
      75              : 
      76              :     // look for the lyrics we are moving from; may be the current lyrics or a previous one
      77              :     // we are extending with several dashes
      78            7 :     Lyrics* fromLyrics = nullptr;
      79            7 :     Segment* segment = state.segment;
      80            7 :     const Segment* curSeg = segment;
      81            8 :     while (segment && segmentsAreAdjacent(segment, curSeg)) {
      82            7 :         ChordRest* cr = toChordRest(segment->element(track));
      83            7 :         if (!cr) {
      84            0 :             segment = segment->prev1(SegmentType::ChordRest);
      85            0 :             continue;
      86              :         }
      87            7 :         fromLyrics = cr->lyrics(verse, placement);
      88            7 :         if (fromLyrics) {
      89            6 :             break;
      90              :         }
      91            1 :         segment = segment->prev1(SegmentType::ChordRest);
      92              :     }
      93              : 
      94            7 :     if (!nextSegment) {
      95            1 :         if (fromLyrics && state.hasFollowingRepeat) {
      96              :             // Allow dash with no end syllable if there is a repeat
      97            1 :             switch (fromLyrics->syllabic()) {
      98            0 :             case LyricsSyllabic::BEGIN:
      99              :             case LyricsSyllabic::MIDDLE:
     100            0 :                 break;
     101            1 :             case LyricsSyllabic::SINGLE:
     102            1 :                 fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::BEGIN));
     103            1 :                 break;
     104            0 :             case LyricsSyllabic::END:
     105            0 :                 fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::MIDDLE));
     106            0 :                 break;
     107              :             }
     108            1 :             fromLyrics->undoChangeProperty(Pid::LYRIC_TICKS, Fraction(0, 1));
     109            1 :             score->setLayoutAll();
     110            1 :             return nullptr;
     111              :         }
     112              : 
     113            0 :         if (state.hasPrecedingRepeat) {
     114              :             // No from lyrics - create incoming partial dash
     115            0 :             score->undoAddElement(createPartialLyricsLine(score, state, false, Fraction(0, 1)));
     116              : 
     117              :             // Don't advance cursor
     118            0 :             Lyrics* toLyrics = createSyllable(initialCR, state, state.styleType, LyricsSyllabic::END);
     119            0 :             score->undoAddElement(toLyrics);
     120            0 :             return toLyrics;
     121              :         }
     122              :     }
     123              : 
     124            6 :     ChordRest* cr = nextSegment ? toChordRest(nextSegment->element(toLyricTrack)) : nullptr;
     125            6 :     Lyrics* toLyrics = cr ? cr->lyrics(verse, placement) : nullptr;
     126              : 
     127            6 :     if (!toLyrics && !cr) {
     128            0 :         return nullptr;
     129              :     }
     130              : 
     131              :     // If no lyrics in current track, check others
     132            6 :     if (!toLyrics) {
     133            2 :         const staff_idx_t staffIdx = track2staff(track);
     134           10 :         for (track_idx_t t = staff2track(staffIdx); t < staff2track(staffIdx + 1); ++t) {
     135            8 :             if (t == track) {
     136            2 :                 continue;
     137              :             }
     138            6 :             cr = toChordRest(nextSegment->element(t));
     139            6 :             if (cr) {
     140            0 :                 toLyrics = cr->lyrics(verse, placement);
     141            0 :                 if (toLyrics) {
     142            0 :                     break;
     143              :                 }
     144              :             }
     145              :         }
     146              :     }
     147              : 
     148              :     // This will be a partial dash at the start of a measure after a repeat
     149              :     // We don't want toLyrics
     150            6 :     toLyrics = state.hasPrecedingRepeat && !fromLyrics ? nullptr : toLyrics;
     151              : 
     152              :     // Make sure we end up with either the cr of toLyrics or cr on correct track
     153            6 :     cr = !toLyrics ? toChordRest(nextSegment->element(track)) : cr;
     154              : 
     155              :     // Disallow dashes between non-adjacent repeat sections eg. 1st volta -> 2nd volta
     156              :     // Instead, try to add partial dashes
     157            6 :     if (cr && fromLyrics && isInNonAdjacentRepeatSection(cr, fromLyrics, track)) {
     158            0 :         fromLyrics = nullptr;
     159              :     }
     160              : 
     161            1 :     const bool startIncomingPartialDash = state.hasPrecedingRepeat && !fromLyrics
     162            7 :                                           && !prevPartialLyricsLine && state.lyrics->xmlText().empty();
     163              : 
     164            6 :     const bool newLyrics = !toLyrics || startIncomingPartialDash;
     165            6 :     if (newLyrics) {
     166              :         /* Don't advance the cursor when starting an incoming partial dash after a repeat,
     167              :          * i.e. when there is no adjacent preceding syllable to dash from: */
     168            2 :         ChordRest* toLyricsChord = startIncomingPartialDash ? initialCR : cr;
     169            2 :         toLyrics = createSyllable(toLyricsChord, state, state.styleType, LyricsSyllabic::END);
     170              :     } else {
     171              :         // as we arrived at toLyrics by a dash, it cannot be initial or isolated
     172            4 :         if (toLyrics->syllabic() == LyricsSyllabic::BEGIN) {
     173            0 :             toLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::MIDDLE));
     174            4 :         } else if (toLyrics->syllabic() == LyricsSyllabic::SINGLE) {
     175            4 :             toLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::END));
     176              :         }
     177              :     }
     178              : 
     179            6 :     if (fromLyrics) {
     180              :         // as we moved away from fromLyrics by a dash,
     181              :         // it can have syll. dashes before and after but cannot be isolated or terminal
     182            5 :         switch (fromLyrics->syllabic()) {
     183            0 :         case LyricsSyllabic::BEGIN:
     184              :         case LyricsSyllabic::MIDDLE:
     185            0 :             break;
     186            5 :         case LyricsSyllabic::SINGLE:
     187            5 :             fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::BEGIN));
     188            5 :             break;
     189            0 :         case LyricsSyllabic::END:
     190            0 :             fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::MIDDLE));
     191            0 :             break;
     192              :         }
     193              :         // for the same reason, it cannot have a melisma
     194            5 :         fromLyrics->undoChangeProperty(Pid::LYRIC_TICKS, Fraction(0, 1));
     195            1 :     } else if (state.hasPrecedingRepeat && !prevPartialLyricsLine) {
     196              :         // No from lyrics - create incoming partial dash
     197            1 :         score->undoAddElement(createPartialLyricsLine(score, state, false, Fraction(0, 1)));
     198            0 :     } else if (prevPartialLyricsLine) {
     199            0 :         const Fraction tickDiff = cr->tick() - prevPartialLyricsLine->tick2();
     200            0 :         prevPartialLyricsLine->undoMoveEnd(tickDiff);
     201            0 :         score->undoAddElement(prevPartialLyricsLine);
     202            0 :         prevPartialLyricsLine->triggerLayout();
     203              :     }
     204              : 
     205            6 :     if (newLyrics) {
     206            2 :         score->undoAddElement(toLyrics);
     207              :     }
     208              : 
     209            6 :     return toLyrics;
     210              : }
     211              : 
     212            5 : EditLyrics::MelismaEnds EditLyrics::addMelisma(Transaction&, Score* score, const InitialState& state)
     213              : {
     214            5 :     Lyrics* lyrics = state.lyrics;
     215            5 :     ChordRest* initialCR = state.chordRest;
     216            5 :     const track_idx_t track = state.track;
     217            5 :     const int verse = state.verse;
     218            5 :     const PlacementV placement = state.placement;
     219            5 :     Segment* segment = state.segment;
     220            5 :     const Fraction endTick = segment->tick(); // a previous melisma cannot extend beyond this point
     221              : 
     222              :     // search next chord
     223            5 :     Segment* nextSegment = segment;
     224            5 :     while ((nextSegment = nextSegment->next1(SegmentType::ChordRest))) {
     225            5 :         EngravingItem* el = nextSegment->element(track);
     226            5 :         if (el && el->isChord()) {
     227            5 :             break;
     228              :         }
     229              :     }
     230              : 
     231            5 :     if (!segmentsAreAdjacent(segment, nextSegment)) {
     232            1 :         nextSegment = nullptr;
     233              :     }
     234              : 
     235              :     // look for the lyrics we are moving from; may be the current lyrics or a previous one
     236              :     // we are extending with several underscores
     237            5 :     const Segment* curSeg = segment;
     238            5 :     Lyrics* fromLyrics = nullptr;
     239            5 :     PartialLyricsLine* prevPartialLyricsLine = nullptr;
     240            7 :     while (segment && segmentsAreAdjacent(segment, curSeg)) {
     241            6 :         ChordRest* cr = toChordRest(segment->element(track));
     242            6 :         if (cr) {
     243            6 :             fromLyrics = cr->lyrics(verse, placement);
     244            6 :             if (fromLyrics) {
     245            4 :                 break;
     246              :             }
     247              :             // Check if there is a partial melisma to extend
     248            2 :             prevPartialLyricsLine = findPartialMelisma(score, cr, verse, placement);
     249            2 :             if (prevPartialLyricsLine) {
     250            0 :                 break;
     251              :             }
     252              :         }
     253            2 :         segment = segment->prev1(SegmentType::ChordRest);
     254              :     }
     255              : 
     256              :     // one-chord melisma?
     257              :     // if still at melisma initial chord and there is a valid next chord (if not,
     258              :     // there will be no melisma anyway), set a temporary melisma duration
     259            5 :     if (fromLyrics == lyrics && nextSegment) {
     260            2 :         lyrics->undoChangeProperty(Pid::LYRIC_TICKS, Lyrics::TEMP_MELISMA_TICKS);
     261            2 :         score->setLayoutAll();
     262              :     }
     263              : 
     264            5 :     if (!nextSegment) {
     265            1 :         if (fromLyrics) {
     266            1 :             switch (fromLyrics->syllabic()) {
     267            1 :             case LyricsSyllabic::SINGLE:
     268              :             case LyricsSyllabic::END:
     269            1 :                 break;
     270            0 :             default:
     271            0 :                 fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::END));
     272            0 :                 break;
     273              :             }
     274            1 :             if (fromLyrics->segment()->tick() < endTick
     275            1 :                 || (endTick + initialCR->actualTicks() == segment->measure()->endTick() && initialCR->hasFollowingJumpItem())) {
     276            1 :                 Fraction ticks = std::max(endTick - fromLyrics->segment()->tick(), Lyrics::TEMP_MELISMA_TICKS);
     277            1 :                 fromLyrics->undoChangeProperty(Pid::LYRIC_TICKS, ticks);
     278              :             }
     279              :         }
     280              : 
     281            1 :         if (state.hasPrecedingRepeat && !prevPartialLyricsLine) {
     282              :             // No from lyrics - create incoming partial melisma
     283            0 :             score->undoAddElement(createPartialLyricsLine(score, state, true, initialCR->ticks()));
     284            1 :         } else if (prevPartialLyricsLine) {
     285            0 :             const Fraction tickDiff = partialMelismaEndTick(score, segment, track, endTick) - prevPartialLyricsLine->tick2();
     286            0 :             prevPartialLyricsLine->undoMoveEnd(tickDiff);
     287              :         }
     288              : 
     289            1 :         score->setLayoutAll();
     290            1 :         return { nullptr, false, fromLyrics };
     291              :     }
     292              : 
     293              :     // if a place for a new lyrics has been found, create a lyrics there
     294            4 :     ChordRest* nextCR = toChordRest(nextSegment->element(track));
     295              : 
     296              :     // Disallow melisma lines between non-adjacent repeat sections eg. 1st volta -> 2nd volta
     297              :     // Instead, try to add partial melisma line
     298            4 :     if (nextCR && fromLyrics && isInNonAdjacentRepeatSection(nextCR, fromLyrics, track)) {
     299            0 :         fromLyrics = nullptr;
     300              :     }
     301              : 
     302            4 :     Lyrics* toLyrics = nextCR->lyrics(verse, placement);
     303            4 :     const bool newLyrics = (toLyrics == nullptr);
     304            4 :     if (!toLyrics) {
     305            3 :         toLyrics = createSyllable(nextCR, state, std::nullopt, LyricsSyllabic::SINGLE);
     306              :     }
     307              :     // as we arrived at toLyrics by an underscore, it cannot have syllabic dashes before
     308            1 :     else if (toLyrics->syllabic() == LyricsSyllabic::MIDDLE) {
     309            0 :         toLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::BEGIN));
     310            1 :     } else if (toLyrics->syllabic() == LyricsSyllabic::END) {
     311            0 :         toLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::SINGLE));
     312              :     }
     313              : 
     314            4 :     if (fromLyrics) {
     315              :         // as we moved away from fromLyrics by an underscore,
     316              :         // it can be isolated or terminal but cannot have dashes after
     317            3 :         switch (fromLyrics->syllabic()) {
     318            3 :         case LyricsSyllabic::SINGLE:
     319              :         case LyricsSyllabic::END:
     320            3 :             break;
     321            0 :         default:
     322            0 :             fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::END));
     323            0 :             break;
     324              :         }
     325              :         // for the same reason, if it has a melisma, this cannot extend beyond toLyrics
     326            3 :         if (fromLyrics->segment()->tick() < endTick) {
     327            1 :             fromLyrics->undoChangeProperty(Pid::LYRIC_TICKS, endTick - fromLyrics->segment()->tick());
     328              :         }
     329            1 :     } else if (state.hasPrecedingRepeat && !prevPartialLyricsLine) {
     330              :         // No from lyrics - create incoming partial melisma
     331            1 :         score->undoAddElement(createPartialLyricsLine(score, state, true, initialCR->ticks()));
     332            0 :     } else if (prevPartialLyricsLine) {
     333            0 :         const Fraction tickDiff = partialMelismaEndTick(score, segment, track, endTick) - prevPartialLyricsLine->tick2();
     334            0 :         prevPartialLyricsLine->undoMoveEnd(tickDiff);
     335            0 :         prevPartialLyricsLine->triggerLayout();
     336              :     }
     337              : 
     338            4 :     return { toLyrics, newLyrics, fromLyrics };
     339              : }
     340              : 
     341            1 : Lyrics* EditLyrics::addVerse(Transaction&, Score* score, const InitialState& state)
     342              : {
     343            1 :     Lyrics* lyrics = createLyrics(state.chordRest, state, state.verse + 1, std::nullopt);
     344            1 :     score->undoAddElement(lyrics);
     345            1 :     return lyrics;
     346              : }
     347              : 
     348            4 : bool EditLyrics::canNavigateToLyrics(const InitialState& state, bool back)
     349              : {
     350            4 :     return findNextLyricsSegment(state, back);
     351              : }
     352              : 
     353            4 : EditLyrics::NavigatedLyrics EditLyrics::navigateToLyrics(Transaction&, Score* score, const InitialState& state, bool back,
     354              :                                                          bool moveOnly)
     355              : {
     356            4 :     const track_idx_t track = state.track;
     357            4 :     const int verse = state.verse;
     358            4 :     const PlacementV placement = state.placement;
     359              : 
     360            4 :     Segment* nextSegment = findNextLyricsSegment(state, back);
     361            4 :     if (!nextSegment) {
     362            0 :         return {};
     363              :     }
     364              : 
     365              :     // look for the lyrics we are moving from; may be the current lyrics or a previous one
     366              :     // if we are skipping several chords with spaces
     367            4 :     Lyrics* fromLyrics = nullptr;
     368            4 :     if (!back) {
     369            3 :         Segment* segment = state.segment;
     370            3 :         while (segment) {
     371            3 :             ChordRest* cr = toChordRest(segment->element(track));
     372            3 :             if (cr) {
     373            3 :                 fromLyrics = cr->lyrics(verse, placement);
     374            3 :                 if (fromLyrics) {
     375            3 :                     break;
     376              :                 }
     377              :             }
     378            0 :             segment = segment->prev1(SegmentType::ChordRest);
     379              :         }
     380              :     }
     381              : 
     382            4 :     ChordRest* cr = toChordRest(nextSegment->element(track));
     383            4 :     if (!cr) {
     384            0 :         LOGD("no next lyrics list: %s", nextSegment->element(track)->typeName());
     385            0 :         return {};
     386              :     }
     387              : 
     388            4 :     Lyrics* nextLyrics = cr->lyrics(verse, placement);
     389            4 :     const bool newLyrics = !nextLyrics;
     390            4 :     if (newLyrics) {
     391            1 :         nextLyrics = createSyllable(cr, state, state.styleType, LyricsSyllabic::SINGLE);
     392              :     }
     393              : 
     394            4 :     if (fromLyrics && !moveOnly) {
     395            2 :         switch (nextLyrics->syllabic()) {
     396              :         // as we arrived at nextLyrics by a [Space], it can be the beginning
     397              :         // of a multi-syllable, but cannot have syllabic dashes before
     398            1 :         case LyricsSyllabic::SINGLE:
     399              :         case LyricsSyllabic::BEGIN:
     400            1 :             break;
     401            1 :         case LyricsSyllabic::END:
     402            1 :             nextLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::SINGLE));
     403            1 :             break;
     404            0 :         case LyricsSyllabic::MIDDLE:
     405            0 :             nextLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::BEGIN));
     406            0 :             break;
     407              :         }
     408              :         // as we moved away from fromLyrics by a [Space], it can be
     409              :         // the end of a multi-syllable, but cannot have syllabic dashes after
     410            2 :         switch (fromLyrics->syllabic()) {
     411            1 :         case LyricsSyllabic::SINGLE:
     412              :         case LyricsSyllabic::END:
     413            1 :             break;
     414            1 :         case LyricsSyllabic::BEGIN:
     415            1 :             fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::SINGLE));
     416            1 :             break;
     417            0 :         case LyricsSyllabic::MIDDLE:
     418            0 :             fromLyrics->undoChangeProperty(Pid::SYLLABIC, int(LyricsSyllabic::END));
     419            0 :             break;
     420              :         }
     421              :         // for the same reason, it cannot have a melisma
     422            2 :         if (fromLyrics->separator() && !fromLyrics->separator()->isEndMelisma()) {
     423            1 :             fromLyrics->undoChangeProperty(Pid::LYRIC_TICKS, Fraction(0, 1));
     424              :         }
     425              :     }
     426              : 
     427            4 :     if (newLyrics) {
     428            1 :         score->undoAddElement(nextLyrics);
     429              :     }
     430              : 
     431            4 :     return { nextLyrics, newLyrics };
     432              : }
     433              : 
     434            2 : Lyrics* EditLyrics::navigateToVerse(Transaction&, Score* score, const InitialState& state, int verse)
     435              : {
     436            2 :     Lyrics* lyrics = state.chordRest->lyrics(verse, state.placement);
     437            2 :     if (!lyrics) {
     438            1 :         lyrics = createLyrics(state.chordRest, state, verse, state.styleType);
     439            1 :         score->undoAddElement(lyrics);
     440              :     }
     441            2 :     return lyrics;
     442              : }
     443              : 
     444            3 : void EditLyrics::shiftLyricsVerse(Transaction&, Lyrics* lyrics, DirectionV direction)
     445              : {
     446            3 :     const int verse = lyrics->verse() + (direction == DirectionV::UP ? -1 : 1);
     447            3 :     if (verse < 0) {
     448            1 :         return;
     449              :     }
     450            2 :     lyrics->undoChangeProperty(Pid::VERSE, verse);
     451              : }
     452              : 
     453           14 : void EditLyrics::addLyrics(Score* score, const Fraction& tick, staff_idx_t staffIdx, const String& text)
     454              : {
     455           14 :     if (text.trimmed().isEmpty()) {
     456            1 :         return;
     457              :     }
     458           13 :     Measure* measure = score->tick2measure(tick);
     459           13 :     Segment* seg = measure->findSegment(SegmentType::ChordRest, tick);
     460           13 :     if (!seg) {
     461            0 :         LOGD("no segment found for lyrics<%s> at tick %d", muPrintable(text), tick.ticks());
     462            0 :         return;
     463              :     }
     464              : 
     465           15 :     for (voice_idx_t voice = 0; voice < VOICES; ++voice) {
     466           15 :         const track_idx_t track = staff2track(staffIdx, voice);
     467           15 :         ChordRest* cr = toChordRest(seg->element(track));
     468           15 :         if (cr) {
     469           13 :             Lyrics* lyrics = Factory::createLyrics(cr);
     470           13 :             lyrics->setXmlText(text);
     471           13 :             lyrics->setTrack(track);
     472           13 :             cr->add(lyrics);
     473           13 :             return;
     474              :         }
     475              :     }
     476            0 :     LOGD("no chord/rest for lyrics<%s> at tick %d, staff %zu", muPrintable(text), tick.ticks(), staffIdx);
     477              : }
     478              : 
     479           10 : Segment* EditLyrics::findNextSyllableSegment(const InitialState& state, track_idx_t& toLyricTrack)
     480              : {
     481           10 :     const track_idx_t track = state.track;
     482           10 :     const staff_idx_t staffIdx = track2staff(track);
     483              : 
     484           10 :     Segment* nextSegment = state.segment;
     485           10 :     while ((nextSegment = nextSegment->next1(SegmentType::ChordRest))) {
     486            9 :         EngravingItem* el = nextSegment->element(track);
     487            9 :         if (!el || !el->isChord()) {
     488            0 :             for (track_idx_t t = staff2track(staffIdx); t < staff2track(staffIdx + 1); ++t) {
     489            0 :                 el = nextSegment->element(t);
     490            0 :                 if (el && el->isChord() && toChord(el)->lyrics(state.verse, state.placement)) {
     491            0 :                     toLyricTrack = t;
     492            0 :                     break;
     493              :                 }
     494              :             }
     495              :         }
     496              : 
     497            9 :         if (el && el->isChord()) {
     498            9 :             break;
     499              :         }
     500              :     }
     501              : 
     502           10 :     return segmentsAreAdjacent(state.segment, nextSegment) ? nextSegment : nullptr;
     503              : }
     504              : 
     505            8 : Segment* EditLyrics::findNextLyricsSegment(const InitialState& state, bool back)
     506              : {
     507            8 :     Segment* nextSegment = state.segment;
     508            8 :     while ((nextSegment = back ? nextSegment->prev1(SegmentType::ChordRest) : nextSegment->next1(SegmentType::ChordRest))) {
     509            6 :         EngravingItem* el = nextSegment->element(state.track);
     510            6 :         if (!el) {
     511            0 :             continue;
     512              :         }
     513            6 :         if (el->isChord()) {
     514            6 :             break;
     515            0 :         } else if (el->isRest() && toChordRest(el)->lyrics(state.verse, state.placement)) {
     516            0 :             break;
     517              :         }
     518              :     }
     519            8 :     return nextSegment;
     520              : }
     521              : 
     522            8 : Lyrics* EditLyrics::createLyrics(ChordRest* cr, const InitialState& state, int verse, std::optional<TextStyleType> styleType)
     523              : {
     524            8 :     Lyrics* lyrics = Factory::createLyrics(cr);
     525            8 :     lyrics->setTrack(state.track);
     526            8 :     lyrics->setVerse(verse);
     527            8 :     lyrics->setTextStyleType(styleType ? *styleType : (lyrics->isEven() ? TextStyleType::LYRICS_EVEN : TextStyleType::LYRICS_ODD));
     528              : 
     529            8 :     lyrics->setPlacement(state.placement);
     530            8 :     lyrics->setPropertyFlags(Pid::PLACEMENT, state.placementFlags);
     531            8 :     lyrics->setCenterBetweenStaves(state.centering);
     532            8 :     lyrics->setFontStyle(state.fontStyle);
     533            8 :     lyrics->setPropertyFlags(Pid::FONT_STYLE, state.fontStyleFlags);
     534            8 :     return lyrics;
     535              : }
     536              : 
     537            6 : Lyrics* EditLyrics::createSyllable(ChordRest* cr, const InitialState& state, std::optional<TextStyleType> styleType,
     538              :                                    LyricsSyllabic syllabic)
     539              : {
     540            6 :     Lyrics* lyrics = createLyrics(cr, state, state.verse, styleType);
     541            6 :     lyrics->setSyllabic(syllabic);
     542            6 :     lyrics->setOffset(PointF(0.0, state.yOffset));
     543            6 :     return lyrics;
     544              : }
     545              : 
     546            2 : PartialLyricsLine* EditLyrics::createPartialLyricsLine(Score* score, const InitialState& state, bool isEndMelisma,
     547              :                                                        const Fraction& ticks)
     548              : {
     549            2 :     const ChordRest* initialCR = state.chordRest;
     550              : 
     551            2 :     PartialLyricsLine* line = Factory::createPartialLyricsLine(score->dummy());
     552            2 :     line->setIsEndMelisma(isEndMelisma);
     553            2 :     line->setVerse(state.verse);
     554            2 :     line->setPlacement(state.placement);
     555            2 :     line->setPropertyFlags(Pid::PLACEMENT, state.placementFlags);
     556            2 :     line->setCenterBetweenStaves(state.centering);
     557            2 :     line->setTick(initialCR->tick());
     558            2 :     line->setTicks(ticks);
     559            2 :     line->setTrack(initialCR->track());
     560            2 :     line->setTrack2(initialCR->track());
     561            2 :     return line;
     562              : }
     563              : 
     564            2 : PartialLyricsLine* EditLyrics::findPartialMelisma(const Score* score, const ChordRest* cr, int verse, PlacementV placement)
     565              : {
     566            4 :     for (auto& spanner : score->spannerMap().findOverlapping(cr->tick().ticks(), cr->endTick().ticks())) {
     567            2 :         if (!spanner.value->isPartialLyricsLine() || spanner.value->staffIdx() != cr->staffIdx()) {
     568            2 :             continue;
     569              :         }
     570            0 :         PartialLyricsLine* lyricsLine = toPartialLyricsLine(spanner.value);
     571            0 :         if (lyricsLine->verse() != verse || lyricsLine->placement() != placement || !lyricsLine->isEndMelisma()) {
     572            0 :             continue;
     573              :         }
     574            0 :         return lyricsLine;
     575              :     }
     576            2 :     return nullptr;
     577              : }
     578              : 
     579            8 : bool EditLyrics::isInNonAdjacentRepeatSection(const ChordRest* toCr, const Lyrics* fromLyrics, track_idx_t track)
     580              : {
     581            8 :     Measure* toLyricsMeasure = toCr->measure();
     582            8 :     Measure* fromLyricsMeasure = fromLyrics->measure();
     583              : 
     584            8 :     if (toLyricsMeasure == fromLyricsMeasure || !fromLyricsMeasure->lastChordRest(track)->hasFollowingJumpItem()) {
     585            8 :         return false;
     586              :     }
     587              : 
     588            0 :     const std::vector<Measure*> previousRepeats = findPreviousRepeatMeasures(toLyricsMeasure);
     589            0 :     return !previousRepeats.empty() && !muse::contains(previousRepeats, fromLyricsMeasure);
     590            0 : }
     591              : 
     592            0 : Fraction EditLyrics::partialMelismaEndTick(const Score* score, Segment* segment, track_idx_t track, const Fraction& endTick)
     593              : {
     594            0 :     Segment* endSegment = segment;
     595            0 :     while (endSegment && endSegment->tick() < endTick) {
     596            0 :         endSegment = endSegment->nextCR(track, true);
     597              :     }
     598            0 :     EngravingItem* endSegmentElement = endSegment->element(track);
     599            0 :     if (endSegment->tick() == endTick && endSegmentElement && endSegmentElement->isChord()) {
     600            0 :         Segment* endChordSeg = endSegment;
     601            0 :         Chord* endChord = toChord(endSegmentElement);
     602              : 
     603            0 :         endSegment = endChordSeg->nextCR(track, false);
     604              : 
     605            0 :         if (!endSegment || endSegment->tick() > endChord->endTick()) {
     606            0 :             endSegment = endChordSeg;
     607            0 :             while (endSegment && endSegment->tick() < endChord->endTick()) {
     608            0 :                 endSegment = endSegment->nextCR(muse::nidx, true);
     609              :             }
     610              :         }
     611              :     }
     612            0 :     return endSegment ? endSegment->tick() : score->endTick();
     613              : }
        

Generated by: LCOV version 2.0-1