/
slurtielayout.cpp
1824 lines (1602 loc) · 70.8 KB
/
slurtielayout.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-License-Identifier: GPL-3.0-only
* MuseScore-CLA-applies
*
* MuseScore
* Music Composition & Notation
*
* Copyright (C) 2023 MuseScore BVBA and others
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "slurtielayout.h"
#include "iengravingfont.h"
#include "compat/dummyelement.h"
#include "dom/slur.h"
#include "dom/chord.h"
#include "dom/score.h"
#include "dom/system.h"
#include "dom/staff.h"
#include "dom/stafftype.h"
#include "dom/ledgerline.h"
#include "dom/note.h"
#include "dom/hook.h"
#include "dom/stem.h"
#include "dom/tremolo.h"
#include "dom/fretcircle.h"
#include "dom/tie.h"
#include "dom/engravingitem.h"
#include "dom/measure.h"
#include "dom/guitarbend.h"
#include "tlayout.h"
#include "chordlayout.h"
#include "tremololayout.h"
#include "../engraving/types/symnames.h"
using namespace mu::engraving;
using namespace mu::engraving::rendering::dev;
void SlurTieLayout::layout(Slur* item, LayoutContext& ctx)
{
if (item->track2() == mu::nidx) {
item->setTrack2(item->track());
}
double _spatium = item->spatium();
if (ctx.conf().isPaletteMode() || item->tick() == Fraction(-1, 1)) {
//
// when used in a palette, slur has no parent and
// tick and tick2 has no meaning so no layout is
// possible and needed
//
SlurSegment* s;
if (item->spannerSegments().empty()) {
s = new SlurSegment(ctx.mutDom().dummyParent()->system());
s->setTrack(item->track());
item->add(s);
} else {
s = item->frontSegment();
}
s->setSpannerSegmentType(SpannerSegmentType::SINGLE);
layoutSegment(s, ctx, PointF(0, 0), PointF(_spatium * 6, 0));
item->setbbox(item->frontSegment()->ldata()->bbox());
return;
}
if (item->startCR() == 0 || item->startCR()->measure() == 0) {
LOGD("track %zu-%zu %p - %p tick %d-%d null start anchor",
item->track(), item->track2(), item->startCR(), item->endCR(), item->tick().ticks(), item->tick2().ticks());
return;
}
if (item->endCR() == 0) { // sanity check
LOGD("no end CR for %d", (item->tick() + item->ticks()).ticks());
item->setEndElement(item->startCR());
item->setTick2(item->tick());
}
switch (item->slurDirection()) {
case DirectionV::UP:
item->setUp(true);
break;
case DirectionV::DOWN:
item->setUp(false);
break;
case DirectionV::AUTO:
{
//
// assumption:
// slurs have only chords or rests as start/end elements
//
if (item->startCR() == 0 || item->endCR() == 0) {
item->setUp(true);
break;
}
Measure* m1 = item->startCR()->measure();
Chord* c1 = item->startCR()->isChord() ? toChord(item->startCR()) : 0;
Chord* c2 = item->endCR()->isChord() ? toChord(item->endCR()) : 0;
item->setUp(!(item->startCR()->up()));
if ((item->endCR()->tick() - item->startCR()->tick()) > m1->ticks()) {
// long slurs are always above
item->setUp(true);
} else {
item->setUp(!(item->startCR()->up()));
}
if (c1 && c2 && isDirectionMixture(c1, c2, ctx) && (c1->noteType() == NoteType::NORMAL)) {
// slurs go above if start and end note have different stem directions,
// but grace notes are exceptions
item->setUp(true);
} else if (m1->hasVoices(item->startCR()->staffIdx(), item->tick(), item->ticks()) && c1 && c1->noteType() == NoteType::NORMAL) {
// in polyphonic passage, slurs go on the stem side
item->setUp(item->startCR()->up());
}
}
break;
}
SlurTiePos sPos;
slurPos(item, &sPos, ctx);
const std::vector<System*>& sl = ctx.dom().systems();
ciSystem is = sl.begin();
while (is != sl.end()) {
if (*is == sPos.system1) {
break;
}
++is;
}
if (is == sl.end()) {
LOGD("Slur::layout first system not found");
}
item->setPos(0, 0);
//---------------------------------------------------------
// count number of segments, if no change, all
// user offsets (drags) are retained
//---------------------------------------------------------
unsigned nsegs = 1;
for (ciSystem iis = is; iis != sl.end(); ++iis) {
if ((*iis)->vbox()) {
continue;
}
if (*iis == sPos.system2) {
break;
}
++nsegs;
}
item->fixupSegments(nsegs);
for (int i = 0; is != sl.end(); ++i, ++is) {
System* system = *is;
if (system->vbox()) {
--i;
continue;
}
SlurSegment* segment = item->segmentAt(i);
segment->setSystem(system);
// case 1: one segment
if (sPos.system1 == sPos.system2) {
segment->setSpannerSegmentType(SpannerSegmentType::SINGLE);
layoutSegment(segment, ctx, sPos.p1, sPos.p2);
}
// case 2: start segment
else if (i == 0) {
segment->setSpannerSegmentType(SpannerSegmentType::BEGIN);
double x = system->ldata()->bbox().width();
layoutSegment(segment, ctx, sPos.p1, PointF(x, sPos.p1.y()));
}
// case 3: middle segment
else if (i != 0 && system != sPos.system2) {
segment->setSpannerSegmentType(SpannerSegmentType::MIDDLE);
double x1 = system->firstNoteRestSegmentX(true);
double x2 = system->ldata()->bbox().width();
double y = item->staffIdx() > system->staves().size() ? system->y() : system->staff(item->staffIdx())->y();
layoutSegment(segment, ctx, PointF(x1, y), PointF(x2, y));
}
// case 4: end segment
else {
segment->setSpannerSegmentType(SpannerSegmentType::END);
double x = system->firstNoteRestSegmentX(true);
layoutSegment(segment, ctx, PointF(x, sPos.p2.y()), sPos.p2);
}
if (system == sPos.system2) {
break;
}
}
item->setbbox(item->spannerSegments().empty() ? RectF() : item->frontSegment()->ldata()->bbox());
}
SpannerSegment* SlurTieLayout::layoutSystem(Slur* item, System* system, LayoutContext& ctx)
{
const double horizontalTieClearance = 0.35 * item->spatium();
const double tieClearance = 0.65 * item->spatium();
const double continuedSlurOffsetY = item->spatium() * .4;
const double continuedSlurMaxDiff = 2.5 * item->spatium();
Fraction stick = system->firstMeasure()->tick();
Fraction etick = system->lastMeasure()->endTick();
SlurSegment* slurSegment = toSlurSegment(TLayout::getNextLayoutSystemSegment(item, system, [](System* parent) {
return new SlurSegment(parent);
}));
SpannerSegmentType sst;
if (item->tick() >= stick) {
//
// this is the first call to layoutSystem,
// processing the first line segment
//
if (item->track2() == mu::nidx) {
item->setTrack2(item->track());
}
if (item->startCR() == 0 || item->startCR()->measure() == 0) {
LOGD("Slur::layout(): track %zu-%zu %p - %p tick %d-%d null start anchor",
item->track(), item->track2(), item->startCR(), item->endCR(), item->tick().ticks(), item->tick2().ticks());
return slurSegment;
}
if (item->endCR() == 0) { // sanity check
item->setEndElement(item->startCR());
item->setTick2(item->tick());
}
computeUp(item, ctx);
if (item->sourceStemArrangement() != -1) {
if (item->sourceStemArrangement() != item->calcStemArrangement(item->startCR(), item->endCR())) {
// copy & paste from incompatible stem arrangement, so reset bezier points
for (int g = 0; g < (int)Grip::GRIPS; ++g) {
slurSegment->ups((Grip)g) = UP();
}
}
}
sst = item->tick2() < etick ? SpannerSegmentType::SINGLE : SpannerSegmentType::BEGIN;
} else if (item->tick() < stick && item->tick2() >= etick) {
sst = SpannerSegmentType::MIDDLE;
} else {
sst = SpannerSegmentType::END;
}
slurSegment->setSpannerSegmentType(sst);
SlurTiePos sPos;
slurPos(item, &sPos, ctx);
PointF p1, p2;
// adjust for ties
p1 = sPos.p1;
p2 = sPos.p2;
bool constrainLeftAnchor = false;
// start anchor, either on the start chordrest or at the beginning of the system
if (sst == SpannerSegmentType::SINGLE || sst == SpannerSegmentType::BEGIN) {
Chord* sc = item->startCR()->isChord() ? toChord(item->startCR()) : nullptr;
// on chord
if (sc) {
Tie* tie = (item->up() ? sc->upNote() : sc->downNote())->tieFor();
PointF endPoint = PointF();
if (tie && (tie->isInside() || tie->up() != item->up())) {
// there is a tie that starts on this chordrest
tie = nullptr;
}
if (tie && !tie->segmentsEmpty()) {
endPoint = tie->segmentAt(0)->ups(Grip::START).pos();
}
bool adjustedVertically = false;
if (tie) {
if (item->up() && tie->up()) {
if (endPoint.y() - p1.y() < tieClearance) {
p1.ry() = endPoint.y() - tieClearance;
adjustedVertically = true;
}
} else if (!item->up() && !tie->up()) {
if (p1.y() - endPoint.y() < tieClearance) {
p1.ry() = endPoint.y() + tieClearance;
adjustedVertically = true;
}
}
}
if (!adjustedVertically && sc->notes()[0]->tieBack() && !sc->notes()[0]->tieBack()->isInside()
&& sc->notes()[0]->tieBack()->up() == item->up()) {
// there is a tie that ends on this chordrest
tie = sc->notes()[0]->tieBack();
if (!tie->segmentsEmpty()) {
endPoint = tie->segmentAt(static_cast<int>(tie->nsegments()) - 1)->ups(Grip::END).pos();
if (abs(endPoint.y() - p1.y()) < tieClearance) {
p1.rx() += horizontalTieClearance;
}
}
}
}
} else if (sst == SpannerSegmentType::END || sst == SpannerSegmentType::MIDDLE) {
// beginning of system
ChordRest* firstCr = system->firstChordRest(item->track2());
double y = p1.y();
if (firstCr && firstCr == item->endCR()) {
constrainLeftAnchor = true;
}
if (firstCr && firstCr->isChord()) {
Chord* chord = toChord(firstCr);
Shape chordShape = chord->shape();
chordShape.removeTypes({ ElementType::ACCIDENTAL });
y = item->up() ? chordShape.top() : chordShape.bottom();
bool isAboveStem = chord->stem() && chord->up() == item->up();
if (!isAboveStem) {
y += continuedSlurOffsetY * (item->up() ? -1 : 1);
}
}
p1 = PointF(system->firstNoteRestSegmentX(true), y);
// adjust for ties at the end of the system
ChordRest* cr = system->firstChordRest(item->track());
if (cr && cr->isChord() && cr->tick() >= stick && cr->tick() <= etick) {
// TODO: can ties go to or from rests?
Chord* c = toChord(cr);
Tie* tie = nullptr;
PointF endPoint;
Tie* tieBack = c->notes()[0]->tieBack();
if (tieBack && !tieBack->isInside() && tieBack->up() == item->up()) {
// there is a tie that ends on this chordrest
if (!tieBack->segmentsEmpty()) { //Checks for spanner segment esxists
tie = tieBack;
endPoint = tie->backSegment()->ups(Grip::START).pos();
}
}
if (tie) {
if (item->up() && tie->up()) {
if (endPoint.y() - p1.y() < tieClearance) {
p1.ry() = endPoint.y() - tieClearance;
}
} else if (!item->up() && !tie->up()) {
if (p1.y() - endPoint.y() < tieClearance) {
p1.ry() = endPoint.y() + tieClearance;
}
}
}
}
}
// end anchor
if (sst == SpannerSegmentType::SINGLE || sst == SpannerSegmentType::END) {
Chord* ec = item->endCR()->isChord() ? toChord(item->endCR()) : nullptr;
// on chord
if (ec) {
Tie* tie = (item->up() ? ec->upNote() : ec->downNote())->tieBack();
PointF endPoint;
if (tie && (tie->isInside() || tie->up() != item->up())) {
tie = nullptr;
}
bool adjustedVertically = false;
if (tie && !tie->segmentsEmpty()) {
endPoint = tie->segmentAt(0)->ups(Grip::END).pos();
if (item->up() && tie->up()) {
if (endPoint.y() - p2.y() < tieClearance) {
p2.ry() = endPoint.y() - tieClearance;
adjustedVertically = true;
}
} else if (!item->up() && !tie->up()) {
if (p2.y() - endPoint.y() < tieClearance) {
p2.ry() = endPoint.y() + tieClearance;
adjustedVertically = true;
}
}
}
Tie* tieFor = ec->notes()[0]->tieFor();
if (!adjustedVertically && tieFor && !tieFor->isInside() && tieFor->up() == item->up()) {
// there is a tie that starts on this chordrest
if (!tieFor->segmentsEmpty() && std::abs(tieFor->frontSegment()->ups(Grip::START).pos().y() - p2.y()) < tieClearance) {
p2.rx() -= horizontalTieClearance;
}
}
}
} else {
// at end of system
ChordRest* lastCr = system->lastChordRest(item->track());
double y = p1.y();
if (lastCr && lastCr == item->startCR()) {
y += 0.25 * item->spatium() * (item->up() ? -1 : 1);
} else if (lastCr && lastCr->isChord()) {
Chord* chord = toChord(lastCr);
Shape chordShape = chord->shape();
chordShape.removeTypes({ ElementType::ACCIDENTAL });
y = item->up() ? chordShape.top() : chordShape.bottom();
bool isAboveStem = chord->stem() && chord->up() == item->up();
if (!isAboveStem) {
y += continuedSlurOffsetY * (item->up() ? -1 : 1);
}
double diff = item->up() ? y - p1.y() : p1.y() - y;
if (diff > continuedSlurMaxDiff) {
y = p1.y() + (y > p1.y() ? continuedSlurMaxDiff : -continuedSlurMaxDiff);
}
}
p2 = PointF(system->endingXForOpenEndedLines(), y);
// adjust for ties at the end of the system
ChordRest* cr = system->lastChordRest(item->track());
if (cr && cr->isChord() && cr->tick() >= stick && cr->tick() <= etick) {
// TODO: can ties go to or from rests?
Chord* c = toChord(cr);
Tie* tie = nullptr;
PointF endPoint;
Tie* tieFor = c->notes()[0]->tieFor();
if (tieFor && !tieFor->isInside() && tieFor->up() == item->up()) {
// there is a tie that starts on this chordrest
if (!tieFor->segmentsEmpty()) { //Checks is spanner segment exists
tie = tieFor;
endPoint = tie->segmentAt(0)->ups(Grip::END).pos();
}
}
if (tie) {
if (item->up() && tie->up()) {
if (endPoint.y() - p2.y() < tieClearance) {
p2.ry() = endPoint.y() - tieClearance;
}
} else if (!item->up() && !tie->up()) {
if (p2.y() - endPoint.y() < tieClearance) {
p2.ry() = endPoint.y() + tieClearance;
}
}
}
}
}
if (constrainLeftAnchor) {
p1.ry() = p2.y() + (0.25 * item->spatium() * (item->up() ? -1 : 1));
}
layoutSegment(slurSegment, ctx, p1, p2);
return slurSegment;
}
//---------------------------------------------------------
// slurPos
// calculate position of start- and endpoint of slur
// relative to System() position
//---------------------------------------------------------
void SlurTieLayout::slurPos(Slur* item, SlurTiePos* sp, LayoutContext& ctx)
{
item->stemFloated().reset();
double _spatium = (item->staffType() ? item->staffType()->lineDistance().val() : 1.0) * item->spatium();
const double stemSideInset = 0.5;
const double stemOffsetX = 0.35;
const double beamClearance = 0.35;
const double beamAnchorInset = 0.15;
const double straightStemXOffset = 0.5; // how far down a straight stem a slur attaches (percent)
const double minOffset = 0.2;
// hack alert!! -- fakeCutout
// The fakeCutout const describes the slope of a line from the top of the stem to the full width of the hook.
// this is necessary because hooks don't have SMuFL cutouts
// Gonville and MuseJazz have really weirdly-shaped hooks compared to Leland and Bravura and Emmentaler,
// so we need to adjust the slope of our hook-avoidance line. this will be unnecessary when hooks have
// SMuFL anchors
bool bulkyHook = ctx.engravingFont()->family() == "Gonville" || ctx.engravingFont()->family() == "MuseJazz";
const double fakeCutoutSlope = bulkyHook ? 1.5 : 1.0;
if (item->endCR() == 0) {
sp->p1 = item->startCR()->pagePos();
sp->p1.rx() += item->startCR()->width();
sp->p2 = sp->p1;
sp->p2.rx() += 5 * _spatium;
sp->system1 = item->startCR()->measure()->system();
sp->system2 = sp->system1;
return;
}
bool useTablature = item->staff() && item->staff()->isTabStaff(item->endCR()->tick());
bool staffHasStems = true; // assume staff uses stems
const StaffType* stt = 0;
if (useTablature) {
stt = item->staff()->staffType(item->tick());
staffHasStems = stt->stemThrough(); // if tab with stems beside, stems do not count for slur pos
}
// start and end cr, chord, and note
ChordRest* scr = item->startCR();
ChordRest* ecr = item->endCR();
Chord* sc = 0;
Note* note1 = 0;
if (scr->isChord()) {
sc = toChord(scr);
note1 = item->up() ? sc->upNote() : sc->downNote();
}
Chord* ec = 0;
Note* note2 = 0;
if (ecr->isChord()) {
ec = toChord(ecr);
note2 = item->up() ? ec->upNote() : ec->downNote();
}
sp->system1 = scr->measure()->system();
sp->system2 = ecr->measure()->system();
if (sp->system1 == 0) {
LOGD("no system1");
return;
}
sp->p1 = scr->pos() + scr->segment()->pos() + scr->measure()->pos();
sp->p2 = ecr->pos() + ecr->segment()->pos() + ecr->measure()->pos();
// adjust for cross-staff
if (scr->vStaffIdx() != item->vStaffIdx() && sp->system1) {
double diff = sp->system1->staff(scr->vStaffIdx())->y() - sp->system1->staff(item->vStaffIdx())->y();
sp->p1.ry() += diff;
}
if (ecr->vStaffIdx() != item->vStaffIdx() && sp->system2) {
double diff = sp->system2->staff(ecr->vStaffIdx())->y() - sp->system2->staff(item->vStaffIdx())->y();
sp->p2.ry() += diff;
}
// account for centering or other adjustments (other than mirroring)
if (note1 && !note1->ldata()->mirror.value()) {
sp->p1.rx() += note1->x();
}
if (note2 && !note2->ldata()->mirror.value()) {
sp->p2.rx() += note2->x();
}
PointF po = PointF();
Stem* stem1 = sc && staffHasStems ? sc->stem() : 0;
Stem* stem2 = ec && staffHasStems ? ec->stem() : 0;
enum class SlurAnchor : char {
NONE, STEM
};
SlurAnchor sa1 = SlurAnchor::NONE;
SlurAnchor sa2 = SlurAnchor::NONE;
if (staffHasStems) {
if (sc && sc->hook() && sc->up() == item->up()) {
sa1 = SlurAnchor::STEM;
}
if (scr->up() == ecr->up() && scr->up() == item->up()) {
if (stem1 && !item->stemSideStartForBeam()) {
sa1 = SlurAnchor::STEM;
}
if (stem2 && !item->stemSideEndForBeam()) {
sa2 = SlurAnchor::STEM;
}
} else if (ecr->segment()->system() != scr->segment()->system()) {
// in the case of continued slurs, we anchor to stem when necessary
if (scr->up() == item->up() && stem1 && !scr->beam()) {
sa1 = SlurAnchor::STEM;
}
if (ecr->up() == item->up() && stem2 && !ecr->beam()) {
sa2 = SlurAnchor::STEM;
}
}
}
double __up = item->up() ? -1.0 : 1.0;
double hw1 = note1 ? note1->tabHeadWidth(stt) : scr->width() * scr->mag(); // if stt == 0, tabHeadWidth()
double hw2 = note2 ? note2->tabHeadWidth(stt) : ecr->width() * ecr->mag(); // defaults to headWidth()
PointF pt;
switch (sa1) {
case SlurAnchor::STEM: //sc can't be null
{
// place slur starting point at stem end point
pt = sc->stemPos() - sc->pagePos() + sc->stem()->ldata()->line.p2();
if (useTablature) { // in tabs, stems are centred on note:
pt.rx() = hw1 * 0.5 + (note1 ? note1->bboxXShift() : 0.0); // skip half notehead to touch stem, anatoly-os: incorrect. half notehead width is not always the stem position
}
// clear the stem (x)
// allow slight overlap (y)
// don't allow overlap with hook if not disabling the autoplace checks against start/end segments in SlurSegment::layoutSegment()
double yadj = -stemSideInset* sc->intrinsicMag();
yadj *= _spatium * __up;
double offset = std::max(stemOffsetX * sc->intrinsicMag(), minOffset);
pt += PointF(offset * _spatium, yadj);
// account for articulations
fixArticulations(item, pt, sc, __up, true);
// adjust for hook
double fakeCutout = 0.0;
if (!ctx.conf().styleB(Sid::useStraightNoteFlags)) {
Hook* hook = sc->hook();
// regular flags
if (hook && hook->ldata()->bbox().translated(hook->pos()).contains(pt)) {
// TODO: in the utopian far future where all hooks have SMuFL cutouts, this fakeCutout business will no
// longer be used. for the time being fakeCutout describes a point on the line y=mx+b, out from the top of the stem
// where y = yadj, m = fakeCutoutSlope, and x = y/m + fakeCutout
fakeCutout = std::min(0.0, std::abs(yadj) - (hook->width() / fakeCutoutSlope));
pt.rx() = sc->stemPosX() - fakeCutout;
}
} else {
Hook* hook = sc->hook();
// straight flags
if (hook && hook->ldata()->bbox().translated(hook->pos()).contains(pt)) {
double hookWidth = hook->width() * hook->mag();
pt.rx() = (hookWidth * straightStemXOffset) + (hook->pos().x() + sc->x());
if (item->up()) {
pt.ry() = sc->downNote()->pos().y() - stem1->height() - (beamClearance * _spatium * .7);
} else {
pt.ry() = sc->upNote()->pos().y() + stem1->height() + (beamClearance * _spatium * .7);
}
}
}
sp->p1 += pt;
}
break;
case SlurAnchor::NONE:
break;
}
switch (sa2) {
case SlurAnchor::STEM: //ec can't be null
{
pt = ec->stemPos() - ec->pagePos() + ec->stem()->ldata()->line.p2();
if (useTablature) {
pt.rx() = hw2 * 0.5;
}
// don't allow overlap with beam
double yadj;
if (ec->beam() && ec->beam()->elements().front() != ec) {
yadj = 0.75;
} else if (ec->tremolo() && ec->tremolo()->twoNotes() && ec->tremolo()->chord2() == ec) {
yadj = 0.75;
} else {
yadj = -stemSideInset;
}
yadj *= _spatium * __up;
double offset = std::max(stemOffsetX * ec->intrinsicMag(), minOffset);
pt += PointF(-offset * _spatium, yadj);
// account for articulations
fixArticulations(item, pt, ec, __up, true);
sp->p2 += pt;
}
break;
case SlurAnchor::NONE:
break;
}
//
// default position:
// horizontal: middle of notehead
// vertical: _spatium * .4 above/below notehead
//
//------p1
// Compute x0, y0 and stemPos
if (sa1 == SlurAnchor::NONE || sa2 == SlurAnchor::NONE) { // need stemPos if sa2 == SlurAnchor::NONE
bool stemPos = false; // p1 starts at chord stem side
// default positions
po.rx() = hw1 * .5 + (note1 ? note1->bboxXShift() : 0.0);
if (note1) {
po.ry() = note1->pos().y();
} else if (item->up()) {
po.ry() = scr->ldata()->bbox().top();
} else {
po.ry() = scr->ldata()->bbox().top() + scr->height();
}
double offset = useTablature ? 0.75 : 0.9;
po.ry() += scr->intrinsicMag() * _spatium * offset * __up;
// adjustments for stem and/or beam
Tremolo* trem = sc ? sc->tremolo() : nullptr;
if (stem1 || (trem && trem->twoNotes())) { //sc not null
Beam* beam1 = sc->beam();
if (beam1 && (beam1->elements().back() != sc) && (sc->up() == item->up())) {
TLayout::layoutBeam(beam1, ctx);
// start chord is beamed but not the last chord of beam group
// and slur direction is same as start chord (stem side)
// in these cases, layout start of slur to stem
double beamWidthSp = ctx.conf().styleS(Sid::beamWidth).val() * beam1->magS();
double offset2 = std::max(beamClearance * sc->intrinsicMag(), minOffset) * _spatium;
double sh = stem1->length() + (beamWidthSp / 2) + offset2;
if (item->up()) {
po.ry() = sc->stemPos().y() - sc->pagePos().y() - sh;
} else {
po.ry() = sc->stemPos().y() - sc->pagePos().y() + sh;
}
po.rx() = sc->stemPosX() + (beamAnchorInset * _spatium * sc->intrinsicMag()) + (stem1->lineWidthMag() / 2 * __up);
// account for articulations
fixArticulations(item, po, sc, __up, true);
// force end of slur to layout to stem as well,
// if start and end chords have same stem direction
stemPos = true;
} else if (trem && trem->twoNotes() && trem->chord2() != sc && sc->up() == item->up()) {
TLayout::layoutTremolo(trem, ctx);
Note* note = item->up() ? sc->upNote() : sc->downNote();
double stemHeight = stem1 ? stem1->length() : defaultStemLengthStart(trem);
double offset2 = std::max(beamClearance * sc->intrinsicMag(), minOffset) * _spatium;
double sh = stemHeight + offset2;
if (item->up()) {
po.ry() = sc->stemPos().y() - sc->pagePos().y() - sh;
} else {
po.ry() = sc->stemPos().y() - sc->pagePos().y() + sh;
}
if (!stem1) {
po.rx() = note->noteheadCenterX();
} else {
po.rx() = sc->stemPosX() + (beamAnchorInset * _spatium * sc->intrinsicMag()) + (stem1->lineWidthMag() / 2. * __up);
}
fixArticulations(item, po, sc, __up, true);
stemPos = true;
} else {
// start chord is not beamed or is last chord of beam group
// or slur direction is opposite that of start chord
// at this point slur is in default position relative to note on slur side
// but we may need to make further adjustments
// if stem and slur are both up
// we need to clear stem horizontally
double stemOffsetMag = stemOffsetX * sc->intrinsicMag();
if (sc->up() && item->up()) {
// stems in tab staves come from the middle of the head, which means it's much easier
// to just subtract an offset from the notehead center (which po already is)
if (useTablature) {
po.rx() += stemOffsetMag * _spatium;
} else {
po.rx() = hw1 + _spatium * stemOffsetMag;
}
}
//
// handle case: stem up - stem down
// stem down - stem up
//
if ((sc->up() != ecr->up()) && (sc->up() == item->up())) {
item->stemFloated().left = true;
// start and end chord have opposite direction
// and slur direction is same as start chord
// (so slur starts on stem side)
// float the start point along the stem to follow direction of movement
// see for example Gould p. 111
// get position of note on slur side for start & end chords
Note* n1 = sc->up() ? sc->upNote() : sc->downNote();
Note* n2 = 0;
if (ec) {
n2 = ec->up() ? ec->upNote() : ec->downNote();
}
// differential in note positions
double yd = (n2 ? n2->pos().y() : ecr->pos().y()) - n1->pos().y();
yd *= .5;
// float along stem according to differential
double sh = stem1->height();
if (item->up() && yd < 0.0) {
po.ry() = std::max(po.y() + yd, sc->downNote()->pos().y() - sh - _spatium);
} else if (!item->up() && yd > 0.0) {
po.ry() = std::min(po.y() + yd, sc->upNote()->pos().y() + sh + _spatium);
}
// account for articulations
fixArticulations(item, po, sc, __up, true);
// we may wish to force end to align to stem as well,
// if it is in same direction
// (but it won't be, so this assignment should have no effect)
stemPos = true;
} else {
// avoid articulations
fixArticulations(item, po, sc, __up, sc->up() == item->up());
}
}
} else if (sc) {
// avoid articulations
fixArticulations(item, po, sc, __up, sc->up() == item->up());
}
// TODO: offset start position if there is another slur ending on this cr
if (sa1 == SlurAnchor::NONE) {
sp->p1 += po;
}
//------p2
if (sa2 == SlurAnchor::NONE) {
// default positions
po.rx() = hw2 * .5 + (note2 ? note2->bboxXShift() : 0.0);
if (note2) {
po.ry() = note2->pos().y();
} else if (item->up()) {
po.ry() = item->endCR()->ldata()->bbox().top();
} else {
po.ry() = item->endCR()->ldata()->bbox().top() + item->endCR()->height();
}
double offset2 = useTablature ? 0.75 : 0.9;
po.ry() += ecr->intrinsicMag() * _spatium * offset2 * __up;
// adjustments for stem and/or beam
Tremolo* trem2 = ec ? ec->tremolo() : nullptr;
if (stem2 || (trem2 && trem2->twoNotes())) { //ec can't be null
Beam* beam2 = ec->beam();
if ((stemPos && (scr->up() == ec->up()))
|| (beam2
&& (!beam2->elements().empty())
&& (beam2->elements().front() != ec)
&& (ec->up() == item->up())
&& sc && (sc->noteType() == NoteType::NORMAL)
)
|| (trem2 && trem2->twoNotes() && ec->up() == item->up())
) {
if (beam2) {
TLayout::layoutBeam(beam2, ctx);
}
if (trem2) {
TLayout::layoutTremolo(trem2, ctx);
}
// slur start was laid out to stem and start and end have same direction
// OR
// end chord is beamed but not the first chord of beam group
// and slur direction is same as end chord (stem side)
// and start chordrest is not a grace chord
// in these cases, layout end of slur to stem
double beamWidthSp = beam2 ? ctx.conf().styleS(Sid::beamWidth).val() : 0;
Note* note = item->up() ? ec->upNote() : ec->downNote();
double stemHeight = stem2 ? stem2->length() + (beamWidthSp / 2) : defaultStemLengthEnd(trem2);
double offset3 = std::max(beamClearance * ec->intrinsicMag(), minOffset) * _spatium;
double sh = stemHeight + offset3;
if (item->up()) {
po.ry() = ec->stemPos().y() - ec->pagePos().y() - sh;
} else {
po.ry() = ec->stemPos().y() - ec->pagePos().y() + sh;
}
if (!stem2) {
// tremolo whole notes
po.setX(note->noteheadCenterX());
} else {
po.setX(ec->stemPosX() + (stem2->lineWidthMag() / 2 * __up) - (beamAnchorInset * _spatium * ec->intrinsicMag()));
}
// account for articulations
fixArticulations(item, po, ec, __up, true);
} else {
// slur was not aligned to stem or start and end have different direction
// AND
// end chord is not beamed or is first chord of beam group
// or slur direction is opposite that of end chord
// if stem and slur are both down,
// we need to clear stem horizontally
double stemOffsetMag = stemOffsetX * ec->intrinsicMag();
if (!ec->up() && !item->up()) {
// stems in tab staves come from the middle of the head, which means it's much easier
// to just subtract an offset from the notehead center (which po already is)
if (useTablature) {
po.rx() -= stemOffsetMag * _spatium;
} else {
po.rx() = -_spatium * stemOffsetMag + note2->x();
}
} else if (useTablature && item->up() && ec->up()) {
// same as above
po.rx() -= _spatium * stemOffsetMag;
}
//
// handle case: stem up - stem down
// stem down - stem up
//
if ((scr->up() != ec->up()) && (ec->up() == item->up())) {
item->stemFloated().right = true;
// start and end chord have opposite direction
// and slur direction is same as end chord
// (so slur end on stem side)
// float the end point along the stem to follow direction of movement
// see for example Gould p. 111
Note* n1 = 0;
if (sc) {
n1 = sc->up() ? sc->upNote() : sc->downNote();
}
Note* n2 = ec->up() ? ec->upNote() : ec->downNote();
double yd = n2->pos().y() - (n1 ? n1->pos().y() : item->startCR()->pos().y());
yd *= .5;
double mh = stem2->height();
if (item->up() && yd > 0.0) {
po.ry() = std::max(po.y() - yd, ec->downNote()->pos().y() - mh - _spatium);
} else if (!item->up() && yd < 0.0) {
po.ry() = std::min(po.y() - yd, ec->upNote()->pos().y() + mh + _spatium);
}
// account for articulations
fixArticulations(item, po, ec, __up, true);
} else {
// avoid articulations
fixArticulations(item, po, ec, __up, ec->up() == item->up());
}
}
} else if (ec) {
// avoid articulations
fixArticulations(item, po, ec, __up, ec->up() == item->up());
}
// TODO: offset start position if there is another slur ending on this cr
sp->p2 += po;
}
}
if (item->staffType()->isTabStaff()) {
SlurTieLayout::avoidPreBendsOnTab(sc, ec, sp);
}
/// adding extra space above slurs for notes in circles
if (Slur::engravingConfiguration()->enableExperimentalFretCircle() && item->staff()->staffType()->isCommonTabStaff()) {
auto adjustSlur = [](Chord* ch, PointF& coord, bool up) {
const Fraction halfFraction = Fraction(1, 2);
if (ch && ch->ticks() >= halfFraction) {
for (EngravingItem* item : ch->el()) {
if (item && item->isFretCircle()) {
coord += PointF(0, toFretCircle(item)->ldata()->offsetFromUpNote * (up ? -1 : 1));
break;
}
}
}
};
adjustSlur(sc, sp->p1, item->up());
adjustSlur(ec, sp->p2, item->up());
}
}
void SlurTieLayout::fixArticulations(Slur* item, PointF& pt, Chord* c, double up, bool stemSide)
{
//
// handle special case of tenuto and staccato
// yo = current offset of slur from chord position
// return unchanged position, or position of outmost "close" articulation
//
double slurTipToArticVertDist = c->spatium() * 0.5 * up;
double slurTipInwardAdjust = 0.1 * item->spatium();
bool start = item->startCR() && item->startCR() == c;
bool end = item->endCR() && item->endCR() == c;
for (Articulation* a : c->articulations()) {
if (!a->layoutCloseToNote() || !a->addToSkyline()) {
continue;
}
// skip if articulation on stem side but slur is not or vice versa
if ((a->up() == c->up()) != stemSide) {
continue;
}
// Correct x-position inwards
Note* note = c->up() ? c->downNote() : c->upNote();
pt.rx() = a->x() - note->x();
if (start) {
pt.rx() += slurTipInwardAdjust;
} else if (end) {
pt.rx() -= slurTipInwardAdjust;
}
// Adjust y-position
if (a->up()) {
pt.ry() = std::min(pt.y(), a->y() + a->height() / 2 * up + slurTipToArticVertDist);
} else {
pt.ry() = std::max(pt.y(), a->y() + a->height() / 2 * up + slurTipToArticVertDist);
}
}
}
void SlurTieLayout::avoidPreBendsOnTab(const Chord* sc, const Chord* ec, SlurTiePos* sp)
{
GuitarBend* bendOnStart = nullptr;
GuitarBend* bendOnEnd = nullptr;
if (sc) {
for (Note* note : sc->notes()) {
GuitarBend* bf = note->bendFor();
GuitarBend* bb = note->bendBack();
if (bf && !bf->segmentsEmpty() && bf->type() == GuitarBendType::PRE_BEND && !bf->angledPreBend()) {
bendOnStart = bf;
} else if (bb && !bb->segmentsEmpty() && bb->type() == GuitarBendType::PRE_BEND && !bb->angledPreBend()) {
bendOnStart = bb;
}
if (bendOnStart) {
break;
}
}
}
if (ec) {
for (Note* note : ec->notes()) {
GuitarBend* bf = note->bendFor();
GuitarBend* bb = note->bendBack();