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) 2023 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 "masklayout.h"
24 :
25 : #include "dom/barline.h"
26 : #include "dom/chord.h"
27 : #include "dom/jump.h"
28 : #include "dom/lyrics.h"
29 : #include "dom/marker.h"
30 : #include "dom/measure.h"
31 : #include "dom/measurenumber.h"
32 : #include "dom/note.h"
33 : #include "dom/parenthesis.h"
34 : #include "dom/score.h"
35 : #include "dom/segment.h"
36 : #include "dom/staff.h"
37 : #include "dom/stafflines.h"
38 : #include "dom/system.h"
39 : #include "dom/page.h"
40 : #include "dom/textlinebase.h"
41 :
42 : using namespace mu::engraving;
43 : using namespace mu::engraving::rendering::score;
44 :
45 4575 : void MaskLayout::computeMasks(LayoutContext& ctx, Page* page)
46 : {
47 4590 : TRACEFUNC;
48 :
49 4575 : bool maskBarlines = ctx.conf().styleB(Sid::maskBarlinesForText);
50 4575 : bool maskSlurs = ctx.conf().styleB(Sid::maskSlurs);
51 4575 : bool maskTies = ctx.conf().styleB(Sid::maskTies);
52 :
53 16091 : for (const System* system : page->systems()) {
54 11516 : std::vector<TextBase*> allSystemText = collectAllSystemText(system);
55 :
56 52567 : for (MeasureBase* mb : system->measures()) {
57 41051 : if (!mb->isMeasure()) {
58 2714 : continue;
59 : }
60 38337 : Measure* measure = toMeasure(mb);
61 :
62 38337 : if (maskBarlines) {
63 101700 : for (const Segment& seg : measure->segments()) {
64 81166 : if (seg.isType(SegmentType::BarLineTypes)) {
65 22458 : computeBarlineMasks(&seg, system, allSystemText, ctx);
66 : }
67 : }
68 : }
69 :
70 38337 : staff_idx_t nstaves = ctx.dom().nstaves();
71 94835 : for (staff_idx_t staffIdx = 0; staffIdx < nstaves; ++staffIdx) {
72 56498 : if (staffIdx >= system->staves().size() || !system->staff(staffIdx)->show()) {
73 1855 : continue;
74 : }
75 54643 : const Staff* staff = ctx.dom().staff(staffIdx);
76 54643 : const StaffType* staffType = staff->staffType(measure->tick());
77 54643 : StaffLines* staffLines = measure->staffLines(staffIdx);
78 54643 : if (staffType->isTabStaff()) {
79 1081 : maskTABStringLinesForFrets(staffLines, ctx);
80 : }
81 : }
82 : }
83 :
84 17381 : for (SpannerSegment* spannerSeg : system->spannerSegments()) {
85 9274 : if (!spannerSeg->isSlurTieSegment() || spannerSeg->isTappingHalfSlurSegment()
86 9274 : || !system->staff(spannerSeg->staffIdx())->show() || !spannerSeg->visible()) {
87 2487 : continue;
88 : }
89 :
90 3378 : spannerSeg->mutldata()->setMask(Shape());
91 :
92 3378 : AutoOnOff slurTieMaskOverride = spannerSeg->getProperty(Pid::MASK_SLURTIE).value<AutoOnOff>();
93 3378 : if (slurTieMaskOverride == AutoOnOff::ON
94 6756 : || (slurTieMaskOverride == AutoOnOff::AUTO
95 3378 : && ((maskSlurs && spannerSeg->isSlurSegment())
96 2864 : || (maskTies && spannerSeg->isTieSegment())))) {
97 1516 : computeSlurTieMasks(toSlurTieSegment(spannerSeg));
98 : }
99 : }
100 11516 : }
101 4575 : }
102 :
103 22458 : void MaskLayout::computeBarlineMasks(const Segment* barlineSement, const System* system, const std::vector<TextBase*>& allSystemText,
104 : LayoutContext& ctx)
105 : {
106 22458 : if (barlineSement->measure()->isLastInSystem() && barlineSement == barlineSement->measure()->lastEnabled()) {
107 4984 : return;
108 : }
109 :
110 17474 : staff_idx_t nstaves = ctx.dom().nstaves();
111 :
112 17474 : std::vector<BarLine*> barlines;
113 17474 : barlines.reserve(nstaves);
114 :
115 45493 : for (staff_idx_t staffIdx = 0; staffIdx < ctx.dom().nstaves(); ++staffIdx) {
116 28019 : if (!system->staff(staffIdx)->show()) {
117 1845 : continue;
118 : }
119 26174 : BarLine* barline = toBarLine(barlineSement->element(staff2track(staffIdx)));
120 26174 : if (!barline || !barline->spanStaff()) {
121 16926 : continue;
122 : }
123 9248 : maskBarlineForText(barline, allSystemText);
124 : }
125 17474 : }
126 :
127 9248 : void MaskLayout::maskBarlineForText(BarLine* barline, const std::vector<TextBase*>& allSystemText)
128 : {
129 9258 : TRACEFUNC;
130 :
131 9248 : PointF barlinePos = barline->pagePos();
132 9248 : Shape barlineShape = barline->shape().translated(barlinePos);
133 :
134 9248 : Shape mask;
135 9248 : const double spatium = barline->spatium();
136 :
137 27141 : for (TextBase* text : allSystemText) {
138 17893 : const double fontSizeScaleFactor = text->size() / 10.0;
139 17893 : const double collisionPadding = 0.2 * spatium * fontSizeScaleFactor;
140 17893 : const bool hasFrame = text->frameType() != FrameType::NO_FRAME;
141 17893 : const bool useHighResShape = !text->isDynamic() && !text->hasFrame();
142 17893 : const double maskPadding = hasFrame ? 0.0 : std::clamp(0.5 * spatium * fontSizeScaleFactor, 0.1 * spatium, spatium);
143 :
144 17893 : PointF textPos = text->pagePos();
145 17893 : if (!barlineShape.intersects(text->ldata()->bbox().translated(textPos).padded(collisionPadding))) {
146 17890 : continue;
147 : }
148 :
149 5 : Shape textShape = (useHighResShape ? text->ldata()->highResShape() : text->ldata()->shape()).translated(textPos);
150 :
151 5 : Shape filteredTextShape = createFilteredItemShape(textShape, barlineShape, collisionPadding);
152 5 : if (filteredTextShape.empty()) {
153 2 : continue;
154 : }
155 :
156 3 : filteredTextShape = filteredTextShape.bbox();
157 3 : filteredTextShape.pad(maskPadding);
158 :
159 3 : mask.add(filteredTextShape.translate(-barlinePos));
160 7 : }
161 :
162 9248 : if (mask.empty()) {
163 9245 : barline->mutldata()->setMask(mask);
164 9245 : return;
165 : }
166 :
167 : // Ensure that we don't leave tiny barline fragments. If two masking
168 : // elements are too close to each other we extend them to join.
169 3 : barlineShape.translate(-barlinePos);
170 3 : const double minFragmentLengh = 0.5 * spatium;
171 3 : cleanupMask(barlineShape, mask, minFragmentLengh);
172 :
173 3 : barline->mutldata()->setMask(mask);
174 27738 : }
175 :
176 3 : void MaskLayout::cleanupMask(const Shape& itemShape, Shape& mask, double minFragmentLength)
177 : {
178 6 : for (size_t i = 0; i < mask.size(); ++i) {
179 3 : ShapeElement& el = mask.elements()[i];
180 :
181 3 : if (std::abs(el.top() - itemShape.top()) < minFragmentLength) {
182 0 : el.adjust(0.0, -minFragmentLength, 0.0, 0.0);
183 : }
184 3 : if (std::abs(itemShape.bottom() - el.bottom()) < minFragmentLength) {
185 3 : el.adjust(0.0, 0.0, 0.0, minFragmentLength);
186 : }
187 3 : if (std::abs(el.left() - itemShape.left()) < minFragmentLength) {
188 0 : el.adjust(-minFragmentLength, 0.0, 0.0, 0.0);
189 : }
190 3 : if (std::abs(itemShape.right() - el.right()) < minFragmentLength) {
191 0 : el.adjust(0.0, 0.0, minFragmentLength, 0.0);
192 : }
193 :
194 3 : for (size_t j = i + 1; j < mask.size(); ++j) {
195 0 : ShapeElement& otherEl = mask.elements()[j];
196 0 : if (intersects(el.left(), el.right(), otherEl.left(), otherEl.right())) {
197 0 : bool otherIsBelow = otherEl.y() > el.y();
198 0 : double vertClearance = otherIsBelow ? otherEl.top() - el.bottom() : el.top() - otherEl.bottom();
199 0 : if (vertClearance < minFragmentLength) {
200 0 : (otherIsBelow ? el : otherEl).adjust(0.0, 0.0, 0.0, minFragmentLength);
201 : }
202 : }
203 0 : if (intersects(el.top(), el.bottom(), otherEl.top(), el.bottom())) {
204 0 : bool otherIsRight = otherEl.x() > el.x();
205 0 : double horClearance = otherIsRight ? otherEl.left() - el.right() : el.left() - otherEl.right();
206 0 : if (horClearance < minFragmentLength) {
207 0 : (otherIsRight ? el : otherEl).adjust(0.0, 0.0, minFragmentLength, 0.0);
208 : }
209 : }
210 : }
211 : }
212 3 : }
213 :
214 11516 : std::vector<TextBase*> MaskLayout::collectAllSystemText(const System* system)
215 : {
216 11531 : TRACEFUNC;
217 :
218 11516 : std::vector<TextBase*> allText;
219 :
220 52567 : for (const MeasureBase* mb : system->measures()) {
221 41051 : if (!mb->isMeasure()) {
222 2714 : continue;
223 : }
224 38337 : const Measure* measure = toMeasure(mb);
225 94835 : for (staff_idx_t i = 0; i < measure->mstaves().size(); ++i) {
226 56498 : if (measure->showMeasureNumberOnStaff(i)) {
227 4940 : if (MeasureNumber* measureNumber = measure->measureNumber(i)) {
228 4940 : allText.push_back(measureNumber);
229 : }
230 : }
231 : }
232 41432 : for (EngravingItem* item : measure->el()) {
233 3095 : const SysStaff* staff = system ? system->staff(item->staffIdx()) : nullptr;
234 3095 : const bool staffVisible = staff && staff->show();
235 3095 : if ((item->isMarker() || item->isJump()) && item->visible() && staffVisible) {
236 537 : allText.push_back(toTextBase(item));
237 : }
238 : }
239 180472 : for (const Segment& s : measure->segments()) {
240 142135 : if (!s.isType(SegmentType::Duration) || !s.enabled()) {
241 65692 : continue;
242 : }
243 83806 : for (EngravingItem* annotation : s.annotations()) {
244 7363 : const SysStaff* staff = system ? system->staff(annotation->staffIdx()) : nullptr;
245 7363 : const bool staffVisible = staff && staff->show();
246 7363 : if (annotation->isTextBase() && annotation->visible() && staffVisible) {
247 6506 : allText.push_back(toTextBase(annotation));
248 : }
249 : }
250 76443 : if (!s.isChordRestType()) {
251 346 : continue;
252 : }
253 514365 : for (EngravingItem* chordRest : s.elist()) {
254 438268 : if (!chordRest || !system->staff(chordRest->staffIdx())->show()) {
255 335173 : continue;
256 : }
257 105469 : for (Lyrics* lyr : toChordRest(chordRest)->lyrics()) {
258 2374 : if (lyr->visible()) {
259 2359 : allText.push_back(lyr);
260 : }
261 : }
262 : }
263 : }
264 : }
265 :
266 17381 : for (SpannerSegment* spannerSegment : system->spannerSegments()) {
267 1299 : if (!spannerSegment->isTextLineBaseSegment() || !system->staff(spannerSegment->staffIdx())->show()
268 7164 : || !spannerSegment->getProperty(Pid::VISIBLE).toBool()) {
269 4572 : continue;
270 : }
271 1293 : TextLineBaseSegment* textLineBaseSegment = static_cast<TextLineBaseSegment*>(spannerSegment);
272 1293 : Text* beginText = textLineBaseSegment->text();
273 1293 : Text* endText = textLineBaseSegment->endText();
274 1293 : if (beginText && !beginText->empty()) {
275 783 : allText.push_back(beginText);
276 : }
277 1293 : if (endText && !endText->empty()) {
278 30 : allText.push_back(endText);
279 : }
280 : }
281 :
282 11516 : return allText;
283 11516 : }
284 :
285 1081 : void MaskLayout::maskTABStringLinesForFrets(StaffLines* staffLines, const LayoutContext& ctx)
286 : {
287 1081 : staff_idx_t staffIdx = staffLines->staffIdx();
288 1081 : bool linesThrough = ctx.dom().staff(staffIdx)->staffType(Fraction())->linesThrough();
289 :
290 1081 : PointF staffLinesPos = staffLines->pagePos();
291 :
292 1081 : double padding = ctx.conf().styleAbsolute(Sid::tabFretPadding);
293 :
294 1081 : track_idx_t startTrack = staff2track(staffIdx);
295 1081 : track_idx_t endTrack = startTrack + VOICES;
296 :
297 1081 : Shape mask;
298 :
299 822 : auto maskFret = [&mask, linesThrough, padding, staffLinesPos] (Chord* chord) {
300 1836 : for (Note* note : chord->notes()) {
301 1014 : if (!note->visible()) {
302 22 : continue;
303 : }
304 992 : if (!note->shouldHideFret() && (!linesThrough || note->fretConflict())) {
305 978 : Shape noteShape = note->ldata()->bbox();
306 978 : noteShape.translate(note->pagePos());
307 :
308 978 : noteShape.pad(padding);
309 978 : mask.add(noteShape.translated(-staffLinesPos));
310 978 : }
311 : }
312 822 : };
313 :
314 822 : auto maskParens = [&mask, linesThrough, padding, staffLinesPos] (Chord* chord) {
315 822 : if (linesThrough) {
316 2 : return;
317 : }
318 833 : for (const NoteParenthesisInfo* i : chord->noteParentheses()) {
319 13 : const Parenthesis* leftParen = i->leftParen();
320 13 : const Parenthesis* rightParen = i->rightParen();
321 13 : bool allHidden = false;
322 13 : for (const Note* note : i->notes()) {
323 13 : if (!note->shouldHideFret()) {
324 13 : allHidden = false;
325 13 : break;
326 : }
327 : }
328 :
329 13 : if (allHidden) {
330 0 : continue;
331 : }
332 :
333 : // HACK: Overlapping mask regions which have the same y value and height cause errors
334 : // Parentheses on single fret marks have the same height as the fret marks
335 : static const double PAREN_PADDING_EPSILON = 1.0;
336 13 : if (leftParen && leftParen->visible()) {
337 10 : Shape leftParenShape = leftParen->ldata()->bbox().translated(leftParen->pagePos());
338 10 : leftParenShape.pad(padding);
339 10 : leftParenShape.adjust(0, -PAREN_PADDING_EPSILON, 0, PAREN_PADDING_EPSILON);
340 10 : mask.add(leftParenShape.translated(-staffLinesPos));
341 10 : }
342 :
343 13 : if (rightParen && rightParen->visible()) {
344 10 : Shape rightParenShape = rightParen->ldata()->bbox().translated(rightParen->pagePos());
345 10 : rightParenShape.pad(padding);
346 10 : rightParenShape.adjust(0, -PAREN_PADDING_EPSILON, 0, PAREN_PADDING_EPSILON);
347 10 : mask.add(rightParenShape.translated(-staffLinesPos));
348 10 : }
349 : }
350 1081 : };
351 :
352 1081 : const Measure* measure = staffLines->measure();
353 2763 : for (Segment* seg = measure->first(SegmentType::ChordRest); seg; seg = seg->next(SegmentType::ChordRest)) {
354 8410 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
355 6728 : EngravingItem* el = seg->element(track);
356 6728 : if (!el || !el->isChord()) {
357 5938 : continue;
358 : }
359 790 : maskFret(toChord(el));
360 790 : maskParens(toChord(el));
361 822 : for (Chord* grace : toChord(el)->graceNotes()) {
362 32 : maskFret(grace);
363 32 : maskParens(grace);
364 : }
365 : }
366 : }
367 :
368 1081 : staffLines->mutldata()->setMask(mask);
369 1081 : }
370 :
371 1516 : void MaskLayout::computeSlurTieMasks(SlurTieSegment* slurTieSegment)
372 : {
373 1524 : TRACEFUNC;
374 :
375 1516 : Spanner* spanner = slurTieSegment->spanner();
376 1516 : const Segment* startSeg = spanner->startSegment();
377 1516 : const Segment* endSeg = spanner->endSegment();
378 1516 : const EngravingItem* startElement = spanner->startElement();
379 1516 : const EngravingItem* endElement = spanner->endElement();
380 1516 : staff_idx_t startStaffIdx = startElement ? startElement->vStaffIdx() : 0;
381 1516 : staff_idx_t endStaffIdx = endElement ? endElement->vStaffIdx() : spanner->score()->nstaves() - 1;
382 1516 : if (startStaffIdx > endStaffIdx) {
383 2 : std::swap(startStaffIdx, endStaffIdx);
384 : }
385 :
386 1516 : std::vector<const EngravingItem*> itemsToMaskOver;
387 3603 : for (const Segment* seg = startSeg; seg && seg != endSeg; seg = seg->next1()) {
388 2087 : if (!seg->enabled()
389 2084 : || !seg->isType(SegmentType::KeySigTypes | SegmentType::TimeSigTypes | SegmentType::ClefTypes)
390 4171 : || seg->system() != slurTieSegment->system()) {
391 2018 : continue;
392 : }
393 140 : for (staff_idx_t staffIdx = startStaffIdx; staffIdx <= endStaffIdx; ++staffIdx) {
394 71 : if (EngravingItem* item = seg->element(staffIdx * VOICES)) { // Key/Time/Clef-type segments never have elements in any voice other than 0
395 69 : itemsToMaskOver.push_back(item);
396 : }
397 : }
398 : }
399 :
400 1516 : PointF slurTiePos = slurTieSegment->pagePos();
401 1516 : Shape slurTieShape = slurTieSegment->shape().translated(slurTiePos);
402 :
403 1516 : Shape mask;
404 1516 : const double spatium = slurTieSegment->spatium();
405 1516 : const double maskPadding = spatium * StyleDef::styleValues[size_t(Sid::keysigLeftMargin)].defaultValue.toDouble() / 2;
406 1516 : const double horizontalCollisionPadding = maskPadding;
407 1516 : const double verticalCollisionPadding = spatium * 0.1;
408 :
409 1585 : for (const EngravingItem* item : itemsToMaskOver) {
410 69 : PointF itemPos = item->pagePos();
411 69 : Shape itemShape = item->shape().translated(itemPos);
412 :
413 69 : if (!slurTieShape.bbox().intersects(itemShape.bbox().adjusted(-horizontalCollisionPadding, -verticalCollisionPadding,
414 : horizontalCollisionPadding, verticalCollisionPadding))) {
415 62 : continue;
416 : }
417 :
418 7 : Shape filteredItemShape = createFilteredItemShape(itemShape, slurTieShape, horizontalCollisionPadding, verticalCollisionPadding);
419 7 : if (filteredItemShape.empty()) {
420 7 : continue;
421 : }
422 :
423 0 : mask.add(filteredItemShape.translate(-slurTiePos));
424 76 : }
425 :
426 1516 : if (mask.empty()) {
427 1516 : slurTieSegment->mutldata()->setMask(mask);
428 1516 : return;
429 : }
430 :
431 0 : slurTieShape.translate(-slurTiePos);
432 :
433 : // Use the mask's bbox to break the slur/tie only once, regardless of the mask elements in between:
434 0 : RectF maskBbox = mask.bbox();
435 :
436 : // Avoid the slur/tie line being partially masked (i.e. it should always be either fully "cut",
437 : // or not masked at all, so we extend the mask vertically to encompass the slur/tie's full height):
438 0 : maskBbox.adjust(0.0, slurTieShape.top() - mask.top(), 0.0, slurTieShape.bottom() - mask.bottom());
439 0 : maskBbox.pad(maskPadding);
440 :
441 0 : slurTieSegment->mutldata()->setMask(maskBbox);
442 6064 : }
443 :
444 5 : Shape MaskLayout::createFilteredItemShape(const Shape& overlyingItemShape, const Shape& maskedItemShape, const double collisionPadding)
445 : {
446 5 : return MaskLayout::createFilteredItemShape(overlyingItemShape, maskedItemShape, collisionPadding, collisionPadding);
447 : }
448 :
449 12 : Shape MaskLayout::createFilteredItemShape(const Shape& overlyingItemShape, const Shape& maskedItemShape,
450 : const double horizontalCollisionPadding, const double verticalCollisionPadding)
451 : {
452 12 : Shape filteredItemShape;
453 12 : filteredItemShape.elements().reserve(overlyingItemShape.elements().size());
454 93 : for (const ShapeElement& el : overlyingItemShape.elements()) {
455 81 : Shape padded = el.adjusted(-horizontalCollisionPadding, -verticalCollisionPadding,
456 81 : horizontalCollisionPadding, verticalCollisionPadding);
457 81 : if (maskedItemShape.intersects(padded)) {
458 5 : filteredItemShape.add(el);
459 : }
460 81 : }
461 12 : return filteredItemShape;
462 0 : }
|