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 : #include "autoplace.h"
23 :
24 : #include "dom/harmony.h"
25 : #include "style/style.h"
26 :
27 : #include "dom/chordrest.h"
28 : #include "dom/lyrics.h"
29 : #include "dom/score.h"
30 : #include "dom/segment.h"
31 : #include "dom/spanner.h"
32 : #include "dom/staff.h"
33 : #include "dom/system.h"
34 : #include "dom/measure.h"
35 : #include "dom/tuplet.h"
36 :
37 : #include "log.h"
38 :
39 : using namespace muse;
40 : using namespace mu::engraving;
41 : using namespace mu::engraving::rendering::score;
42 :
43 9891 : void Autoplace::autoplaceSegmentElement(const EngravingItem* item, EngravingItem::LayoutData* ldata, bool above, bool add)
44 : {
45 9891 : const Segment* segment = toSegment(item->findAncestor(ElementType::SEGMENT));
46 9891 : const Measure* measure = segment ? segment->measure() : nullptr;
47 9891 : const System* system = measure ? measure->system() : nullptr;
48 9891 : IF_ASSERT_FAILED(system) {
49 0 : return;
50 : }
51 9891 : LD_CONDITION(segment->ldata()->isSetPos());
52 9891 : LD_CONDITION(measure->ldata()->isSetPos());
53 :
54 9891 : const double spatium = item->style().spatium() * item->staff()->staffMag(item);
55 : // TODO: proper item-to-item table for horizontal clearance in skyline
56 9891 : const double minSkylineHorizontalClearance = item->isArticulationOrFermata() ? 0.0 : item->style().styleAbsolute(
57 7158 : Sid::skylineMinHorizontalClearance) * item->mag();
58 :
59 9891 : doAutoplaceElement(item, ldata, system, above, add, minSkylineHorizontalClearance, spatium);
60 : }
61 :
62 6609 : void Autoplace::autoplaceMeasureElement(const EngravingItem* item, EngravingItem::LayoutData* ldata, bool above, bool add)
63 : {
64 6609 : const Measure* measure = toMeasure(item->ownershipParent());
65 6609 : const System* system = measure ? measure->system() : nullptr;
66 6609 : IF_ASSERT_FAILED(system) {
67 0 : return;
68 : }
69 6609 : LD_CONDITION(measure->ldata()->isSetPos());
70 :
71 6609 : const double spatium = item->style().spatium() * item->staff()->staffMag(item);
72 : // TODO: check this value for measure-owned items
73 : // const double minSkylineHorizontalClearance = item->isMeasureNumber() ? 0.0 : item->style().styleAbsolute(
74 : // Sid::skylineMinHorizontalClearance) * item->mag();
75 6609 : const double minSkylineHorizontalClearance = 0.0;
76 6609 : doAutoplaceElement(item, ldata, system, above, add, minSkylineHorizontalClearance, spatium);
77 : }
78 :
79 1411 : void Autoplace::autoplaceSpannerSegment(const SpannerSegment* item, EngravingItem::LayoutData* ldata)
80 : {
81 1411 : if (item->spanner()->anchor() == Spanner::Anchor::NOTE) {
82 0 : return;
83 : }
84 1411 : const System* system = item->system();
85 1411 : IF_ASSERT_FAILED(system) {
86 0 : return;
87 : }
88 1411 : const double mag = !item->systemFlag() && !item->spanner()->systemFlag() ? item->staff()->staffMag(item->spanner()->tick()) : 1.0;
89 1411 : const double spatium = item->style().spatium() * mag;
90 :
91 1411 : const bool above = item->spanner()->placeAbove();
92 1411 : doAutoplaceElement(item, ldata, system, above, /* add = */ false, /* minSkylineHorizontalClearance = */ 0.0, spatium);
93 : }
94 :
95 17905 : double Autoplace::distanceToMoveItem(const EngravingItem* item, const SkylineLine& staffSkyline, const Shape& shape, bool above,
96 : double minSkylineHorizontalClearance, double minDistance)
97 : {
98 35810 : const SkylineLine filteredSkyline = staffSkyline.getFilteredCopy([item](const ShapeElement& shapeEl) {
99 391285 : const EngravingItem* skylineItem = shapeEl.item();
100 391285 : if (!skylineItem) {
101 5176 : return false;
102 : }
103 386109 : return itemsShouldIgnoreEachOther(item, skylineItem);
104 17905 : });
105 :
106 17905 : if (filteredSkyline.elements().empty()) {
107 1315 : return 0.0;
108 : }
109 :
110 16590 : const double d = above ? filteredSkyline.minDistanceToShapeAbove(shape, minSkylineHorizontalClearance)
111 2991 : : filteredSkyline.minDistanceToShapeBelow(shape, minSkylineHorizontalClearance);
112 :
113 16590 : if (!(d > -minDistance)) {
114 10387 : return 0.0;
115 : }
116 :
117 6203 : const double yd = (d + minDistance) * (above ? -1.0 : 1.0);
118 :
119 6203 : return yd;
120 17905 : }
121 :
122 17911 : void Autoplace::doAutoplaceElement(const EngravingItem* item, EngravingItem::LayoutData* ldata, const System* system, bool above, bool add,
123 : double minSkylineHorizontalClearance, double spatium)
124 : {
125 17911 : DO_ASSERT(autoplaceAppliesToType(item->type()));
126 17911 : IF_ASSERT_FAILED(system) {
127 6 : return;
128 : }
129 :
130 17911 : if (!item->autoplace() || !item->ownershipParent()) {
131 6 : return;
132 : }
133 :
134 17905 : const staff_idx_t staffIdx = item->effectiveStaffIdx();
135 17905 : if (staffIdx == muse::nidx) {
136 0 : return;
137 : }
138 :
139 17905 : LD_CONDITION(ldata->isSetPos());
140 17905 : LD_CONDITION(ldata->isSetBbox());
141 :
142 17905 : const double minDistance = item->minDistance().toAbsolute(spatium);
143 :
144 17905 : SysStaff* sysStaff = system->staff(staffIdx);
145 17905 : SkylineLine& staffSkyline = above ? sysStaff->skyline().north() : sysStaff->skyline().south();
146 : // TODO [J.M] staffOffset will be applied twice for spanner segments.
147 : // We really need to sort out staffOffset and have it applied as early and infrequently as possible. Can't keep up with ~30 callsites
148 : // Shape shape = item->ldata()->shape().translate(item->systemPos() - item->offset() + item->staffOffset());
149 17905 : Shape shape = item->ldata()->shape();
150 17905 : if (item->isSpannerSegment()) {
151 1411 : shape.translate(item->ldata()->pos());
152 : } else {
153 16494 : shape.translate(item->systemPos() - item->offset() + item->staffOffset());
154 : }
155 :
156 17905 : const double yd = distanceToMoveItem(item, staffSkyline, shape, above, minSkylineHorizontalClearance, minDistance);
157 :
158 17905 : ldata->moveY(yd);
159 17905 : shape.translateY(yd);
160 :
161 17905 : if (add && item->addToSkyline()) {
162 16324 : staffSkyline.add(shape.translated(item->offset()));
163 : }
164 17905 : }
165 :
166 416161 : bool Autoplace::itemsShouldIgnoreEachOther(const EngravingItem* itemToAutoplace, const EngravingItem* itemInSkyline)
167 : {
168 416161 : if (itemToAutoplace == itemInSkyline) {
169 29 : return true;
170 : }
171 :
172 416132 : const EngravingItem* skylineItemOwner = itemInSkyline->ownershipParentItem();
173 416132 : if (itemInSkyline->isText() && skylineItemOwner && skylineItemOwner->isSLineSegment()) {
174 0 : return itemsShouldIgnoreEachOther(itemToAutoplace, skylineItemOwner);
175 : }
176 :
177 416132 : ElementType type1 = itemToAutoplace->type();
178 416132 : ElementType type2 = itemInSkyline->type();
179 :
180 416132 : if (type1 == ElementType::TIMESIG) {
181 0 : return type2 != ElementType::KEYSIG;
182 : }
183 :
184 416132 : if (type1 == ElementType::FRET_DIAGRAM && (type2 == ElementType::FRET_DIAGRAM || type2 == ElementType::HARMONY)) {
185 754 : bool isFretDiagAgainstItsOwnHarmony = skylineItemOwner == itemToAutoplace;
186 754 : bool areOnDifferentSegments = itemToAutoplace->findAncestor(ElementType::SEGMENT)
187 754 : != itemInSkyline->findAncestor(ElementType::SEGMENT);
188 754 : return isFretDiagAgainstItsOwnHarmony || areOnDifferentSegments;
189 : }
190 :
191 415378 : if ((type1 == ElementType::DYNAMIC || type1 == ElementType::HAIRPIN_SEGMENT)
192 75762 : && (type2 == ElementType::DYNAMIC || type2 == ElementType::HAIRPIN_SEGMENT)) {
193 3921 : return true;
194 : }
195 :
196 411457 : if (itemToAutoplace->isLyricsLineSegment() && itemInSkyline->isLyricsLineSegment()) {
197 0 : const LyricsLineSegment* a = toLyricsLineSegment(itemToAutoplace);
198 0 : const LyricsLineSegment* b = toLyricsLineSegment(itemInSkyline);
199 0 : return a->verse() == b->verse() && a->lyricsPlaceAbove() == b->lyricsPlaceAbove();
200 : }
201 :
202 411457 : if (itemToAutoplace->isTuplet() && itemInSkyline->isTuplet()) {
203 : // A tuplet avoids the tuplets nested inside it
204 26720 : for (const Tuplet* t = toTuplet(itemInSkyline)->tuplet(); t; t = t->tuplet()) {
205 10692 : if (t == itemToAutoplace) {
206 4172 : return false;
207 : }
208 : }
209 : }
210 :
211 407285 : if (type1 == type2) {
212 : // Items of same type should ignore each other in most cases
213 : static const std::set<ElementType> TEXT_BASED_TYPES_WHICH_IGNORE_EACH_OTHER {
214 : ElementType::DYNAMIC,
215 : ElementType::EXPRESSION,
216 : ElementType::STICKING
217 37691 : };
218 37665 : return !itemToAutoplace->isTextBase() || muse::contains(TEXT_BASED_TYPES_WHICH_IGNORE_EACH_OTHER, type1);
219 : }
220 :
221 369620 : if ((type1 == ElementType::DYNAMIC || type1 == ElementType::EXPRESSION)
222 41873 : && (type2 == ElementType::DYNAMIC || type2 == ElementType::EXPRESSION)) {
223 : // Dynamics and expressions should ignore each other if on the same segment
224 68 : return itemToAutoplace->parent() == itemInSkyline->parent();
225 : }
226 :
227 369552 : if ((type1 == ElementType::TUPLET || type1 == ElementType::STAFF_LINES)
228 81634 : && (type2 == ElementType::STAFF_LINES || type2 == ElementType::TUPLET)) {
229 6088 : const Score* score = itemToAutoplace->score();
230 6088 : const bool outOfStaff = score ? !score->style().styleB(Sid::tupletOutOfStaff) : false;
231 6088 : return outOfStaff;
232 : }
233 :
234 363464 : if ((type1 == ElementType::FIGURED_BASS || type1 == ElementType::FIGURED_BASS_ITEM)
235 2332 : && (type2 == ElementType::FIGURED_BASS || type2 == ElementType::FIGURED_BASS_ITEM)) {
236 651 : return true;
237 : }
238 :
239 362813 : if (itemToAutoplace->isArticulationOrFermata() && itemInSkyline->isArticulationOrFermata()) {
240 : // Ignore fermatas and articulations on other segments
241 1715 : return itemToAutoplace->findAncestor(ElementType::SEGMENT) != itemInSkyline->findAncestor(ElementType::SEGMENT);
242 : }
243 :
244 361098 : return itemToAutoplace->ldata()->itemSnappedBefore() == itemInSkyline || itemToAutoplace->ldata()->itemSnappedAfter() == itemInSkyline;
245 : }
|