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) 2021 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 "utils.h"
24 :
25 : #include <cmath>
26 : #include <map>
27 :
28 : #include "containers.h"
29 :
30 : #include "actionicon.h"
31 : #include "accidental.h"
32 : #include "arpeggio.h"
33 : #include "chord.h"
34 : #include "chordrest.h"
35 : #include "clef.h"
36 : #include "fret.h"
37 : #include "harmony.h"
38 : #include "laissezvib.h"
39 : #include "lyrics.h"
40 : #include "marker.h"
41 : #include "masterscore.h"
42 : #include "repeatlist.h"
43 : #include "keysig.h"
44 : #include "measure.h"
45 : #include "measurenumber.h"
46 : #include "measurerepeat.h"
47 : #include "note.h"
48 : #include "page.h"
49 : #include "part.h"
50 : #include "playcounttext.h"
51 : #include "partialtie.h"
52 : #include "pitchspelling.h"
53 : #include "rest.h"
54 : #include "score.h"
55 : #include "segment.h"
56 : #include "select.h"
57 : #include "sig.h"
58 : #include "staff.h"
59 : #include "system.h"
60 : #include "spanner.h"
61 : #include "tremolosinglechord.h"
62 : #include "tremolotwochord.h"
63 : #include "tuplet.h"
64 : #include "drumset.h"
65 : #include "barline.h"
66 : #include "durationtype.h"
67 :
68 : #include "log.h"
69 :
70 : using namespace mu;
71 :
72 : namespace mu::engraving {
73 : //---------------------------------------------------------
74 : // handleRect
75 : //---------------------------------------------------------
76 :
77 0 : RectF handleRect(const PointF& pos)
78 : {
79 0 : return RectF(pos.x() - 4, pos.y() - 4, 8, 8);
80 : }
81 :
82 : //---------------------------------------------------------
83 : // tick2measure
84 : //---------------------------------------------------------
85 :
86 63821 : Measure* Score::tick2measure(const Fraction& tick) const
87 : {
88 63821 : if (tick == Fraction(-1, 1)) { // special number
89 13 : return lastMeasure();
90 : }
91 63808 : if (tick <= Fraction(0, 1)) {
92 22952 : return firstMeasure();
93 : }
94 :
95 40856 : return m_measures.measureByTick(tick.ticks());
96 : }
97 :
98 : //---------------------------------------------------------
99 : // tick2measureMM
100 : //---------------------------------------------------------
101 :
102 101362 : Measure* Score::tick2measureMM(const Fraction& t) const
103 : {
104 101362 : Fraction tick(t);
105 101362 : if (tick == Fraction(-1, 1)) {
106 35 : return lastMeasureMM();
107 : }
108 101327 : if (tick < Fraction(0, 1)) {
109 1 : tick = Fraction(0, 1);
110 : }
111 :
112 101327 : Measure* measure = m_measures.measureByTick(t.ticks());
113 101327 : if (!measure) {
114 171 : LOGD("tick2measureMM %d not found", tick.ticks());
115 171 : return nullptr;
116 : }
117 :
118 101156 : return measure->coveringMMRestOrThis();
119 : }
120 :
121 19 : MeasureBase* Score::tick2measureBase(const Fraction& tick) const
122 : {
123 19 : if (tick == Fraction(-1, 1)) { // special number
124 0 : return m_measures.last();
125 : }
126 19 : if (tick <= Fraction(0, 1)) {
127 9 : return m_measures.first();
128 : }
129 :
130 10 : return m_measures.firstMeasureBaseAtTick(tick.ticks());
131 : }
132 :
133 : //---------------------------------------------------------
134 : // tick2segment
135 : //---------------------------------------------------------
136 :
137 4978 : Segment* Score::tick2segmentMM(const Fraction& tick, bool first, SegmentType st) const
138 : {
139 4978 : return tick2segment(tick, first, st, true);
140 : }
141 :
142 167 : Segment* Score::tick2segmentMM(const Fraction& tick) const
143 : {
144 167 : return tick2segment(tick, false, SegmentType::All, true);
145 : }
146 :
147 83 : Segment* Score::tick2segmentMM(const Fraction& tick, bool first) const
148 : {
149 83 : return tick2segment(tick, first, SegmentType::All, true);
150 : }
151 :
152 24492 : Segment* Score::tick2segment(const Fraction& t, bool first, SegmentType st, bool useMMrest) const
153 : {
154 24492 : Fraction tick(t);
155 : Measure* m;
156 24492 : if (useMMrest) {
157 5362 : m = tick2measureMM(tick);
158 : // When mmRest force tick to the first segment of mmRest.
159 5362 : if (m && m->isMMRest() && tick != m->endTick()) {
160 10 : tick = m->tick();
161 : }
162 : } else {
163 19130 : m = tick2measure(tick);
164 : }
165 :
166 24492 : if (m == 0) {
167 596 : LOGD() << "no measure for tick " << tick.ticks();
168 596 : return 0;
169 : }
170 78997 : for (Segment* segment = m->first(st); segment;) {
171 78281 : Fraction t1 = segment->tick();
172 78281 : Segment* nsegment = segment->next(st);
173 78281 : if (tick == t1) {
174 23425 : if (first) {
175 23180 : return segment;
176 : } else {
177 5525 : if (!nsegment || tick < nsegment->tick()) {
178 5280 : return segment;
179 : }
180 : }
181 : }
182 55101 : segment = nsegment;
183 : }
184 716 : return 0;
185 : }
186 :
187 55 : Segment* Score::tick2segment(const Fraction& tick) const
188 : {
189 55 : return tick2segment(tick, false, SegmentType::All, false);
190 : }
191 :
192 0 : Segment* Score::tick2segment(const Fraction& tick, bool first) const
193 : {
194 0 : return tick2segment(tick, first, SegmentType::All, false);
195 : }
196 :
197 : //---------------------------------------------------------
198 : // tick2leftSegment
199 : /// return the segment at this tick position if any or
200 : /// the first segment *before* this tick position
201 : //---------------------------------------------------------
202 :
203 1593 : Segment* Score::tick2leftSegment(const Fraction& tick, bool useMMrest, SegmentType segType) const
204 : {
205 1593 : Measure* m = useMMrest ? tick2measureMM(tick) : tick2measure(tick);
206 1593 : if (m == 0) {
207 126 : LOGD("tick2leftSegment(): not found tick %d", tick.ticks());
208 126 : return 0;
209 : }
210 :
211 : // loop over all segments
212 1467 : Segment* ps = 0;
213 2805 : for (Segment* s = m->first(segType); s; s = s->next(segType)) {
214 2639 : if (tick < s->tick()) {
215 148 : return ps;
216 2491 : } else if (tick == s->tick()) {
217 1153 : return s;
218 : }
219 1338 : ps = s;
220 : }
221 166 : return ps;
222 : }
223 :
224 : //---------------------------------------------------------
225 : // tick2rightSegment
226 : /// return the segment at this tick position if any or
227 : /// the first segment *after* this tick position
228 : //---------------------------------------------------------
229 :
230 2461 : Segment* Score::tick2rightSegment(const Fraction& tick, bool useMMrest, SegmentType segType) const
231 : {
232 2461 : Measure* m = useMMrest ? tick2measureMM(tick) : tick2measure(tick);
233 2461 : if (m == 0) {
234 : //LOGD("tick2nearestSegment(): not found tick %d", tick.ticks());
235 102 : return 0;
236 : }
237 : // loop over all segments
238 8227 : for (Segment* s = m->first(segType); s; s = s->next1(segType)) {
239 8046 : if (tick <= s->tick()) {
240 2178 : return s;
241 : }
242 : }
243 181 : return 0;
244 : }
245 :
246 : //---------------------------------------------------------
247 : // tick2beatType
248 : //---------------------------------------------------------
249 :
250 0 : BeatType Score::tick2beatType(const Fraction& tick) const
251 : {
252 0 : Measure* m = tick2measure(tick);
253 0 : Fraction msrTick = m->tick();
254 0 : TimeSigFrac timeSig = sigmap()->timesig(msrTick).nominal();
255 :
256 0 : int rtick = (tick - msrTick).ticks();
257 :
258 0 : if (m->isAnacrusis()) { // measure is incomplete (anacrusis)
259 0 : rtick += timeSig.ticksPerMeasure() - m->ticks().ticks();
260 : }
261 :
262 0 : return timeSig.rtick2beatType(rtick);
263 : }
264 :
265 1128 : void Score::checkChordList()
266 : {
267 1128 : m_chordList.checkChordList(style());
268 1128 : }
269 :
270 : //---------------------------------------------------------
271 : // next/prevChordNote
272 : //
273 : // returns the top note of the next/previous chord. If a
274 : // chord exists in the same track as note,
275 : // it is used. If not, the topmost existing chord is used.
276 : // May return nullptr if there is no next/prev note
277 : //---------------------------------------------------------
278 :
279 0 : Note* nextChordNote(Note* note)
280 : {
281 0 : track_idx_t track = note->track();
282 0 : track_idx_t fromTrack = (track / VOICES) * VOICES;
283 0 : track_idx_t toTrack = fromTrack + VOICES;
284 : // TODO : limit to same instrument, not simply to same staff!
285 0 : Segment* seg = note->chord()->segment()->nextCR(track, true);
286 0 : while (seg) {
287 0 : EngravingItem* targetElement = seg->element(track);
288 : // if a chord exists in the same track, return its top note
289 0 : if (targetElement && targetElement->isChord()) {
290 0 : return toChord(targetElement)->upNote();
291 : }
292 : // if not, return topmost chord in track range
293 0 : for (track_idx_t i = fromTrack; i < toTrack; i++) {
294 0 : targetElement = seg->element(i);
295 0 : if (targetElement && targetElement->isChord()) {
296 0 : return toChord(targetElement)->upNote();
297 : }
298 : }
299 0 : seg = seg->nextCR(track, true);
300 : }
301 0 : return nullptr;
302 : }
303 :
304 0 : Note* prevChordNote(Note* note)
305 : {
306 0 : track_idx_t track = note->track();
307 0 : track_idx_t fromTrack = (track / VOICES) * VOICES;
308 0 : track_idx_t toTrack = fromTrack + VOICES;
309 : // TODO : limit to same instrument, not simply to same staff!
310 0 : Segment* seg = note->chord()->segment()->prev1();
311 0 : while (seg) {
312 0 : if (seg->segmentType() == SegmentType::ChordRest) {
313 0 : EngravingItem* targetElement = seg->element(track);
314 : // if a chord exists in the same track, return its top note
315 0 : if (targetElement && targetElement->isChord()) {
316 0 : return toChord(targetElement)->upNote();
317 : }
318 : // if not, return topmost chord in track range
319 0 : for (track_idx_t i = fromTrack; i < toTrack; i++) {
320 0 : targetElement = seg->element(i);
321 0 : if (targetElement && targetElement->isChord()) {
322 0 : return toChord(targetElement)->upNote();
323 : }
324 : }
325 : }
326 0 : seg = seg->prev1();
327 : }
328 0 : return nullptr;
329 : }
330 :
331 : //---------------------------------------------------------
332 : // pitchKeyAdjust
333 : // change entered note to sounding pitch dependent
334 : // on key.
335 : // Example: if F is entered in G-major, a Fis is played
336 : // key -7 ... +7
337 : //---------------------------------------------------------
338 :
339 901 : int pitchKeyAdjust(int step, Key key)
340 : {
341 : static int ptab[15][7] = {
342 : // c d e f g a b
343 : { -1, 1, 3, 4, 6, 8, 10 }, // Bes
344 : { -1, 1, 3, 5, 6, 8, 10 }, // Ges
345 : { 0, 1, 3, 5, 6, 8, 10 }, // Des
346 : { 0, 1, 3, 5, 7, 8, 10 }, // As
347 : { 0, 2, 3, 5, 7, 8, 10 }, // Es
348 : { 0, 2, 3, 5, 7, 9, 10 }, // B
349 : { 0, 2, 4, 5, 7, 9, 10 }, // F
350 : { 0, 2, 4, 5, 7, 9, 11 }, // C
351 : { 0, 2, 4, 6, 7, 9, 11 }, // G
352 : { 1, 2, 4, 6, 7, 9, 11 }, // D
353 : { 1, 2, 4, 6, 8, 9, 11 }, // A
354 : { 1, 3, 4, 6, 8, 9, 11 }, // E
355 : { 1, 3, 4, 6, 8, 10, 11 }, // H
356 : { 1, 3, 5, 6, 8, 10, 11 }, // Fis
357 : { 1, 3, 5, 6, 8, 10, 12 }, // Cis
358 : };
359 901 : return ptab[int(key) + 7][step];
360 : }
361 :
362 : //---------------------------------------------------------
363 : // y2pitch
364 : //---------------------------------------------------------
365 :
366 0 : int y2pitch(double y, ClefType clef, double _spatium)
367 : {
368 0 : int l = lrint(y / _spatium * 2.0);
369 0 : return line2pitch(l, clef, Key::C);
370 : }
371 :
372 : //---------------------------------------------------------
373 : // line2pitch
374 : // key -7 ... +7
375 : //---------------------------------------------------------
376 :
377 329 : int line2pitch(int line, ClefType clef, Key key)
378 : {
379 329 : int l = std::max(ClefInfo::pitchOffset(clef) - line, 0);
380 329 : int octave = l / STEP_DELTA_OCTAVE;
381 329 : l %= STEP_DELTA_OCTAVE;
382 :
383 329 : int pitch = pitchKeyAdjust(l, key) + octave * PITCH_DELTA_OCTAVE;
384 329 : return clampPitch(pitch);
385 : }
386 :
387 : static const char16_t* valSharp[] = {
388 : u"c",
389 : u"c♯",
390 : u"d",
391 : u"d♯",
392 : u"e",
393 : u"f",
394 : u"f♯",
395 : u"g",
396 : u"g♯",
397 : u"a",
398 : u"a♯",
399 : u"b"
400 : };
401 : static const char16_t* valFlat[] = {
402 : u"c",
403 : u"d♭",
404 : u"d",
405 : u"e♭",
406 : u"e",
407 : u"f",
408 : u"g♭",
409 : u"g",
410 : u"a♭",
411 : u"a",
412 : u"b♭",
413 : u"b"
414 : };
415 :
416 : /*!
417 : * Returns the string representation of the given pitch.
418 : *
419 : * Returns the latin letter name, accidental, and octave numeral.
420 : * Uses upper case only for pitches 0-24.
421 : *
422 : * @param v
423 : * The pitch number of the note.
424 : *
425 : * @return
426 : * The string representation of the note.
427 : */
428 0 : String pitch2string(int v, bool useFlats)
429 : {
430 0 : if (!pitchIsValid(v)) {
431 0 : return String(u"----");
432 : }
433 0 : int octave = (v / PITCH_DELTA_OCTAVE) - 1;
434 0 : String o;
435 0 : o = String::number(octave);
436 0 : int i = v % PITCH_DELTA_OCTAVE;
437 :
438 0 : String pitchStr = useFlats ? valFlat[i] : valSharp[i];
439 0 : if (octave < 0) {
440 0 : pitchStr = pitchStr.toUpper();
441 : }
442 :
443 0 : return pitchStr + o;
444 0 : }
445 :
446 : /*!
447 : * Returns the pitch number of the given string.
448 : *
449 : * @param s
450 : * The string representation of the note.
451 : *
452 : * @return
453 : * The pitch number of the note.
454 : */
455 0 : int string2pitch(const String& s)
456 : {
457 0 : if (s == String(u"----")) {
458 0 : return INVALID_PITCH;
459 : }
460 :
461 0 : String value = s;
462 :
463 0 : bool negative = s.contains(u'-');
464 0 : int octave = String(s[s.size() - 1]).toInt() * (negative ? -1 : 1);
465 0 : if (octave < -1 || octave > 9) {
466 0 : return -1;
467 : }
468 :
469 0 : value = value.mid(0, value.size() - (negative ? 2 : 1));
470 0 : value = value.toLower();
471 :
472 0 : int pitchIndex = -1;
473 0 : for (int i = 0; i < PITCH_DELTA_OCTAVE; ++i) {
474 0 : if (value == valFlat[i] || value == valSharp[i]) {
475 0 : pitchIndex = i;
476 0 : break;
477 : }
478 : }
479 :
480 0 : if (pitchIndex == -1) {
481 0 : return -1;
482 : }
483 :
484 0 : return (octave + 1) * PITCH_DELTA_OCTAVE + pitchIndex;
485 0 : }
486 :
487 29 : String convertPitchStringFlatsAndSharpsToUnicode(const String& str)
488 : {
489 29 : if (str.isEmpty()) {
490 0 : return String();
491 : }
492 :
493 29 : String value = String(str[0]);
494 50 : for (size_t i = 1; i < str.size(); ++i) {
495 21 : Char symbol = str.at(i).toLower();
496 21 : if (symbol == u'b') {
497 21 : value.append(u'♭');
498 0 : } else if (symbol == u'#') {
499 0 : value.append(u'♯');
500 : } else {
501 0 : value.append(symbol);
502 : }
503 : }
504 :
505 29 : return value;
506 29 : }
507 :
508 : //---------------------------------------------------------
509 : // searchTieNote
510 : // search Note to tie to "note"
511 : //---------------------------------------------------------
512 :
513 388 : Note* searchTieNote(const Note* note, const Segment* nextSegment, const bool disableOverRepeats)
514 : {
515 388 : if (!note) {
516 0 : return nullptr;
517 : }
518 :
519 388 : Note* note2 = nullptr;
520 388 : Chord* chord = note->chord();
521 388 : Segment* seg = chord->segment();
522 :
523 388 : if (!nextSegment) {
524 259 : nextSegment = seg->next1(SegmentType::ChordRest);
525 282 : while (nextSegment && nextSegment->tick() < chord->endTick()) {
526 23 : nextSegment = nextSegment->next1(SegmentType::ChordRest);
527 : }
528 : }
529 :
530 388 : if (!nextSegment) {
531 28 : return nullptr;
532 : }
533 :
534 360 : if (disableOverRepeats && !segmentsAreAdjacent(seg, nextSegment)) {
535 4 : return nullptr;
536 : }
537 :
538 356 : if (chord->isGraceBefore()) {
539 1 : chord = toChord(chord->ownershipParent());
540 :
541 : // try to tie to next grace note
542 :
543 1 : size_t index = note->chord()->graceIndex();
544 2 : for (Chord* c : chord->graceNotes()) {
545 1 : if (c->graceIndex() == index + 1) {
546 0 : note2 = c->findNote(note->pitch());
547 0 : if (note2) {
548 0 : return note2;
549 : }
550 : }
551 : }
552 :
553 : // try to tie to note in parent chord
554 1 : note2 = chord->findNote(note->pitch());
555 1 : if (note2) {
556 1 : return note2;
557 : }
558 355 : } else if (chord->isGraceAfter()) {
559 : // grace after
560 : // we will try to tie to note in next normal chord, below
561 : // meanwhile, set chord to parent chord so the endTick calculation will make sense
562 0 : chord = toChord(chord->ownershipParent());
563 : } else {
564 : // normal chord
565 : // try to tie to grace note after if present
566 355 : std::vector<Chord*> gna = chord->graceNotesAfter();
567 355 : if (!gna.empty()) {
568 0 : Chord* gc = gna.front();
569 0 : note2 = gc->findNote(note->pitch());
570 0 : if (note2) {
571 0 : return note2;
572 : }
573 : }
574 355 : }
575 : // at this point, chord is a regular chord, not a grace chord
576 : // and we are looking for a note in the *next* chord (grace or regular)
577 :
578 355 : int idx1 = note->unisonIndex();
579 355 : Part* part = chord->part();
580 355 : const TrackRange trackRange = part->trackRange();
581 1787 : for (track_idx_t track = trackRange.startTrack; track < trackRange.endTrack; ++track) {
582 1433 : EngravingItem* e = nextSegment->element(track);
583 1433 : if (!e || !e->isChord()) {
584 1106 : continue;
585 : }
586 327 : Chord* c = toChord(e);
587 327 : if (c->vStaffIdx() != chord->vStaffIdx()) {
588 : // this check is needed as we are iterating over all staves to capture cross-staff chords
589 0 : continue;
590 : }
591 : // if there are grace notes before, try to tie to first one
592 327 : std::vector<Chord*> gnb = c->graceNotesBefore();
593 327 : if (!gnb.empty()) {
594 1 : Chord* gc = gnb.front();
595 1 : Note* gn2 = gc->findNote(note->pitch());
596 1 : if (gn2) {
597 1 : return gn2;
598 : }
599 : }
600 326 : int idx2 = 0;
601 532 : for (Note* n : c->notes()) {
602 509 : if (n->pitch() == note->pitch()) {
603 303 : if (idx1 == idx2) {
604 303 : if (!note2 || c->track() == chord->track()) {
605 303 : note2 = n;
606 303 : break;
607 : }
608 : } else {
609 0 : ++idx2;
610 : }
611 : }
612 : }
613 327 : }
614 354 : return note2;
615 : }
616 :
617 : //---------------------------------------------------------
618 : // absStep
619 : /// Compute absolute step.
620 : /// C D E F G A B ....
621 : //---------------------------------------------------------
622 :
623 204628 : int absStep(int tpc, int pitch)
624 : {
625 204628 : int line = tpc2step(tpc) + (pitch / PITCH_DELTA_OCTAVE) * STEP_DELTA_OCTAVE;
626 204628 : int tpcPitch = tpc2pitch(tpc);
627 :
628 204628 : if (tpcPitch < MIN_PITCH) {
629 194 : line += STEP_DELTA_OCTAVE;
630 : } else {
631 204434 : line -= (tpcPitch / PITCH_DELTA_OCTAVE) * STEP_DELTA_OCTAVE;
632 : }
633 204628 : return line;
634 : }
635 :
636 288 : int absStep(int pitch)
637 : {
638 : // TODO - does this need to be key-aware?
639 288 : int tpc = pitch2tpc(pitch, Key::C, Prefer::NEAREST);
640 288 : return absStep(tpc, pitch);
641 : }
642 :
643 45 : int absStep(int line, ClefType clef)
644 : {
645 45 : return ClefInfo::pitchOffset(clef) - line;
646 : }
647 :
648 : //---------------------------------------------------------
649 : // relStep
650 : /// Compute relative step from absolute step
651 : /// which depends on actual clef. Step 0 starts on the
652 : /// first (top) staff line.
653 : //---------------------------------------------------------
654 :
655 60344 : int relStep(int line, ClefType clef)
656 : {
657 60344 : return ClefInfo::pitchOffset(clef) - line;
658 : }
659 :
660 0 : int relStep(int pitch, int tpc, ClefType clef)
661 : {
662 0 : return relStep(absStep(tpc, pitch), clef);
663 : }
664 :
665 : //---------------------------------------------------------
666 : // pitch2step
667 : // returns one of { 0, 1, 2, 3, 4, 5, 6 }
668 : //---------------------------------------------------------
669 :
670 0 : int pitch2step(int pitch)
671 : {
672 : // C C# D D# E F F# G G# A A# B
673 : static const char tab[12] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
674 0 : return tab[pitch % 12];
675 : }
676 :
677 : //---------------------------------------------------------
678 : // step2pitch
679 : // returns one of { 0, 2, 4, 5, 7, 9, 11 }
680 : //---------------------------------------------------------
681 :
682 3235 : int step2pitch(int step)
683 : {
684 : static const char tab[7] = { 0, 2, 4, 5, 7, 9, 11 };
685 3235 : return tab[step % 7];
686 : }
687 :
688 : //---------------------------------------------------------
689 : // convertLine
690 : // find the line in clefF corresponding to lineL2 in clefR
691 : //---------------------------------------------------------
692 :
693 22 : int convertLine(int lineL2, ClefType clefL, ClefType clefR)
694 : {
695 22 : int lineR2 = lineL2;
696 22 : int goalpitch = line2pitch(lineL2, clefL, Key::C);
697 : int p;
698 22 : while ((p = line2pitch(lineR2, clefR, Key::C)) > goalpitch && p < MAX_PITCH) {
699 0 : lineR2++;
700 : }
701 22 : while ((p = line2pitch(lineR2, clefR, Key::C)) < goalpitch && p > MIN_PITCH) {
702 0 : lineR2--;
703 : }
704 22 : return lineR2;
705 : }
706 :
707 : //---------------------------------------------------------
708 : // convertLine
709 : // find the line in clef for NoteL corresponding to lineL2 in clef for noteR
710 : // for example middle C is line 10 in Treble clef, but is line -2 in Bass clef.
711 : //---------------------------------------------------------
712 :
713 22 : int convertLine(int lineL2, const Note* noteL, const Note* noteR)
714 : {
715 44 : return convertLine(lineL2,
716 22 : noteL->chord()->staff()->clef(noteL->chord()->tick()),
717 44 : noteR->chord()->staff()->clef(noteR->chord()->tick()));
718 : }
719 :
720 : //---------------------------------------------------------
721 : // chromaticPitchSteps -- an articulation such as a trill, or mordant consists of several notes
722 : // played in succession. The pitch offsets of each such note in the sequence can be represented either
723 : // as a number of steps in the diatonic scale, or in half steps as on a piano keyboard.
724 : // this function, articulationExcursion, takes deltastep indicating the number of steps in the
725 : // diatonic scale, and calculates (and returns) the number of half steps, taking several things into account.
726 : // E.g., the key signature, a trill from e to f, is to be understood as a trill between E and F# if we are
727 : // in the key of G.
728 : // E.g., if previously (looking backward in time) in the same measure there is another note on the same
729 : // staff line/space, and that note has an accidental (sharp,flat,natural,etc), then we want to match that
730 : // tone exactly.
731 : // E.g., If there are multiple notes on the same line/space, then we only consider the most
732 : // recent one, but avoid looking forward in time after the current note.
733 : // E.g., Also if there is an accidental // on a note one (or more) octaves above or below we
734 : // observe its accidental as well.
735 : // E.g., Still another case is that if two staves are involved (such as a glissando between two
736 : // notes on different staves) then we have to search both staves for the most recent accidental.
737 : //
738 : // noteL is the note to measure the deltastep from, i.e., ornaments are w.r.t. this note
739 : // noteR is the note to search backward from to find accidentals.
740 : // for ornament calculation noteL and noteR are the same, but for glissando they are
741 : // the start and end note of glissando.
742 : // nominalDiatonicSteps is the desired number of diatonic steps between the base note and this articulation step.
743 : //---------------------------------------------------------
744 :
745 26 : int chromaticPitchSteps(const Note* noteL, const Note* noteR, const int nominalDiatonicSteps)
746 : {
747 26 : if (0 == nominalDiatonicSteps) {
748 4 : return 0;
749 : }
750 22 : Chord* chordL = noteL->chord();
751 22 : Chord* chordR = noteR->chord();
752 22 : int epitchL = noteL->epitch();
753 22 : Fraction tickL = chordL->tick();
754 : // we cannot use staffL = chord->staff() because that won't correspond to the noteL->line()
755 : // in the case the user has pressed Shift-Cmd->Up or Shift-Cmd-Down.
756 : // Therefore we have to take staffMove() into account using vStaffIdx().
757 22 : Staff* staffL = noteL->score()->staff(chordL->vStaffIdx());
758 22 : ClefType clefL = staffL->clef(tickL);
759 : // line represents the ledger line of the staff. 0 is the top line, 1, is the space between the top 2 lines,
760 : // ... 8 is the bottom line.
761 22 : int lineL = noteL->line();
762 22 : if (lineL == INVALID_LINE) {
763 4 : int absLine = absStep(noteL->tpc(), noteL->epitch());
764 4 : ClefType clef = noteL->staff()->clef(noteL->tick());
765 4 : lineL = relStep(absLine, clef);
766 : }
767 :
768 : // we use line - deltastep, because lines are oriented from top to bottom, while step is oriented from bottom to top.
769 22 : int lineL2 = lineL - nominalDiatonicSteps;
770 22 : Measure* measureR = chordR->segment()->measure();
771 :
772 22 : Segment* segment = noteL->chord()->segment();
773 22 : int lineR2 = convertLine(lineL2, noteL, noteR);
774 : // is there another note in this segment on the same line?
775 : // if so, use its pitch exactly.
776 22 : int halfsteps = 0;
777 22 : staff_idx_t staffIdx = noteL->chord()->staff()->idx(); // cannot use staffL->idx() because of staffMove()
778 22 : track_idx_t startTrack = staffIdx * VOICES;
779 22 : track_idx_t endTrack = startTrack + VOICES;
780 22 : bool done = false;
781 110 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
782 88 : EngravingItem* e = segment->element(track);
783 88 : if (!e || !e->isChord()) {
784 66 : continue;
785 : }
786 22 : Chord* chord = toChord(e);
787 22 : if (chord->vStaffIdx() != chordL->vStaffIdx()) {
788 0 : continue;
789 : }
790 22 : if (!done) {
791 22 : if (staffL->isPitchedStaff(segment->tick())) {
792 22 : bool error = false;
793 22 : AccidentalVal acciv2 = measureR->findAccidental(chordR->segment(), chordR->vStaffIdx(), lineR2, error);
794 22 : int acci2 = int(acciv2);
795 : // epitch (effective pitch) is a visible pitch so line2pitch returns exactly that.
796 22 : halfsteps = line2pitch(lineL - nominalDiatonicSteps, clefL, Key::C) + acci2 - epitchL;
797 : } else {
798 : // cannot rely on accidentals or key signatures
799 0 : halfsteps = nominalDiatonicSteps;
800 : }
801 : }
802 : }
803 22 : return halfsteps;
804 : }
805 :
806 0 : static void noteValToEffectivePitchAndTpc(const NoteVal& nval, const Staff* staff, const Fraction& tick, int& epitch, int& tpc)
807 : {
808 0 : const bool concertPitch = staff->concertPitch();
809 :
810 0 : if (concertPitch) {
811 0 : epitch = nval.pitch;
812 : } else {
813 0 : const int pitchOffset = staff->originPart(tick)->instrument(tick)->transpose().chromatic;
814 0 : epitch = nval.pitch - pitchOffset;
815 : }
816 :
817 0 : tpc = nval.tpc(concertPitch);
818 0 : if (tpc == static_cast<int>(mu::engraving::Tpc::TPC_INVALID)) {
819 0 : tpc = pitch2tpc(epitch, staff->key(tick), mu::engraving::Prefer::NEAREST);
820 : }
821 0 : }
822 :
823 0 : int noteValToLine(const NoteVal& nval, const Staff* staff, const Fraction& tick)
824 : {
825 0 : if (staff->isDrumStaff(tick)) {
826 0 : const Drumset* drumset = staff->originPart(tick)->instrument(tick)->drumset();
827 0 : if (drumset) {
828 0 : return drumset->line(nval.pitch);
829 : }
830 : }
831 :
832 0 : if (nval.isRest()) {
833 0 : return staff->middleLine(tick);
834 : }
835 :
836 0 : int epitch = nval.pitch;
837 0 : int tpc = static_cast<int>(mu::engraving::Tpc::TPC_INVALID);
838 0 : noteValToEffectivePitchAndTpc(nval, staff, tick, epitch, tpc);
839 :
840 0 : return relStep(epitch, tpc, staff->clef(tick));
841 : }
842 :
843 0 : AccidentalVal noteValToAccidentalVal(const NoteVal& nval, const Staff* staff, const Fraction& tick)
844 : {
845 0 : if (nval.isRest()) {
846 0 : return AccidentalVal::NATURAL;
847 : }
848 :
849 0 : if (staff->isDrumStaff(tick)) {
850 0 : return AccidentalVal::NATURAL;
851 : }
852 :
853 0 : int epitch = nval.pitch;
854 0 : int tpc = static_cast<int>(mu::engraving::Tpc::TPC_INVALID);
855 0 : noteValToEffectivePitchAndTpc(nval, staff, tick, epitch, tpc);
856 :
857 0 : return tpc2alter(tpc);
858 : }
859 :
860 4620 : int compareNotesPos(const Note* n1, const Note* n2)
861 : {
862 4620 : if (n1->line() != n2->line() && !(n1->staffType()->isTabStaff())) {
863 4526 : return n2->line() - n1->line();
864 94 : } else if (n1->string() != n2->string()) {
865 90 : return n2->string() - n1->string();
866 : } else {
867 4 : return n1->pitch() - n2->pitch();
868 : }
869 : }
870 :
871 : //---------------------------------------------------------
872 : // skipTuplet
873 : // return segment of rightmost chord/rest in a
874 : // (possible nested) tuplet
875 : //---------------------------------------------------------
876 :
877 563 : Segment* skipTuplet(Tuplet* tuplet)
878 : {
879 563 : DurationElement* nde = tuplet->elements().back();
880 578 : while (nde->isTuplet()) {
881 15 : tuplet = toTuplet(nde);
882 15 : nde = tuplet->elements().back();
883 : }
884 563 : return toChordRest(nde)->segment();
885 : }
886 :
887 : //---------------------------------------------------------
888 : // timeSigSymIdsFromString
889 : // replace ascii with bravura symbols
890 : //---------------------------------------------------------
891 :
892 14934 : SymIdList timeSigSymIdsFromString(const String& string, TimeSigStyle timeSigStyle)
893 : {
894 : static const std::map<Char, SymId> dict = {
895 : { '+', SymId::timeSigPlusSmall },
896 : { '0', SymId::timeSig0 },
897 : { '1', SymId::timeSig1 },
898 : { '2', SymId::timeSig2 },
899 : { '3', SymId::timeSig3 },
900 : { '4', SymId::timeSig4 },
901 : { '5', SymId::timeSig5 },
902 : { '6', SymId::timeSig6 },
903 : { '7', SymId::timeSig7 },
904 : { '8', SymId::timeSig8 },
905 : { '9', SymId::timeSig9 },
906 : { 'C', SymId::timeSigCommon },
907 : { '(', SymId::timeSigParensLeftSmall },
908 : { ')', SymId::timeSigParensRightSmall },
909 : { '[', SymId::timeSigBracketLeftSmall },
910 : { ']', SymId::timeSigBracketRightSmall },
911 : { u'¢', SymId::timeSigCutCommon },
912 : { u'½', SymId::timeSigFractionHalf },
913 : { u'¼', SymId::timeSigFractionQuarter },
914 : { '*', SymId::timeSigMultiply },
915 : { 'X', SymId::timeSigMultiply },
916 : { 'x', SymId::timeSigMultiply },
917 : { u'×', SymId::timeSigMultiply },
918 : { 59664, SymId::mensuralProlation1 },
919 : { 79, SymId::mensuralProlation2 }, // 'O'
920 : { 59665, SymId::mensuralProlation2 },
921 : { 216, SymId::mensuralProlation3 }, // 'Ø'
922 : { 59666, SymId::mensuralProlation3 },
923 : { 59667, SymId::mensuralProlation4 },
924 : { 59668, SymId::mensuralProlation5 },
925 : { 59669, SymId::mensuralProlation6 },
926 : { 59670, SymId::mensuralProlation7 },
927 : { 59671, SymId::mensuralProlation8 },
928 : { 59672, SymId::mensuralProlation9 },
929 : { 59673, SymId::mensuralProlation10 },
930 : { 59674, SymId::mensuralProlation11 },
931 14962 : };
932 :
933 : static const std::map<Char, SymId> dictLarge = {
934 : { '+', SymId::timeSigPlusSmallLarge },
935 : { '0', SymId::timeSig0Large },
936 : { '1', SymId::timeSig1Large },
937 : { '2', SymId::timeSig2Large },
938 : { '3', SymId::timeSig3Large },
939 : { '4', SymId::timeSig4Large },
940 : { '5', SymId::timeSig5Large },
941 : { '6', SymId::timeSig6Large },
942 : { '7', SymId::timeSig7Large },
943 : { '8', SymId::timeSig8Large },
944 : { '9', SymId::timeSig9Large },
945 : { 'C', SymId::timeSigCommonLarge },
946 : { '(', SymId::timeSigParensLeftSmallLarge },
947 : { ')', SymId::timeSigParensRightSmallLarge },
948 : { '[', SymId::timeSigBracketLeftSmallLarge },
949 : { ']', SymId::timeSigBracketRightSmallLarge },
950 : { u'¢', SymId::timeSigCutCommonLarge },
951 : { u'½', SymId::timeSigFractionHalfLarge },
952 : { u'¼', SymId::timeSigFractionQuarterLarge },
953 : { '*', SymId::timeSigMultiplyLarge },
954 : { 'X', SymId::timeSigMultiplyLarge },
955 : { 'x', SymId::timeSigMultiplyLarge },
956 : { u'×', SymId::timeSigMultiplyLarge },
957 14962 : };
958 :
959 : static const std::map<Char, SymId> dictNarrow = {
960 : { '+', SymId::timeSigPlusSmallNarrow },
961 : { '0', SymId::timeSig0Narrow },
962 : { '1', SymId::timeSig1Narrow },
963 : { '2', SymId::timeSig2Narrow },
964 : { '3', SymId::timeSig3Narrow },
965 : { '4', SymId::timeSig4Narrow },
966 : { '5', SymId::timeSig5Narrow },
967 : { '6', SymId::timeSig6Narrow },
968 : { '7', SymId::timeSig7Narrow },
969 : { '8', SymId::timeSig8Narrow },
970 : { '9', SymId::timeSig9Narrow },
971 : { 'C', SymId::timeSigCommonNarrow },
972 : { '(', SymId::timeSigParensLeftSmallNarrow },
973 : { ')', SymId::timeSigParensRightSmallNarrow },
974 : { '[', SymId::timeSigBracketLeftSmallNarrow },
975 : { ']', SymId::timeSigBracketRightSmallNarrow },
976 : { u'¢', SymId::timeSigCutCommonNarrow },
977 : { u'½', SymId::timeSigFractionHalfNarrow },
978 : { u'¼', SymId::timeSigFractionQuarterNarrow },
979 : { '*', SymId::timeSigMultiplyNarrow },
980 : { 'X', SymId::timeSigMultiplyNarrow },
981 : { 'X', SymId::timeSigMultiplyNarrow },
982 : { u'×', SymId::timeSigMultiplyNarrow },
983 14962 : };
984 :
985 14934 : SymIdList list;
986 30082 : for (size_t i = 0; i < string.size(); ++i) {
987 15148 : SymId sym = muse::value(
988 : timeSigStyle == TimeSigStyle::NARROW ? dictNarrow
989 : : timeSigStyle == TimeSigStyle::LARGE ? dictLarge
990 : : dict,
991 15148 : string.at(i), SymId::noSym);
992 15148 : if (sym != SymId::noSym) {
993 15138 : list.push_back(sym);
994 : }
995 : }
996 14934 : return list;
997 0 : }
998 :
999 : //---------------------------------------------------------
1000 : // actualTicks
1001 : //---------------------------------------------------------
1002 :
1003 7173 : Fraction actualTicks(Fraction duration, Tuplet* tuplet, Fraction timeStretch)
1004 : {
1005 7173 : Fraction f = duration / timeStretch;
1006 7454 : for (Tuplet* t = tuplet; t; t = t->tuplet()) {
1007 281 : f /= t->ratio();
1008 : }
1009 7173 : return f;
1010 : }
1011 :
1012 0 : bool dragPositionToMeasure(const PointF& pos, const Score* score,
1013 : Measure** measure, staff_idx_t* staffIdx,
1014 : const double spacingFactor)
1015 : {
1016 0 : const System* preferredSystem = (*measure) ? (*measure)->system() : nullptr;
1017 :
1018 0 : MeasureBase* mb = score->searchMeasureBase(pos, /*scanMeasuresOnly*/ true, preferredSystem, spacingFactor);
1019 0 : Measure* m = mb && mb->isMeasure() ? toMeasure(mb) : nullptr;
1020 0 : if (!m) {
1021 0 : return false;
1022 : }
1023 :
1024 0 : const System* system = m->system();
1025 0 : const double y = pos.y() - system->canvasPos().y();
1026 0 : const staff_idx_t i = system->searchStaff(y, *staffIdx, spacingFactor);
1027 0 : if (!score->staff(i)) {
1028 0 : return false;
1029 : }
1030 :
1031 0 : *measure = m;
1032 0 : *staffIdx = i;
1033 0 : return true;
1034 : }
1035 :
1036 0 : bool dragPositionToSegment(const PointF& pos, const Measure* measure, const staff_idx_t staffIdx,
1037 : Segment** segment,
1038 : const double spacingFactor, const bool allowTimeAnchor)
1039 : {
1040 0 : const track_idx_t strack = staffIdx * VOICES;
1041 0 : const track_idx_t etrack = strack + VOICES;
1042 :
1043 0 : const double x = pos.x() - measure->canvasPos().x();
1044 0 : const SegmentType st = allowTimeAnchor ? SegmentType::Duration : SegmentType::ChordRest;
1045 0 : Segment* s = measure->searchSegment(x, st, strack, etrack, *segment, spacingFactor);
1046 0 : if (!s) {
1047 0 : return false;
1048 : }
1049 :
1050 0 : *segment = segmentOrChordRestSegmentAtSameTick(s);
1051 0 : return true;
1052 : }
1053 :
1054 0 : Segment* segmentOrChordRestSegmentAtSameTick(Segment* segment)
1055 : {
1056 0 : IF_ASSERT_FAILED(segment) {
1057 0 : return nullptr;
1058 : }
1059 :
1060 : // If TimeTick and ChordRest segments are at the same tick, prefer ChordRest
1061 0 : if (segment->isTimeTickType() && segment->measure()) {
1062 0 : if (Segment* crSegAtSameTick = segment->measure()->findSegmentR(SegmentType::ChordRest, segment->rtick())) {
1063 0 : return crSegAtSameTick;
1064 : }
1065 : }
1066 :
1067 0 : return segment;
1068 : }
1069 :
1070 494 : double yStaffDifference(const System* system1, const System* system2, staff_idx_t staffIdx)
1071 : {
1072 494 : if (!system1 || !system2) {
1073 0 : return 0.0;
1074 : }
1075 :
1076 494 : const SysStaff* staff1 = system1->staff(staffIdx);
1077 494 : const SysStaff* staff2 = system2->staff(staffIdx);
1078 494 : if (!staff1 || !staff2) {
1079 24 : return 0.0;
1080 : }
1081 470 : return staff1->y() - staff2->y();
1082 : }
1083 :
1084 29 : bool allowRemoveWhenRemovingStaves(EngravingItem* item, staff_idx_t startStaff, staff_idx_t endStaff)
1085 : {
1086 : // Sanity checks
1087 29 : if (!item || item->staffIdx() == muse::nidx || startStaff == muse::nidx || endStaff == muse::nidx) {
1088 0 : return false;
1089 : }
1090 :
1091 29 : Score* score = item->score();
1092 29 : if (score->nstaves() == 1) {
1093 6 : return true;
1094 : }
1095 :
1096 23 : if (endStaff == 0) { // Default initialized
1097 12 : endStaff = startStaff + 1;
1098 : }
1099 :
1100 23 : staff_idx_t staffIdx = item->staffIdx();
1101 23 : if (staffIdx < startStaff || staffIdx >= endStaff) {
1102 8 : return false;
1103 : }
1104 :
1105 15 : Staff* nextRemaining = score->staff(endStaff);
1106 15 : bool nextRemainingIsSystemObjectStaff = nextRemaining && nextRemaining->isSystemObjectStaff();
1107 15 : if (item->isTopSystemObject() && !nextRemainingIsSystemObjectStaff) {
1108 0 : return false;
1109 : }
1110 :
1111 15 : return true;
1112 : }
1113 :
1114 217 : bool moveDownWhenAddingStaves(EngravingItem* item, staff_idx_t startStaff, staff_idx_t endStaff)
1115 : {
1116 : // Sanity checks
1117 217 : if (!item || item->staffIdx() == muse::nidx || startStaff == muse::nidx || endStaff == muse::nidx) {
1118 0 : return false;
1119 : }
1120 :
1121 217 : if (item->staffIdx() < startStaff) {
1122 195 : return false;
1123 : }
1124 :
1125 22 : if (endStaff == 0) { // Default initialized
1126 10 : endStaff = startStaff + 1;
1127 : }
1128 :
1129 22 : Score* score = item->score();
1130 22 : Staff* nextAfterInserted = score->staff(endStaff);
1131 22 : bool nextAfterInsertedIsSystemObjectStaff = nextAfterInserted && nextAfterInserted->isSystemObjectStaff();
1132 22 : if (item->isTopSystemObject() && !nextAfterInsertedIsSystemObjectStaff) {
1133 12 : return false;
1134 : }
1135 :
1136 10 : return true;
1137 : }
1138 :
1139 97749 : void collectChordsAndRest(Segment* segment, staff_idx_t staffIdx, std::vector<Chord*>& chords, std::vector<Rest*>& rests)
1140 : {
1141 97749 : if (!segment) {
1142 0 : return;
1143 : }
1144 :
1145 97749 : track_idx_t startTrack = staffIdx * VOICES;
1146 97749 : track_idx_t endTrack = startTrack + VOICES;
1147 :
1148 488745 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
1149 390996 : EngravingItem* e = segment->element(track);
1150 390996 : if (!e) {
1151 296217 : continue;
1152 : }
1153 94779 : if (e->isChord() && !toChordRest(e)->staffMove()) {
1154 51178 : chords.push_back(toChord(e));
1155 43601 : } else if (e->isRest() && !toChordRest(e)->staffMove() && !toRest(e)->isFullMeasureRest()) {
1156 10740 : rests.push_back(toRest(e));
1157 : }
1158 : }
1159 : }
1160 :
1161 10083 : void collectChordsOverlappingRests(Segment* segment, staff_idx_t staffIdx, std::vector<Chord*>& chords)
1162 : {
1163 : // Check if previous segments contain chords in other voices
1164 : // whose duration overlaps with rests on this segment
1165 :
1166 10083 : track_idx_t startTrack = staffIdx * VOICES;
1167 10083 : track_idx_t endTrack = startTrack + VOICES;
1168 :
1169 10083 : std::set<track_idx_t> tracksToCheck;
1170 50415 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
1171 40332 : EngravingItem* item = segment->element(track);
1172 40332 : if (!item || !item->isRest()) {
1173 29584 : tracksToCheck.insert(track);
1174 : }
1175 : }
1176 :
1177 10083 : Fraction curTick = segment->rtick();
1178 48108 : for (Segment* prevSeg = segment->prev(); prevSeg; prevSeg = prevSeg->prev()) {
1179 38025 : if (!prevSeg->isChordRestType()) {
1180 9734 : continue;
1181 : }
1182 28291 : Fraction prevSegTick = prevSeg->rtick();
1183 111458 : for (track_idx_t track : tracksToCheck) {
1184 83167 : EngravingItem* e = prevSeg->element(track);
1185 83167 : if (!e || !e->isChord()) {
1186 83067 : continue;
1187 : }
1188 1156 : Chord* chord = toChord(e);
1189 1156 : Fraction chordEndTick = prevSegTick + chord->actualTicks();
1190 1156 : if (chordEndTick <= curTick) {
1191 1056 : continue;
1192 : }
1193 :
1194 100 : chords.push_back(chord);
1195 : }
1196 : }
1197 10083 : }
1198 :
1199 89 : std::vector<EngravingItem*> collectSystemObjects(const Score* score, const std::vector<Staff*>& staves)
1200 : {
1201 91 : TRACEFUNC;
1202 :
1203 89 : std::vector<EngravingItem*> result;
1204 :
1205 89 : const TimeSigPlacement timeSigPlacement = score->style().styleV(Sid::timeSigPlacement).value<TimeSigPlacement>();
1206 89 : const bool isOnStaffTimeSig = timeSigPlacement != TimeSigPlacement::NORMAL;
1207 :
1208 129 : for (const Measure* measure = score->firstMeasure(); measure; measure = measure->nextMeasure()) {
1209 40 : for (EngravingItem* measureElement : measure->el()) {
1210 0 : if (!measureElement || !measureElement->systemFlag() || measureElement->isLayoutBreak()) {
1211 0 : continue;
1212 : }
1213 0 : if (!staves.empty()) {
1214 0 : if (muse::contains(staves, measureElement->staff())) {
1215 0 : result.push_back(measureElement);
1216 : }
1217 0 : } else if (measureElement->isTopSystemObject()) {
1218 0 : result.push_back(measureElement);
1219 : }
1220 : }
1221 :
1222 136 : for (const Segment& seg : measure->segments()) {
1223 96 : if (seg.isType(SegmentType::Duration | SegmentType::EndBarLine)) {
1224 71 : for (EngravingItem* annotation : seg.annotations()) {
1225 7 : if (!annotation || !annotation->systemFlag()) {
1226 0 : continue;
1227 : }
1228 :
1229 7 : if (!staves.empty()) {
1230 4 : if (muse::contains(staves, annotation->staff())) {
1231 2 : result.push_back(annotation);
1232 : }
1233 3 : } else if (annotation->isTopSystemObject()) {
1234 3 : result.push_back(annotation);
1235 : }
1236 : }
1237 : }
1238 :
1239 96 : if (isOnStaffTimeSig && seg.isType(SegmentType::TimeSigTypes)) {
1240 0 : for (EngravingItem* item : seg.elist()) {
1241 0 : if (!item || !item->isTimeSig()) {
1242 0 : continue;
1243 : }
1244 :
1245 0 : if (!staves.empty()) {
1246 0 : if (muse::contains(staves, item->staff())) {
1247 0 : result.push_back(item);
1248 : }
1249 0 : } else if (item->staffIdx() == 0) {
1250 0 : result.push_back(item);
1251 : }
1252 : }
1253 : }
1254 : }
1255 : }
1256 :
1257 89 : for (const auto& pair : score->spanner()) {
1258 0 : Spanner* spanner = pair.second;
1259 0 : if (!spanner->systemFlag()) {
1260 0 : continue;
1261 : }
1262 :
1263 0 : if (!staves.empty()) {
1264 0 : if (muse::contains(staves, spanner->staff())) {
1265 0 : result.push_back(spanner);
1266 : }
1267 0 : } else if (spanner->isTopSystemObject()) {
1268 0 : result.push_back(spanner);
1269 : }
1270 : }
1271 :
1272 89 : return result;
1273 89 : }
1274 :
1275 13776 : std::unordered_set<EngravingItem*> collectElementsAnchoredToChordRest(const ChordRest* cr)
1276 : {
1277 13776 : std::unordered_set<EngravingItem*> elems;
1278 13806 : for (EngravingItem* lyric : cr->lyrics()) {
1279 30 : elems.emplace(lyric);
1280 : }
1281 13776 : if (!cr->isChord()) {
1282 5187 : return elems;
1283 : }
1284 8589 : const Chord* chord = toChord(cr);
1285 8589 : if (Arpeggio* arp = chord->arpeggio()) {
1286 5 : elems.emplace(arp);
1287 : }
1288 8589 : if (TremoloTwoChord* tremTwo = chord->tremoloTwoChord()) {
1289 6 : elems.emplace(tremTwo);
1290 : }
1291 8589 : if (TremoloSingleChord* tremSing = chord->tremoloSingleChord()) {
1292 5 : elems.emplace(tremSing);
1293 : }
1294 8825 : for (Articulation* art : chord->articulations()) {
1295 236 : elems.emplace(art);
1296 : }
1297 : // Chord brackets and chord lines
1298 8592 : for (EngravingItem* e : chord->el()) {
1299 3 : elems.emplace(e);
1300 : }
1301 8646 : for (Chord* grace : chord->graceNotes()) {
1302 57 : elems.emplace(grace);
1303 57 : for (Articulation* gArt : grace->articulations()) {
1304 0 : elems.emplace(gArt);
1305 : }
1306 57 : if (TremoloSingleChord* gTremSing = grace->tremoloSingleChord()) {
1307 0 : elems.emplace(gTremSing);
1308 : }
1309 : }
1310 8589 : return elems;
1311 0 : }
1312 :
1313 9985 : std::unordered_set<EngravingItem*> collectElementsAnchoredToNote(const Note* note, bool includeForwardTiesSpanners,
1314 : bool includeBackwardTiesSpanners)
1315 : {
1316 9985 : std::unordered_set<EngravingItem*> elems;
1317 9985 : LaissezVib* lv = note->laissezVib();
1318 9985 : if (lv && !lv->segmentsEmpty()) {
1319 4 : elems.emplace(lv);
1320 : }
1321 9985 : PartialTie* ipt = note->incomingPartialTie();
1322 9985 : if (ipt && !ipt->segmentsEmpty()) {
1323 15 : elems.emplace(ipt);
1324 : }
1325 9985 : PartialTie* opt = note->outgoingPartialTie();
1326 9985 : if (opt && !opt->segmentsEmpty()) {
1327 13 : elems.emplace(opt);
1328 : }
1329 : // The following is a bit of a hack - addressing properly would require a fingering rework...
1330 9989 : for (EngravingItem* elem : note->el()) {
1331 4 : if (elem->isFingering()) {
1332 4 : elems.emplace(elem);
1333 : }
1334 : }
1335 9985 : if (includeForwardTiesSpanners) {
1336 9985 : Tie* tieFor = note->tieFor();
1337 9985 : if (tieFor && !tieFor->segmentsEmpty()) {
1338 1770 : elems.emplace(tieFor);
1339 : }
1340 10020 : for (Spanner* sp : note->spannerFor()) {
1341 35 : if (sp->segmentsEmpty()) {
1342 0 : continue;
1343 : }
1344 35 : elems.emplace(sp);
1345 : }
1346 : }
1347 9985 : if (includeBackwardTiesSpanners) {
1348 0 : Tie* tieBack = note->tieBack();
1349 0 : if (tieBack && !tieBack->segmentsEmpty()) {
1350 0 : elems.emplace(tieBack);
1351 : }
1352 0 : for (Spanner* sp : note->spannerBack()) {
1353 0 : if (sp->segmentsEmpty()) {
1354 0 : continue;
1355 : }
1356 0 : elems.emplace(sp);
1357 : }
1358 : }
1359 9985 : const NoteParenthesisInfo* noteParenInfo = note->parenthesisInfo();
1360 9985 : if (noteParenInfo && noteParenInfo->notes().size()) {
1361 0 : elems.emplace(noteParenInfo->leftParen());
1362 0 : elems.emplace(noteParenInfo->rightParen());
1363 : }
1364 9985 : return elems;
1365 0 : }
1366 :
1367 1788 : bool noteAnchoredSpannerIsInRange(const Spanner* spanner, const Fraction& rangeStart, const Fraction& rangeEnd)
1368 : {
1369 1788 : IF_ASSERT_FAILED(rangeStart < rangeEnd) {
1370 0 : return false;
1371 : }
1372 1788 : const EngravingItem* startElement = spanner->startElement();
1373 1788 : const EngravingItem* endElement = spanner->endElement();
1374 1788 : IF_ASSERT_FAILED(startElement && startElement->isNote() && endElement && endElement->isNote()) {
1375 0 : LOGD() << "Cannot calculate isInRange - might be a partial tie or laissez vibrer";
1376 0 : return false;
1377 : }
1378 1788 : const Note* startNote = toNote(startElement);
1379 1788 : const Segment* startSeg = startNote->chord()->segment();
1380 1788 : if (startSeg->tick() < rangeStart) {
1381 0 : return false;
1382 : }
1383 1788 : const Note* endNote = toNote(endElement);
1384 1788 : const Segment* endSeg = endNote->chord()->segment();
1385 1788 : return !endSeg || endSeg->tick() <= rangeEnd;
1386 : }
1387 :
1388 17 : String formatUniqueExcerptName(const String& baseName, const StringList& allExcerptLowerNames)
1389 : {
1390 17 : String result = baseName;
1391 17 : int num = 0;
1392 :
1393 17 : while (allExcerptLowerNames.contains(result.toLower())) {
1394 0 : result = baseName + String(u" (%1)").arg(++num);
1395 : }
1396 :
1397 17 : return result;
1398 0 : }
1399 :
1400 13 : bool isFirstSystemKeySig(const KeySig* ks)
1401 : {
1402 13 : if (!ks) {
1403 0 : return false;
1404 : }
1405 13 : const System* sys = ks->measure()->system();
1406 13 : if (!sys) {
1407 0 : return false;
1408 : }
1409 13 : return ks->tick() == sys->firstMeasure()->tick();
1410 : }
1411 :
1412 1224 : String bendAmountToString(int fulls, int quarts, bool useFractions)
1413 : {
1414 1224 : String string = fulls != 0 ? String::number(fulls) : String();
1415 :
1416 1224 : if (useFractions) {
1417 1224 : switch (std::abs(quarts)) {
1418 118 : case 1:
1419 118 : string += u"\u00BC";
1420 118 : break;
1421 271 : case 2:
1422 271 : string += u"\u00BD";
1423 271 : break;
1424 13 : case 3:
1425 13 : string += u"\u00BE";
1426 13 : break;
1427 822 : default:
1428 822 : break;
1429 : }
1430 : } else {
1431 0 : if (!string.empty() && quarts != 0) {
1432 0 : string += u" ";
1433 : }
1434 0 : switch (std::abs(quarts)) {
1435 0 : case 1:
1436 0 : string += u"1/4";
1437 0 : break;
1438 0 : case 2:
1439 0 : string += u"1/2";
1440 0 : break;
1441 0 : case 3:
1442 0 : string += u"3/4";
1443 0 : break;
1444 0 : default:
1445 0 : break;
1446 : }
1447 : }
1448 :
1449 1224 : return string;
1450 0 : }
1451 :
1452 1009 : InstrumentTrackId makeInstrumentTrackId(const EngravingItem* item)
1453 : {
1454 1009 : const Part* part = item->part();
1455 1009 : if (!part) {
1456 0 : return InstrumentTrackId();
1457 : }
1458 :
1459 : InstrumentTrackId trackId {
1460 : part->id(),
1461 0 : part->instrumentId(item->tick())
1462 1009 : };
1463 :
1464 1009 : return trackId;
1465 1009 : }
1466 :
1467 36644 : std::vector<Measure*> findFollowingRepeatMeasures(const Measure* measure)
1468 : {
1469 36644 : const MasterScore* master = measure->masterScore();
1470 36644 : const Score* score = measure->score();
1471 :
1472 36644 : const Measure* masterMeasure = master->tick2measureMM(measure->tick());
1473 :
1474 36644 : const RepeatList& repeatList = master->repeatList(true, false);
1475 :
1476 36644 : std::vector<Measure*> measures;
1477 :
1478 82442 : for (auto it = repeatList.begin(); it != repeatList.end(); it++) {
1479 45798 : const RepeatSegment* rs = *it;
1480 45798 : const auto nextSegIt = std::next(it);
1481 45798 : if (!rs->endsWithMeasure(masterMeasure) || nextSegIt == repeatList.end()) {
1482 43932 : continue;
1483 : }
1484 :
1485 : // Get next segment
1486 1866 : const RepeatSegment* nextSeg = *nextSegIt;
1487 1866 : const Measure* firstMasterMeasure = nextSeg->firstMeasure();
1488 1866 : Measure* firstMeasure = firstMasterMeasure ? score->tick2measureMM(firstMasterMeasure->tick()) : nullptr;
1489 1866 : if (!firstMeasure) {
1490 0 : continue;
1491 : }
1492 :
1493 1866 : measures.push_back(firstMeasure);
1494 : }
1495 :
1496 36644 : return measures;
1497 0 : }
1498 :
1499 296 : std::vector<Measure*> findPreviousRepeatMeasures(const Measure* measure)
1500 : {
1501 296 : const MasterScore* master = measure->masterScore();
1502 296 : const Score* score = measure->score();
1503 :
1504 296 : const Measure* masterMeasure = master->tick2measureMM(measure->tick());
1505 :
1506 296 : const RepeatList& repeatList = master->repeatList(true, false);
1507 :
1508 296 : std::vector<Measure*> measures;
1509 :
1510 296 : if (repeatList.empty()) {
1511 0 : return measures;
1512 : }
1513 :
1514 393 : for (auto it = repeatList.begin() + 1; it != repeatList.end(); it++) {
1515 97 : const RepeatSegment* rs = *it;
1516 97 : const auto prevSegIt = std::prev(it);
1517 97 : if (!rs->startsWithMeasure(masterMeasure) || prevSegIt == repeatList.end()) {
1518 57 : continue;
1519 : }
1520 :
1521 : // Get next segment
1522 40 : const RepeatSegment* prevSeg = *prevSegIt;
1523 40 : const Measure* lastMasterMeasure = prevSeg->lastMeasure();
1524 40 : Measure* lastMeasure = lastMasterMeasure ? score->tick2measureMM(lastMasterMeasure->tick()) : nullptr;
1525 40 : if (!lastMeasure) {
1526 0 : continue;
1527 : }
1528 :
1529 40 : measures.push_back(lastMeasure);
1530 : }
1531 :
1532 296 : return measures;
1533 0 : }
1534 :
1535 5 : bool repeatHasPartialLyricLine(const Measure* endRepeatMeasure)
1536 : {
1537 5 : const std::vector<Measure*> measures = findFollowingRepeatMeasures(endRepeatMeasure);
1538 5 : const Score* score = endRepeatMeasure->score();
1539 :
1540 7 : for (const Measure* measure : measures) {
1541 5 : const SpannerMap::IntervalList& spanners = score->spannerMap().findOverlapping(measure->tick().ticks(), measure->endTick().ticks());
1542 :
1543 7 : for (auto& spanner : spanners) {
1544 5 : if (spanner.value->isPartialLyricsLine() && spanner.start == measure->tick().ticks()) {
1545 3 : return true;
1546 : }
1547 : }
1548 : }
1549 :
1550 2 : return false;
1551 5 : }
1552 :
1553 15124 : bool segmentsAreAdjacent(const Segment* firstSeg, const Segment* secondSeg)
1554 : {
1555 15124 : if (!firstSeg || !secondSeg) {
1556 40 : return false;
1557 : }
1558 15084 : const MasterScore* master = firstSeg->masterScore();
1559 :
1560 15084 : const Measure* firstMeasure = firstSeg->measure();
1561 15084 : const Measure* secondMeasure = secondSeg->measure();
1562 :
1563 15084 : if (firstMeasure == secondMeasure) {
1564 14030 : return true;
1565 : }
1566 :
1567 1054 : const Measure* firstMasterMeasure = master->tick2measure(firstMeasure->tick());
1568 1054 : const Measure* secondMasterMeasure = master->tick2measure(secondMeasure->tick());
1569 :
1570 1054 : if (firstMasterMeasure) {
1571 1054 : firstMasterMeasure = firstMasterMeasure->coveringMMRestOrThis();
1572 : }
1573 1054 : if (secondMasterMeasure) {
1574 1054 : secondMasterMeasure = secondMasterMeasure->coveringMMRestOrThis();
1575 : }
1576 :
1577 1054 : IF_ASSERT_FAILED(firstMasterMeasure && secondMasterMeasure) {
1578 0 : return false;
1579 : }
1580 :
1581 1054 : const Score* score = firstSeg->score();
1582 1054 : const RepeatList& repeatList = score->repeatList(true, false);
1583 :
1584 1054 : std::vector<const Measure*> measures;
1585 :
1586 1054 : bool firstMeasureSegmentFound = false;
1587 1054 : bool secondMeasureSegmentFound = false;
1588 :
1589 1206 : for (auto it = repeatList.begin(); it != repeatList.end(); it++) {
1590 1182 : const RepeatSegment* rs = *it;
1591 1182 : const auto nextSegIt = std::next(it);
1592 :
1593 : // Check if measures are in the same repeat segment
1594 1182 : bool containsFirstMeasure = rs->containsMeasure(firstMasterMeasure);
1595 1182 : bool containsSecondMeasure = rs->containsMeasure(secondMasterMeasure);
1596 1182 : firstMeasureSegmentFound |= containsFirstMeasure;
1597 1182 : secondMeasureSegmentFound |= containsSecondMeasure;
1598 :
1599 1182 : if (containsFirstMeasure && containsSecondMeasure) {
1600 1030 : return true;
1601 : }
1602 :
1603 : // Continue to build list of measures at the start of following repeat segments
1604 152 : if (!rs->endsWithMeasure(firstMasterMeasure) || nextSegIt == repeatList.end()) {
1605 80 : continue;
1606 : }
1607 :
1608 : // Get next segment
1609 72 : const RepeatSegment* nextSeg = *nextSegIt;
1610 72 : const Measure* nextSegFirstMeasure = nextSeg->firstMeasure();
1611 72 : if (!nextSegFirstMeasure) {
1612 0 : continue;
1613 : }
1614 :
1615 72 : measures.push_back(nextSegFirstMeasure);
1616 : }
1617 :
1618 : // Check if second segment is in a following measure in the repeat structure
1619 34 : for (const Measure* m : measures) {
1620 12 : if (m == secondMasterMeasure) {
1621 2 : return true;
1622 : }
1623 : }
1624 :
1625 22 : if (!firstMeasureSegmentFound && !secondMeasureSegmentFound) {
1626 : // The measures are outside of the (invalid) repeat structure
1627 0 : return firstMasterMeasure->nextMeasure() == secondMasterMeasure;
1628 : }
1629 :
1630 22 : return false;
1631 1054 : }
1632 :
1633 14437 : bool segmentsAreInDifferentRepeatSegments(const Segment* firstSeg, const Segment* secondSeg)
1634 : {
1635 14437 : if (!firstSeg || !secondSeg) {
1636 39 : return false;
1637 : }
1638 14398 : const MasterScore* master = firstSeg->masterScore();
1639 :
1640 14398 : Measure* firstMeasure = firstSeg->measure();
1641 14398 : Measure* secondMeasure = secondSeg->measure();
1642 :
1643 14398 : if (firstMeasure == secondMeasure) {
1644 13633 : return false;
1645 : }
1646 :
1647 765 : const Measure* firstMasterMeasure = master->tick2measure(firstMeasure->tick());
1648 765 : const Measure* secondMasterMeasure = master->tick2measure(secondMeasure->tick());
1649 :
1650 765 : Score* score = firstSeg->score();
1651 :
1652 765 : const RepeatList& repeatList = score->repeatList(true, false);
1653 :
1654 765 : std::vector<const Measure*> measures;
1655 :
1656 1520 : for (auto it = repeatList.begin(); it != repeatList.end(); it++) {
1657 787 : const RepeatSegment* rs = *it;
1658 :
1659 787 : if (!rs->containsMeasure(firstMasterMeasure) || !rs->containsMeasure(secondMasterMeasure)) {
1660 32 : return true;
1661 : }
1662 : }
1663 :
1664 733 : return false;
1665 765 : }
1666 :
1667 2 : bool isValidBarLineForRepeatSection(const Segment* firstSeg, const Segment* secondSeg)
1668 : {
1669 2 : if (!firstSeg || !secondSeg) {
1670 0 : return false;
1671 : }
1672 2 : if (!firstSeg->isType(SegmentType::BarLineTypes)) {
1673 2 : return false;
1674 : }
1675 :
1676 0 : const MasterScore* master = firstSeg->masterScore();
1677 :
1678 0 : Measure* firstMeasure = firstSeg->measure();
1679 0 : Measure* secondMeasure = secondSeg->measure();
1680 :
1681 0 : const Measure* firstMasterMeasure = master->tick2measure(firstMeasure->tick());
1682 0 : const Measure* secondMasterMeasure = master->tick2measure(secondMeasure->tick());
1683 0 : const Measure* adjacentMasterMeasure = firstMasterMeasure->nextMeasure();
1684 :
1685 0 : Score* score = firstSeg->score();
1686 :
1687 0 : const RepeatList& repeatList = score->repeatList(true, false);
1688 :
1689 0 : std::vector<const Measure*> measures;
1690 :
1691 0 : bool segEndsWithBl = false;
1692 0 : bool adjacentAndSecondShareSegment = false;
1693 :
1694 0 : for (auto it = repeatList.begin(); it != repeatList.end(); it++) {
1695 0 : const RepeatSegment* rs = *it;
1696 :
1697 0 : if (rs->endsWithMeasure(firstMasterMeasure)) {
1698 0 : segEndsWithBl = true;
1699 : }
1700 :
1701 0 : if (rs->startsWithMeasure(adjacentMasterMeasure) && rs->containsMeasure(secondMasterMeasure)) {
1702 0 : adjacentAndSecondShareSegment = true;
1703 : }
1704 : }
1705 :
1706 0 : return segEndsWithBl && adjacentAndSecondShareSegment;
1707 0 : }
1708 :
1709 74 : PartialLyricsLine* findPrevPartialLyricsLineDash(Lyrics* lyrics)
1710 : {
1711 74 : Score* score = lyrics->score();
1712 88 : for (auto sp : score->spannerMap().findOverlapping(lyrics->tick().ticks(), lyrics->tick().ticks())) {
1713 16 : if (!sp.value->isPartialLyricsLine() || sp.value->track() != lyrics->track()) {
1714 13 : continue;
1715 : }
1716 3 : PartialLyricsLine* partialLine = toPartialLyricsLine(sp.value);
1717 3 : if (partialLine->isEndMelisma() || partialLine->verse() != lyrics->verse() || partialLine->placement() != lyrics->placement()) {
1718 1 : continue;
1719 : }
1720 2 : return partialLine;
1721 : }
1722 :
1723 72 : return nullptr;
1724 : }
1725 :
1726 5 : MeasureBeat findBeat(const Score* score, int tick)
1727 : {
1728 5 : MeasureBeat measureBeat;
1729 5 : if (!score || !score->checkHasMeasures()) {
1730 0 : return measureBeat;
1731 : }
1732 :
1733 5 : int ticks = 0;
1734 5 : int beatIndex = 0;
1735 5 : score->sigmap()->tickValues(tick, &measureBeat.measureIndex, &beatIndex, &ticks);
1736 :
1737 5 : const TimeSigFrac timeSig = score->sigmap()->timesig(Fraction::fromTicks(tick)).timesig();
1738 5 : const int ticksB = ticks_beat(timeSig.denominator());
1739 :
1740 5 : measureBeat.beat = beatIndex + ticks / static_cast<float>(ticksB);
1741 5 : measureBeat.maxMeasureIndex = const_cast<Score*>(score)->measures()->size() - 1;
1742 5 : measureBeat.maxBeatIndex = timeSig.numerator() - 1;
1743 :
1744 5 : return measureBeat;
1745 : }
1746 :
1747 0 : bool isElementInFretBox(const EngravingItem* item)
1748 : {
1749 0 : if (item->isHarmony()) {
1750 0 : return toHarmony(item)->isInFretBox();
1751 0 : } else if (item->isFretDiagram()) {
1752 0 : return toFretDiagram(item)->isInFretBox();
1753 : }
1754 0 : return false;
1755 : }
1756 :
1757 16 : std::vector<EngravingItem*> filterTargetElements(const Selection& sel, EngravingItem* dropElement, bool& unique)
1758 : {
1759 16 : bool uniqueMeasures = false;
1760 16 : bool uniqueStaves = false;
1761 :
1762 16 : switch (dropElement->type()) {
1763 : // Brackets have a special logic for range selections.
1764 : // For other selections add only one to each staff:
1765 0 : case ElementType::BRACKET:
1766 0 : if (sel.isRange()) {
1767 0 : unique = true;
1768 0 : return { sel.startSegment()->firstElementForNavigation(sel.staffStart()) };
1769 : }
1770 0 : uniqueStaves = true;
1771 0 : break;
1772 :
1773 : // Barlines are only added to measures in range selections:
1774 0 : case ElementType::BAR_LINE:
1775 0 : uniqueMeasures = sel.isRange();
1776 0 : break;
1777 :
1778 : // For multi-measure measure repeats, avoid overlap by adding only one per staff:
1779 0 : case ElementType::MEASURE_REPEAT:
1780 0 : uniqueStaves = true;
1781 0 : uniqueMeasures = toMeasureRepeat(dropElement)->numMeasures() == 1;
1782 0 : break;
1783 :
1784 : // Add these elements once per measure in list selections, else once total:
1785 0 : case ElementType::MARKER:
1786 : case ElementType::JUMP:
1787 : case ElementType::SPACER: //! HACK - these should be applied "per staff" in range selections (see issue #33486)
1788 : case ElementType::STAFFTYPE_CHANGE: //! HACK - these should be applied "per staff" in range selections (see issue #33486)
1789 : case ElementType::VBOX:
1790 : case ElementType::HBOX:
1791 : case ElementType::TBOX:
1792 : case ElementType::FBOX:
1793 : case ElementType::MEASURE:
1794 0 : if (sel.isRange()) {
1795 0 : unique = true;
1796 0 : return { sel.startSegment()->firstElementForNavigation(sel.staffStart()) };
1797 : }
1798 0 : uniqueMeasures = true;
1799 0 : break;
1800 :
1801 : // Add these elements once per measure, always:
1802 0 : case ElementType::MEASURE_NUMBER:
1803 0 : uniqueMeasures = true;
1804 0 : break;
1805 :
1806 0 : case ElementType::ACTION_ICON: {
1807 0 : const ActionIconType actionType = toActionIcon(dropElement)->actionType();
1808 0 : switch (actionType) {
1809 0 : case ActionIconType::STAFF_TYPE_CHANGE: //! HACK - these should be applied "per staff" in range selections (see issue #33486)
1810 : case ActionIconType::VFRAME:
1811 : case ActionIconType::HFRAME:
1812 : case ActionIconType::TFRAME:
1813 : case ActionIconType::FFRAME:
1814 : case ActionIconType::MEASURE:
1815 0 : if (sel.isRange()) {
1816 0 : unique = true;
1817 0 : return { sel.startSegment()->firstElementForNavigation(sel.staffStart()) };
1818 : }
1819 0 : break;
1820 0 : default: break;
1821 : }
1822 0 : break;
1823 : }
1824 16 : default: break;
1825 : }
1826 :
1827 16 : unique = uniqueStaves || uniqueMeasures;
1828 16 : if (!unique) {
1829 16 : return sel.elements();
1830 : }
1831 :
1832 0 : std::vector<EngravingItem*> result;
1833 0 : if (uniqueStaves && uniqueMeasures) {
1834 0 : std::vector<MStaff*> foundMStaves;
1835 0 : for (EngravingItem* e : sel.elements()) {
1836 0 : if (Measure* m = e->findMeasure()) {
1837 0 : if (!muse::contains(foundMStaves, m->mstaves().at(e->staffIdx()))) {
1838 0 : result.emplace_back(e);
1839 0 : foundMStaves.emplace_back(m->mstaves().at(e->staffIdx()));
1840 : }
1841 : }
1842 : }
1843 0 : } else if (uniqueStaves) {
1844 0 : std::vector<staff_idx_t> foundStaves;
1845 0 : for (EngravingItem* e : sel.elements()) {
1846 0 : if (!muse::contains(foundStaves, e->staffIdx())) {
1847 0 : result.emplace_back(e);
1848 0 : foundStaves.emplace_back(e->staffIdx());
1849 : }
1850 : }
1851 0 : } else {
1852 0 : std::vector<MeasureBase*> foundMeasures;
1853 0 : for (EngravingItem* e : sel.elements()) {
1854 0 : if (MeasureBase* mb = e->findMeasureBase()) {
1855 0 : if (!muse::contains(foundMeasures, mb)) {
1856 0 : result.emplace_back(e);
1857 0 : foundMeasures.emplace_back(mb);
1858 : }
1859 : }
1860 : }
1861 0 : }
1862 0 : return result;
1863 : }
1864 :
1865 234 : Lyrics* searchNextLyrics(Segment* s, staff_idx_t staffIdx, int verse, PlacementV p)
1866 : {
1867 234 : Lyrics* l = nullptr;
1868 234 : const Segment* originalSeg = s;
1869 417 : while ((s = s->next1(SegmentType::ChordRest))) {
1870 412 : if (!segmentsAreAdjacent(originalSeg, s)) {
1871 1 : return nullptr;
1872 : }
1873 :
1874 411 : track_idx_t strack = staffIdx * VOICES;
1875 411 : track_idx_t etrack = strack + VOICES;
1876 : // search through all tracks of current staff looking for a lyric in specified verse
1877 1147 : for (track_idx_t track = strack; track < etrack; ++track) {
1878 964 : ChordRest* cr = toChordRest(s->element(track));
1879 964 : if (cr) {
1880 : // cr with lyrics found, but does it have a syllable in specified verse?
1881 342 : l = cr->lyrics(verse, p);
1882 342 : if (l) {
1883 228 : break;
1884 : }
1885 : }
1886 : }
1887 411 : if (l) {
1888 228 : break;
1889 : }
1890 : }
1891 233 : return l;
1892 : }
1893 :
1894 16247 : bool noteIsBefore(const Note* n1, const Note* n2)
1895 : {
1896 16247 : const int l1 = n1->line();
1897 16247 : const int l2 = n2->line();
1898 16247 : if (l1 != l2) {
1899 14716 : return l1 > l2;
1900 : }
1901 :
1902 1531 : const int p1 = n1->pitch();
1903 1531 : const int p2 = n2->pitch();
1904 1531 : if (p1 != p2) {
1905 1318 : return p1 < p2;
1906 : }
1907 :
1908 213 : if (n1->tieBack()) {
1909 0 : if (n2->tieBack() && !n2->incomingPartialTie()) {
1910 0 : const Note* sn1 = n1->tieBack()->startNote();
1911 0 : const Note* sn2 = n2->tieBack()->startNote();
1912 0 : if (sn1->chord() == sn2->chord()) {
1913 0 : return sn1->unisonIndex() < sn2->unisonIndex();
1914 : }
1915 0 : return sn1->chord()->isBefore(sn2->chord());
1916 : } else {
1917 0 : return true; // place tied notes before
1918 : }
1919 : }
1920 :
1921 213 : return false;
1922 : }
1923 :
1924 1128 : void updatePercussionNotes(Chord* c, const Drumset* drumset)
1925 : {
1926 1133 : TRACEFUNC;
1927 1140 : for (Chord* ch : c->graceNotes()) {
1928 12 : updatePercussionNotes(ch, drumset);
1929 : }
1930 1128 : std::vector<Note*> lnotes(c->notes()); // we need a copy!
1931 2305 : for (Note* note : lnotes) {
1932 1177 : if (!drumset) {
1933 0 : note->setLine(0);
1934 : } else {
1935 1177 : int pitch = note->pitch();
1936 1177 : if (!drumset->isValid(pitch)) {
1937 76 : note->setLine(0);
1938 : //! NOTE May be called too often
1939 : //LOGW("unmapped drum note %d", pitch);
1940 1101 : } else if (!note->fixed()) {
1941 1078 : note->undoChangeProperty(Pid::HEAD_GROUP, drumset->noteHead(pitch));
1942 1078 : note->setLine(drumset->line(pitch));
1943 : }
1944 : }
1945 : }
1946 1128 : }
1947 :
1948 3704 : int jianpuAugmentationLinesForDuration(const TDuration& duration)
1949 : {
1950 58 : const auto getLinesByDots = [&duration](int base) {
1951 58 : int lines = base - 1;
1952 58 : int dots = duration.dots();
1953 62 : while (dots > 0) {
1954 4 : base /= 2;
1955 4 : lines += base;
1956 4 : dots--;
1957 : }
1958 58 : return lines;
1959 3704 : };
1960 :
1961 3704 : switch (duration.type()) {
1962 1 : case DurationType::V_LONG:
1963 1 : return getLinesByDots(16);
1964 3 : case DurationType::V_BREVE:
1965 3 : return getLinesByDots(8);
1966 4 : case DurationType::V_WHOLE:
1967 4 : return getLinesByDots(4);
1968 50 : case DurationType::V_HALF:
1969 50 : return getLinesByDots(2);
1970 3646 : default:
1971 3646 : return 0;
1972 : }
1973 : }
1974 :
1975 13 : int jianpuDiminutionLinesForDuration(const TDuration& duration)
1976 : {
1977 13 : switch (duration.type()) {
1978 5 : case DurationType::V_LONG:
1979 : case DurationType::V_BREVE:
1980 : case DurationType::V_WHOLE:
1981 : case DurationType::V_HALF:
1982 : case DurationType::V_QUARTER:
1983 5 : return 0;
1984 1 : case DurationType::V_EIGHTH:
1985 1 : return 1;
1986 1 : case DurationType::V_16TH:
1987 1 : return 2;
1988 1 : case DurationType::V_32ND:
1989 1 : return 3;
1990 1 : case DurationType::V_64TH:
1991 1 : return 4;
1992 1 : case DurationType::V_128TH:
1993 1 : return 5;
1994 1 : case DurationType::V_256TH:
1995 1 : return 6;
1996 1 : case DurationType::V_512TH:
1997 1 : return 7;
1998 1 : case DurationType::V_1024TH:
1999 1 : return 8;
2000 0 : default:
2001 0 : return 0;
2002 : }
2003 : }
2004 : }
|