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 <cfloat>
23 :
24 : #include "tlayout.h"
25 :
26 : #include "global/realfn.h"
27 : #include "global/types/number.h"
28 : #include "draw/fontmetrics.h"
29 :
30 : #include "iengravingconfiguration.h" // IWYU pragma: keep
31 : #include "iengravingfont.h"
32 :
33 : #include "infrastructure/rtti.h"
34 : #include "infrastructure/ld_access.h"
35 :
36 : #include "types/typesconv.h"
37 : #include "types/symnames.h"
38 : #include "dom/score.h"
39 : #include "dom/utils.h"
40 :
41 : #include "dom/accidental.h"
42 : #include "dom/ambitus.h"
43 : #include "dom/arpeggio.h"
44 : #include "dom/articulation.h"
45 :
46 : #include "dom/barline.h"
47 : #include "dom/beam.h"
48 : #include "dom/bend.h"
49 : #include "dom/box.h"
50 : #include "dom/bracket.h"
51 : #include "dom/breath.h"
52 :
53 : #include "dom/chord.h"
54 : #include "dom/chordline.h"
55 : #include "dom/clef.h"
56 : #include "dom/capo.h"
57 :
58 : #include "dom/deadslapped.h"
59 : #include "dom/dynamic.h"
60 :
61 : #include "dom/expression.h"
62 :
63 : #include "dom/fermata.h"
64 : #include "dom/figuredbass.h"
65 : #include "dom/fingering.h"
66 : #include "dom/fret.h"
67 :
68 : #include "dom/glissando.h"
69 : #include "dom/gradualtempochange.h"
70 : #include "dom/guitarbend.h"
71 :
72 : #include "dom/hairpin.h"
73 : #include "dom/hammeronpulloff.h"
74 : #include "dom/harppedaldiagram.h"
75 : #include "dom/harmonicmark.h"
76 : #include "dom/harmony.h"
77 : #include "dom/hook.h"
78 :
79 : #include "dom/image.h"
80 : #include "dom/instrchange.h"
81 : #include "dom/instrumentname.h"
82 :
83 : #include "dom/jianpudurationline.h"
84 : #include "dom/jianpuoctavedot.h"
85 : #include "dom/jump.h"
86 :
87 : #include "dom/keysig.h"
88 :
89 : #include "dom/layoutbreak.h"
90 : #include "dom/ledgerline.h"
91 : #include "dom/letring.h"
92 : #include "dom/line.h"
93 : #include "dom/linkedobjects.h"
94 : #include "dom/lyrics.h"
95 :
96 : #include "dom/marker.h"
97 : #include "dom/measure.h"
98 : #include "dom/measurebase.h"
99 : #include "dom/measurenumber.h"
100 : #include "dom/measurenumberbase.h"
101 : #include "dom/measurerepeat.h"
102 : #include "dom/mmrest.h"
103 : #include "dom/mmrestrange.h"
104 :
105 : #include "dom/note.h"
106 : #include "dom/notedot.h"
107 : #include "dom/noteline.h"
108 :
109 : #include "dom/ornament.h"
110 : #include "dom/ottava.h"
111 :
112 : #include "dom/page.h"
113 : #include "dom/palmmute.h"
114 : #include "dom/parenthesis.h"
115 : #include "dom/part.h"
116 : #include "dom/pedal.h"
117 : #include "dom/pickscrape.h"
118 : #include "dom/playtechannotation.h"
119 :
120 : #include "dom/rasgueado.h"
121 : #include "dom/rehearsalmark.h"
122 : #include "dom/rest.h"
123 :
124 : #include "dom/shadownote.h"
125 : #include "dom/slur.h"
126 : #include "dom/spacer.h"
127 : #include "dom/staff.h"
128 : #include "dom/stafflines.h"
129 : #include "dom/staffstate.h"
130 : #include "dom/stafftext.h"
131 : #include "dom/stafftype.h"
132 : #include "dom/stafftypechange.h"
133 : #include "dom/stem.h"
134 : #include "dom/stemslash.h"
135 : #include "dom/sticking.h"
136 : #include "dom/stringtunings.h"
137 : #include "dom/symbol.h"
138 : #include "dom/system.h"
139 : #include "dom/systemdivider.h"
140 : #include "dom/systemlockindicator.h"
141 : #include "dom/systemtext.h"
142 : #include "dom/soundflag.h"
143 :
144 : #include "dom/tapping.h"
145 : #include "dom/tempotext.h"
146 : #include "dom/text.h"
147 : #include "dom/textline.h"
148 : #include "dom/tie.h"
149 : #include "dom/timesig.h"
150 : #include "dom/tremolosinglechord.h"
151 : #include "dom/tremolotwochord.h"
152 : #include "dom/tremolobar.h"
153 : #include "dom/trill.h"
154 : #include "dom/tripletfeel.h"
155 : #include "dom/tuplet.h"
156 :
157 : #include "dom/vibrato.h"
158 : #include "dom/volta.h"
159 :
160 : #include "dom/whammybar.h"
161 :
162 : #include "dom/factory.h"
163 :
164 : #include "editing/editchord.h"
165 : #include "editing/navigation.h"
166 : #include "editing/noteinput.h"
167 :
168 : #include "accidentalslayout.h"
169 : #include "arpeggiolayout.h"
170 : #include "autoplace.h"
171 : #include "beamlayout.h"
172 : #include "boxlayout.h"
173 : #include "chordlayout.h"
174 : #include "dynamicslayout.h"
175 : #include "guitarbendlayout.h"
176 : #include "lyricslayout.h"
177 : #include "masklayout.h"
178 : #include "slurtielayout.h"
179 : #include "tremololayout.h"
180 : #include "tupletlayout.h"
181 : #include "horizontalspacing.h"
182 : #include "measurelayout.h"
183 : #include "tappinglayout.h"
184 : #include "harmonylayout.h"
185 : #include "markerlayout.h"
186 : #include "measurenumberlayout.h"
187 : #include "parenthesislayout.h"
188 : #include "restlayout.h"
189 : #include "textlayout.h"
190 :
191 : using namespace muse;
192 : using namespace muse::draw;
193 : using namespace mu::engraving;
194 : using namespace mu::engraving::rtti;
195 : using namespace mu::engraving::rendering::score;
196 :
197 : #define LAYOUT_CALL_ITEM(item) LAYOUT_CALL() << LAYOUT_ITEM_INFO(item);
198 :
199 162821 : void TLayout::layoutItem(EngravingItem* item, LayoutContext& ctx)
200 : {
201 : //DO_ASSERT(!ctx.conf().isPaletteMode());
202 :
203 162821 : EngravingItem::LayoutData* ldata = item->mutldata();
204 :
205 162821 : switch (item->type()) {
206 0 : case ElementType::ACCIDENTAL:
207 0 : layoutAccidental(item_cast<const Accidental*>(item), static_cast<Accidental::LayoutData*>(ldata), ctx.conf());
208 0 : break;
209 0 : case ElementType::ACTION_ICON:
210 0 : layoutActionIcon(item_cast<const ActionIcon*>(item), static_cast<ActionIcon::LayoutData*>(ldata));
211 0 : break;
212 0 : case ElementType::AMBITUS:
213 0 : layoutAmbitus(item_cast<const Ambitus*>(item), static_cast<Ambitus::LayoutData*>(ldata), ctx);
214 0 : break;
215 445 : case ElementType::ARPEGGIO:
216 445 : layoutArpeggio(item_cast<const Arpeggio*>(item), static_cast<Arpeggio::LayoutData*>(ldata), ctx.conf());
217 445 : break;
218 0 : case ElementType::CHORD_BRACKET:
219 0 : layoutChordBracket(item_cast<const ChordBracket*>(item), static_cast<ChordBracket::LayoutData*>(ldata), ctx.conf());
220 0 : break;
221 2684 : case ElementType::ARTICULATION:
222 2684 : layoutArticulation(item_cast<Articulation*>(item), static_cast<Articulation::LayoutData*>(ldata));
223 2684 : break;
224 0 : case ElementType::BAR_LINE:
225 0 : layoutBarLine(item_cast<const BarLine*>(item), static_cast<BarLine::LayoutData*>(ldata), ctx);
226 0 : break;
227 0 : case ElementType::BEAM: layoutBeam(item_cast<Beam*>(item), ctx);
228 0 : break;
229 4 : case ElementType::BEND:
230 4 : layoutBend(item_cast<const Bend*>(item), static_cast<Bend::LayoutData*>(ldata));
231 4 : break;
232 147 : case ElementType::HBOX:
233 147 : layoutHBox(item_cast<const HBox*>(item), static_cast<HBox::LayoutData*>(ldata), ctx);
234 147 : break;
235 0 : case ElementType::VBOX:
236 0 : layoutVBox(item_cast<const VBox*>(item), static_cast<VBox::LayoutData*>(ldata), ctx);
237 0 : break;
238 0 : case ElementType::FBOX:
239 0 : layoutFBox(item_cast<const FBox*>(item), static_cast<FBox::LayoutData*>(ldata), ctx);
240 0 : break;
241 0 : case ElementType::TBOX:
242 0 : layoutTBox(item_cast<const TBox*>(item), static_cast<TBox::LayoutData*>(ldata), ctx);
243 0 : break;
244 0 : case ElementType::BRACKET:
245 0 : layoutBracket(item_cast<const Bracket*>(item), static_cast<Bracket::LayoutData*>(ldata), ctx.conf());
246 0 : break;
247 168 : case ElementType::BREATH:
248 168 : layoutBreath(item_cast<const Breath*>(item), static_cast<Breath::LayoutData*>(ldata), ctx.conf());
249 168 : break;
250 54792 : case ElementType::CHORD: layoutChord(item_cast<Chord*>(item), ctx);
251 54792 : break;
252 168 : case ElementType::CHORDLINE:
253 168 : layoutChordLine(item_cast<const ChordLine*>(item), static_cast<ChordLine::LayoutData*>(ldata), ctx.conf());
254 168 : break;
255 0 : case ElementType::CLEF:
256 0 : layoutClef(item_cast<const Clef*>(item), static_cast<Clef::LayoutData*>(ldata), ctx.conf());
257 0 : break;
258 44 : case ElementType::CAPO:
259 44 : layoutCapo(item_cast<const Capo*>(item), static_cast<Capo::LayoutData*>(ldata), ctx);
260 44 : break;
261 0 : case ElementType::DEAD_SLAPPED:
262 0 : layoutDeadSlapped(item_cast<const DeadSlapped*>(item), static_cast<DeadSlapped::LayoutData*>(ldata));
263 0 : break;
264 1145 : case ElementType::DYNAMIC:
265 1145 : layoutDynamic(item_cast<Dynamic*>(item), static_cast<Dynamic::LayoutData*>(ldata), ctx.conf());
266 1145 : break;
267 34 : case ElementType::EXPRESSION:
268 34 : layoutExpression(item_cast<const Expression*>(item), static_cast<Expression::LayoutData*>(ldata));
269 34 : break;
270 566 : case ElementType::FERMATA:
271 566 : layoutFermata(item_cast<const Fermata*>(item), static_cast<Fermata::LayoutData*>(ldata));
272 566 : break;
273 161 : case ElementType::FIGURED_BASS:
274 161 : layoutFiguredBass(item_cast<const FiguredBass*>(item), static_cast<FiguredBass::LayoutData*>(ldata), ctx);
275 161 : break;
276 0 : case ElementType::FINGERING:
277 0 : layoutFingering(item_cast<const Fingering*>(item), static_cast<Fingering::LayoutData*>(ldata));
278 0 : break;
279 404 : case ElementType::FRET_DIAGRAM:
280 404 : layoutFretDiagram(item_cast<const FretDiagram*>(item), static_cast<FretDiagram::LayoutData*>(ldata), ctx);
281 404 : break;
282 237 : case ElementType::GLISSANDO: layoutGlissando(item_cast<Glissando*>(item), ctx);
283 237 : break;
284 976 : case ElementType::GLISSANDO_SEGMENT: layoutGlissandoSegment(item_cast<GlissandoSegment*>(item), ctx);
285 976 : break;
286 0 : case ElementType::GRADUAL_TEMPO_CHANGE: layoutGradualTempoChange(item_cast<GradualTempoChange*>(item), ctx);
287 0 : break;
288 22 : case ElementType::GRADUAL_TEMPO_CHANGE_SEGMENT: layoutGradualTempoChangeSegment(item_cast<GradualTempoChangeSegment*>(item), ctx);
289 22 : break;
290 409 : case ElementType::GUITAR_BEND: layoutGuitarBend(item_cast<GuitarBend*>(item), ctx);
291 409 : break;
292 1250 : case ElementType::GUITAR_BEND_SEGMENT: layoutGuitarBendSegment(item_cast<GuitarBendSegment*>(item), ctx);
293 1250 : break;
294 6 : case ElementType::GUITAR_BEND_HOLD_SEGMENT: GuitarBendLayout::layoutHoldLine(item_cast<GuitarBendHoldSegment*>(item));
295 6 : break;
296 0 : case ElementType::HAIRPIN: layoutHairpin(item_cast<Hairpin*>(item), ctx);
297 0 : break;
298 483 : case ElementType::HAIRPIN_SEGMENT: layoutHairpinSegment(item_cast<HairpinSegment*>(item), ctx);
299 483 : break;
300 0 : case ElementType::HAMMER_ON_PULL_OFF: layoutHammerOnPullOff(item_cast<HammerOnPullOff*>(item), ctx);
301 0 : break;
302 0 : case ElementType::HAMMER_ON_PULL_OFF_SEGMENT: layoutHammerOnPullOffSegment(item_cast<HammerOnPullOffSegment*>(item), ctx);
303 0 : break;
304 76 : case ElementType::HAMMER_ON_PULL_OFF_TEXT: layoutHammerOnPullOffText(item_cast<HammerOnPullOffText*>(item), ctx);
305 76 : break;
306 32 : case ElementType::HARP_DIAGRAM:
307 32 : layoutHarpPedalDiagram(item_cast<const HarpPedalDiagram*>(item), static_cast<HarpPedalDiagram::LayoutData*>(ldata));
308 32 : break;
309 2280 : case ElementType::HARMONY:
310 2280 : layoutHarmony(item_cast<Harmony*>(item), static_cast<Harmony::LayoutData*>(ldata), ctx);
311 2280 : break;
312 29 : case ElementType::HARMONIC_MARK_SEGMENT: layoutHarmonicMarkSegment(item_cast<HarmonicMarkSegment*>(item), ctx);
313 29 : break;
314 0 : case ElementType::HOOK:
315 0 : layoutHook(item_cast<const Hook*>(item), static_cast<Hook::LayoutData*>(ldata));
316 0 : break;
317 60 : case ElementType::IMAGE:
318 60 : layoutImage(item_cast<const Image*>(item), static_cast<Image::LayoutData*>(ldata));
319 60 : break;
320 94 : case ElementType::INSTRUMENT_CHANGE:
321 94 : layoutInstrumentChange(item_cast<const InstrumentChange*>(item), static_cast<InstrumentChange::LayoutData*>(ldata));
322 94 : break;
323 0 : case ElementType::INSTRUMENT_NAME:
324 0 : layoutInstrumentName(item_cast<const InstrumentName*>(item), static_cast<InstrumentName::LayoutData*>(ldata));
325 0 : break;
326 0 : case ElementType::JIANPU_DURATION_LINE:
327 0 : layoutJianpuDurationLine(item_cast<JianpuDurationLine*>(item), ctx);
328 0 : break;
329 0 : case ElementType::JIANPU_OCTAVE_DOT:
330 0 : layoutJianpuOctaveDot(item_cast<JianpuOctaveDot*>(item), ctx);
331 0 : break;
332 216 : case ElementType::JUMP:
333 216 : layoutJump(item_cast<const Jump*>(item), static_cast<Jump::LayoutData*>(ldata));
334 216 : break;
335 0 : case ElementType::KEYSIG:
336 0 : layoutKeySig(item_cast<const KeySig*>(item), static_cast<KeySig::LayoutData*>(ldata), ctx.conf());
337 0 : break;
338 0 : case ElementType::LAISSEZ_VIB:
339 0 : layoutLaissezVib(item_cast<LaissezVib*>(item));
340 0 : break;
341 2623 : case ElementType::LAYOUT_BREAK:
342 2623 : layoutLayoutBreak(item_cast<const LayoutBreak*>(item), static_cast<LayoutBreak::LayoutData*>(ldata));
343 2623 : break;
344 : // TODO: case ElementType::STAFF_VISIBILTY_INDICATOR:
345 0 : case ElementType::SYSTEM_LOCK_INDICATOR:
346 0 : layoutSystemIndicatorIcon(item_cast<const SystemIndicatorIcon*>(item), static_cast<IndicatorIcon::LayoutData*>(ldata));
347 0 : break;
348 53 : case ElementType::STAVE_SHARING_CHANGE:
349 53 : layoutStaveSharingChange(item_cast<const StaveSharingChange*>(item), static_cast<IndicatorIcon::LayoutData*>(ldata));
350 53 : break;
351 0 : case ElementType::LET_RING: layoutLetRing(item_cast<LetRing*>(item), ctx);
352 0 : break;
353 31 : case ElementType::LET_RING_SEGMENT: layoutLetRingSegment(item_cast<LetRingSegment*>(item), ctx);
354 31 : break;
355 0 : case ElementType::LEDGER_LINE: layoutLedgerLine(item_cast<LedgerLine*>(item), ctx);
356 0 : break;
357 0 : case ElementType::LYRICS: layoutLyrics(item_cast<Lyrics*>(item), ctx);
358 0 : break;
359 0 : case ElementType::LYRICSLINE_SEGMENT: layoutLyricsLineSegment(item_cast<LyricsLineSegment*>(item), ctx);
360 0 : break;
361 9 : case ElementType::PARTIAL_LYRICSLINE_SEGMENT: layoutLyricsLineSegment(item_cast<LyricsLineSegment*>(item), ctx);
362 9 : break;
363 309 : case ElementType::MARKER:
364 309 : layoutMarker(item_cast<Marker*>(item), static_cast<Marker::LayoutData*>(ldata), ctx);
365 309 : break;
366 0 : case ElementType::MEASURE_NUMBER:
367 0 : layoutMeasureNumber(item_cast<MeasureNumber*>(item), static_cast<MeasureNumber::LayoutData*>(ldata), ctx);
368 0 : break;
369 376 : case ElementType::MEASURE_REPEAT:
370 376 : layoutMeasureRepeat(item_cast<const MeasureRepeat*>(item), static_cast<MeasureRepeat::LayoutData*>(ldata), ctx);
371 376 : break;
372 225 : case ElementType::MMREST:
373 225 : layoutMMRest(item_cast<const MMRest*>(item), static_cast<MMRest::LayoutData*>(ldata), ctx);
374 225 : break;
375 0 : case ElementType::MMREST_RANGE:
376 0 : layoutMMRestRange(item_cast<MMRestRange*>(item), static_cast<MMRestRange::LayoutData*>(ldata), ctx);
377 0 : break;
378 0 : case ElementType::NOTE:
379 0 : layoutNote(item_cast<const Note*>(item), static_cast<Note::LayoutData*>(ldata));
380 0 : break;
381 0 : case ElementType::NOTEDOT:
382 0 : layoutNoteDot(item_cast<const NoteDot*>(item), static_cast<NoteDot::LayoutData*>(ldata));
383 0 : break;
384 0 : case ElementType::NOTEHEAD:
385 0 : layoutSymbol(item_cast<const NoteHead*>(item), static_cast<NoteHead::LayoutData*>(ldata), ctx);
386 0 : break;
387 3 : case ElementType::NOTELINE:
388 3 : layoutNoteLine(item_cast<NoteLine*>(item), ctx);
389 3 : break;
390 12 : case ElementType::NOTELINE_SEGMENT:
391 12 : layoutNoteLineSegment(item_cast<NoteLineSegment*>(item), ctx);
392 12 : break;
393 636 : case ElementType::ORNAMENT:
394 636 : layoutOrnament(item_cast<const Ornament*>(item), static_cast<Ornament::LayoutData*>(ldata), ctx.conf());
395 636 : break;
396 0 : case ElementType::OTTAVA: layoutOttava(item_cast<Ottava*>(item), ctx);
397 0 : break;
398 161 : case ElementType::OTTAVA_SEGMENT: layoutOttavaSegment(item_cast<OttavaSegment*>(item), ctx);
399 161 : break;
400 0 : case ElementType::PALM_MUTE: layoutPalmMute(item_cast<PalmMute*>(item), ctx);
401 0 : break;
402 14 : case ElementType::PALM_MUTE_SEGMENT: layoutPalmMuteSegment(item_cast<PalmMuteSegment*>(item), ctx);
403 14 : break;
404 0 : case ElementType::PAGE_LOCK_INDICATOR:
405 0 : layoutPageLockIndicator(item_cast<const PageLockIndicator*>(item), static_cast<PageLockIndicator::LayoutData*>(ldata));
406 0 : break;
407 0 : case ElementType::PARENTHESIS: layoutParenthesis(item_cast<Parenthesis*>(item), static_cast<Parenthesis::LayoutData*>(ldata), ctx);
408 0 : break;
409 0 : case ElementType::PEDAL: layoutPedal(item_cast<Pedal*>(item), ctx);
410 0 : break;
411 119 : case ElementType::PEDAL_SEGMENT: layoutPedalSegment(item_cast<PedalSegment*>(item), ctx);
412 119 : break;
413 2 : case ElementType::PICK_SCRAPE_SEGMENT: layoutPickScrapeSegment(item_cast<PickScrapeSegment*>(item), ctx);
414 2 : break;
415 0 : case ElementType::PLAY_COUNT_TEXT:
416 0 : layoutPlayCountText(item_cast<PlayCountText*>(item), static_cast<PlayCountText::LayoutData*>(ldata));
417 0 : break;
418 87 : case ElementType::PLAYTECH_ANNOTATION:
419 87 : layoutPlayTechAnnotation(item_cast<const PlayTechAnnotation*>(item), static_cast<PlayTechAnnotation::LayoutData*>(ldata));
420 87 : break;
421 4 : case ElementType::RASGUEADO_SEGMENT: layoutRasgueadoSegment(item_cast<RasgueadoSegment*>(item), ctx);
422 4 : break;
423 127 : case ElementType::REHEARSAL_MARK:
424 127 : layoutRehearsalMark(item_cast<const RehearsalMark*>(item), static_cast<RehearsalMark::LayoutData*>(ldata));
425 127 : break;
426 75939 : case ElementType::REST:
427 75939 : layoutRest(item_cast<const Rest*>(item), static_cast<Rest::LayoutData*>(ldata), ctx);
428 75939 : break;
429 0 : case ElementType::SHADOW_NOTE: layoutShadowNote(item_cast<ShadowNote*>(item), ctx);
430 0 : break;
431 0 : case ElementType::SLUR: layoutSlur(item_cast<Slur*>(item), ctx);
432 0 : break;
433 0 : case ElementType::SPACER: layoutSpacer(item_cast<Spacer*>(item), ctx);
434 0 : break;
435 0 : case ElementType::STAFF_STATE:
436 0 : layoutStaffState(item_cast<const StaffState*>(item), static_cast<StaffState::LayoutData*>(ldata));
437 0 : break;
438 1018 : case ElementType::STAFF_TEXT:
439 1018 : layoutStaffText(item_cast<const StaffText*>(item), static_cast<StaffText::LayoutData*>(ldata));
440 1018 : break;
441 87 : case ElementType::STAVE_SHARING_LABEL:
442 87 : layoutStaveSharingLabel(item_cast<const StaveSharingLabel*>(item), static_cast<StaffText::LayoutData*>(ldata));
443 87 : break;
444 19 : case ElementType::STAFFTYPE_CHANGE:
445 19 : layoutStaffTypeChange(item_cast<const StaffTypeChange*>(item), static_cast<StaffTypeChange::LayoutData*>(ldata), ctx.conf());
446 19 : break;
447 0 : case ElementType::SOUND_FLAG:
448 0 : layoutSoundFlag(item_cast<const SoundFlag*>(item), static_cast<SoundFlag::LayoutData*>(ldata));
449 0 : break;
450 0 : case ElementType::STEM:
451 0 : layoutStem(item_cast<const Stem*>(item), static_cast<Stem::LayoutData*>(ldata), ctx.conf());
452 0 : break;
453 0 : case ElementType::STEM_SLASH:
454 0 : layoutStemSlash(item_cast<const StemSlash*>(item), static_cast<StemSlash::LayoutData*>(ldata), ctx.conf());
455 0 : break;
456 131 : case ElementType::STICKING:
457 131 : layoutSticking(item_cast<const Sticking*>(item), static_cast<Sticking::LayoutData*>(ldata));
458 131 : break;
459 19 : case ElementType::STRING_TUNINGS:
460 19 : layoutStringTunings(item_cast<StringTunings*>(item), ctx);
461 19 : break;
462 50 : case ElementType::SYMBOL:
463 50 : layoutSymbol(item_cast<const Symbol*>(item), static_cast<Symbol::LayoutData*>(ldata), ctx);
464 50 : break;
465 0 : case ElementType::FSYMBOL:
466 0 : layoutFSymbol(item_cast<const FSymbol*>(item), static_cast<FSymbol::LayoutData*>(ldata));
467 0 : break;
468 0 : case ElementType::SYSTEM_DIVIDER:
469 0 : layoutSystemDivider(item_cast<const SystemDivider*>(item), static_cast<SystemDivider::LayoutData*>(ldata), ctx);
470 0 : break;
471 0 : case ElementType::SYSTEM_TEXT:
472 0 : layoutSystemText(item_cast<const SystemText*>(item), static_cast<SystemText::LayoutData*>(ldata));
473 0 : break;
474 134 : case ElementType::TAPPING:
475 134 : layoutTapping(toTapping(item), static_cast<Tapping::LayoutData*>(ldata), ctx);
476 134 : break;
477 0 : case ElementType::TAPPING_HALF_SLUR:
478 0 : layoutTappingHalfSlur(toTappingHalfSlur(item));
479 0 : break;
480 1194 : case ElementType::TEMPO_TEXT:
481 1194 : layoutTempoText(item_cast<const TempoText*>(item), static_cast<TempoText::LayoutData*>(ldata));
482 1194 : break;
483 11396 : case ElementType::TEXT:
484 11396 : layoutText(item_cast<const Text*>(item), static_cast<Text::LayoutData*>(ldata));
485 11396 : break;
486 0 : case ElementType::TEXTLINE: layoutTextLine(item_cast<TextLine*>(item), ctx);
487 0 : break;
488 55 : case ElementType::TEXTLINE_SEGMENT: layoutTextLineSegment(item_cast<TextLineSegment*>(item), ctx);
489 55 : break;
490 0 : case ElementType::TIE: layoutTie(item_cast<Tie*>(item), ctx);
491 0 : break;
492 0 : case ElementType::TIMESIG:
493 0 : layoutTimeSig(item_cast<const TimeSig*>(item), static_cast<TimeSig::LayoutData*>(ldata), ctx);
494 0 : break;
495 498 : case ElementType::TIME_TICK_ANCHOR: layoutTimeTickAnchor(static_cast<TimeTickAnchor*>(item), ctx);
496 498 : break;
497 0 : case ElementType::TREMOLO_SINGLECHORD: layoutTremoloSingle(item_cast<TremoloSingleChord*>(item), ctx);
498 0 : break;
499 0 : case ElementType::TREMOLO_TWOCHORD: layoutTremoloTwo(item_cast<TremoloTwoChord*>(item), ctx);
500 0 : break;
501 38 : case ElementType::TREMOLOBAR:
502 38 : layoutTremoloBar(item_cast<const TremoloBar*>(item), static_cast<TremoloBar::LayoutData*>(ldata));
503 38 : break;
504 0 : case ElementType::TRILL: layoutTrill(item_cast<Trill*>(item), ctx);
505 0 : break;
506 116 : case ElementType::TRILL_SEGMENT: layoutTrillSegment(item_cast<TrillSegment*>(item), ctx);
507 116 : break;
508 0 : case ElementType::TRIPLET_FEEL:
509 0 : layoutTripletFeel(item_cast<const TripletFeel*>(item), static_cast<TripletFeel::LayoutData*>(ldata));
510 0 : break;
511 0 : case ElementType::TUPLET: layoutTuplet(item_cast<Tuplet*>(item), ctx);
512 0 : break;
513 0 : case ElementType::VIBRATO: layoutVibrato(item_cast<Vibrato*>(item), ctx);
514 0 : break;
515 28 : case ElementType::VIBRATO_SEGMENT: layoutVibratoSegment(item_cast<VibratoSegment*>(item), ctx);
516 28 : break;
517 0 : case ElementType::VOLTA: layoutVolta(item_cast<Volta*>(item), ctx);
518 0 : break;
519 376 : case ElementType::VOLTA_SEGMENT: layoutVoltaSegment(item_cast<VoltaSegment*>(item), ctx);
520 376 : break;
521 0 : case ElementType::WHAMMY_BAR: layoutLine(item_cast<WhammyBar*>(item), ctx);
522 0 : break;
523 0 : case ElementType::WHAMMY_BAR_SEGMENT: layoutWhammyBarSegment(item_cast<WhammyBarSegment*>(item), ctx);
524 0 : break;
525 0 : default:
526 0 : LOGE() << "not found in layout types item: " << item->typeName();
527 0 : DO_ASSERT(false);
528 : }
529 162821 : }
530 :
531 4102 : void TLayout::layoutAccidental(const Accidental* item, Accidental::LayoutData* ldata, const LayoutConfiguration& conf)
532 : {
533 : LAYOUT_CALL_ITEM(item);
534 : LD_INDEPENDENT;
535 :
536 4102 : if (ldata->isValid()) {
537 95 : return;
538 : }
539 :
540 4017 : ldata->syms.clear();
541 :
542 : // TODO: remove Accidental in layout
543 : // don't show accidentals for tab or slash notation; Jianpu shows its
544 : // accidental as part of the digit label instead of this glyph
545 4017 : if (item->onTabStaff() || item->onJianpuStaff() || (item->note() && item->note()->fixed())) {
546 5 : ldata->setIsSkipDraw(true);
547 5 : return;
548 : }
549 4012 : ldata->setIsSkipDraw(false);
550 4012 : ldata->setPos(PointF());
551 :
552 4012 : if (item->accidentalType() == AccidentalType::NONE) {
553 5 : return;
554 : }
555 :
556 4007 : ldata->column = 0;
557 :
558 4007 : auto accidentalSingleSym = [](const Accidental* item) -> SymId
559 : {
560 : // if the accidental is standard (doubleflat, flat, natural, sharp or double sharp)
561 : // and it has either no bracket or parentheses, then we have glyphs straight from smufl.
562 :
563 4007 : if (item->bracket() == AccidentalBracket::PARENTHESIS && !item->parentNoteHasParentheses()) {
564 57 : switch (item->accidentalType()) {
565 4 : case AccidentalType::FLAT: return SymId::accidentalFlatParens;
566 4 : case AccidentalType::FLAT2: return SymId::accidentalDoubleFlatParens;
567 9 : case AccidentalType::NATURAL: return SymId::accidentalNaturalParens;
568 4 : case AccidentalType::SHARP: return SymId::accidentalSharpParens;
569 4 : case AccidentalType::SHARP2: return SymId::accidentalDoubleSharpParens;
570 32 : default:
571 32 : break;
572 : }
573 : }
574 3982 : return SymId::noSym;
575 : };
576 :
577 57 : auto accidentalBracketSyms = [](AccidentalBracket type) -> std::pair<SymId, SymId>
578 : {
579 57 : switch (type) {
580 57 : case AccidentalBracket::PARENTHESIS: return { SymId::accidentalParensLeft, SymId::accidentalParensRight };
581 0 : case AccidentalBracket::BRACKET: return { SymId::accidentalBracketLeft, SymId::accidentalBracketRight };
582 0 : case AccidentalBracket::BRACE: return { SymId::accidentalCombiningOpenCurlyBrace, SymId::accidentalCombiningCloseCurlyBrace };
583 0 : case AccidentalBracket::NONE: return { SymId::noSym, SymId::noSym };
584 : }
585 0 : return { SymId::noSym, SymId::noSym };
586 : };
587 :
588 4007 : Shape shape;
589 :
590 : // Single?
591 4007 : SymId singleSym = accidentalSingleSym(item);
592 4007 : if (singleSym != SymId::noSym && conf.engravingFont()->isValid(singleSym)) {
593 0 : Accidental::LayoutData::Sym s(singleSym, 0.0, 0.0);
594 0 : ldata->syms.push_back(s);
595 :
596 0 : shape.add(item->symShapeWithCutouts(singleSym));
597 : }
598 : // Multi
599 : else {
600 4007 : double margin = conf.styleAbsolute(Sid::bracketedAccidentalPadding);
601 4007 : double x = 0.0;
602 :
603 4007 : std::pair<SymId, SymId> bracketSyms;
604 4007 : bool isNeedBracket = item->bracket() != AccidentalBracket::NONE && !item->parentNoteHasParentheses();
605 4007 : if (isNeedBracket) {
606 57 : bracketSyms = accidentalBracketSyms(item->bracket());
607 : }
608 :
609 : // Left
610 4007 : if (bracketSyms.first != SymId::noSym) {
611 : Accidental::LayoutData::Sym ls(bracketSyms.first, 0.0,
612 57 : item->bracket() == AccidentalBracket::BRACE ? item->spatium() * 0.4 : 0.0);
613 57 : ldata->syms.push_back(ls);
614 57 : shape.add(item->symBbox(bracketSyms.first), item);
615 :
616 57 : x += item->symAdvance(bracketSyms.first) + margin;
617 : }
618 :
619 : // Main
620 4007 : SymId mainSym = item->symId();
621 4007 : Accidental::LayoutData::Sym ms(mainSym, x, 0.0);
622 4007 : ldata->syms.push_back(ms);
623 4007 : shape.add(item->symShapeWithCutouts(mainSym).translated(PointF(x, 0.0)));
624 :
625 : // Right
626 4007 : if (bracketSyms.second != SymId::noSym) {
627 57 : x += item->symAdvance(mainSym) + margin;
628 :
629 : Accidental::LayoutData::Sym rs(bracketSyms.second, x,
630 57 : item->bracket() == AccidentalBracket::BRACE ? item->spatium() * 0.4 : 0.0);
631 57 : ldata->syms.push_back(rs);
632 57 : shape.add(item->symBbox(bracketSyms.second).translated(x, 0.0), item);
633 : }
634 : }
635 :
636 4007 : ldata->setShape(shape);
637 4007 : }
638 :
639 0 : void TLayout::layoutActionIcon(const ActionIcon* item, ActionIcon::LayoutData* ldata)
640 : {
641 : LAYOUT_CALL_ITEM(item);
642 : LD_INDEPENDENT;
643 :
644 0 : if (ldata->isValid()) {
645 0 : return;
646 : }
647 :
648 0 : FontMetrics fontMetrics(item->iconFont());
649 0 : ldata->setBbox(fontMetrics.boundingRect(Char(item->icon())));
650 0 : ldata->setPos(PointF());
651 0 : }
652 :
653 2 : void TLayout::layoutAmbitus(const Ambitus* item, Ambitus::LayoutData* ldata, const LayoutContext& ctx)
654 : {
655 : LAYOUT_CALL_ITEM(item);
656 : LD_INDEPENDENT;
657 :
658 2 : if (ldata->isValid()) {
659 0 : return;
660 : }
661 :
662 2 : const double spatium = item->spatium();
663 2 : const double headWdt = item->headWidth();
664 :
665 2 : Accidental::LayoutData* topAccData = item->topAccidental()->mutldata();
666 2 : Accidental::LayoutData* bottomAccData = item->bottomAccidental()->mutldata();
667 :
668 2 : ldata->setPos(PointF());
669 :
670 : //
671 : // NOTEHEADS Y POS
672 : //
673 : {
674 2 : const Fraction tick = item->segment()->tick();
675 2 : const Staff* stf = ctx.dom().staff(item->staffIdx());
676 2 : const ClefType clf = stf->clef(tick);
677 2 : const Key key = stf->key(tick);
678 2 : const double lineDist = stf->lineDistance(tick) * spatium;
679 :
680 : // top notehead
681 2 : if (!pitchIsValid(item->topPitch()) || !tpcIsValid(item->topTpc())) {
682 0 : ldata->topPos.setY(0.0); // if uninitialized, set to top staff line
683 : } else {
684 2 : int topLine = Ambitus::staffLine(item->topTpc(), item->topPitch(), clf);
685 2 : ldata->topPos.setY(topLine * lineDist * 0.5);
686 : // compute accidental
687 2 : AccidentalType accidType = Ambitus::accidentalType(item->topTpc(), key);
688 2 : item->topAccidental()->setAccidentalType(accidType);
689 2 : TLayout::layoutAccidental(item->topAccidental(), topAccData, ctx.conf());
690 2 : topAccData->setPosY(ldata->topPos.y());
691 : }
692 :
693 : // bottom notehead
694 2 : if (!pitchIsValid(item->bottomPitch()) || !tpcIsValid(item->bottomTpc())) {
695 0 : const int numOfLines = stf->lines(tick);
696 0 : ldata->bottomPos.setY((numOfLines - 1) * lineDist); // if uninitialized, set to last staff line
697 : } else {
698 2 : int bottomLine = Ambitus::staffLine(item->bottomTpc(), item->bottomPitch(), clf);
699 2 : ldata->bottomPos.setY(bottomLine * lineDist * 0.5);
700 : // compute accidental
701 2 : AccidentalType accidType = Ambitus::accidentalType(item->bottomTpc(), key);
702 2 : item->bottomAccidental()->setAccidentalType(accidType);
703 2 : TLayout::layoutAccidental(item->bottomAccidental(), bottomAccData, ctx.conf());
704 2 : bottomAccData->setPosY(ldata->bottomPos.y());
705 : }
706 : }
707 :
708 : //
709 : // NOTEHEAD X POS
710 : //
711 : // Note: manages colliding accidentals
712 : //
713 : {
714 2 : double accNoteDist = item->absoluteFromSpatium(ctx.conf().styleS(Sid::accidentalNoteDistance));
715 2 : double xAccidOffTop = topAccData->bbox().width() + accNoteDist;
716 2 : double xAccidOffBottom = bottomAccData->bbox().width() + accNoteDist;
717 :
718 : // if top accidental extends down more than bottom accidental extends up,
719 : // AND ambitus is not leaning right, bottom accidental needs to be displaced
720 2 : bool collision = (item->direction() != DirectionH::RIGHT)
721 3 : && (topAccData->pos().y() + topAccData->bbox().y() + topAccData->bbox().height()
722 1 : > bottomAccData->pos().y() + bottomAccData->bbox().y());
723 2 : if (collision) {
724 : // displace bottom accidental (also attempting to 'undercut' flats)
725 0 : AccidentalType bottomAccType = item->bottomAccidental()->accidentalType();
726 0 : xAccidOffBottom = xAccidOffTop + ((bottomAccType == AccidentalType::FLAT
727 0 : || bottomAccType == AccidentalType::FLAT2
728 0 : || bottomAccType == AccidentalType::NATURAL)
729 0 : ? bottomAccData->bbox().width() * 0.5 : bottomAccData->bbox().width());
730 : }
731 :
732 2 : switch (item->direction()) {
733 1 : case DirectionH::AUTO: // noteheads one above the other
734 : // left align noteheads and right align accidentals 'hanging' on the left
735 1 : ldata->topPos.setX(0.0);
736 1 : ldata->bottomPos.setX(0.0);
737 1 : topAccData->setPosX(-xAccidOffTop);
738 1 : bottomAccData->setPosX(-xAccidOffBottom);
739 1 : break;
740 0 : case DirectionH::LEFT: // top notehead at the left of bottom notehead
741 : // place top notehead at left margin; bottom notehead at right of top head;
742 : // top accid. 'hanging' on left of top head and bottom accid. 'hanging' at left of bottom head
743 0 : ldata->topPos.setX(0.0);
744 0 : ldata->bottomPos.setX(headWdt);
745 0 : topAccData->setPosX(-xAccidOffTop);
746 0 : bottomAccData->setPosX(collision ? -xAccidOffBottom : headWdt - xAccidOffBottom);
747 0 : break;
748 1 : case DirectionH::RIGHT: // top notehead at the right of bottom notehead
749 : // bottom notehead at left margin; top notehead at right of bottomnotehead
750 : // top accid. 'hanging' on left of top head and bottom accid. 'hanging' at left of bottom head
751 1 : ldata->topPos.setX(headWdt);
752 1 : ldata->bottomPos.setX(0.0);
753 1 : topAccData->setPosX(headWdt - xAccidOffTop);
754 1 : bottomAccData->setPosX(-xAccidOffBottom);
755 1 : break;
756 : }
757 : }
758 :
759 : // LINE
760 : // compute line from top note centre to bottom note centre
761 : {
762 4 : LineF fullLine(ldata->topPos.x() + headWdt * 0.5,
763 : ldata->topPos.y(),
764 4 : ldata->bottomPos.x() + headWdt * 0.5,
765 2 : ldata->bottomPos.y());
766 : // shorten line on each side by offsets
767 2 : double yDelta = ldata->bottomPos.y() - ldata->topPos.y();
768 2 : if (!muse::RealIsNull(yDelta)) {
769 2 : double off = Ambitus::LINEOFFSET_DEFAULT.toAbsolute(spatium);
770 2 : PointF p1 = fullLine.pointAt(off / yDelta);
771 2 : PointF p2 = fullLine.pointAt(1 - (off / yDelta));
772 2 : ldata->line = LineF(p1, p2);
773 : } else {
774 0 : ldata->line = fullLine;
775 : }
776 : }
777 :
778 : // BBOX
779 : {
780 2 : RectF headRect(0, -0.5 * spatium, headWdt, 1 * spatium);
781 4 : ldata->setBbox(headRect.translated(ldata->topPos).united(headRect.translated(ldata->bottomPos))
782 2 : .united(topAccData->bbox().translated(topAccData->pos()))
783 4 : .united(bottomAccData->bbox().translated(bottomAccData->pos())));
784 : }
785 : }
786 :
787 466 : void TLayout::layoutArpeggio(const Arpeggio* item, Arpeggio::LayoutData* ldata, const LayoutConfiguration& conf)
788 : {
789 : LAYOUT_CALL_ITEM(item);
790 :
791 : //! NOTE Can be edited and relayout,
792 : //! in this case the reset layout data has not yet been done
793 : // if (ldata->isValid()) {
794 : // return;
795 : // }
796 :
797 466 : if (conf.styleB(Sid::arpeggioHiddenInStdIfTab)) {
798 0 : if (item->staff() && item->staff()->isPitchedStaff(item->tick())) {
799 0 : for (Staff* s : item->staff()->staffList()) {
800 0 : if (s->onSameScore(item) && s->isTabStaff(item->tick()) && s->visible()) {
801 0 : ldata->setIsSkipDraw(true);
802 : }
803 0 : }
804 : }
805 : }
806 466 : ldata->setIsSkipDraw(false);
807 466 : if (!ldata->isSetPos()) {
808 165 : ldata->setPos(PointF());
809 : }
810 :
811 466 : IF_ASSERT_FAILED(item->ownershipParent()) {
812 0 : return;
813 : }
814 :
815 : //! NOTE Must already be set previously
816 466 : Chord* parentChord = item->chord();
817 466 : LD_CONDITION(parentChord->upNote()->ldata()->isSetPos());
818 466 : LD_CONDITION(parentChord->downNote()->ldata()->isSetPos());
819 :
820 394 : auto symbolLine = [](const std::shared_ptr<const IEngravingFont>& f, Arpeggio::LayoutData* data, SymId end, SymId fill)
821 : {
822 394 : data->symbols.clear();
823 :
824 394 : double w = data->bottom - data->top;
825 394 : double w1 = f->advance(end, data->magS);
826 394 : double w2 = f->advance(fill, data->magS);
827 394 : int n = lrint((w - w1) / w2);
828 1803 : for (int i = 0; i < n; ++i) {
829 1409 : data->symbols.push_back(fill);
830 : }
831 394 : data->symbols.push_back(end);
832 394 : };
833 :
834 466 : ldata->arpeggioHeight = ArpeggioLayout::computeHeight(item);
835 466 : ldata->top = ArpeggioLayout::calcTop(item, conf);
836 466 : ldata->bottom = ArpeggioLayout::calcBottom(item, ldata->arpeggioHeight, conf);
837 :
838 466 : ldata->setMag(item->staff() ? item->staff()->staffMag(item->tick()) : item->mag());
839 466 : ldata->magS = conf.magS(ldata->mag());
840 :
841 466 : std::shared_ptr<const IEngravingFont> font = conf.engravingFont();
842 466 : switch (item->arpeggioType()) {
843 294 : case ArpeggioType::NORMAL: {
844 294 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUp, SymId::wiggleArpeggiatoUp);
845 : // string is rotated -90 degrees
846 294 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
847 294 : ldata->setBbox(RectF(0.0, -ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
848 294 : } break;
849 :
850 46 : case ArpeggioType::UP: {
851 46 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUpArrow, SymId::wiggleArpeggiatoUp);
852 : // string is rotated -90 degrees
853 46 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
854 46 : ldata->setBbox(RectF(0.0, -ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
855 46 : } break;
856 :
857 54 : case ArpeggioType::DOWN: {
858 54 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUpArrow, SymId::wiggleArpeggiatoUp);
859 : // string is rotated +90 degrees (so that UpArrow turns into a DownArrow)
860 54 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
861 54 : ldata->setBbox(RectF(0.0, ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
862 54 : } break;
863 :
864 10 : case ArpeggioType::UP_STRAIGHT: {
865 10 : double x1 = item->spatium() * 0.5;
866 10 : ldata->symsBBox = font->bbox(SymId::arrowheadBlackUp, ldata->magS);
867 10 : double w = ldata->symsBBox.width();
868 10 : ldata->setBbox(RectF(x1 - w * 0.5, ldata->top, w, ldata->bottom));
869 10 : } break;
870 :
871 10 : case ArpeggioType::DOWN_STRAIGHT: {
872 10 : double x1 = item->spatium() * 0.5;
873 10 : ldata->symsBBox = font->bbox(SymId::arrowheadBlackDown, ldata->magS);
874 10 : double w = ldata->symsBBox.width();
875 10 : ldata->setBbox(RectF(x1 - w * 0.5, ldata->top, w, ldata->bottom));
876 10 : } break;
877 :
878 52 : case ArpeggioType::BRACKET: {
879 52 : double w = conf.styleS(Sid::arpeggioHookLen).val() * item->spatium();
880 52 : ldata->setBbox(RectF(0.0, ldata->top, w, ldata->bottom));
881 52 : } break;
882 : }
883 :
884 : // Loop through staves spanned & regenerate chord shape
885 : // This makes sure the arpeggio's shape is added to the shape of each chord it spans
886 466 : Chord* chord = item->chord();
887 466 : Segment* seg = chord->segment();
888 466 : staff_idx_t staveSpan = (item->track() + item->span() - 1) / VOICES;
889 980 : for (staff_idx_t staffIdx = item->staffIdx(); staffIdx <= staveSpan; staffIdx++) {
890 514 : seg->createShape(staffIdx);
891 : }
892 466 : }
893 :
894 0 : static Shape getChordsShape(const ChordBracket* item)
895 : {
896 0 : Chord* owner = item->chord();
897 0 : Shape result = owner->shape();
898 0 : result.removeTypes({ ElementType::CHORD_BRACKET, ElementType::ARPEGGIO });
899 0 : if (!owner->segment() || !owner->staff()) {
900 0 : return result;
901 : }
902 :
903 0 : const track_idx_t startTrack = staff2track(owner->staffIdx());
904 0 : const track_idx_t endTrack = startTrack + VOICES;
905 :
906 0 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
907 0 : EngravingItem* e = owner->segment()->element(track);
908 0 : if (track == owner->track()) {
909 0 : continue;
910 : }
911 :
912 0 : if (!e || !e->isChord()) {
913 0 : continue;
914 : }
915 :
916 0 : Chord* chord = toChord(e);
917 0 : if (chord->vStaffIdx() != owner->vStaffIdx()) {
918 0 : continue;
919 : }
920 :
921 0 : Shape chordShape = chord->shape();
922 0 : chordShape.removeTypes({ ElementType::CHORD_BRACKET, ElementType::ARPEGGIO });
923 0 : chordShape.translate(chord->pos() - owner->pos());
924 0 : result.add(chordShape);
925 0 : }
926 :
927 0 : return result;
928 0 : }
929 :
930 0 : void TLayout::layoutChordBracket(const ChordBracket* item, Arpeggio::LayoutData* ldata, const LayoutConfiguration& conf)
931 : {
932 0 : double spatium = item->spatium();
933 0 : ldata->arpeggioHeight = ArpeggioLayout::computeHeight(item);
934 0 : ldata->top = ArpeggioLayout::calcTop(item, conf) - 0.25 * spatium;
935 0 : ldata->bottom = ArpeggioLayout::calcBottom(item, ldata->arpeggioHeight, conf) + 0.5 * spatium;
936 :
937 0 : const double minLen = 2 * item->spatium();
938 0 : double diff = minLen - std::abs(ldata->bottom);
939 0 : if (diff > 0) {
940 0 : ldata->bottom += diff;
941 0 : if (item->hookPos() == DirectionV::DOWN) {
942 0 : ldata->top -= diff;
943 0 : } else if (item->hookPos() == DirectionV::AUTO) {
944 0 : ldata->top -= 0.5 * diff;
945 : }
946 : }
947 :
948 0 : ldata->setMag(item->staff() ? item->staff()->staffMag(item->tick()) : item->mag());
949 0 : ldata->magS = conf.magS(ldata->mag());
950 :
951 0 : ldata->setShape(Shape(RectF(0.0, ldata->top, item->absoluteFromSpatium(item->hookLength()), ldata->bottom).normalized(), item));
952 :
953 0 : const Note* upnote = item->chord()->upNote();
954 0 : ldata->setPosY(upnote->y() + upnote->ldata()->bbox().top());
955 :
956 0 : Shape chordShape = getChordsShape(item);
957 0 : Shape itemShape = item->shape().translated(PointF(0.0, item->ldata()->pos().y()));
958 0 : if (item->rightSide()) {
959 0 : double x = HorizontalSpacing::minHorizontalDistance(chordShape, itemShape, spatium);
960 0 : x = std::max(x, 0.0);
961 0 : ldata->setPosX(x);
962 : } else {
963 0 : double x = HorizontalSpacing::minHorizontalDistance(itemShape, chordShape, spatium);
964 0 : x = std::max(x, 0.0);
965 0 : ldata->setPosX(-x);
966 : }
967 :
968 : // Loop through staves spanned & regenerate chord shape
969 : // This makes sure the arpeggio's shape is added to the shape of each chord it spans
970 0 : Chord* chord = item->chord();
971 0 : Segment* seg = chord->segment();
972 0 : staff_idx_t staveSpan = (item->track() + item->span() - 1) / VOICES;
973 0 : for (staff_idx_t staffIdx = item->staffIdx(); staffIdx <= staveSpan; staffIdx++) {
974 0 : seg->createShape(staffIdx);
975 : }
976 0 : }
977 :
978 3435 : void TLayout::layoutArticulation(Articulation* item, Articulation::LayoutData* ldata)
979 : {
980 : LAYOUT_CALL_ITEM(item);
981 3435 : if (item->isHiddenOnTabStaff()) {
982 0 : ldata->setIsSkipDraw(true);
983 0 : return;
984 : }
985 3435 : ldata->setIsSkipDraw(false);
986 3435 : ldata->setPos(PointF());
987 :
988 : //! NOTE Must already be set previously
989 3435 : LD_CONDITION(ldata->symId.has_value());
990 :
991 3435 : if (item->textType() != ArticulationTextType::NO_TEXT) {
992 68 : if (!item->text()) {
993 28 : Text* text = new Text(item, TextStyleType::ARTICULATION);
994 28 : static const ElementStyle elementStyle;
995 28 : text->initElementStyle(&elementStyle);
996 28 : item->setText(text);
997 : }
998 :
999 68 : Text* text = item->text();
1000 68 : text->setXmlText(TConv::text(item->textType()));
1001 68 : text->setTrack(item->track());
1002 68 : text->setOwnershipParent(item);
1003 68 : text->setSelected(item->selected());
1004 :
1005 68 : TextLayout::layoutBaseTextBase(item->text(), item->text()->mutldata());
1006 : }
1007 :
1008 3435 : fillArticulationShape(item, ldata);
1009 : }
1010 :
1011 3435 : void TLayout::fillArticulationShape(const Articulation* item, Articulation::LayoutData* ldata)
1012 : {
1013 : LAYOUT_CALL_ITEM(item);
1014 :
1015 3435 : SymId sym = item->symId();
1016 3435 : if (sym == SymId::articAccentAbove || sym == SymId::articAccentBelow) {
1017 424 : RectF symBbox = item->symBbox(sym);
1018 424 : double width = symBbox.width();
1019 424 : double height = symBbox.height();
1020 424 : double thirdWidth = width / 3;
1021 424 : double thirdHeight = height / 3;
1022 424 : RectF base = RectF(0.0, 0.0, thirdWidth, height);
1023 424 : RectF center = RectF(thirdWidth, thirdHeight, thirdWidth, thirdHeight);
1024 424 : RectF tip = RectF(2 * thirdWidth, 1.25 * thirdHeight, thirdWidth, 0.5 * thirdHeight);
1025 424 : Shape shape;
1026 424 : shape.add(base, item);
1027 424 : shape.add(center, item);
1028 424 : shape.add(tip, item);
1029 424 : PointF translate(0.0, sym == SymId::articAccentAbove ? -height : 0.0);
1030 424 : ldata->setShape(shape.translate(translate));
1031 3435 : } else if (item->textType() == ArticulationTextType::NO_TEXT) {
1032 2943 : ldata->setShape(Shape(item->symBbox(sym), item));
1033 : } else {
1034 68 : ldata->setShape(Shape(item->text()->ldata()->bbox(), item));
1035 : }
1036 3435 : }
1037 :
1038 129209 : static double barLineWidth(const BarLine* item, const MStyle& style, double dotWidth)
1039 : {
1040 129209 : double w = 0.0;
1041 129209 : switch (item->barLineType()) {
1042 963 : case BarLineType::DOUBLE:
1043 963 : w = style.styleAbsolute(Sid::doubleBarWidth) * 2.0
1044 963 : + style.styleAbsolute(Sid::doubleBarDistance);
1045 963 : break;
1046 32 : case BarLineType::DOUBLE_HEAVY:
1047 32 : w = style.styleAbsolute(Sid::endBarWidth) * 2.0
1048 32 : + style.styleAbsolute(Sid::endBarDistance);
1049 32 : break;
1050 0 : case BarLineType::END_START_REPEAT:
1051 0 : w = style.styleAbsolute(Sid::endBarWidth)
1052 0 : + style.styleAbsolute(Sid::barWidth) * 2.0
1053 0 : + style.styleAbsolute(Sid::endBarDistance) * 2.0
1054 0 : + style.styleAbsolute(Sid::repeatBarlineDotSeparation) * 2.0
1055 0 : + dotWidth * 2;
1056 0 : break;
1057 1531 : case BarLineType::START_REPEAT:
1058 : case BarLineType::END_REPEAT:
1059 1531 : w = style.styleAbsolute(Sid::endBarWidth)
1060 1531 : + style.styleAbsolute(Sid::barWidth)
1061 1531 : + style.styleAbsolute(Sid::endBarDistance)
1062 1531 : + style.styleAbsolute(Sid::repeatBarlineDotSeparation)
1063 : + dotWidth;
1064 1531 : break;
1065 13180 : case BarLineType::END:
1066 : case BarLineType::REVERSE_END:
1067 13180 : w = style.styleAbsolute(Sid::endBarWidth)
1068 13180 : + style.styleAbsolute(Sid::barWidth)
1069 13180 : + style.styleAbsolute(Sid::endBarDistance);
1070 13180 : break;
1071 82 : case BarLineType::BROKEN:
1072 82 : w = style.styleAbsolute(Sid::dashBarWidth);
1073 82 : break;
1074 113381 : case BarLineType::NORMAL:
1075 : case BarLineType::DOTTED:
1076 113381 : w = style.styleAbsolute(Sid::barWidth);
1077 113381 : break;
1078 40 : case BarLineType::HEAVY:
1079 40 : w = style.styleAbsolute(Sid::endBarWidth);
1080 40 : break;
1081 : }
1082 129209 : return w;
1083 : }
1084 :
1085 129209 : void TLayout::layoutBarLine(const BarLine* item, BarLine::LayoutData* ldata, const LayoutContext& ctx)
1086 : {
1087 : LAYOUT_CALL_ITEM(item);
1088 :
1089 : // barlines hidden on this staff
1090 129209 : if (item->staff() && item->segment()) {
1091 129209 : if ((!item->staff()->staffTypeForElement(item)->showBarlines() && item->segment()->segmentType() == SegmentType::EndBarLine)
1092 129209 : || (item->staff()->hideSystemBarLine() && item->segment()->segmentType() == SegmentType::BeginBarLine)) {
1093 0 : ldata->setPos(PointF());
1094 0 : ldata->setBbox(RectF());
1095 0 : ldata->setIsSkipDraw(true);
1096 0 : return;
1097 : }
1098 : }
1099 :
1100 129209 : ldata->setIsSkipDraw(false);
1101 129209 : ldata->setPos(PointF());
1102 :
1103 : // Check conditions
1104 :
1105 : //! NOTE Here the goal is not just to check whether the conditions are met,
1106 : //! but the goal is to clearly show these conditions to the developer.
1107 129209 : for (const EngravingItem* e : *item->el()) {
1108 0 : switch (e->type()) {
1109 0 : case ElementType::ARTICULATION:
1110 : // form Articulation layout
1111 0 : LD_CONDITION(item_cast<const Articulation*>(e)->ldata()->symId.has_value());
1112 0 : break;
1113 0 : case ElementType::SYMBOL:
1114 : LD_INDEPENDENT;
1115 0 : break;
1116 0 : case ElementType::IMAGE:
1117 : // will be done
1118 : // LD_CONDITION(parentLD->isSetBbox());
1119 : LD_INDEPENDENT;
1120 0 : break;
1121 0 : default:
1122 0 : UNREACHABLE;
1123 : }
1124 : }
1125 :
1126 129209 : ldata->setMag(ctx.conf().styleB(Sid::scaleBarlines) && item->staff() ? item->staff()->staffMag(item->tick()) : 1.0);
1127 : // Note: the true values of y1 and y2 are computed in layout2() (can be done only
1128 : // after staff distances are known). This is a temporary layout.
1129 129209 : const double spatium = item->spatium();
1130 :
1131 129209 : ldata->y1 = spatium * .5 * item->spanFrom();
1132 129209 : ldata->y2 = spatium * .5 * (8.0 + item->spanTo());
1133 :
1134 129209 : const IEngravingFontPtr font = ctx.engravingFont();
1135 129209 : const double magS = ctx.conf().magS(ldata->mag());
1136 :
1137 129209 : double w = barLineWidth(item, ctx.conf().style(), font->width(SymId::repeatDot, magS)) * ldata->mag();
1138 129209 : RectF r(0.0, ldata->y1, w, ldata->y2 - ldata->y1);
1139 :
1140 129209 : if (ctx.conf().styleB(Sid::repeatBarTips)) {
1141 157 : switch (item->barLineType()) {
1142 4 : case BarLineType::START_REPEAT: {
1143 4 : r.unite(font->bbox(SymId::bracketTop, magS).translated(0, ldata->y1));
1144 4 : } break;
1145 8 : case BarLineType::END_REPEAT: {
1146 8 : double w1 = font->width(SymId::reversedBracketTop, magS) - w;
1147 8 : r.unite(font->bbox(SymId::reversedBracketTop, magS).translated(-w1, ldata->y1));
1148 8 : } break;
1149 0 : case BarLineType::END_START_REPEAT: {
1150 0 : double w1 = font->width(SymId::reversedBracketTop, magS) - w;
1151 0 : r.unite(font->bbox(SymId::reversedBracketTop, magS).translated(-w1, ldata->y1));
1152 0 : r.unite(font->bbox(SymId::bracketTop, magS).translated(0, ldata->y1));
1153 0 : } break;
1154 145 : default:
1155 145 : break;
1156 : }
1157 : }
1158 :
1159 129209 : ldata->setBbox(r);
1160 :
1161 129209 : updateBarlineShape(item, ldata, ctx);
1162 :
1163 : //! NOTE The types are listed here explicitly to show what types there are (see add method)
1164 : //! and accordingly show what the barline layout depends on.
1165 129209 : for (EngravingItem* e : *item->el()) {
1166 0 : switch (e->type()) {
1167 0 : case ElementType::ARTICULATION: {
1168 0 : Articulation* a = item_cast<Articulation*>(e);
1169 0 : Articulation::LayoutData* aldata = a->mutldata();
1170 0 : TLayout::layoutArticulation(a, aldata);
1171 0 : DirectionV dir = a->direction();
1172 0 : double distance = 0.5 * spatium;
1173 0 : double x = /*barline*/ ldata->bbox().width() * 0.5;
1174 0 : if (dir == DirectionV::DOWN) {
1175 0 : double botY = ldata->y2 + distance;
1176 0 : aldata->setPos(PointF(x, botY));
1177 : } else {
1178 0 : double topY = ldata->y1 - distance;
1179 0 : aldata->setPos(PointF(x, topY));
1180 : }
1181 0 : } break;
1182 0 : case ElementType::SYMBOL: {
1183 0 : Symbol* sb = item_cast<Symbol*>(e);
1184 0 : TLayout::layoutSymbol(sb, sb->mutldata(), ctx);
1185 0 : } break;
1186 0 : case ElementType::IMAGE: {
1187 0 : Image* im = item_cast<Image*>(e);
1188 0 : TLayout::layoutImage(im, im->mutldata());
1189 0 : } break;
1190 0 : default:
1191 0 : UNREACHABLE;
1192 : }
1193 : }
1194 :
1195 129209 : if (!item->segment()) {
1196 0 : return;
1197 : }
1198 :
1199 129209 : if (Fermata* fermata = toFermata(item->segment()->findAnnotation(ElementType::FERMATA, item->track(), item->track() + VOICES))) {
1200 52 : layoutFermata(fermata, fermata->mutldata());
1201 : }
1202 : }
1203 :
1204 188097 : void TLayout::updateBarlineShape(const BarLine* item, BarLine::LayoutData* ldata, const LayoutContext& ctx)
1205 : {
1206 : LAYOUT_CALL_ITEM(item);
1207 :
1208 188097 : RectF bb = ldata->bbox();
1209 188097 : if (item->staff()) {
1210 : // actual height may include span to next staff
1211 : // but this should not be included in shapes or skylines
1212 188097 : double sp = item->spatium();
1213 188097 : int span = item->staff()->lines(item->tick()) - 1;
1214 : int sFrom;
1215 : int sTo;
1216 188097 : if (span == 0 && item->spanTo() == 0) {
1217 206 : sFrom = BARLINE_SPAN_1LINESTAFF_FROM;
1218 206 : sTo = item->spanStaff() ? 0 : BARLINE_SPAN_1LINESTAFF_TO;
1219 : } else {
1220 187891 : sFrom = item->spanFrom();
1221 187891 : sTo = item->spanStaff() ? 0 : item->spanTo();
1222 : }
1223 188097 : double y = sp * sFrom * 0.5;
1224 188097 : double h = sp * (span + (sTo - sFrom) * 0.5);
1225 188097 : if (ctx.conf().styleB(Sid::repeatBarTips)) {
1226 232 : switch (item->barLineType()) {
1227 20 : case BarLineType::START_REPEAT:
1228 : case BarLineType::END_REPEAT:
1229 : case BarLineType::END_START_REPEAT: {
1230 20 : if (item->isTop()) {
1231 10 : double top = item->symBbox(SymId::bracketTop).height();
1232 10 : y -= top;
1233 10 : h += top;
1234 : }
1235 20 : if (item->isBottom()) {
1236 10 : double bottom = item->symBbox(SymId::bracketBottom).height();
1237 10 : h += bottom;
1238 : }
1239 : }
1240 : default:
1241 232 : break;
1242 : }
1243 : }
1244 188097 : bb.setTop(y);
1245 188097 : bb.setHeight(h);
1246 : }
1247 :
1248 188097 : ldata->setShape(Shape(bb, item));
1249 188097 : }
1250 :
1251 : //---------------------------------------------------------
1252 : // called after system layout; set vertical dimensions
1253 : //---------------------------------------------------------
1254 64876 : void TLayout::layoutBarLine2(BarLine* item, LayoutContext& ctx)
1255 : {
1256 : LAYOUT_CALL_ITEM(item);
1257 :
1258 64876 : BarLine::LayoutData* ldata = item->mutldata();
1259 :
1260 64876 : if (ldata->isSkipDraw()) {
1261 0 : return;
1262 : }
1263 :
1264 64876 : item->calcY();
1265 64876 : RectF bbox = ldata->bbox();
1266 64876 : bbox.setTop(ldata->y1);
1267 64876 : bbox.setBottom(ldata->y2);
1268 :
1269 64876 : if (ctx.conf().styleB(Sid::repeatBarTips)) {
1270 75 : switch (item->barLineType()) {
1271 4 : case BarLineType::START_REPEAT:
1272 4 : bbox.unite(item->symBbox(SymId::bracketTop).translated(0, ldata->y1));
1273 4 : bbox.unite(item->symBbox(SymId::bracketBottom).translated(0, ldata->y2));
1274 4 : break;
1275 4 : case BarLineType::END_REPEAT:
1276 : {
1277 4 : double w1 = item->symBbox(SymId::reversedBracketTop).x() - bbox.x();
1278 4 : bbox.unite(item->symBbox(SymId::reversedBracketTop).translated(-w1, ldata->y1));
1279 4 : bbox.unite(item->symBbox(SymId::reversedBracketBottom).translated(-w1, ldata->y2));
1280 4 : break;
1281 : }
1282 0 : case BarLineType::END_START_REPEAT:
1283 : {
1284 0 : double w1 = item->symBbox(SymId::reversedBracketTop).x() - bbox.x();
1285 0 : bbox.unite(item->symBbox(SymId::reversedBracketTop).translated(-w1, ldata->y1));
1286 0 : bbox.unite(item->symBbox(SymId::reversedBracketBottom).translated(-w1, ldata->y2));
1287 0 : bbox.unite(item->symBbox(SymId::bracketTop).translated(0, ldata->y1));
1288 0 : bbox.unite(item->symBbox(SymId::bracketBottom).translated(0, ldata->y2));
1289 0 : break;
1290 : }
1291 67 : default:
1292 67 : break;
1293 : }
1294 : }
1295 :
1296 64876 : ldata->setBbox(bbox);
1297 : }
1298 :
1299 10063 : void TLayout::layoutBeam(Beam* item, const LayoutContext& ctx)
1300 : {
1301 : LAYOUT_CALL_ITEM(item);
1302 10063 : BeamLayout::layout(item, ctx);
1303 10063 : }
1304 :
1305 160 : void TLayout::layoutBeam1(Beam* item, LayoutContext& ctx)
1306 : {
1307 : LAYOUT_CALL_ITEM(item);
1308 160 : BeamLayout::layout1(item, ctx);
1309 160 : }
1310 :
1311 4 : void TLayout::layoutBend(const Bend* item, Bend::LayoutData* ldata)
1312 : {
1313 : LAYOUT_CALL_ITEM(item);
1314 4 : IF_ASSERT_FAILED(item->ownershipParent()) {
1315 0 : return;
1316 : }
1317 :
1318 4 : LD_CONDITION(item->note()->ldata()->isSetPos());
1319 4 : LD_CONDITION(item->note()->ldata()->isSetBbox());
1320 :
1321 4 : double spatium = item->spatium();
1322 4 : double lw = item->absoluteFromSpatium(item->lineWidth());
1323 :
1324 4 : const Note::LayoutData* noteLD = item->note()->ldata();
1325 4 : PointF notePos = noteLD->pos();
1326 4 : notePos.ry() = std::max(notePos.y(), 0.0);
1327 :
1328 4 : ldata->noteWidth = noteLD->bbox().width();
1329 4 : ldata->notePos = notePos;
1330 :
1331 4 : RectF bb;
1332 :
1333 4 : FontMetrics fm(item->font(spatium));
1334 :
1335 4 : size_t n = item->points().size();
1336 4 : double x = ldata->noteWidth;
1337 4 : double y = -spatium * .8;
1338 4 : double x2 = 0.0, y2 = 0.0;
1339 :
1340 4 : double aw = spatium * .5;
1341 4 : PolygonF arrowUp;
1342 4 : arrowUp << PointF(0, 0) << PointF(aw * .5, aw) << PointF(-aw * .5, aw);
1343 4 : PolygonF arrowDown;
1344 4 : arrowDown << PointF(0, 0) << PointF(aw * .5, -aw) << PointF(-aw * .5, -aw);
1345 :
1346 8 : for (size_t pt = 0; pt < n; ++pt) {
1347 8 : if (pt == (n - 1)) {
1348 0 : break;
1349 : }
1350 8 : int pitch = item->points().at(pt).pitch;
1351 8 : if (pt == 0 && pitch) {
1352 0 : y2 = -ldata->notePos.y() - spatium * 2;
1353 0 : x2 = x;
1354 0 : bb.unite(RectF(x, y, x2 - x, y2 - y));
1355 :
1356 0 : bb.unite(arrowUp.translated(x2, y2 + spatium * .2).boundingRect());
1357 :
1358 0 : int idx = (pitch + 12) / 25;
1359 0 : const char* l = Bend::label[idx];
1360 0 : bb.unite(fm.boundingRect(String::fromAscii(l)));
1361 0 : y = y2;
1362 : }
1363 8 : if (pitch == item->points().at(pt + 1).pitch) {
1364 4 : if (pt == (n - 2)) {
1365 4 : break;
1366 : }
1367 0 : x2 = x + spatium;
1368 0 : y2 = y;
1369 0 : bb.unite(RectF(x, y, x2 - x, y2 - y));
1370 4 : } else if (pitch < item->points().at(pt + 1).pitch) {
1371 : // up
1372 4 : x2 = x + spatium * .5;
1373 4 : y2 = -ldata->notePos.y() - spatium * 2;
1374 4 : double dx = x2 - x;
1375 4 : double dy = y2 - y;
1376 :
1377 4 : PainterPath path;
1378 4 : path.moveTo(x, y);
1379 4 : path.cubicTo(x + dx / 2, y, x2, y + dy / 4, x2, y2);
1380 4 : bb.unite(path.boundingRect());
1381 4 : bb.unite(arrowUp.translated(x2, y2 + spatium * .2).boundingRect());
1382 :
1383 4 : int idx = (item->points().at(pt + 1).pitch + 12) / 25;
1384 4 : const char* l = Bend::label[idx];
1385 4 : bb.unite(fm.boundingRect(String::fromAscii(l)));
1386 4 : } else {
1387 : // down
1388 0 : x2 = x + spatium * .5;
1389 0 : y2 = y + spatium * 3;
1390 0 : double dx = x2 - x;
1391 0 : double dy = y2 - y;
1392 :
1393 0 : PainterPath path;
1394 0 : path.moveTo(x, y);
1395 0 : path.cubicTo(x + dx / 2, y, x2, y + dy / 4, x2, y2);
1396 0 : bb.unite(path.boundingRect());
1397 :
1398 0 : bb.unite(arrowDown.translated(x2, y2 - spatium * .2).boundingRect());
1399 0 : }
1400 4 : x = x2;
1401 4 : y = y2;
1402 : }
1403 4 : bb.adjust(-lw, -lw, lw, lw);
1404 :
1405 4 : ldata->setBbox(bb);
1406 4 : ldata->setPos(PointF());
1407 4 : }
1408 :
1409 147 : void TLayout::layoutHBox(const HBox* item, HBox::LayoutData* ldata, const LayoutContext& ctx)
1410 : {
1411 : LAYOUT_CALL_ITEM(item);
1412 147 : BoxLayout::layoutHBox(item, ldata, ctx);
1413 147 : }
1414 :
1415 0 : void TLayout::layoutVBox(const VBox* item, VBox::LayoutData* ldata, const LayoutContext& ctx)
1416 : {
1417 : LAYOUT_CALL_ITEM(item);
1418 0 : BoxLayout::layoutVBox(item, ldata, ctx);
1419 0 : }
1420 :
1421 0 : void TLayout::layoutFBox(const FBox* item, FBox::LayoutData* ldata, const LayoutContext& ctx)
1422 : {
1423 : LAYOUT_CALL_ITEM(item);
1424 :
1425 0 : BoxLayout::layoutFBox(item, ldata, ctx);
1426 0 : }
1427 :
1428 0 : void TLayout::layoutTBox(const TBox* item, TBox::LayoutData* ldata, const LayoutContext& ctx)
1429 : {
1430 : LAYOUT_CALL_ITEM(item);
1431 0 : BoxLayout::layoutTBox(item, ldata, ctx);
1432 0 : }
1433 :
1434 9025 : void TLayout::layoutBracket(const Bracket* item, Bracket::LayoutData* ldata, const LayoutConfiguration& conf)
1435 : {
1436 : LAYOUT_CALL_ITEM(item);
1437 9025 : LD_CONDITION(ldata->bracketHeight.has_value());
1438 9025 : if (!ldata->bracketHeight.has_value()) {
1439 0 : return;
1440 : }
1441 :
1442 9025 : const_cast<Bracket*>(item)->setVisible(item->bi()->visible());
1443 :
1444 9025 : ldata->braceSymbol = item->braceSymbol();
1445 :
1446 9025 : switch (item->bracketType()) {
1447 5029 : case BracketType::BRACE: {
1448 5029 : String musicalSymbolFont = conf.styleSt(Sid::musicalSymbolFont);
1449 5029 : if (musicalSymbolFont == "Emmentaler" || musicalSymbolFont == "Gonville") {
1450 779 : ldata->braceSymbol = SymId::noSym;
1451 779 : double w = conf.styleAbsolute(Sid::akkoladeWidth);
1452 779 : double h2 = ldata->bracketHeight * 0.5;
1453 :
1454 : #define XM(a) (a + 700) * w / 700
1455 : #define YM(a) (a + 7100) * h2 / 7100
1456 :
1457 779 : PainterPath path;
1458 779 : path.moveTo(XM(-8), YM(-2048));
1459 779 : path.cubicTo(XM(-8), YM(-3192), XM(-360), YM(-4304), XM(-360), YM(-5400)); // c 0
1460 779 : path.cubicTo(XM(-360), YM(-5952), XM(-264), YM(-6488), XM(32), YM(-6968)); // c 1
1461 779 : path.cubicTo(XM(36), YM(-6974), XM(38), YM(-6984), XM(38), YM(-6990)); // c 0
1462 779 : path.cubicTo(XM(38), YM(-7008), XM(16), YM(-7024), XM(0), YM(-7024)); // c 0
1463 779 : path.cubicTo(XM(-8), YM(-7024), XM(-22), YM(-7022), XM(-32), YM(-7008)); // c 1
1464 779 : path.cubicTo(XM(-416), YM(-6392), XM(-544), YM(-5680), XM(-544), YM(-4960)); // c 0
1465 779 : path.cubicTo(XM(-544), YM(-3800), XM(-168), YM(-2680), XM(-168), YM(-1568)); // c 0
1466 779 : path.cubicTo(XM(-168), YM(-1016), XM(-264), YM(-496), XM(-560), YM(-16)); // c 1
1467 779 : path.lineTo(XM(-560), YM(0)); // l 1
1468 779 : path.lineTo(XM(-560), YM(16)); // l 1
1469 779 : path.cubicTo(XM(-264), YM(496), XM(-168), YM(1016), XM(-168), YM(1568)); // c 0
1470 779 : path.cubicTo(XM(-168), YM(2680), XM(-544), YM(3800), XM(-544), YM(4960)); // c 0
1471 779 : path.cubicTo(XM(-544), YM(5680), XM(-416), YM(6392), XM(-32), YM(7008)); // c 1
1472 779 : path.cubicTo(XM(-22), YM(7022), XM(-8), YM(7024), XM(0), YM(7024)); // c 0
1473 779 : path.cubicTo(XM(16), YM(7024), XM(38), YM(7008), XM(38), YM(6990)); // c 0
1474 779 : path.cubicTo(XM(38), YM(6984), XM(36), YM(6974), XM(32), YM(6968)); // c 1
1475 779 : path.cubicTo(XM(-264), YM(6488), XM(-360), YM(5952), XM(-360), YM(5400)); // c 0
1476 779 : path.cubicTo(XM(-360), YM(4304), XM(-8), YM(3192), XM(-8), YM(2048)); // c 0
1477 779 : path.cubicTo(XM(-8), YM(1320), XM(-136), YM(624), XM(-512), YM(0)); // c 1
1478 779 : path.cubicTo(XM(-136), YM(-624), XM(-8), YM(-1320), XM(-8), YM(-2048)); // c 0*/
1479 779 : ldata->path = path;
1480 779 : ldata->setBbox(path.boundingRect());
1481 779 : ldata->shape.add(ldata->bbox());
1482 779 : ldata->bracketWidth = w + conf.styleAbsolute(Sid::akkoladeBarDistance);
1483 779 : } else {
1484 4250 : if (item->braceSymbol() == SymId::noSym) {
1485 0 : ldata->braceSymbol = SymId::brace;
1486 : }
1487 4250 : double h = ldata->bracketHeight;
1488 4250 : double w = item->symWidth(ldata->braceSymbol) * item->magx();
1489 4250 : ldata->setBbox(RectF(0, 0, w, h));
1490 4250 : ldata->shape.add(ldata->bbox());
1491 4250 : ldata->bracketWidth = w + conf.styleAbsolute(Sid::akkoladeBarDistance);
1492 : }
1493 5029 : }
1494 5029 : break;
1495 2812 : case BracketType::NORMAL: {
1496 2812 : double w = conf.styleAbsolute(Sid::bracketWidth) * .5;
1497 2812 : double bd = item->spatium() * (conf.styleSt(Sid::musicalSymbolFont) == "Leland" ? .5 : .25);
1498 :
1499 2812 : Shape shape = RectF(-w, -bd, w * 2, 2 * (ldata->bracketHeight * 0.5 + bd));
1500 2812 : shape.add(item->symBbox(SymId::bracketTop).translated(PointF(-w, -bd)));
1501 2812 : shape.add(item->symBbox(SymId::bracketBottom).translated(PointF(-w, bd + ldata->bracketHeight)));
1502 2812 : ldata->setShape(shape);
1503 :
1504 2812 : ldata->bracketWidth = conf.styleAbsolute(Sid::bracketWidth) + conf.styleAbsolute(Sid::bracketDistance);
1505 2812 : }
1506 2812 : break;
1507 1142 : case BracketType::SQUARE:
1508 : {
1509 1142 : double w = conf.styleAbsolute(Sid::staffLineWidth) * .5;
1510 1142 : double x = -w;
1511 1142 : double y = -w;
1512 1142 : double h = (ldata->bracketHeight * 0.5 + w) * 2;
1513 1142 : w += (.5 * item->spatium() + 2 * w);
1514 1142 : ldata->setBbox(RectF(x, y, w, h));
1515 1142 : ldata->shape.add(ldata->bbox());
1516 :
1517 1142 : ldata->bracketWidth = conf.styleAbsolute(Sid::staffLineWidth) / 2 + 0.5 * item->spatium();
1518 : }
1519 1142 : break;
1520 42 : case BracketType::LINE: {
1521 42 : double _spatium = item->spatium();
1522 42 : double w = 0.67 * conf.styleAbsolute(Sid::bracketWidth) * .5;
1523 42 : double x = -w;
1524 42 : double bd = _spatium * .25;
1525 42 : double y = -bd;
1526 42 : double h = (-y + ldata->bracketHeight * 0.5) * 2;
1527 42 : ldata->setBbox(RectF(x, y, w, h));
1528 42 : ldata->shape.add(ldata->bbox());
1529 :
1530 42 : ldata->bracketWidth = 0.67 * conf.styleAbsolute(Sid::bracketWidth) + conf.styleAbsolute(Sid::bracketDistance);
1531 : }
1532 42 : break;
1533 0 : case BracketType::GROUP:
1534 0 : layoutGroupBracket(item, ldata, conf);
1535 0 : break;
1536 0 : case BracketType::NO_BRACKET:
1537 0 : break;
1538 : }
1539 : }
1540 :
1541 0 : void TLayout::layoutGroupBracket(const Bracket* item, Bracket::LayoutData* ldata, const LayoutConfiguration& conf)
1542 : {
1543 0 : BracketItem* bracketItem = item->bracketItem();
1544 :
1545 0 : double w = conf.styleAbsolute(Sid::groupBracketLineWidth) * 0.5;
1546 0 : double x = 0.0;
1547 0 : double y = -w;
1548 0 : double h = (ldata->bracketHeight * 0.5 + w) * 2;
1549 0 : double width = conf.styleAbsolute(Sid::groupBracketHookLen);
1550 :
1551 0 : ldata->bracketWidth = std::max(width, 2 * w);
1552 :
1553 0 : Shape shape;
1554 0 : shape.add(RectF(x, y, width, h), item);
1555 0 : ldata->setShape(shape);
1556 :
1557 0 : if (!item->text()) {
1558 0 : Text* bracketText = new Text(const_cast<Bracket*>(item), TextStyleType::GROUP_BRACKET);
1559 0 : bracketText->setSelected(item->selected());
1560 0 : bracketText->setGenerated(true);
1561 0 : const_cast<Bracket*>(item)->setText(bracketText);
1562 : }
1563 :
1564 0 : Text* text = item->text();
1565 0 : text->setVisible(bracketItem->visible() && bracketItem->showText());
1566 0 : text->setAlign(Align(text->align().horizontal, AlignV::VCENTER));
1567 0 : text->setPosition(AlignH::HCENTER);
1568 :
1569 0 : Orientation textOrientation = conf.styleV(Sid::groupBracketTextOrientation).value<Orientation>();
1570 0 : text->setTextAngle(textOrientation == Orientation::VERTICAL ? -90.0 : 0.0);
1571 :
1572 0 : double textPadding = 0.5 * item->spatium();
1573 0 : double bracketHeight = item->ldata()->bbox().height();
1574 0 : double minHeight = bracketHeight - 6 * textPadding;
1575 :
1576 0 : if (textOrientation == Orientation::HORIZONTAL) {
1577 0 : text->setXmlText(item->labelType() == InstrumentNameType::LONG ? bracketItem->longName() : bracketItem->shortName());
1578 0 : layoutText(text, text->mutldata());
1579 : } else {
1580 : // Vertical labels ignore labelType(): use the long name if it fits, otherwise the short name
1581 0 : text->setXmlText(bracketItem->longName());
1582 0 : layoutText(text, text->mutldata());
1583 0 : if (text->ldata()->bbox().height() > minHeight) {
1584 0 : text->setXmlText(bracketItem->shortName());
1585 0 : layoutText(text, text->mutldata());
1586 : }
1587 : }
1588 :
1589 0 : text->mutldata()->setPosY(bracketHeight / 2);
1590 :
1591 0 : switch (conf.styleV(Sid::groupBracketTextAlign).value<DirectionH>()) {
1592 0 : case DirectionH::AUTO:
1593 : {
1594 0 : text->mutldata()->setPosX(0.0);
1595 0 : if (text->visible()) {
1596 0 : RectF mask = text->ldata()->bbox().translated(text->pos()).padded(textPadding);
1597 0 : ldata->setMask(mask);
1598 : }
1599 0 : break;
1600 : }
1601 0 : case DirectionH::LEFT:
1602 0 : text->mutldata()->setPosX(-text->mutldata()->bbox().right() - textPadding);
1603 0 : break;
1604 0 : case DirectionH::RIGHT:
1605 0 : text->mutldata()->setPosX(-text->mutldata()->bbox().left() + textPadding);
1606 : }
1607 :
1608 0 : shape.add(text->ldata()->shape().translated(text->pos()));
1609 0 : ldata->setShape(shape);
1610 0 : }
1611 :
1612 168 : void TLayout::layoutBreath(const Breath* item, Breath::LayoutData* ldata, const LayoutConfiguration& conf)
1613 : {
1614 : LAYOUT_CALL_ITEM(item);
1615 : LD_INDEPENDENT;
1616 :
1617 : // TODO review with all other instances of this
1618 : // if (ldata->isValid()) {
1619 : // return;
1620 : // }
1621 :
1622 168 : const double voiceOffset = item->placeBelow() ? item->staff()->staffHeight(item->tick()) : 0.0;
1623 168 : if (item->isCaesura()) {
1624 42 : if (item->symId() == SymId::chantCaesura) {
1625 : // special handling for chant caesura, which is higher than the normal one
1626 2 : ldata->setPosY(1.5 * item->spatium() + voiceOffset);
1627 : } else {
1628 40 : ldata->setPosY(item->symHeight(item->symId()) / 2 + voiceOffset);
1629 : }
1630 126 : } else if ((conf.styleSt(Sid::musicalSymbolFont) == "Emmentaler") && (item->symId() == SymId::breathMarkComma)) {
1631 15 : const double shift = item->placeBelow() ? -0.5 * item->spatium() + item->symHeight(item->symId()) : 0.5 * item->spatium();
1632 15 : ldata->setPosY(shift + voiceOffset);
1633 : } else {
1634 111 : const double shift = item->placeBelow() ? 0.5 * item->spatium() + item->symHeight(item->symId()) : -0.5 * item->spatium();
1635 111 : ldata->setPosY(shift + voiceOffset);
1636 : }
1637 :
1638 168 : ldata->setBbox(item->symBbox(item->symId()));
1639 :
1640 168 : ldata->setPosX(-ldata->bbox().right());
1641 168 : }
1642 :
1643 54875 : void TLayout::layoutChord(Chord* item, LayoutContext& ctx)
1644 : {
1645 : LAYOUT_CALL_ITEM(item);
1646 54875 : ChordLayout::layout(item, ctx);
1647 54875 : }
1648 :
1649 168 : void TLayout::layoutChordLine(const ChordLine* item, ChordLine::LayoutData* ldata, const LayoutConfiguration& conf)
1650 : {
1651 : LAYOUT_CALL_ITEM(item);
1652 168 : IF_ASSERT_FAILED(item->ownershipParent()) {
1653 0 : return;
1654 : }
1655 :
1656 168 : Note* note = nullptr;
1657 168 : const double spatium = item->spatium();
1658 :
1659 168 : if (item->note()) {
1660 115 : note = item->chord()->findNote(item->note()->pitch());
1661 : }
1662 :
1663 168 : if (!note) {
1664 53 : note = item->chord()->upNote();
1665 : }
1666 :
1667 168 : const Note::LayoutData* noteLD = note->ldata();
1668 :
1669 : //! NOTE Temporary, instead of item->chord()->shape()
1670 168 : LD_CONDITION(item->chord()->ldata()->isSetBbox());
1671 :
1672 168 : LD_CONDITION(noteLD->isSetPos());
1673 168 : LD_CONDITION(noteLD->isSetBbox());
1674 :
1675 168 : ldata->setMag(item->chord()->mag());
1676 :
1677 168 : layoutChordLinePath(item, ldata, spatium, item->chordMag());
1678 :
1679 168 : double x = 0.0;
1680 168 : double y = noteLD->pos().y();
1681 168 : double horOffset = 0.33 * spatium; // one third of a space away from the note
1682 168 : double vertOffset = 0.25 * spatium; // one quarter of a space from the center line
1683 : // Get chord shape
1684 168 : Shape chordShape = item->chord()->shape();
1685 : // ...but remove from the shape items that the chordline shouldn't try to avoid
1686 : // (especially the chordline itself)
1687 168 : chordShape.remove_if([](ShapeElement& shapeEl){
1688 378 : if (!shapeEl.item()) {
1689 0 : return true;
1690 : }
1691 378 : const EngravingItem* item = shapeEl.item();
1692 378 : if (item->isChordLine() || item->isHarmony() || item->isLyrics()) {
1693 1 : return true;
1694 : }
1695 377 : return false;
1696 : });
1697 :
1698 168 : const RectF r = item->isWavy() ? conf.engravingFont()->bbox(item->waveSym(), item->magS()) : ldata->path.boundingRect();
1699 :
1700 168 : y += item->isBelow() ? vertOffset : -vertOffset;
1701 :
1702 168 : const double yBelow = y + r.height();
1703 : double chordEdge
1704 168 : = item->isToTheLeft() ? chordShape.leftMostEdgeAtHeight(y, yBelow) : chordShape.rightMostEdgeAtHeight(y, yBelow);
1705 :
1706 168 : if (!item->isToTheLeft()) {
1707 : // Always place lines to the right of dots
1708 91 : double xMin = chordShape.left();
1709 286 : for (ShapeElement& shapeEl : chordShape.elements()) {
1710 195 : if (!shapeEl.item() || !shapeEl.item()->isNoteDot()) {
1711 195 : continue;
1712 : }
1713 :
1714 0 : xMin = std::max(xMin, shapeEl.right());
1715 : }
1716 91 : chordEdge = std::max(chordEdge, xMin);
1717 : }
1718 :
1719 168 : x += item->isToTheLeft() ? chordEdge - horOffset : chordEdge + horOffset;
1720 168 : ldata->setPos(x, y);
1721 :
1722 168 : if (!item->isWavy()) {
1723 162 : int x1 = 0, y1 = 0, width = 0, height = 0;
1724 :
1725 162 : x1 = r.x();
1726 162 : y1 = r.y();
1727 162 : width = r.width();
1728 162 : height = r.height();
1729 162 : ldata->setBbox(x1, y1, width, height);
1730 : } else {
1731 : // Align an eighth space above/below the centre of the note
1732 6 : if (item->isBelow()) {
1733 4 : ldata->moveY(r.height() - spatium * 0.125);
1734 : } else {
1735 2 : ldata->moveY(spatium * 0.075);
1736 : }
1737 :
1738 6 : if (item->isToTheLeft()) {
1739 2 : ldata->moveX(-r.width());
1740 : }
1741 :
1742 6 : ldata->setBbox(r);
1743 : }
1744 168 : }
1745 :
1746 174 : void TLayout::layoutChordLinePath(const ChordLine* item, ChordLine::LayoutData* ldata, double spatium, double mag)
1747 : {
1748 174 : ldata->path = PainterPath();
1749 174 : if (item->chordLineType() == ChordLineType::NOTYPE || item->isWavy()) {
1750 7 : return;
1751 : }
1752 :
1753 167 : const ChordLine::Points defaultPoints = item->defaultPoints();
1754 167 : const ChordLine::Points& offsets = item->bezierOffsets();
1755 167 : ChordLine::Points points;
1756 1002 : for (size_t i = 0; i < points.size(); ++i) {
1757 668 : points[i] = (defaultPoints[i] * spatium + offsets[i]) * mag;
1758 : }
1759 :
1760 167 : PainterPath path;
1761 167 : path.moveTo(points[size_t(ChordLine::Point::START)]);
1762 167 : if (item->isStraight()) {
1763 95 : path.lineTo(points[size_t(ChordLine::Point::END)]);
1764 : } else {
1765 72 : path.cubicTo(points[size_t(ChordLine::Point::BEZIER1)], points[size_t(ChordLine::Point::BEZIER2)],
1766 72 : points[size_t(ChordLine::Point::END)]);
1767 : }
1768 167 : ldata->path = path;
1769 167 : }
1770 :
1771 27816 : void TLayout::layoutClef(const Clef* item, Clef::LayoutData* ldata, const LayoutConfiguration& conf)
1772 : {
1773 : LAYOUT_CALL_ITEM(item);
1774 : LD_INDEPENDENT;
1775 :
1776 : // determine current number of lines and line distance
1777 27816 : int lines = 5;
1778 27816 : Spatium lineDist = 1.0_sp;
1779 27816 : int stepOffset = 0;
1780 27816 : Spatium staffOffsetY = 0_sp;
1781 :
1782 27816 : Segment* clefSeg = item->segment();
1783 :
1784 : // check clef visibility and type compatibility
1785 27816 : if (clefSeg && item->staff()) {
1786 27816 : StaffType* stVisibility = item->staff()->staffType(item->tick());
1787 27816 : bool show = stVisibility->genClef(); // check staff type allows clef display
1788 :
1789 27816 : const bool endOfMeasureClef = clefSeg->rtick() == clefSeg->measure()->ticks();
1790 27816 : const Fraction tick = endOfMeasureClef && !item->isTrailer() ? item->tick() : clefSeg->measure()->tick();
1791 :
1792 27816 : const Fraction tickPrev = tick - Fraction::eps();
1793 :
1794 27816 : if (item->staff()->isJianpuStaff(tick)) {
1795 195 : ldata->setShape(Shape());
1796 195 : ldata->setBbox(RectF());
1797 195 : ldata->symId = SymId::noSym;
1798 195 : return; // Jianpu do not need to show clef
1799 : }
1800 :
1801 27621 : const StaffType* st = item->staff()->staffType(tick);
1802 27621 : const StaffType* stPrev = !tickPrev.negative() ? item->staff()->staffType(tickPrev) : nullptr;
1803 27621 : StaffGroup staffGroup = st->group();
1804 27621 : const bool hideClef = st->isTabStaff() ? conf.styleB(Sid::hideTabClefAfterFirst) : !conf.styleB(Sid::genClef);
1805 :
1806 : // if not tab, use instrument->useDrumset to set staffGroup (to allow pitched to unpitched in same staff)
1807 27621 : if (staffGroup != StaffGroup::TAB) {
1808 27022 : staffGroup = item->originPart()->instrument(item->tick())->useDrumset() ? StaffGroup::PERCUSSION : StaffGroup::STANDARD;
1809 : }
1810 :
1811 : // check clef is compatible with staff type group:
1812 27621 : if (ClefInfo::staffGroup(item->clefType()) != staffGroup) {
1813 451 : if (tick > Fraction(0, 1) && (!item->generated() || hideClef)) { // if clef is not generated, hide it
1814 0 : show = false;
1815 : } else { // if generated, replace with initial clef type
1816 : // TODO : instead of initial staff clef (which is assumed to be compatible)
1817 : // use the last compatible clef previously found in staff
1818 451 : const_cast<Clef*>(item)->setClefType(item->staff()->clefType(Fraction(0, 1)));
1819 : }
1820 : }
1821 :
1822 : // if clef not to show or not compatible with staff group
1823 27621 : if (!show) {
1824 0 : ldata->setShape(Shape());
1825 0 : ldata->setBbox(RectF());
1826 0 : ldata->symId = SymId::noSym;
1827 0 : LOGD("invisible clef at tick %d(%d) staff %zu",
1828 0 : item->segment()->tick().ticks(), item->segment()->tick().ticks() / 1920, item->staffIdx());
1829 0 : return;
1830 : }
1831 27621 : lines = st->lines(); // init values from staff type
1832 27621 : lineDist = st->lineDistance();
1833 27621 : stepOffset = st->stepOffset();
1834 :
1835 27621 : const Spatium stOffset = st->yoffset();
1836 27621 : const Spatium stPrevOffset = stPrev && clefSeg->rtick() != Fraction(0, 1) ? stPrev->yoffset() : 0.0_sp;
1837 27621 : staffOffsetY = item->isHeader() ? 0.0_sp : stOffset - stPrevOffset;
1838 : }
1839 :
1840 27621 : double _spatium = item->spatium();
1841 27621 : Spatium yoff = 0.0_sp;
1842 27621 : if (item->clefType() != ClefType::INVALID && item->clefType() != ClefType::MAX) {
1843 27621 : ldata->symId = ClefInfo::symId(item->clefType());
1844 27621 : yoff = lineDist * (5 - ClefInfo::line(item->clefType()));
1845 : } else {
1846 0 : ldata->symId = SymId::noSym;
1847 : }
1848 :
1849 27621 : switch (item->clefType()) {
1850 3 : case ClefType::C_19C: // 19th C clef is like a G clef
1851 3 : yoff = lineDist * 1.5;
1852 3 : break;
1853 599 : case ClefType::TAB: // TAB clef
1854 : case ClefType::TAB4: // TAB clef 4 strings
1855 : case ClefType::TAB_SERIF: // TAB clef alternate style
1856 : case ClefType::TAB4_SERIF: // TAB clef alternate style
1857 : // on tablature, position clef at half the number of spaces * line distance
1858 599 : yoff = lineDist * (lines - 1) * .5;
1859 599 : stepOffset = 0; // ignore stepOffset for TAB and percussion clefs
1860 599 : break;
1861 1129 : case ClefType::PERC: // percussion clefs
1862 : case ClefType::PERC2:
1863 1129 : yoff = lineDist * (lines - 1) * 0.5;
1864 1129 : stepOffset = 0;
1865 1129 : break;
1866 0 : case ClefType::INVALID:
1867 : case ClefType::MAX:
1868 0 : LOGD("Clef::layout: invalid type");
1869 0 : return;
1870 25890 : default:
1871 25890 : break;
1872 : }
1873 : // clefs on palette or at start of system/measure are left aligned
1874 : // other clefs are right aligned
1875 55242 : Shape shape(item->symShapeWithCutouts(ldata->symId));
1876 27621 : bool isMidMeasureClef = item->isMidMeasureClef();
1877 27621 : double x = isMidMeasureClef ? -shape.right() : 0.0;
1878 27621 : ldata->setPos(PointF(x, (yoff + (Spatium(stepOffset) * 0.5) + staffOffsetY).toAbsolute(_spatium)));
1879 27621 : if (item->isMidMeasureClef()) {
1880 744 : ldata->setShape(shape);
1881 : } else {
1882 26877 : ldata->setBbox(item->symBbox(ldata->symId));
1883 : }
1884 : }
1885 :
1886 44 : void TLayout::layoutCapo(const Capo* item, Capo::LayoutData* ldata, const LayoutContext&)
1887 : {
1888 : LAYOUT_CALL_ITEM(item);
1889 : //! NOTE Looks like it doesn't belong here
1890 44 : if (item->shouldAutomaticallyGenerateText() || item->empty()) {
1891 44 : if (const Part* part = item->originPart()) {
1892 44 : if (const StringData* stringData = part->stringData(item->tick(), item->staff()->idx())) {
1893 44 : String text = item->generateText(stringData->strings());
1894 44 : const_cast<Capo*>(item)->setXmlText(text);
1895 44 : }
1896 : }
1897 : }
1898 :
1899 44 : TextLayout::layoutBaseTextBase(item, ldata);
1900 :
1901 44 : if (item->autoplace()) {
1902 44 : const Segment* s = item->segment();
1903 44 : const Measure* m = s->measure();
1904 44 : LD_CONDITION(ldata->isSetPos());
1905 44 : LD_CONDITION(m->ldata()->isSetPos());
1906 44 : LD_CONDITION(s->ldata()->isSetPos());
1907 : }
1908 :
1909 44 : Autoplace::autoplaceSegmentElement(item, ldata);
1910 44 : }
1911 :
1912 0 : void TLayout::layoutDeadSlapped(const DeadSlapped* item, DeadSlapped::LayoutData* ldata)
1913 : {
1914 : LAYOUT_CALL_ITEM(item);
1915 : LD_INDEPENDENT;
1916 :
1917 0 : if (ldata->isValid()) {
1918 0 : return;
1919 : }
1920 :
1921 0 : const double deadSlappedWidth = item->spatium() * 2;
1922 0 : RectF rect = RectF(0, 0, deadSlappedWidth, item->staff()->staffHeight());
1923 0 : ldata->setBbox(rect);
1924 0 : ldata->setPos(PointF());
1925 :
1926 : // fillPath
1927 : {
1928 0 : constexpr double crossThinknessPercentage = 0.1;
1929 0 : double height = rect.height();
1930 0 : double width = rect.width();
1931 0 : double crossThickness = width * crossThinknessPercentage;
1932 :
1933 0 : PointF topLeft = PointF(rect.x(), rect.y());
1934 0 : PointF bottomRight = topLeft + PointF(width, height);
1935 0 : PointF topRight = topLeft + PointF(width, 0);
1936 0 : PointF bottomLeft = topLeft + PointF(0, height);
1937 0 : PointF offsetX = PointF(crossThickness, 0);
1938 :
1939 0 : PainterPath path1;
1940 0 : path1.moveTo(topLeft);
1941 0 : path1.lineTo(topLeft + offsetX);
1942 0 : path1.lineTo(bottomRight);
1943 0 : path1.lineTo(bottomRight - offsetX);
1944 0 : path1.lineTo(topLeft);
1945 :
1946 0 : PainterPath path2;
1947 0 : path2.moveTo(topRight);
1948 0 : path2.lineTo(topRight - offsetX);
1949 0 : path2.lineTo(bottomLeft);
1950 0 : path2.lineTo(bottomLeft + offsetX);
1951 0 : path2.lineTo(topRight);
1952 :
1953 0 : ldata->path1 = path1;
1954 0 : ldata->path2 = path2;
1955 0 : }
1956 : }
1957 :
1958 67 : void TLayout::layoutJianpuDurationLine(JianpuDurationLine* item, const LayoutContext& ctx)
1959 : {
1960 : LAYOUT_CALL_ITEM(item);
1961 67 : double mag = item->mag();
1962 67 : JianpuDurationLine::LayoutData* ldata = item->mutldata();
1963 67 : ldata->setMag(mag);
1964 67 : ldata->lineWidth = ctx.conf().styleAbsolute(Sid::jianpuDurationLineThickness) * mag;
1965 67 : if (item->staff()) {
1966 67 : const_cast<JianpuDurationLine*>(item)->setColor(item->staff()->staffType(item->tick())->color());
1967 : }
1968 :
1969 67 : double w2 = ldata->lineWidth * .5;
1970 67 : ldata->setBbox(0, -w2, ldata->len, 2 * w2);
1971 67 : }
1972 :
1973 1145 : void TLayout::layoutDynamic(Dynamic* item, Dynamic::LayoutData* ldata, const LayoutConfiguration& conf)
1974 : {
1975 1145 : DynamicsLayout::layoutDynamic(item, ldata, conf);
1976 1145 : }
1977 :
1978 34 : void TLayout::layoutExpression(const Expression* item, Expression::LayoutData* ldata)
1979 : {
1980 : LAYOUT_CALL_ITEM(item);
1981 34 : IF_ASSERT_FAILED(item->ownershipParent()) {
1982 0 : return;
1983 : }
1984 :
1985 34 : const_cast<Expression*>(item)->setPlacementBasedOnVoiceAssignment(item->style().styleV(
1986 : Sid::dynamicsHairpinVoiceBasedPlacement).value<DirectionV>());
1987 :
1988 34 : TextLayout::layoutBaseTextBase(item, ldata);
1989 :
1990 34 : const Segment* segment = item->segment();
1991 :
1992 34 : if (item->align().horizontal != AlignH::LEFT) {
1993 0 : Chord* chordToAlign = nullptr;
1994 : // Look for chord in this staff
1995 0 : track_idx_t startTrack = track2staff(item->staffIdx());
1996 0 : track_idx_t endTrack = startTrack + VOICES;
1997 0 : for (track_idx_t track = startTrack; track < endTrack; ++track) {
1998 0 : EngravingItem* e = segment->element(track);
1999 0 : if (e && e->isChord()) {
2000 0 : chordToAlign = toChord(e);
2001 0 : break;
2002 : }
2003 : }
2004 :
2005 0 : if (chordToAlign && !chordToAlign->notes().empty()) {
2006 0 : const Note* note = chordToAlign->notes().at(0);
2007 0 : double headWidth = note->headWidth();
2008 0 : bool center = item->align().horizontal == AlignH::HCENTER;
2009 0 : ldata->moveX(headWidth * (center ? 0.5 : 1));
2010 : }
2011 : }
2012 :
2013 34 : ldata->disconnectSnappedItems();
2014 :
2015 34 : if (!item->autoplace()) {
2016 0 : return;
2017 : }
2018 :
2019 34 : const Segment* s = item->segment();
2020 34 : const Measure* m = s->measure();
2021 34 : LD_CONDITION(ldata->isSetPos());
2022 34 : LD_CONDITION(m->ldata()->isSetPos());
2023 34 : LD_CONDITION(s->ldata()->isSetPos());
2024 :
2025 34 : Dynamic* dynamic = toDynamic(segment->findAnnotation(ElementType::DYNAMIC, item->track(), item->track()));
2026 34 : if (!dynamic) {
2027 32 : Segment* timeTickSeg = m->findSegmentR(SegmentType::TimeTick, s->rtick());
2028 32 : dynamic = timeTickSeg ? toDynamic(timeTickSeg->findAnnotation(ElementType::DYNAMIC, item->track(), item->track())) : nullptr;
2029 : }
2030 2 : if (!dynamic || dynamic->placeAbove() != item->placeAbove() || dynamic->voiceAssignment() != item->voiceAssignment()
2031 36 : || !dynamic->visible()) {
2032 32 : Autoplace::autoplaceSegmentElement(item, ldata);
2033 32 : return;
2034 : }
2035 :
2036 2 : LD_CONDITION(dynamic->ldata()->isSetBbox()); // dynamic->shape()
2037 2 : LD_CONDITION(dynamic->ldata()->isSetPos());
2038 :
2039 : // If there is a dynamic on same segment and track make space for it horizontally
2040 2 : double padding = item->computeDynamicExpressionDistance(dynamic);
2041 2 : double dynamicRight = dynamic->shape().translate(dynamic->pos()).right();
2042 2 : double expressionLeft = ldata->bbox().translated(item->pos()).left();
2043 2 : double difference = expressionLeft - dynamicRight - padding;
2044 2 : ldata->moveX(-difference);
2045 :
2046 2 : if (item->snapToDynamics()) {
2047 2 : ldata->connectItemSnappedBefore(dynamic);
2048 : }
2049 :
2050 2 : Autoplace::autoplaceSegmentElement(item, ldata);
2051 : }
2052 :
2053 618 : void TLayout::layoutFermata(const Fermata* item, Fermata::LayoutData* ldata)
2054 : {
2055 : LAYOUT_CALL_ITEM(item);
2056 618 : const StaffType* stType = item->staffType();
2057 618 : if (stType && stType->isHiddenElementOnTab(Sid::fermataShowTabCommon, Sid::fermataShowTabSimple)) {
2058 0 : ldata->setIsSkipDraw(true);
2059 0 : return;
2060 : }
2061 618 : ldata->setIsSkipDraw(false);
2062 618 : ldata->setPos(PointF());
2063 :
2064 618 : double x = 0.0;
2065 618 : double y = item->placeAbove() ? 0.0 : item->staff()->staffHeight(item->tick());
2066 618 : const Segment* s = item->segment();
2067 618 : const track_idx_t layoutParentTrack = s->isType(SegmentType::BarLineTypes) ? item->staffIdx() * VOICES : item->track();
2068 618 : const EngravingItem* e = s->element(layoutParentTrack);
2069 :
2070 618 : if (e) {
2071 618 : LD_CONDITION(e->ldata()->isSetBbox()); // e->shape()
2072 618 : LD_CONDITION(e->ldata()->isSetPos());
2073 :
2074 618 : if (e->isChord()) {
2075 481 : const Chord* chord = toChord(e);
2076 481 : x = chord->x() + ChordLayout::centerX(chord);
2077 137 : } else if (e->isRest()) {
2078 56 : const Rest* rest = toRest(e);
2079 56 : x = rest->x() + rest->centerX();
2080 : } else {
2081 81 : x = e->x() + e->shape().left() + e->width() * item->staff()->staffMag(Fraction(0, 1)) * .5;
2082 : }
2083 : }
2084 :
2085 618 : String name = String::fromAscii(SymNames::nameForSymId(item->symId()).ascii());
2086 618 : if (item->placeAbove()) {
2087 517 : if (name.endsWith(u"Below")) {
2088 : //! NOTE It is not clear whether SymId is layout data or given data.
2089 0 : const_cast<Fermata*>(item)->setSymId(SymNames::symIdByName(name.left(name.size() - 5) + u"Above"));
2090 : }
2091 : } else {
2092 101 : if (name.endsWith(u"Above")) {
2093 : //! NOTE It is not clear whether SymId is layout data or given data.
2094 19 : const_cast<Fermata*>(item)->setSymId(SymNames::symIdByName(name.left(name.size() - 5) + u"Below"));
2095 : }
2096 : }
2097 :
2098 618 : ldata->setShape(Shape(item->symBbox(item->symId()), item));
2099 618 : x -= 0.5 * ldata->bbox().width();
2100 :
2101 618 : ldata->setPos(x, y);
2102 :
2103 618 : PointF defaultPos = item->defaultPos();
2104 618 : ldata->move(defaultPos);
2105 :
2106 618 : if (item->autoplace()) {
2107 618 : const Segment* s2 = item->segment();
2108 618 : const Measure* m = s2->measure();
2109 618 : LD_CONDITION(ldata->isSetPos());
2110 618 : LD_CONDITION(m->ldata()->isSetPos());
2111 618 : LD_CONDITION(s2->ldata()->isSetPos());
2112 : }
2113 :
2114 618 : Autoplace::autoplaceSegmentElement(item, ldata);
2115 618 : }
2116 :
2117 : //---------------------------------------------------------
2118 : // FiguredBassItem layout
2119 : // creates the display text (set as element text) and computes
2120 : // the horiz. offset needed to align the right part as well as the vert. offset
2121 : //---------------------------------------------------------
2122 216 : void TLayout::layoutFiguredBassItem(const FiguredBassItem* item, FiguredBassItem::LayoutData* ldata, const LayoutContext& ctx)
2123 : {
2124 : LAYOUT_CALL_ITEM(item);
2125 : // construct font metrics
2126 216 : int fontIdx = 0;
2127 216 : Font f(FiguredBass::FBFonts().at(fontIdx).family, Font::Type::Tablature);
2128 :
2129 : // font size in pixels, scaled according to spatium()
2130 : // (use the same font selection as used in draw() below)
2131 216 : double m = ctx.conf().styleD(Sid::figuredBassFontSize) * item->spatium() / item->defaultSpatium();
2132 216 : f.setPointSizeF(m);
2133 216 : FontMetrics fm(f);
2134 :
2135 216 : String str;
2136 216 : double x = item->symWidth(SymId::noteheadBlack) * .5;
2137 216 : double x1 = 0.0;
2138 216 : double x2 = 0.0;
2139 :
2140 : // create display text
2141 216 : int font = 0;
2142 216 : int style = ctx.conf().styleI(Sid::figuredBassStyle);
2143 :
2144 216 : if (item->parenth1() != FiguredBassItem::Parenthesis::NONE) {
2145 0 : str.append(FiguredBass::FBFonts().at(font).displayParenthesis[int(item->parenth1())]);
2146 : }
2147 :
2148 : // prefix
2149 216 : if (item->prefix() != FiguredBassItem::Modifier::NONE) {
2150 : // if no digit, the string created so far 'hangs' to the left of the note
2151 18 : if (item->digit() == FBIDigitNone) {
2152 8 : x1 = fm.width(str);
2153 : }
2154 18 : str.append(FiguredBass::FBFonts().at(font).displayAccidental[int(item->prefix())]);
2155 : // if no digit, the string from here onward 'hangs' to the right of the note
2156 18 : if (item->digit() == FBIDigitNone) {
2157 8 : x2 = fm.width(str);
2158 : }
2159 : }
2160 :
2161 216 : if (item->parenth2() != FiguredBassItem::Parenthesis::NONE) {
2162 4 : str.append(FiguredBass::FBFonts().at(font).displayParenthesis[int(item->parenth2())]);
2163 : }
2164 :
2165 : // digit
2166 216 : if (item->digit() != FBIDigitNone) {
2167 : // if some digit, the string created so far 'hangs' to the left of the note
2168 188 : x1 = fm.width(str);
2169 : // if suffix is a combining shape, combine it with digit (multi-digit numbers cannot be combined)
2170 : // unless there is a parenthesis in between
2171 188 : if ((item->digit() < 10)
2172 188 : && (item->suffix() == FiguredBassItem::Modifier::CROSS
2173 183 : || item->suffix() == FiguredBassItem::Modifier::BACKSLASH
2174 181 : || item->suffix() == FiguredBassItem::Modifier::SLASH)
2175 376 : && item->parenth3() == FiguredBassItem::Parenthesis::NONE) {
2176 11 : str.append(FiguredBass::FBFonts().at(font).displayDigit[style][item->digit()][int(item->suffix())
2177 11 : - (int(FiguredBassItem::Modifier::CROSS)
2178 : - 1)]);
2179 : }
2180 : // if several digits or no shape combination, convert _digit to font styled chars
2181 : else {
2182 177 : String digits;
2183 177 : int digit = item->digit();
2184 : while (true) {
2185 177 : digits.prepend(FiguredBass::FBFonts().at(font).displayDigit[style][(digit % 10)][0]);
2186 177 : digit /= 10;
2187 177 : if (digit == 0) {
2188 177 : break;
2189 : }
2190 : }
2191 177 : str.append(digits);
2192 177 : }
2193 : // if some digit, the string from here onward 'hangs' to the right of the note
2194 188 : x2 = fm.width(str);
2195 : }
2196 :
2197 216 : if (item->parenth3() != FiguredBassItem::Parenthesis::NONE) {
2198 4 : str.append(FiguredBass::FBFonts().at(font).displayParenthesis[int(item->parenth3())]);
2199 : }
2200 :
2201 : // suffix
2202 : // append only if non-combining shape or cannot combine (no digit or parenthesis in between)
2203 216 : if (item->suffix() != FiguredBassItem::Modifier::NONE
2204 248 : && ((item->suffix() != FiguredBassItem::Modifier::CROSS
2205 27 : && item->suffix() != FiguredBassItem::Modifier::BACKSLASH
2206 25 : && item->suffix() != FiguredBassItem::Modifier::SLASH)
2207 11 : || item->digit() == FBIDigitNone
2208 11 : || item->parenth3() != FiguredBassItem::Parenthesis::NONE)) {
2209 21 : str.append(FiguredBass::FBFonts().at(font).displayAccidental[int(item->suffix())]);
2210 : }
2211 :
2212 216 : if (item->parenth4() != FiguredBassItem::Parenthesis::NONE) {
2213 0 : str.append(FiguredBass::FBFonts().at(font).displayParenthesis[int(item->parenth4())]);
2214 : }
2215 :
2216 : //! NOTE Look like, this text not layout data (not depends by position)
2217 216 : ldata->displayText = str; // this text will be displayed
2218 :
2219 216 : if (str.size()) { // if some text
2220 196 : x = x - (x1 + x2) * 0.5; // position the text so that [x1<-->x2] is centered below the note
2221 : } else { // if no text (but possibly a line)
2222 20 : x = 0; // start at note left margin
2223 : }
2224 : // vertical position
2225 216 : double h = fm.lineSpacing();
2226 216 : h *= ctx.conf().styleD(Sid::figuredBassLineHeight);
2227 216 : double y = 0.0;
2228 216 : if (ctx.conf().styleI(Sid::figuredBassAlignment) == 0) { // top alignment: stack down from first item
2229 216 : y = h * item->ord();
2230 : } else { // bottom alignment: stack up from last item
2231 0 : y = -h * (item->figuredBass()->itemsCount() - item->ord());
2232 : }
2233 216 : ldata->setPos(x, y);
2234 : // determine bbox from text width
2235 : // w = fm.width(str);
2236 216 : double w = fm.width(str);
2237 216 : ldata->textWidth = w;
2238 : // if there is a cont.line, extend width to cover the whole FB element duration line
2239 216 : int lineLen = 0;
2240 216 : if (item->contLine() != FiguredBassItem::ContLine::NONE
2241 216 : && (lineLen = item->figuredBass()->ldata()->lineLength(0)) > w) {
2242 32 : w = lineLen;
2243 : }
2244 216 : ldata->setBbox(0, 0, w, h);
2245 216 : }
2246 :
2247 : // lays out the duration indicator line(s), filling the _lineLengths array
2248 : // and the length of printed lines (used by continuation lines)
2249 :
2250 141 : static void layoutLines(const FiguredBass* item, FiguredBass::LayoutData* ldata, const LayoutContext& ctx)
2251 : {
2252 141 : std::vector<double> lineLengths = ldata->lineLengths;
2253 141 : if (item->ticks() <= Fraction(0, 1) || !item->segment()) {
2254 7 : lineLengths.resize(1); // be sure to always have
2255 7 : lineLengths[0] = 0; // at least 1 item in array
2256 7 : ldata->lineLengths = lineLengths;
2257 7 : return;
2258 : }
2259 :
2260 134 : ChordRest* lastCR = nullptr; // the last ChordRest of this
2261 134 : Segment* nextSegm = nullptr; // the Segment beyond this' segment
2262 134 : Fraction nextTick = item->segment()->tick() + item->ticks(); // the tick beyond this' duration
2263 :
2264 : // locate the measure containing the last tick of this; it is either:
2265 : // the same measure containing nextTick, if nextTick is not the first tick of a measure
2266 : // (and line should stop right before it)
2267 : // or the previous measure, if nextTick is the first tick of a measure
2268 : // (and line should stop before any measure terminal segment (bar, clef, ...) )
2269 :
2270 134 : const Measure* m = ctx.dom().tick2measure(nextTick - Fraction::eps());
2271 134 : if (m) {
2272 : // locate the first segment (of ANY type) right after this' last tick
2273 650 : for (nextSegm = m->first(SegmentType::All); nextSegm; nextSegm = nextSegm->next()) {
2274 650 : if (nextSegm->tick() >= nextTick) {
2275 134 : break;
2276 : }
2277 : }
2278 : // locate the last ChordRest of this
2279 134 : if (nextSegm) {
2280 134 : track_idx_t startTrack = trackZeroVoice(item->track());
2281 134 : track_idx_t endTrack = startTrack + VOICES;
2282 148 : for (const Segment* seg = nextSegm->prev1(); seg; seg = seg->prev1()) {
2283 204 : for (track_idx_t t = startTrack; t < endTrack; ++t) {
2284 190 : EngravingItem* el = seg->element(t);
2285 190 : if (el && el->isChordRest()) {
2286 134 : lastCR = toChordRest(el);
2287 134 : break;
2288 : }
2289 : }
2290 148 : if (lastCR) {
2291 134 : break;
2292 : }
2293 : }
2294 : }
2295 : }
2296 134 : if (!m || !nextSegm) {
2297 0 : LOGD("FiguredBass layout: no segment found for tick %d", nextTick.ticks());
2298 0 : lineLengths.resize(1); // be sure to always have
2299 0 : lineLengths[0] = 0; // at least 1 item in array
2300 0 : ldata->lineLengths = lineLengths;
2301 0 : return;
2302 : }
2303 :
2304 : // get length of printed lines from horiz. page position of lastCR
2305 : // (enter a bit 'into' the ChordRest for clarity)
2306 134 : double printedLineLength = lastCR ? lastCR->pageX() - item->pageX() + 1.5 * item->spatium() : 3 * item->spatium();
2307 134 : ldata->printedLineLength = printedLineLength;
2308 :
2309 : // get duration indicator line from page position of nextSegm
2310 : // TODO: lines on the following systems, when the duration ends on another system. The systems after
2311 : // the item's own may not have been laid out yet, so this needs an approach like that for spanners.
2312 134 : if (nextSegm->measure()->system() != item->segment()->measure()->system()) {
2313 0 : nextSegm = item->segment()->next1();
2314 : }
2315 134 : int len = nextSegm->pageX() - item->pageX() - 4; // stop 4 raster units before next segm
2316 134 : ldata->lineLengths = { static_cast<double>(len) };
2317 141 : }
2318 :
2319 161 : void TLayout::layoutFiguredBass(const FiguredBass* item, FiguredBass::LayoutData* ldata, const LayoutContext& ctx)
2320 : {
2321 : LAYOUT_CALL_ITEM(item);
2322 : // VERTICAL POSITION:
2323 161 : const double y = ctx.conf().styleD(Sid::figuredBassYOffset) * item->spatium();
2324 161 : ldata->setPos(PointF(0.0, y));
2325 :
2326 : // BOUNDING BOX and individual item layout (if required)
2327 161 : TextLayout::layoutBaseTextBase1(item, ldata); // prepare structs and data expected by Text methods
2328 : // if element could be parsed into items, layout each element
2329 : // Items list will be empty in edit mode (see FiguredBass::startEdit).
2330 : // TODO: consider disabling specific layout in case text style is changed (tid() != TextStyleName::FIGURED_BASS).
2331 161 : if (item->items().size() > 0) {
2332 141 : Shape shape;
2333 141 : layoutLines(item, ldata, ctx);
2334 : // layout each item and enlarge bbox to include items bboxes
2335 357 : for (FiguredBassItem* fit : item->items()) {
2336 216 : FiguredBassItem::LayoutData* fildata = fit->mutldata();
2337 216 : layoutFiguredBassItem(fit, fildata, ctx);
2338 216 : shape.add(fildata->bbox().translated(fit->pos()), fit);
2339 : }
2340 141 : ldata->setShape(shape);
2341 141 : }
2342 161 : }
2343 :
2344 408 : void TLayout::layoutFingering(const Fingering* item, Fingering::LayoutData* ldata)
2345 : {
2346 : LAYOUT_CALL_ITEM(item);
2347 408 : IF_ASSERT_FAILED(item->ownershipParent()) {
2348 10 : return;
2349 : }
2350 :
2351 408 : Fraction tick = item->ownershipParentItem()->tick();
2352 408 : const Staff* st = item->staff();
2353 408 : if (st && st->isTabStaff(tick)
2354 816 : && (!st->staffType(tick)->showTabFingering() || item->textStyleType() == TextStyleType::STRING_NUMBER)) {
2355 10 : ldata->setIsSkipDraw(true);
2356 10 : return;
2357 : }
2358 398 : ldata->setIsSkipDraw(false);
2359 :
2360 398 : TextLayout::layoutBaseTextBase(item, ldata);
2361 398 : ldata->setPosY(0.0); // handle placement below
2362 :
2363 398 : if (item->autoplace()) {
2364 398 : const Note* note = item->note();
2365 398 : const Chord* chord = note->chord();
2366 :
2367 398 : bool voices = chord->measure()->hasVoices(chord->staffIdx(), chord->tick(), chord->actualTicks());
2368 398 : bool tight = voices && chord->notes().size() == 1 && !chord->beam() && item->textStyleType() != TextStyleType::STRING_NUMBER;
2369 :
2370 398 : double headWidth = note->bboxRightPos();
2371 :
2372 : // temporarily exclude self from chord shape
2373 398 : const_cast<Fingering*>(item)->setAutoplace(false);
2374 :
2375 398 : if (item->layoutType() == ElementType::CHORD) {
2376 390 : bool above = item->placeAbove();
2377 390 : const Stem* stem = chord->stem();
2378 390 : const Segment* segment = chord->segment();
2379 390 : const Measure* measure = segment->measure();
2380 390 : double sp = item->spatium();
2381 390 : double md = item->minDistance().val() * sp;
2382 390 : const SysStaff* ss = measure->system()->staff(chord->vStaffIdx());
2383 390 : const Staff* vStaff = chord->staff(); // TODO: use current height at tick
2384 :
2385 390 : LD_CONDITION(measure->ldata()->isSetPos());
2386 390 : LD_CONDITION(segment->ldata()->isSetPos());
2387 390 : LD_CONDITION(chord->ldata()->isSetPos());
2388 390 : LD_CONDITION(note->ldata()->isSetPos());
2389 390 : LD_CONDITION(note->ldata()->isSetBbox());
2390 : //! NOTE A lot of spam
2391 : // LD_CONDITION(note->ldata()->mirror.has_value());
2392 390 : if (stem) {
2393 390 : LD_CONDITION(stem->ldata()->isSetPos());
2394 390 : LD_CONDITION(stem->ldata()->isSetBbox());
2395 : }
2396 390 : LD_CONDITION(chord->upNote()->ldata()->isSetBbox());
2397 390 : LD_CONDITION(chord->upNote()->ldata()->isSetPos());
2398 390 : LD_CONDITION(chord->downNote()->ldata()->isSetBbox());
2399 390 : LD_CONDITION(chord->downNote()->ldata()->isSetPos());
2400 :
2401 390 : if (note->ldata()->mirror.value(LD_ACCESS::BAD)) {
2402 0 : ldata->moveX(-note->ldata()->pos().x());
2403 : }
2404 390 : ldata->moveX(headWidth * .5);
2405 390 : if (above) {
2406 295 : if (tight) {
2407 20 : if (chord->stem()) {
2408 20 : ldata->moveX(-0.8 * sp);
2409 : }
2410 20 : ldata->moveY(-1.5 * sp);
2411 : } else {
2412 275 : RectF r = ldata->bbox().translated(measure->pos() + segment->pos() + chord->pos() + note->pos() + item->pos());
2413 275 : SkylineLine sk(false);
2414 275 : sk.add(r, const_cast<Fingering*>(item));
2415 275 : double d = sk.minDistance(ss->skyline().north());
2416 275 : double yd = 0.0;
2417 275 : if (d > 0.0 && item->isStyled(Pid::MIN_DISTANCE)) {
2418 259 : yd -= d + item->ldata()->bbox().height() * .25;
2419 : }
2420 : // force extra space above staff & chord (but not other fingerings)
2421 275 : double top = 0.0;
2422 275 : if (chord->up() && chord->beam() && stem) {
2423 53 : top = stem->y() + stem->ldata()->bbox().top();
2424 : } else {
2425 222 : const Note* un = chord->upNote();
2426 222 : top = std::min(0.0, un->y() + un->ldata()->bbox().top());
2427 : }
2428 275 : top -= md;
2429 275 : double diff = (ldata->bbox().bottom() + ldata->pos().y() + yd + note->y()) - top;
2430 275 : if (diff > 0.0) {
2431 202 : yd -= diff;
2432 : }
2433 275 : ldata->moveY(yd);
2434 275 : }
2435 : } else {
2436 95 : if (tight) {
2437 10 : if (chord->stem()) {
2438 10 : ldata->moveX(0.8 * sp);
2439 : }
2440 10 : ldata->moveY(1.5 * sp);
2441 : } else {
2442 85 : RectF r = ldata->bbox().translated(measure->pos() + segment->pos() + chord->pos() + note->pos() + item->pos());
2443 85 : SkylineLine sk(true);
2444 85 : sk.add(r, const_cast<Fingering*>(item));
2445 85 : double d = ss->skyline().south().minDistance(sk);
2446 85 : double yd = 0.0;
2447 85 : if (d > 0.0 && item->isStyled(Pid::MIN_DISTANCE)) {
2448 85 : yd += d + item->ldata()->bbox().height() * .25;
2449 : }
2450 : // force extra space below staff & chord (but not other fingerings)
2451 : double bottom;
2452 85 : if (!chord->up() && chord->beam() && stem) {
2453 10 : bottom = stem->y() + stem->ldata()->bbox().bottom();
2454 : } else {
2455 75 : const Note* dn = chord->downNote();
2456 75 : bottom = std::max(vStaff->staffHeight(item->tick()), dn->y() + dn->ldata()->bbox().bottom());
2457 : }
2458 85 : bottom += md;
2459 85 : double diff = bottom - (ldata->bbox().top() + ldata->pos().y() + yd + note->y());
2460 85 : if (diff > 0.0) {
2461 83 : yd += diff;
2462 : }
2463 85 : ldata->moveY(yd);
2464 85 : }
2465 : }
2466 8 : } else if (item->textStyleType() == TextStyleType::LH_GUITAR_FINGERING) {
2467 : // place to left of note
2468 8 : double left = note->shape().left();
2469 8 : if (left + note->x() < 0.0) {
2470 2 : ldata->moveX(left);
2471 : } else {
2472 6 : ldata->moveX(-note->x());
2473 : }
2474 : }
2475 : // for other fingering styles, do not autoplace
2476 :
2477 : // restore autoplace
2478 398 : const_cast<Fingering*>(item)->setAutoplace(true);
2479 : }
2480 : }
2481 :
2482 685 : void TLayout::layoutFretDiagram(const FretDiagram* item, FretDiagram::LayoutData* ldata, const LayoutContext& ctx)
2483 : {
2484 : LAYOUT_CALL_ITEM(item);
2485 685 : double spatium = item->spatium() * item->userMag();
2486 685 : ldata->stringLineWidth = spatium * 0.08;
2487 1326 : ldata->nutLineWidth = ((item->fretOffset() || !item->showNut()) ? ldata->stringLineWidth
2488 641 : : ctx.conf().styleAbsolute(Sid::fretNutThickness) * item->userMag());
2489 685 : ldata->nutY = -0.5 * (ldata->nutLineWidth - ldata->stringLineWidth);
2490 685 : ldata->stringDist = ctx.conf().styleAbsolute(Sid::fretStringSpacing) * item->userMag();
2491 685 : ldata->fretDist = ctx.conf().styleAbsolute(Sid::fretFretSpacing) * item->userMag();
2492 685 : ldata->markerSize = ldata->stringDist * .8;
2493 685 : ldata->markerY = ldata->nutY - 0.5 * ldata->nutLineWidth - ldata->markerSize - 0.20 * spatium;
2494 685 : bool extendedStyle = item->style().styleB(Sid::fretStyleExtended);
2495 685 : ldata->stringExtendTop = item->fretOffset() && extendedStyle ? -spatium * .2 : 0.0;
2496 685 : ldata->stringExtendBottom = extendedStyle ? 0.5 * ldata->fretDist : 0.0;
2497 685 : ldata->dotDiameter = ctx.conf().styleAbsolute(Sid::fretDotSpatiumSize) * item->userMag();
2498 685 : ldata->gridHeight = 0.0;
2499 :
2500 685 : double shapeMarginAboveDiagram = ldata->fretDist * 1.5;
2501 685 : double w = ldata->stringDist * (item->strings() - 1) + ldata->markerSize;
2502 685 : double h = item->frets() * ldata->fretDist + ldata->stringExtendBottom + 0.5 * ldata->stringLineWidth + shapeMarginAboveDiagram;
2503 685 : double y = -shapeMarginAboveDiagram;
2504 685 : double x = -(ldata->markerSize * .5);
2505 :
2506 685 : bool isVertical = item->orientation() == Orientation::VERTICAL;
2507 :
2508 : // Allocate space for fret offset number
2509 685 : if (item->fretOffset() > 0) {
2510 44 : double padding = 0.4 * ldata->stringDist;
2511 48 : for (auto i : item->barres()) {
2512 36 : FretItem::Barre barre = i.second;
2513 36 : if (!barre.exists()) {
2514 0 : continue;
2515 : }
2516 32 : if ((barre.startString == 0 && item->numPos() == 0)
2517 68 : || ((barre.endString == -1 || barre.endString == item->strings() - 1) && item->numPos() == 1)) {
2518 32 : padding += 0.20 * ldata->dotDiameter * ctx.conf().styleD(Sid::barreLineWidth);
2519 32 : break;
2520 : }
2521 : }
2522 44 : Font scaledFont(item->fretNumFont());
2523 44 : FontMetrics fm2(scaledFont);
2524 44 : String fretText = String::number(item->fretOffset() + 1);
2525 44 : if (ctx.conf().styleB(Sid::fretUseCustomSuffix)) {
2526 0 : fretText += ctx.conf().styleSt(Sid::fretCustomSuffix);
2527 : }
2528 44 : ldata->fretText = fretText;
2529 44 : double numw = isVertical ? fm2.width(fretText) : fm2.tightBoundingRect(fretText).height();
2530 44 : double xdiff = numw + padding;
2531 44 : w += xdiff;
2532 44 : x += (item->numPos() == 0) == isVertical ? -xdiff : 0;
2533 44 : ldata->fretNumPadding = padding;
2534 44 : }
2535 :
2536 685 : if (!isVertical) {
2537 0 : double tempW = w,
2538 0 : tempX = x;
2539 0 : w = h;
2540 0 : h = tempW;
2541 0 : x = y;
2542 0 : y = tempX;
2543 : }
2544 :
2545 : // When changing how bbox is calculated, don't forget to update the centerX and rightX methods too.
2546 685 : Shape shape;
2547 685 : shape.add(RectF(x, y, w, h), item);
2548 :
2549 685 : ldata->gridHeight = h;
2550 :
2551 685 : ldata->fingeringItems.clear();
2552 685 : if (item->showFingering()) {
2553 0 : const double padding = 0.2 * spatium;
2554 0 : Font fingeringFont = item->fingeringFont();
2555 0 : FontMetrics fontMetrics(fingeringFont);
2556 0 : double fontHeight = fontMetrics.capHeight();
2557 0 : for (size_t i = 0; i < item->fingering().size(); ++i) {
2558 0 : int finger = item->fingering()[i];
2559 0 : if (finger == 0) {
2560 0 : continue;
2561 : }
2562 0 : String fingerS = String::number(finger);
2563 0 : double width = fontMetrics.width(fingerS);
2564 0 : double digitHeight = fontMetrics.tightBoundingRect(fingerS).height();
2565 0 : double xOff = -0.5 * width;
2566 0 : double fingerX = isVertical ? (item->strings() - i - 1) * ldata->stringDist + xOff : x + w + padding;
2567 0 : double fingerY = isVertical ? y + h + fontHeight + padding : i * ldata->stringDist + 0.5 * digitHeight;
2568 0 : ldata->fingeringItems.push_back(FretDiagram::FingeringItem(fingerS, PointF(fingerX, fingerY)));
2569 0 : shape.add(ShapeElement(fontMetrics.tightBoundingRect(fingerS).translated(PointF(fingerX, fingerY)), item));
2570 0 : }
2571 0 : }
2572 :
2573 685 : ldata->setShape(shape);
2574 :
2575 685 : if (item->ownershipParent()->isSegment()) {
2576 : // We need to get the width of the notehead/rest in order to position the fret diagram correctly
2577 615 : Segment* pSeg = item->segment();
2578 615 : double noteheadWidth = 0.0;
2579 615 : if (pSeg->isChordRestType()) {
2580 615 : staff_idx_t idx = item->staff()->idx();
2581 615 : for (EngravingItem* e = pSeg->firstElementOfSegment(idx); e; e = pSeg->nextElementOfSegment(e, idx)) {
2582 481 : if (e->isRest()) {
2583 268 : const Rest* r = toRest(e);
2584 268 : LD_CONDITION(r->ldata()->sym.has_value());
2585 268 : noteheadWidth = item->symWidth(r->ldata()->sym());
2586 268 : break;
2587 213 : } else if (e->isNote()) {
2588 213 : Note* n = toNote(e);
2589 213 : noteheadWidth = n->headWidth();
2590 213 : break;
2591 : }
2592 : }
2593 : }
2594 615 : if (RealIsNull(noteheadWidth)) {
2595 : // If note or rest not found, use standard notehead width
2596 134 : noteheadWidth = item->symWidth(SymId::noteheadBlack);
2597 : }
2598 :
2599 615 : ldata->setPos((noteheadWidth - item->mainWidth()) / 2, -(ldata->shape().bottom() + item->styleP(Sid::fretY)));
2600 : }
2601 :
2602 685 : Harmony* harmony = item->harmony();
2603 685 : if (harmony) {
2604 514 : TLayout::layoutHarmony(harmony, harmony->mutldata(), ctx);
2605 514 : if (item->visible() && harmony->autoplace()) {
2606 514 : double vertDist = ldata->bbox().top() - harmony->ldata()->bbox().translated(harmony->pos()).bottom();
2607 514 : double diff = vertDist - harmony->minDistance().val() * item->spatium();
2608 514 : if (diff < 0) {
2609 493 : harmony->mutldata()->moveY(diff);
2610 : }
2611 : }
2612 : }
2613 :
2614 685 : ldata->slurPaths.clear();
2615 886 : for (auto i : item->barres()) {
2616 201 : FretItem::Barre barre = i.second;
2617 201 : if (!barre.exists()) {
2618 0 : continue;
2619 : }
2620 201 : int startString = barre.startString;
2621 201 : int endString = barre.endString != -1 ? barre.endString : item->strings() - 1;
2622 201 : int fret = i.first;
2623 201 : bool slurStyleBarre = ctx.conf().styleB(Sid::barreAppearanceSlur);
2624 1320 : for (int string = startString; string <= endString; ++string) {
2625 1119 : if (slurStyleBarre) {
2626 0 : const_cast<FretDiagram*>(item)->addDotForDotStyleBarre(string, fret);
2627 : } else {
2628 1119 : const_cast<FretDiagram*>(item)->removeDotForDotStyleBarre(string, fret);
2629 : }
2630 : }
2631 201 : int length = endString - startString;
2632 201 : double insetX = (length < 2 ? 1.25 : 2) * ldata->stringLineWidth;
2633 201 : double insetY = fret == 1 ? ldata->nutLineWidth + ldata->stringLineWidth : length < 2 ? 2 * ldata->stringLineWidth : insetX;
2634 201 : double startX = startString * ldata->stringDist + insetX;
2635 201 : double endX = (endString) * ldata->stringDist - insetX;
2636 201 : double shoulderXoffset = 0.2 * (endX - startX);
2637 201 : double startEndY = (fret - 1) * ldata->fretDist - insetY;
2638 201 : double shoulderY = startEndY - (length < 2 ? 0.3 : length < 3 ? 0.4 : 0.5) * ldata->fretDist;
2639 201 : double slurThickness = (length < 2 ? 0.08 : 0.1) * item->spatium() * item->userMag();
2640 201 : double shoulderYfor = shoulderY - slurThickness;
2641 201 : double shoulderYback = shoulderY + slurThickness;
2642 201 : PointF bezier1for = PointF(startX + shoulderXoffset, shoulderYfor);
2643 201 : PointF bezier2for = PointF(endX - shoulderXoffset, shoulderYfor);
2644 201 : PointF bezier1back = PointF(startX + shoulderXoffset, shoulderYback);
2645 201 : PointF bezier2back = PointF(endX - shoulderXoffset, shoulderYback);
2646 201 : PainterPath slurPath = PainterPath();
2647 201 : slurPath.moveTo(startX, startEndY);
2648 201 : slurPath.cubicTo(bezier1for, bezier2for, PointF(endX, startEndY));
2649 201 : slurPath.cubicTo(bezier2back, bezier1back, PointF(startX, startEndY));
2650 201 : ldata->slurPaths.push_back(slurPath);
2651 201 : }
2652 685 : }
2653 :
2654 486 : void TLayout::layoutGlissando(Glissando* item, LayoutContext& ctx)
2655 : {
2656 : LAYOUT_CALL_ITEM(item);
2657 :
2658 486 : if (item->startElement()) {
2659 486 : item->setTick(item->startElement()->tick());
2660 : }
2661 :
2662 486 : if (item->endElement()) {
2663 486 : item->setTick2(item->endElement()->tick());
2664 : }
2665 :
2666 486 : TLayout::layoutLine(const_cast<Glissando*>(item), ctx);
2667 :
2668 486 : if (item->spannerSegments().empty()) {
2669 0 : LOGD("no segments");
2670 0 : return;
2671 : }
2672 486 : Glissando::LayoutData* ldata = item->mutldata();
2673 486 : ldata->setPos(0.0, 0.0);
2674 :
2675 486 : String instrId = item->staff()->part()->instrumentId(item->tick());
2676 486 : bool harpStaff = instrId == "harp";
2677 486 : if (!item->isHarpGliss().has_value()) {
2678 71 : item->setIsHarpGliss(harpStaff);
2679 : } else {
2680 415 : if (harpStaff != item->isHarpGliss().value()) {
2681 : // Preserve whether this gliss has its default playback style
2682 0 : bool defaultStyle = false;
2683 0 : if (item->isStyled(Pid::GLISS_STYLE)) {
2684 0 : defaultStyle = true;
2685 : }
2686 0 : item->setIsHarpGliss(harpStaff);
2687 0 : if (defaultStyle) {
2688 0 : item->resetProperty(Pid::GLISS_STYLE);
2689 : }
2690 :
2691 : // Make sure harp glisses can only be diatonic and chromatic
2692 0 : GlissandoStyle glissStyle = item->glissandoStyle();
2693 0 : if (item->isHarpGliss().value()
2694 0 : && (glissStyle != GlissandoStyle::DIATONIC
2695 0 : && glissStyle != GlissandoStyle::CHROMATIC)) {
2696 0 : item->setGlissandoStyle(GlissandoStyle::DIATONIC);
2697 : }
2698 : }
2699 : }
2700 :
2701 486 : layoutNoteAnchoredLine(item, ldata, ctx);
2702 :
2703 486 : ChordLayout::addLineAttachPoints(item);
2704 486 : }
2705 :
2706 976 : void TLayout::layoutGlissandoSegment(GlissandoSegment* item, LayoutContext&)
2707 : {
2708 : LAYOUT_CALL_ITEM(item);
2709 :
2710 976 : GlissandoSegment::LayoutData* ldata = item->mutldata();
2711 976 : if (item->pos2().x() <= 0) {
2712 332 : ldata->setBbox(RectF());
2713 332 : return;
2714 : }
2715 :
2716 644 : if (item->staff()) {
2717 644 : ldata->setMag(item->staff()->staffMag(item->tick()));
2718 : }
2719 644 : RectF r = RectF(0.0, 0.0, item->pos2().x(), item->pos2().y()).normalized();
2720 644 : double lw = item->absoluteFromSpatium(item->lineWidth()) * .5;
2721 644 : item->setbbox(r.adjusted(-lw, -lw, lw, lw));
2722 : }
2723 :
2724 1044 : void TLayout::layoutGraceNotesGroup(GraceNotesGroup* item, LayoutContext& ctx)
2725 : {
2726 1044 : const StaffType* staffType = item->parent()->staffType();
2727 1044 : if (staffType->isTabStaff() && !item->empty()) {
2728 45 : const Staff* staff = item->parent()->staff();
2729 45 : if (staff->links()) {
2730 33 : for (EngravingObject* linkedSt : *staff->links()) {
2731 33 : Staff* linkedStaff = toStaff(linkedSt);
2732 66 : if (linkedStaff->score() == staff->score() && !linkedStaff->staffType()->isTabStaff()
2733 66 : && linkedStaff->idx() < staff->idx()) {
2734 : // HACK: align grace notes in TAB to their linked notation staff.
2735 : // CAUTION: Works only if notation staff is above the TAB.
2736 : // TODO: refactor grace notes to allow doing this properly.
2737 33 : alignTabGraceNotesToMainStave(item, linkedStaff);
2738 33 : return;
2739 : }
2740 : }
2741 : }
2742 : }
2743 :
2744 : LAYOUT_CALL_ITEM(item);
2745 1011 : Shape _shape;
2746 2354 : for (size_t i = item->size() - 1; i != muse::nidx; --i) {
2747 1343 : Chord* grace = item->at(i);
2748 1343 : Shape graceShape = grace->shape();
2749 1343 : Shape groupShape = _shape;
2750 1343 : groupShape.remove_if([grace](ShapeElement& s) {
2751 1695 : if (!s.item() || (s.item()->isStem() && s.item()->vStaffIdx() != grace->vStaffIdx())) {
2752 0 : return true;
2753 : }
2754 1695 : return false;
2755 : });
2756 : double offset;
2757 1343 : offset = -std::max(HorizontalSpacing::minHorizontalDistance(graceShape, groupShape, grace->spatium()), 0.0);
2758 : // Adjust spacing for cross-beam situations
2759 1343 : if (i < item->size() - 1 && grace->stem()) {
2760 268 : Chord* prevGrace = item->at(i + 1);
2761 268 : if (prevGrace->up() != grace->up()) {
2762 0 : double crossCorrection = grace->notes().front()->headWidth() - grace->stem()->width();
2763 0 : if (prevGrace->up() && !grace->up()) {
2764 0 : offset += crossCorrection;
2765 : } else {
2766 0 : offset -= crossCorrection;
2767 : }
2768 : }
2769 : }
2770 1343 : _shape.add(graceShape.translate(PointF(offset, 0.0)));
2771 1343 : double xpos = offset - item->parent()->rxoffset() - item->parent()->ldata()->pos().x();
2772 1343 : grace->setPos(xpos, 0.0);
2773 1343 : }
2774 :
2775 1011 : const Segment* appendedSeg = item->appendedSegment();
2776 1011 : Chord* parentChord = toChord(item->parent());
2777 1011 : Shape staffShape = appendedSeg->staffShape(parentChord->staffIdx());
2778 1011 : bool isTabStaff = parentChord->staffType() && parentChord->staffType()->isTabStaff();
2779 1011 : if (isTabStaff) {
2780 12 : staffShape.remove_if([](ShapeElement& el) {
2781 : // Ignore stems on tab staves
2782 18 : return el.item() && el.item()->isStem();
2783 : });
2784 : }
2785 1011 : double _shapeSpatium = HorizontalSpacing::shapeSpatium(_shape);
2786 1011 : double xPos = -HorizontalSpacing::minHorizontalDistance(_shape, staffShape, _shapeSpatium);
2787 :
2788 : // If the parent chord is cross-staff, also check against shape in the other staff and take the minimum
2789 1011 : if (item->parent()->staffMove() != 0) {
2790 0 : double xPosCross = -HorizontalSpacing::minHorizontalDistance(_shape,
2791 0 : appendedSeg->staffShape(item->parent()->vStaffIdx()),
2792 0 : _shapeSpatium);
2793 0 : xPos = std::min(xPos, xPosCross);
2794 : }
2795 : // Same if the grace note itself is cross-staff
2796 1011 : Chord* firstGN = item->back();
2797 1011 : if (firstGN->staffMove() != 0) {
2798 0 : double xPosCross = -HorizontalSpacing::minHorizontalDistance(_shape,
2799 : appendedSeg->staffShape(firstGN->vStaffIdx()),
2800 0 : _shapeSpatium);
2801 0 : xPos = std::min(xPos, xPosCross);
2802 : }
2803 : // Safety net in case the shape checks don't succeed
2804 1011 : xPos = std::min(xPos, -double(ctx.conf().styleAbsolute(Sid::graceToMainNoteDist) + firstGN->notes().front()->headWidth() / 2));
2805 :
2806 1011 : item->setPos(xPos, 0.0);
2807 :
2808 1011 : if (isTabStaff) {
2809 12 : ChordLayout::layoutStem(parentChord, ctx);
2810 : }
2811 1011 : }
2812 :
2813 106192 : void TLayout::layoutGraceNotesGroup2(const GraceNotesGroup* item, GraceNotesGroup::LayoutData* ldata)
2814 : {
2815 : LAYOUT_CALL_ITEM(item);
2816 : //! NOTE A lot of spam
2817 : // LD_CONDITION(ldata->isSetPos());
2818 :
2819 106192 : Shape shape(Shape::Type::Composite);
2820 107105 : for (const Chord* grace : *item) {
2821 913 : LD_CONDITION(grace->ldata()->isSetShape());
2822 913 : LD_CONDITION(grace->ldata()->isSetPos());
2823 :
2824 913 : shape.add(grace->shape(LD_ACCESS::PASS).translate(grace->pos() - item->pos()));
2825 : }
2826 106192 : ldata->setShape(shape);
2827 106192 : }
2828 :
2829 33 : void TLayout::alignTabGraceNotesToMainStave(GraceNotesGroup* graceNotes, const Staff* notationStaff)
2830 : {
2831 : // HACK: align grace notes in TAB to their linked notation staff.
2832 : // CAUTION: Works only if notation staff is above the TAB.
2833 : // TODO: refactor grace notes to allow doing this properly.
2834 :
2835 70 : for (Chord* grace : *graceNotes) {
2836 37 : Chord* linkedGrace = toChord(grace->findLinkedInStaff(notationStaff));
2837 37 : if (!linkedGrace) {
2838 0 : continue;
2839 : }
2840 37 : PointF linkedGracePos = linkedGrace->ldata()->pos();
2841 37 : grace->setPos(linkedGracePos);
2842 : }
2843 33 : }
2844 :
2845 22 : void TLayout::manageTempoChangeSnapping(GradualTempoChangeSegment* item, LayoutContext& ctx)
2846 : {
2847 22 : GradualTempoChangeSegment::LayoutData* ldata = item->mutldata();
2848 :
2849 22 : ldata->disconnectSnappedItems();
2850 22 : GradualTempoChangeSegment* tempoChangeSegmentSnappedBefore = item->findElementToSnapBefore();
2851 22 : if (tempoChangeSegmentSnappedBefore) {
2852 0 : ldata->connectItemSnappedBefore(tempoChangeSegmentSnappedBefore);
2853 0 : doLayoutGradualTempoChangeSegment(tempoChangeSegmentSnappedBefore, ctx);
2854 : }
2855 :
2856 22 : TempoText* tempoTextSnappedAfter = item->findElementToSnapAfter();
2857 22 : if (tempoTextSnappedAfter) {
2858 4 : ldata->connectItemSnappedAfter(tempoTextSnappedAfter);
2859 : }
2860 22 : }
2861 :
2862 22 : void TLayout::doLayoutGradualTempoChangeSegment(GradualTempoChangeSegment* item, LayoutContext& ctx)
2863 : {
2864 22 : GradualTempoChangeSegment::LayoutData* ldata = item->mutldata();
2865 22 : ldata->setPosY(0.0);
2866 :
2867 4 : auto extendLineToSnappedItemAfter = [item](EngravingItem* itemAfter) {
2868 4 : assert(itemAfter->isGradualTempoChangeSegment() || itemAfter->isTempoText());
2869 4 : if (item->tempoChange()->adjustForRehearsalMark(false)
2870 4 : || itemAfter->findAncestor(ElementType::SYSTEM) != item->system()) {
2871 0 : return;
2872 : }
2873 :
2874 4 : double xItemPos = itemAfter->pageX() - item->system()->pageX();
2875 4 : double itemLeftEdge = xItemPos + itemAfter->ldata()->bbox().left();
2876 :
2877 4 : double padding = item->spatium();
2878 4 : if (itemAfter->isTempoText()) {
2879 4 : const double fontSizeScaleFactor = toTempoText(itemAfter)->size() / 10.0;
2880 4 : padding = item->spatium() * fontSizeScaleFactor;
2881 0 : } else if (itemAfter->isGradualTempoChangeSegment()) {
2882 0 : Text* startText = toGradualTempoChangeSegment(itemAfter)->text();
2883 0 : if (startText) {
2884 0 : const double fontSizeScaleFactor = startText->size() / 10.0;
2885 0 : padding = item->spatium() * fontSizeScaleFactor;
2886 : }
2887 : }
2888 :
2889 4 : double maxTempoLineEnd = itemLeftEdge - padding;
2890 4 : double xEndDiff = maxTempoLineEnd - (item->pos().x() + item->pos2().x());
2891 4 : item->rxpos2() += xEndDiff;
2892 22 : };
2893 :
2894 22 : if (ldata->itemSnappedAfter() && ldata->itemSnappedAfter()->isTempoText()) {
2895 4 : TempoText* tempoTextSnappedAfter = toTempoText(ldata->itemSnappedAfter());
2896 4 : extendLineToSnappedItemAfter(tempoTextSnappedAfter);
2897 18 : } else if (ldata->itemSnappedAfter() && ldata->itemSnappedAfter()->isGradualTempoChangeSegment()) {
2898 0 : GradualTempoChangeSegment* tempoChangeSegmentSnappedAfter = toGradualTempoChangeSegment(ldata->itemSnappedAfter());
2899 0 : extendLineToSnappedItemAfter(tempoChangeSegmentSnappedAfter);
2900 : }
2901 :
2902 22 : layoutTextLineBaseSegment(item, ctx);
2903 :
2904 22 : Autoplace::autoplaceSpannerSegment(item, ldata);
2905 22 : }
2906 :
2907 22 : void TLayout::layoutGradualTempoChangeSegment(GradualTempoChangeSegment* item, LayoutContext& ctx)
2908 : {
2909 : LAYOUT_CALL_ITEM(item);
2910 :
2911 22 : manageTempoChangeSnapping(item, ctx);
2912 :
2913 22 : doLayoutGradualTempoChangeSegment(item, ctx);
2914 22 : }
2915 :
2916 0 : void TLayout::layoutGradualTempoChange(GradualTempoChange* item, LayoutContext& ctx)
2917 : {
2918 : LAYOUT_CALL_ITEM(item);
2919 0 : layoutLine(item, ctx);
2920 0 : }
2921 :
2922 1250 : void TLayout::layoutGuitarBend(GuitarBend* item, LayoutContext& ctx, System* system)
2923 : {
2924 : LAYOUT_CALL_ITEM(item);
2925 1250 : item->computeBendAmount();
2926 :
2927 1250 : GuitarBendLayout::updateSegmentsAndLayout(item, ctx, system);
2928 :
2929 1250 : item->updateHoldLine();
2930 1250 : if (item->holdLine()) {
2931 6 : GuitarBendLayout::updateSegmentsAndLayout(item->holdLine(), ctx, system);
2932 : }
2933 1250 : }
2934 :
2935 1250 : void TLayout::layoutGuitarBendSegment(GuitarBendSegment* item, LayoutContext& ctx)
2936 : {
2937 : LAYOUT_CALL_ITEM(item);
2938 1250 : bool tabStaff = item->staffType()->isTabStaff();
2939 1250 : if (tabStaff) {
2940 94 : GuitarBendLayout::layoutTabStaff(item, ctx);
2941 : } else {
2942 1156 : GuitarBendLayout::layoutStandardStaff(item, ctx);
2943 : }
2944 :
2945 1250 : SysStaff* staff = item->system()->staff(item->staffIdx());
2946 1250 : Skyline& skyline = staff->skyline();
2947 1250 : SkylineLine& skylineLine = tabStaff ? skyline.north() : (item->guitarBend()->ldata()->up() ? skyline.north() : skyline.south());
2948 1250 : skylineLine.add(item->shape().translate(item->pos()));
2949 :
2950 1250 : fillGuitarBendSegmentShape(item, item->mutldata());
2951 1250 : }
2952 :
2953 2573 : void TLayout::fillGuitarBendSegmentShape(const GuitarBendSegment* item, GuitarBendSegment::LayoutData* ldata)
2954 : {
2955 : LAYOUT_CALL_ITEM(item);
2956 2573 : Shape shape;
2957 2573 : shape.add(ldata->bbox(), item);
2958 2573 : if (!item->bendText()->empty()) {
2959 204 : shape.add(item->bendText()->shape().translate(item->bendText()->pos()));
2960 : }
2961 2573 : ldata->setShape(shape);
2962 2573 : }
2963 :
2964 483 : void TLayout::layoutHairpinSegment(HairpinSegment* item, LayoutContext& ctx)
2965 : {
2966 : LAYOUT_CALL_ITEM(item);
2967 483 : HairpinSegment::LayoutData* ldata = item->mutldata();
2968 :
2969 483 : const StaffType* stType = item->staffType();
2970 :
2971 483 : if (stType && stType->isHiddenElementOnTab(Sid::hairpinShowTabCommon, Sid::hairpinShowTabSimple)) {
2972 0 : ldata->setIsSkipDraw(true);
2973 0 : return;
2974 : }
2975 483 : ldata->setIsSkipDraw(false);
2976 :
2977 483 : const double _spatium = item->spatium();
2978 :
2979 483 : ldata->disconnectSnappedItems();
2980 :
2981 483 : manageHairpinSnapping(item, ctx);
2982 :
2983 483 : HairpinType type = item->hairpin()->hairpinType();
2984 483 : if (item->hairpin()->isLineType()) {
2985 26 : item->setTwoLines(false);
2986 26 : layoutTextLineBaseSegment(item, ctx);
2987 26 : item->setDrawCircledTip(false);
2988 26 : item->setCircledTipRadius(0.0);
2989 : } else {
2990 457 : item->setTwoLines(true);
2991 :
2992 457 : item->hairpin()->setBeginTextAlign({ AlignH::LEFT, AlignV::VCENTER });
2993 457 : item->hairpin()->setEndTextAlign({ AlignH::RIGHT, AlignV::VCENTER });
2994 457 : item->hairpin()->setBeginTextPosition(AlignH::LEFT);
2995 457 : item->hairpin()->setEndTextPosition(AlignH::RIGHT);
2996 :
2997 457 : double x1 = 0.0;
2998 457 : layoutTextLineBaseSegment(item, ctx);
2999 457 : if (!item->text()->empty()) {
3000 42 : x1 = item->text()->width() + _spatium * .5;
3001 : }
3002 :
3003 457 : Transform t;
3004 457 : double h1 = item->hairpin()->hairpinHeight().val() * _spatium * .5;
3005 457 : double h2 = item->hairpin()->hairpinContHeight().val() * _spatium * .5;
3006 :
3007 457 : double x = item->pos2().x();
3008 457 : if (!item->endText()->empty()) {
3009 4 : x -= (item->endText()->width() + _spatium * .5); // 0.5 spatium distance
3010 : }
3011 457 : if (x < _spatium) { // minimum size of hairpin
3012 4 : x = _spatium;
3013 : }
3014 457 : double y = item->pos2().y();
3015 457 : double len = sqrt(x * x + y * y);
3016 457 : t.rotateRadians(asin(y / len));
3017 :
3018 457 : item->setDrawCircledTip(item->hairpin()->hairpinCircledTip());
3019 457 : item->setCircledTipRadius(item->drawCircledTip() ? 0.6 * _spatium * .5 : 0.0);
3020 :
3021 457 : LineF l1, l2;
3022 :
3023 457 : switch (type) {
3024 358 : case HairpinType::CRESC_HAIRPIN: {
3025 358 : switch (item->spannerSegmentType()) {
3026 356 : case SpannerSegmentType::SINGLE:
3027 : case SpannerSegmentType::BEGIN: {
3028 356 : l1.setLine(x1 + item->circledTipRadius() * 2.0, 0.0, len, h1);
3029 356 : l2.setLine(x1 + item->circledTipRadius() * 2.0, 0.0, len, -h1);
3030 356 : PointF circledTip;
3031 356 : circledTip.setX(x1 + item->circledTipRadius());
3032 356 : circledTip.setY(0.0);
3033 356 : item->setCircledTip(circledTip);
3034 356 : } break;
3035 :
3036 2 : case SpannerSegmentType::MIDDLE:
3037 : case SpannerSegmentType::END:
3038 2 : item->setDrawCircledTip(false);
3039 2 : l1.setLine(x1, h2, len, h1);
3040 2 : l2.setLine(x1, -h2, len, -h1);
3041 2 : break;
3042 : }
3043 : }
3044 358 : break;
3045 99 : case HairpinType::DIM_HAIRPIN: {
3046 99 : switch (item->spannerSegmentType()) {
3047 99 : case SpannerSegmentType::SINGLE:
3048 : case SpannerSegmentType::END: {
3049 99 : l1.setLine(x1, h1, len - item->circledTipRadius() * 2, 0.0);
3050 99 : l2.setLine(x1, -h1, len - item->circledTipRadius() * 2, 0.0);
3051 99 : PointF circledTip;
3052 99 : circledTip.setX(len - item->circledTipRadius());
3053 99 : circledTip.setY(0.0);
3054 99 : item->setCircledTip(circledTip);
3055 99 : } break;
3056 0 : case SpannerSegmentType::BEGIN:
3057 : case SpannerSegmentType::MIDDLE:
3058 0 : item->setDrawCircledTip(false);
3059 0 : l1.setLine(x1, h1, len, +h2);
3060 0 : l2.setLine(x1, -h1, len, -h2);
3061 0 : break;
3062 : }
3063 : }
3064 99 : break;
3065 0 : default:
3066 0 : break;
3067 : }
3068 :
3069 : // Do Coord rotation
3070 457 : l1 = t.map(l1);
3071 457 : l2 = t.map(l2);
3072 457 : if (item->drawCircledTip()) {
3073 3 : item->setCircledTip(t.map(item->circledTip()));
3074 : }
3075 :
3076 457 : ldata->points[0] = l1.p1();
3077 457 : ldata->points[1] = l1.p2();
3078 457 : ldata->points[2] = l2.p1();
3079 457 : ldata->points[3] = l2.p2();
3080 457 : ldata->npoints = 4;
3081 :
3082 457 : ldata->joinedHairpin.clear();
3083 457 : if (item->spannerSegmentType() != SpannerSegmentType::MIDDLE) {
3084 456 : if (type == HairpinType::DIM_HAIRPIN && item->spannerSegmentType() != SpannerSegmentType::BEGIN) {
3085 99 : ldata->joinedHairpin << ldata->points[0] << ldata->points[1] << ldata->points[2]; // [top-left, joint, bottom-left]
3086 357 : } else if (type == HairpinType::CRESC_HAIRPIN && item->spannerSegmentType() != SpannerSegmentType::END) {
3087 356 : ldata->joinedHairpin << ldata->points[1] << ldata->points[0] << ldata->points[3]; // [top-right, joint, bottom-right]
3088 : }
3089 : }
3090 :
3091 457 : RectF r = RectF(l1.p1(), l1.p2()).normalized().united(RectF(l2.p1(), l2.p2()).normalized());
3092 457 : if (!item->text()->empty()) {
3093 42 : r.unite(item->text()->ldata()->bbox());
3094 : }
3095 457 : if (!item->endText()->empty()) {
3096 4 : r.unite(item->endText()->ldata()->bbox().translated(x + item->endText()->ldata()->bbox().width(), 0.0));
3097 : }
3098 457 : double w = item->absoluteFromSpatium(ctx.conf().styleS(Sid::hairpinLineWidth));
3099 457 : item->setbbox(r.adjusted(-w * .5, -w * .5, w, w));
3100 : }
3101 :
3102 483 : if (!item->ownershipParent()) {
3103 0 : item->setPos(PointF());
3104 0 : item->roffset() = PointF();
3105 0 : return;
3106 : }
3107 :
3108 483 : if (item->autoplace()) {
3109 483 : Autoplace::autoplaceSpannerSegment(item, ldata);
3110 : }
3111 : }
3112 :
3113 483 : void TLayout::manageHairpinSnapping(HairpinSegment* item, LayoutContext& ctx)
3114 : {
3115 483 : HairpinSegment::LayoutData* ldata = item->mutldata();
3116 :
3117 483 : EngravingItem* possibleSnapBeforeElement = nullptr;
3118 483 : EngravingItem* possibleSnapAfterElement = nullptr;
3119 483 : if (item->isSingleBeginType()) {
3120 478 : possibleSnapBeforeElement = item->findElementToSnapBefore();
3121 : }
3122 483 : if (item->isSingleEndType() && item->hairpin()->ticks().isNotZero()) {
3123 474 : possibleSnapAfterElement = item->findElementToSnapAfter();
3124 : }
3125 :
3126 483 : bool doSnapBefore = possibleSnapBeforeElement && item->hairpin()->snapToItemBefore();
3127 483 : if (doSnapBefore && possibleSnapBeforeElement->isHairpinSegment()) {
3128 58 : doSnapBefore = doSnapBefore && toHairpinSegment(possibleSnapBeforeElement)->hairpin()->snapToItemAfter()
3129 58 : && possibleSnapBeforeElement->ldata()->itemSnappedBefore() != item;
3130 : }
3131 :
3132 483 : if (doSnapBefore) {
3133 135 : ldata->connectItemSnappedBefore(possibleSnapBeforeElement);
3134 : }
3135 :
3136 483 : bool doSnapAfter = possibleSnapAfterElement && item->hairpin()->snapToItemAfter();
3137 483 : if (doSnapAfter && possibleSnapAfterElement->isHairpinSegment()) {
3138 0 : doSnapAfter = doSnapAfter && toHairpinSegment(possibleSnapAfterElement)->hairpin()->snapToItemBefore()
3139 0 : && possibleSnapAfterElement->ldata()->itemSnappedAfter() != item;
3140 : }
3141 :
3142 483 : if (doSnapAfter) {
3143 69 : ldata->connectItemSnappedAfter(possibleSnapAfterElement);
3144 : }
3145 :
3146 : // In case of dynamics/expressions before or after, make space for them horizontally
3147 483 : double mag = item->staff()->staffMag(item);
3148 483 : double hairpinDistToDynOrExpr = ctx.conf().style().styleAbsolute(Sid::autoplaceHairpinDynamicsDistance) * mag;
3149 :
3150 135 : bool makeSpaceBefore = (doSnapBefore && possibleSnapBeforeElement->isTextBase())
3151 618 : || (possibleSnapBeforeElement && possibleSnapBeforeElement->isDynamic());
3152 :
3153 483 : if (makeSpaceBefore && possibleSnapBeforeElement->findAncestor(ElementType::SYSTEM) == item->system()) {
3154 106 : double xItemPos = possibleSnapBeforeElement->pageX() - item->system()->pageX();
3155 106 : double itemRightEdge = xItemPos + possibleSnapBeforeElement->ldata()->bbox().right();
3156 106 : double xMinHairpinStart = itemRightEdge + hairpinDistToDynOrExpr;
3157 106 : double xStartDiff = ldata->pos().x() - xMinHairpinStart;
3158 106 : if (xStartDiff < 0) {
3159 106 : ldata->setPosX(xMinHairpinStart);
3160 106 : item->rxpos2() += xStartDiff;
3161 : }
3162 : }
3163 :
3164 69 : bool makeSpaceAfter = (doSnapAfter && possibleSnapAfterElement->isTextBase())
3165 552 : || (possibleSnapAfterElement && possibleSnapAfterElement->isDynamic());
3166 :
3167 483 : if (makeSpaceAfter && possibleSnapAfterElement->findAncestor(ElementType::SYSTEM) == item->system()) {
3168 69 : double xItemPos = possibleSnapAfterElement->pageX() - item->system()->pageX();
3169 69 : double itemLeftEdge = xItemPos + possibleSnapAfterElement->ldata()->bbox().left();
3170 69 : double xMaxHairpinEnd = itemLeftEdge - hairpinDistToDynOrExpr;
3171 69 : double xEndDiff = xMaxHairpinEnd - (item->pos().x() + item->pos2().x());
3172 69 : const double EXTEND_THRESHOLD = 3.0 * item->spatium();
3173 69 : if (xEndDiff < 0) {
3174 27 : item->rxpos2() += xEndDiff;
3175 42 : } else if (item->hairpin()->snapToItemAfter() && xEndDiff > EXTEND_THRESHOLD) {
3176 0 : item->rxpos2() += xEndDiff;
3177 : }
3178 : }
3179 483 : }
3180 :
3181 0 : void TLayout::layoutHairpin(Hairpin* item, LayoutContext& ctx)
3182 : {
3183 : LAYOUT_CALL_ITEM(item);
3184 0 : item->setPos(0.0, 0.0);
3185 0 : layoutTextLineBase(item, ctx);
3186 0 : }
3187 :
3188 0 : void TLayout::layoutHammerOnPullOff(HammerOnPullOff* item, LayoutContext& ctx)
3189 : {
3190 0 : layoutSlur(toSlur(item), ctx);
3191 0 : }
3192 :
3193 62 : void TLayout::layoutHammerOnPullOffSegment(HammerOnPullOffSegment* item, LayoutContext& ctx)
3194 : {
3195 : // The layout of the slur has already been done. Here we layout the H/P letters.
3196 62 : item->updateHopoText();
3197 :
3198 62 : System* system = item->system();
3199 62 : Fraction systemEndTick = system->endTick();
3200 62 : Skyline& sk = system->staff(item->staffIdx())->skyline();
3201 :
3202 138 : for (HammerOnPullOffText* hopoText : item->hopoText()) {
3203 76 : bool above = hopoText->placeAbove();
3204 :
3205 76 : Align align;
3206 76 : align.vertical = AlignV::BASELINE;
3207 76 : align.horizontal = AlignH::HCENTER;
3208 76 : hopoText->setPosition(AlignH::HCENTER);
3209 76 : hopoText->setAlign(align);
3210 76 : layoutItem(hopoText, ctx);
3211 :
3212 76 : const Chord* startChord = hopoText->startChord();
3213 76 : const Chord* endChord = hopoText->endChord();
3214 76 : double startX = startChord->systemPos().x() + startChord->upNote()->headWidth();
3215 76 : double endX = startX;
3216 76 : if (endChord->tick() < systemEndTick) {
3217 76 : endX = endChord->systemPos().x();
3218 : } else {
3219 : // The last endChord of this segment is in next system. Use end barline instead.
3220 0 : Segment* endSeg = system->lastMeasure()->last(SegmentType::BarLineTypes);
3221 0 : endX = endSeg ? endSeg->systemPos().x() : endX;
3222 : }
3223 76 : if (startChord->stem() && endChord->stem() && startChord->up() == above && endChord->up() == above) {
3224 : // Mid-way between centered on the notes and centered on the stems
3225 20 : endX += (above ? 0.5 : -0.5) * endChord->upNote()->headWidth();
3226 : }
3227 76 : double centerX = 0.5 * (startX + endX);
3228 :
3229 76 : double vertPadding = 0.5 * item->spatium();
3230 76 : Shape hopoTextShape = hopoText->ldata()->shape().translated(PointF(centerX, 0.0));
3231 76 : SkylineLine& skl = above ? sk.north() : sk.south();
3232 152 : SkylineLine skyline = skl.getFilteredCopy([](const ShapeElement& el) {
3233 2542 : return el.item() && el.item()->isArticulationFamily();
3234 76 : });
3235 :
3236 76 : double y = above ? -skyline.minDistanceToShapeAbove(hopoTextShape) : skyline.minDistanceToShapeBelow(hopoTextShape);
3237 76 : y += above ? -vertPadding : vertPadding;
3238 76 : y = above ? std::min(y, -vertPadding) : std::max(y, item->staff()->staffHeight(item->tick()) + vertPadding);
3239 :
3240 76 : Note* startNote = above ? startChord->upNote() : startChord->downNote();
3241 76 : Note* endNote = above ? endChord->upNote() : endChord->downNote();
3242 76 : double yNoteLimit = above ? std::min(startNote->y(), endNote->y()) - 2 * vertPadding
3243 0 : : std::max(startNote->y(), endNote->y()) + 2 * vertPadding;
3244 76 : y = above ? std::min(y, yNoteLimit) : std::max(y, yNoteLimit);
3245 :
3246 76 : hopoText->mutldata()->setPos(centerX, y);
3247 :
3248 76 : hopoTextShape.translateY(y);
3249 76 : skl.add(hopoTextShape);
3250 76 : }
3251 :
3252 62 : Shape hopoSegmentShape = item->mutldata()->shape();
3253 138 : for (HammerOnPullOffText* hopoText : item->hopoText()) {
3254 76 : hopoSegmentShape.add(hopoText->ldata()->shape().translated(hopoText->pos()));
3255 : }
3256 :
3257 62 : item->mutldata()->setShape(hopoSegmentShape);
3258 62 : }
3259 :
3260 76 : void TLayout::layoutHammerOnPullOffText(HammerOnPullOffText* item, LayoutContext& ctx)
3261 : {
3262 76 : TextLayout::layoutBaseTextBase(item, ctx);
3263 76 : }
3264 :
3265 32 : void TLayout::layoutHarpPedalDiagram(const HarpPedalDiagram* item, HarpPedalDiagram::LayoutData* ldata)
3266 : {
3267 : LAYOUT_CALL_ITEM(item);
3268 32 : const_cast<HarpPedalDiagram*>(item)->updateDiagramText();
3269 :
3270 32 : TextLayout::layoutBaseTextBase(item, ldata);
3271 :
3272 32 : if (item->autoplace()) {
3273 32 : const Segment* s = toSegment(item->ownershipParent());
3274 32 : const Measure* m = s->measure();
3275 :
3276 32 : LD_CONDITION(ldata->isSetPos());
3277 32 : LD_CONDITION(m->ldata()->isSetPos());
3278 32 : LD_CONDITION(s->ldata()->isSetPos());
3279 : }
3280 :
3281 32 : Autoplace::autoplaceSegmentElement(item, ldata);
3282 32 : }
3283 :
3284 29 : void TLayout::layoutHarmonicMarkSegment(HarmonicMarkSegment* item, LayoutContext& ctx)
3285 : {
3286 : LAYOUT_CALL_ITEM(item);
3287 29 : HarmonicMarkSegment::LayoutData* ldata = item->mutldata();
3288 29 : const StaffType* stType = item->staffType();
3289 29 : if (stType
3290 29 : && (!stType->isTabStaff()
3291 0 : || stType->isHiddenElementOnTab(Sid::harmonicMarkShowTabCommon, Sid::harmonicMarkShowTabSimple))) {
3292 29 : ldata->setIsSkipDraw(true);
3293 29 : return;
3294 : }
3295 0 : ldata->setIsSkipDraw(false);
3296 :
3297 0 : layoutTextLineBaseSegment(item, ctx);
3298 :
3299 0 : Autoplace::autoplaceSpannerSegment(item, ldata);
3300 : }
3301 :
3302 3638 : void TLayout::layoutHarmony(Harmony* item, Harmony::LayoutData* ldata, const LayoutContext& ctx)
3303 : {
3304 : LAYOUT_CALL_ITEM(item);
3305 : LD_INDEPENDENT;
3306 :
3307 3638 : HarmonyLayout::layoutHarmony(item, ldata, ctx);
3308 3638 : }
3309 :
3310 5376 : void TLayout::layoutHook(const Hook* item, Hook::LayoutData* ldata)
3311 : {
3312 : LAYOUT_CALL_ITEM(item);
3313 : LD_INDEPENDENT;
3314 :
3315 5376 : if (ldata->isValid()) {
3316 3161 : return;
3317 : }
3318 :
3319 2215 : ldata->setBbox(item->symBbox(item->sym()));
3320 : }
3321 :
3322 60 : void TLayout::layoutImage(const Image* item, Image::LayoutData* ldata)
3323 : {
3324 : LAYOUT_CALL_ITEM(item);
3325 60 : IF_ASSERT_FAILED(item->layoutParent()) {
3326 0 : return;
3327 : }
3328 :
3329 60 : ldata->setPos(0.0, 0.0);
3330 60 : const_cast<Image*>(item)->init();
3331 :
3332 60 : SizeF imageSize = item->size();
3333 :
3334 : // if autoscale && inside a box, scale to box relevant size
3335 60 : if (item->autoScale()
3336 60 : && ((item->layoutParent()->isHBox() || item->layoutParent()->isVBox()))) {
3337 0 : const EngravingItem::LayoutData* parentLD = item->layoutParent()->ldata();
3338 :
3339 0 : LD_CONDITION(parentLD->isSetBbox());
3340 :
3341 0 : if (item->lockAspectRatio()) {
3342 0 : double f = item->sizeIsSpatium() ? item->spatium() : DPMM;
3343 0 : SizeF size(item->imageSize());
3344 0 : double ratio = size.width() / size.height();
3345 0 : double w = parentLD->bbox().width();
3346 0 : double h = parentLD->bbox().height();
3347 0 : if ((w / h) < ratio) {
3348 0 : imageSize.setWidth(w / f);
3349 0 : imageSize.setHeight((w / ratio) / f);
3350 : } else {
3351 0 : imageSize.setHeight(h / f);
3352 0 : imageSize.setWidth(h * ratio / f);
3353 : }
3354 : } else {
3355 0 : imageSize = item->pixel2size(parentLD->bbox().size());
3356 : }
3357 : }
3358 :
3359 60 : const_cast<Image*>(item)->setSize(imageSize);
3360 :
3361 : // in any case, adjust position relative to parent
3362 60 : ldata->setBbox(RectF(PointF(), item->size2pixel(imageSize)));
3363 : }
3364 :
3365 94 : void TLayout::layoutInstrumentChange(const InstrumentChange* item, InstrumentChange::LayoutData* ldata)
3366 : {
3367 : LAYOUT_CALL_ITEM(item);
3368 94 : TextLayout::layoutBaseTextBase(item, ldata);
3369 :
3370 94 : if (item->autoplace()) {
3371 94 : const Segment* s = toSegment(item->ownershipParent());
3372 94 : const Measure* m = s->measure();
3373 94 : LD_CONDITION(ldata->isSetPos());
3374 94 : LD_CONDITION(m->ldata()->isSetPos());
3375 94 : LD_CONDITION(s->ldata()->isSetPos());
3376 : }
3377 :
3378 94 : Autoplace::autoplaceSegmentElement(item, ldata);
3379 94 : }
3380 :
3381 9716 : void TLayout::layoutInstrumentName(const InstrumentName* item, InstrumentName::LayoutData* ldata)
3382 : {
3383 : LAYOUT_CALL_ITEM(item);
3384 : LD_INDEPENDENT;
3385 : //! NOTE There are problems, an investigation is required
3386 : // if (ldata->isValid()) {
3387 : // return;
3388 : // }
3389 :
3390 9716 : TextLayout::layoutBaseTextBase(item, ldata);
3391 9716 : }
3392 :
3393 216 : void TLayout::layoutJump(const Jump* item, Jump::LayoutData* ldata)
3394 : {
3395 : LAYOUT_CALL_ITEM(item);
3396 216 : LD_CONDITION(item->layoutParent()->ldata()->isSetBbox());
3397 :
3398 216 : TextLayout::layoutBaseTextBase(item, ldata);
3399 :
3400 216 : AlignH position = item->position();
3401 :
3402 : // ignore courtesy keysig, timesig, but fall back if needed
3403 216 : Measure* measure = toMeasure(item->parent());
3404 216 : const BarLine* bl = measure->endBarLine();
3405 216 : double xAdj = bl ? bl->segment()->x() + bl->ldata()->bbox().width() : measure->width();
3406 :
3407 216 : bool avoidBarline = item->staffIdx() != 0 && position != AlignH::HCENTER;
3408 216 : if (avoidBarline) {
3409 1 : bool startRepeat = measure->repeatStart();
3410 1 : bool endRepeat = measure->repeatEnd();
3411 1 : staff_idx_t blAboveIdx = item->staffIdx() - 1;
3412 :
3413 1 : const double fontSizeScaleFactor = item->size() / 10.0;
3414 1 : double padding = (startRepeat || endRepeat) ? 0.0 : 0.5 * item->spatium() * fontSizeScaleFactor;
3415 :
3416 1 : if (position == AlignH::LEFT) {
3417 0 : const BarLine* blAbove = startRepeat || !measure->prevMeasure()
3418 0 : ? measure->startBarLine(blAboveIdx) : measure->prevMeasure()->endBarLine(blAboveIdx);
3419 0 : double blWidth = (startRepeat && blAbove) ? blAbove->width() : 0.0;
3420 0 : xAdj += padding + blWidth;
3421 1 : } else if (position == AlignH::RIGHT) {
3422 1 : const BarLine* blAbove = measure->endBarLine(blAboveIdx);
3423 1 : double blWidth = blAbove ? blAbove->width() : 0.0;
3424 1 : xAdj -= blWidth + padding;
3425 : }
3426 : }
3427 216 : ldata->moveX(xAdj);
3428 :
3429 216 : if (item->autoplace()) {
3430 216 : LD_CONDITION(ldata->isSetPos());
3431 216 : LD_CONDITION(ldata->isSetBbox());
3432 216 : LD_CONDITION(measure->ldata()->isSetPos());
3433 : }
3434 :
3435 216 : Autoplace::autoplaceMeasureElement(item, ldata);
3436 216 : }
3437 :
3438 8856 : static void keySigAddLayout(const KeySig* item, const LayoutConfiguration& conf, SymId sym, int line, KeySig::LayoutData* ldata)
3439 : {
3440 8856 : double _spatium = item->spatium();
3441 8856 : double step = _spatium * (item->staff() ? item->staff()->staffTypeForElement(item)->lineDistance().val() * 0.5 : 0.5);
3442 8856 : KeySym ks;
3443 8856 : ks.sym = sym;
3444 8856 : Spatium x = 0.0_sp;
3445 8856 : if (!ldata->keySymbols.empty()) {
3446 5557 : const KeySym& previous = ldata->keySymbols.back();
3447 5557 : Spatium accidentalGap = conf.styleS(Sid::keysigAccidentalDistance);
3448 5557 : if (previous.sym != sym) {
3449 2 : accidentalGap *= 2;
3450 5555 : } else if (previous.sym == SymId::accidentalNatural && sym == SymId::accidentalNatural) {
3451 248 : accidentalGap = conf.styleS(Sid::keysigNaturalDistance);
3452 : }
3453 5557 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(previous.sym), _spatium);
3454 5557 : x = previous.xPos + previousWidth + accidentalGap;
3455 5557 : bool isAscending = line < previous.line;
3456 5557 : SmuflAnchorId currentCutout = isAscending ? SmuflAnchorId::cutOutSW : SmuflAnchorId::cutOutNW;
3457 5557 : SmuflAnchorId previousCutout = isAscending ? SmuflAnchorId::cutOutNE : SmuflAnchorId::cutOutSE;
3458 5557 : PointF cutout = item->symSmuflAnchor(sym, currentCutout);
3459 5557 : double currentCutoutY = line * step + cutout.y();
3460 5557 : double previousCutoutY = previous.line * step + item->symSmuflAnchor(previous.sym, previousCutout).y();
3461 5557 : if ((isAscending && currentCutoutY < previousCutoutY) || (!isAscending && currentCutoutY > previousCutoutY)) {
3462 5469 : x -= Spatium::fromAbsolute(cutout.x(), _spatium);
3463 : }
3464 : }
3465 8856 : ks.xPos = x;
3466 8856 : ks.line = line;
3467 8856 : ldata->keySymbols.push_back(ks);
3468 8856 : }
3469 :
3470 26769 : void TLayout::layoutKeySig(const KeySig* item, KeySig::LayoutData* ldata, const LayoutConfiguration& conf)
3471 : {
3472 : LAYOUT_CALL_ITEM(item);
3473 : LD_INDEPENDENT;
3474 :
3475 : //! NOTE There are problems, an investigation is required
3476 : // if (ldata->isValid()) {
3477 : // return;
3478 : // }
3479 :
3480 26769 : ldata->setBbox(RectF());
3481 26769 : ldata->keySymbols.clear();
3482 26769 : ldata->jianpuLabel = String();
3483 :
3484 26769 : const Staff* staff = item->staff();
3485 26769 : const StaffType* stVisibility = staff ? staff->staffType(item->tick()) : nullptr;
3486 26769 : if (stVisibility && !stVisibility->genKeysig()) {
3487 427 : return;
3488 : }
3489 :
3490 26537 : const StaffType* st = item->staffType();
3491 26537 : double spatium = item->spatium();
3492 :
3493 26537 : if (staff && staff->isJianpuStaff(item->tick())) {
3494 : // Jianpu staves show a numbering label (e.g. "1=C") in place of the standard â™/♯ symbols
3495 195 : ldata->jianpuLabel = jianpuKeyLabel(item->keySigEvent());
3496 :
3497 195 : const double height = (st ? st->jianpuBoxH() : spatium) * item->mag();
3498 195 : double width = 0.0;
3499 195 : if (!ldata->jianpuLabel.isEmpty()) {
3500 195 : Font font(st ? st->jianpuFont() : Font());
3501 195 : font.setPointSizeF(font.pointSizeF() * item->mag());
3502 195 : width = FontMetrics(font).width(ldata->jianpuLabel);
3503 195 : }
3504 195 : ldata->setPosY(item->staffOffsetY());
3505 195 : ldata->setBbox(0.0, -height * 0.5, width, height);
3506 195 : return;
3507 : }
3508 :
3509 26342 : const Segment* s = item->segment();
3510 26342 : track_idx_t track = item->track();
3511 26342 : double step = spatium * (st ? st->lineDistance().val() * 0.5 : 0.5);
3512 :
3513 : // determine current clef for this staff
3514 26342 : ClefType clef = ClefType::G;
3515 26342 : if (item->staff()) {
3516 : // Look for a clef before the key signature at the same tick
3517 26342 : Clef* c = nullptr;
3518 26342 : if (s) {
3519 52566 : for (Segment* seg = s->prev1(); !c && seg && seg->tick() == item->tick(); seg = seg->prev1()) {
3520 52295 : const bool isClefSeg = seg->isClefType() || seg->isHeaderClefType()
3521 52295 : || (seg->isClefRepeatAnnounceType() && s->isKeySigRepeatAnnounceType());
3522 26224 : if (seg->enabled() && isClefSeg) {
3523 24117 : c = toClef(seg->element(track));
3524 : }
3525 : }
3526 : }
3527 : // If no clef found, get the clef type from the clefs list (using the previous tick)
3528 26342 : clef = c ? c->clefType() : item->staff()->clef(item->tick() - Fraction::eps());
3529 : }
3530 :
3531 26342 : int t1 = int(item->key());
3532 26342 : const signed char* lines = ClefInfo::lines(clef);
3533 :
3534 26342 : if (item->isCustom() && !item->isAtonal()) {
3535 50 : Spatium accidentalGap = conf.styleS(Sid::keysigAccidentalDistance);
3536 : // add standard key accidentals first, if necessary
3537 50 : for (int i = 1; i <= std::abs(t1) && std::abs(t1) <= 7; ++i) {
3538 0 : bool drop = false;
3539 0 : for (const CustDef& cd: item->customKeyDefs()) {
3540 0 : int degree = item->degInKey(cd.degree);
3541 : // if custom keysig accidental takes place, don't create tonal accidental
3542 0 : if ((degree * 2 + 2) % 7 == (t1 < 0 ? 8 - i : i) % 7) {
3543 0 : drop = true;
3544 0 : break;
3545 : }
3546 : }
3547 0 : if (!drop) {
3548 0 : KeySym ks;
3549 0 : int lineIndexOffset = t1 > 0 ? -1 : 6;
3550 0 : ks.sym = t1 > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
3551 0 : ks.line = lines[lineIndexOffset + i];
3552 0 : if (!ldata->keySymbols.empty()) {
3553 0 : const KeySym& previous = ldata->keySymbols.back();
3554 0 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(previous.sym), spatium);
3555 0 : ks.xPos = previous.xPos + previousWidth + accidentalGap;
3556 : } else {
3557 0 : ks.xPos = 0_sp;
3558 : }
3559 : // TODO octave metters?
3560 0 : ldata->keySymbols.push_back(ks);
3561 : }
3562 : }
3563 148 : for (const CustDef& cd : item->customKeyDefs()) {
3564 98 : SymId sym = item->symInKey(cd.sym, cd.degree);
3565 98 : int degree = item->degInKey(cd.degree);
3566 98 : bool flat = std::string(SymNames::nameForSymId(sym).ascii()).find("Flat") != std::string::npos;
3567 98 : int accIdx = (degree * 2 + 1) % 7; // C D E F ... index to F C G D index
3568 98 : accIdx = flat ? 13 - accIdx : accIdx;
3569 98 : int line = lines[accIdx] + cd.octAlt * 7;
3570 98 : Spatium xpos = cd.xAlt;
3571 98 : if (!ldata->keySymbols.empty()) {
3572 48 : const KeySym& previous = ldata->keySymbols.back();
3573 48 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(previous.sym), spatium);
3574 48 : xpos += previous.xPos + previousWidth + accidentalGap;
3575 : }
3576 : // if translated symbol if out of range, add key accidental followed by untranslated symbol
3577 98 : if (sym == SymId::noSym) {
3578 0 : KeySym ks;
3579 0 : ks.line = line;
3580 0 : ks.xPos = xpos;
3581 : // for quadruple sharp use two double sharps
3582 0 : if (cd.sym == SymId::accidentalTripleSharp) {
3583 0 : ks.sym = SymId::accidentalDoubleSharp;
3584 0 : sym = SymId::accidentalDoubleSharp;
3585 : } else {
3586 0 : ks.sym = t1 > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
3587 0 : sym = cd.sym;
3588 : }
3589 0 : ldata->keySymbols.push_back(ks);
3590 0 : xpos += Spatium(t1 < 0 ? 0.7 : 1); // flats closer
3591 : }
3592 : // create symbol; natural only if is user defined
3593 98 : if (sym != SymId::accidentalNatural || sym == cd.sym) {
3594 98 : KeySym ks;
3595 98 : ks.sym = sym;
3596 98 : ks.line = line;
3597 98 : ks.xPos = xpos;
3598 98 : ldata->keySymbols.push_back(ks);
3599 : }
3600 : }
3601 : } else {
3602 26292 : auto layoutSharpsFlats = [&]() {
3603 26292 : if (std::abs(t1) > 7) {
3604 0 : LOGD("illegal t1 key %d", t1);
3605 0 : return;
3606 : }
3607 26292 : SymId symbol = t1 > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
3608 26292 : int lineIndexOffset = t1 > 0 ? 0 : 7;
3609 34834 : for (int i = 0; i < std::abs(t1); ++i) {
3610 8542 : keySigAddLayout(item, conf, symbol, lines[lineIndexOffset + i], ldata);
3611 : }
3612 26292 : };
3613 :
3614 : // Naturals are shown if:
3615 : // Key signature is courtesy, mid-measure or prev. measure has no section break and no courtesy keysig.
3616 : // AND we're not force hiding naturals (continuous mode)
3617 : // AND key sig is CMaj/Amin OR style says they are on
3618 26292 : const Measure* pm = item->measure() ? item->measure()->prevMeasureMM() : nullptr;
3619 26292 : const bool isCourtesy = s && (s->isType(SegmentType::CourtesyKeySigTypes) || !s->rtick().isZero());
3620 26292 : const bool prevTrailerCourtesy = pm && !pm->sectionBreak() && (!pm->trailer() || !pm->hasCourtesyKeySig());
3621 52584 : if (!item->hideNaturals() && track != muse::nidx
3622 26292 : && (conf.styleI(Sid::keySigNaturals) != int(KeySigNatural::NONE) || (t1 == 0))
3623 52584 : && (isCourtesy || prevTrailerCourtesy)) {
3624 9305 : KeySigEvent prevKsEvent = item->staff() ? item->staff()->keySigEvent(item->tick() - Fraction::eps()) : KeySigEvent();
3625 9305 : int t2 = int(prevKsEvent.key());
3626 :
3627 : // Handle naturals in continuation courtesy
3628 9305 : if (pm && s && s->isType(SegmentType::KeySigStartRepeatAnnounce)) {
3629 0 : Segment* prevCourtesySeg = pm->findSegmentR(SegmentType::KeySigRepeatAnnounce, pm->ticks());
3630 0 : if (prevCourtesySeg && prevCourtesySeg->element(track)) {
3631 0 : t2 = int(toKeySig(prevCourtesySeg->element(track))->key());
3632 : }
3633 : }
3634 : // Don't show naturals when going from sharps to flats, if style says so
3635 9305 : const bool sameAccidentals = t1 * t2 >= 0;
3636 9305 : if (t2 != 0 && (sameAccidentals || conf.styleB(Sid::keySigShowNaturalsChangingSharpsFlats))) {
3637 110 : auto key2accidentals = [](int key) -> int {
3638 110 : switch (std::abs(key)) {
3639 28 : case 7: return 0x7f;
3640 0 : case 6: return 0x3f;
3641 8 : case 5: return 0x1f;
3642 8 : case 4: return 0xf;
3643 12 : case 3: return 0x7;
3644 18 : case 2: return 0x3;
3645 4 : case 1: return 0x1;
3646 32 : case 0: return 0;
3647 0 : default:
3648 0 : LOGD("illegal key %d", key);
3649 0 : return 0;
3650 : }
3651 : };
3652 :
3653 72 : int naturals = key2accidentals(t2);
3654 : // remove redundant naturals
3655 72 : if (!((t1 > 0) ^ (t2 > 0))) {
3656 38 : naturals &= ~key2accidentals(t1);
3657 : }
3658 72 : auto layoutNaturals = [&]() {
3659 72 : int lineIndexOffset = t2 > 0 ? 0 : 7;
3660 576 : for (int i = 0; i < 7; ++i) {
3661 504 : if (naturals & (1 << i)) {
3662 314 : keySigAddLayout(item, conf, SymId::accidentalNatural, lines[i + lineIndexOffset], ldata);
3663 : }
3664 : }
3665 144 : };
3666 : // Naturals should go BEFORE accidentals if style says so
3667 : // or going from sharps to flats or vice versa (i.e. t1 & t2 have opposite signs)
3668 72 : if (conf.styleI(Sid::keySigNaturals) == int(KeySigNatural::BEFORE) || !sameAccidentals) {
3669 8 : layoutNaturals();
3670 8 : layoutSharpsFlats();
3671 : } else {
3672 64 : layoutSharpsFlats();
3673 64 : layoutNaturals();
3674 : }
3675 9233 : } else if (prevKsEvent.custom()) {
3676 0 : Fraction prevKeyTick = Fraction::fromTicks(item->staff()->keyList().currentKeyTick(
3677 0 : (item->tick() - Fraction::eps()).ticks()));
3678 0 : Segment* prevKsSeg = item->score()->tick2segment(prevKeyTick, true, SegmentType::KeySig);
3679 0 : if (prevKsSeg) {
3680 0 : KeySig* prevCustomKeySig = toKeySig(prevKsSeg->element(item->track()));
3681 0 : if (prevCustomKeySig) {
3682 0 : for (KeySym keySym : prevCustomKeySig->ldata()->keySymbols) {
3683 0 : if (keySym.sym != SymId::accidentalNatural) {
3684 0 : keySigAddLayout(item, conf, SymId::accidentalNatural, keySym.line, ldata);
3685 : }
3686 : }
3687 : }
3688 : }
3689 : }
3690 9305 : }
3691 :
3692 : // No naturals were added, so just create a regular keysig
3693 26292 : if (ldata->keySymbols.empty()) {
3694 26220 : layoutSharpsFlats();
3695 : }
3696 : }
3697 :
3698 26342 : ldata->setPosY((st ? step * st->stepOffset() : 0.0) + item->staffOffsetY());
3699 :
3700 26342 : Shape keySigShape;
3701 35296 : for (const KeySym& ks : ldata->keySymbols) {
3702 8954 : double x = ks.xPos.toAbsolute(spatium);
3703 8954 : double y = ks.line * step;
3704 8954 : Shape ksShape = item->symShapeWithCutouts(ks.sym);
3705 40957 : for (const ShapeElement& ksElem : ksShape.elements()) {
3706 32003 : keySigShape.add(ksElem.translated(x, y), item);
3707 : }
3708 8954 : }
3709 26342 : ldata->setShape(keySigShape);
3710 26342 : }
3711 :
3712 0 : void TLayout::layoutLaissezVib(LaissezVib* item)
3713 : {
3714 : UNUSED(item);
3715 0 : }
3716 :
3717 2623 : void TLayout::layoutLayoutBreak(const LayoutBreak* item, LayoutBreak::LayoutData* ldata)
3718 : {
3719 : LAYOUT_CALL_ITEM(item);
3720 : LD_INDEPENDENT;
3721 :
3722 2623 : if (ldata->isValid() || !item->configuration()->canLayoutIcons()) {
3723 2623 : return;
3724 : }
3725 :
3726 0 : FontMetrics metrics(item->font());
3727 0 : RectF bbox = metrics.boundingRect(item->iconCode());
3728 0 : ldata->setShape(Shape(bbox, item));
3729 0 : }
3730 :
3731 313 : void TLayout::layoutSystemIndicatorIcon(const SystemIndicatorIcon* item, IndicatorIcon::LayoutData* ldata)
3732 : {
3733 313 : if (!item->configuration()->canLayoutIcons()) {
3734 313 : return;
3735 : }
3736 :
3737 0 : const MeasureBase* endMB = nullptr;
3738 :
3739 0 : Shape shape;
3740 :
3741 0 : const FontMetrics metrics(item->font());
3742 0 : const RectF iconBox = metrics.boundingRect(item->iconCode());
3743 0 : shape.add(iconBox, item);
3744 :
3745 0 : if (item->isSystemLockIndicator()) {
3746 0 : const SystemLockIndicator* sli = toSystemLockIndicator(item);
3747 0 : endMB = sli->systemLock()->endMB();
3748 0 : if (item->selected()) {
3749 : // Draw the range rect...
3750 0 : const RangeLock* lock = sli->systemLock();
3751 :
3752 0 : double xStart = lock->startMB()->x();
3753 0 : double xEnd = lock->endMB()->x() + lock->endMB()->width();
3754 :
3755 0 : double width = xEnd - xStart;
3756 0 : double height = iconBox.height() + sli->spatium();
3757 :
3758 0 : double y = iconBox.top() - 0.5 * sli->spatium();
3759 :
3760 0 : ldata->rangeRect = RectF(xStart, y, width, height).translated(-sli->x(), 0.0);
3761 0 : shape.add(ldata->rangeRect);
3762 : }
3763 : } else {
3764 0 : endMB = item->system()->last();
3765 : }
3766 :
3767 0 : ldata->setShape(shape);
3768 :
3769 0 : const double spatium = item->spatium();
3770 :
3771 0 : double x = endMB->x() + endMB->width();
3772 0 : x -= iconBox.right() + 0.5 * spatium;
3773 :
3774 0 : double xOffset = endMB->x() + endMB->width();
3775 0 : for (EngravingItem* el : endMB->el()) {
3776 0 : if (el->isLayoutBreak()) {
3777 0 : xOffset = std::min(xOffset, endMB->x() + el->x() + el->ldata()->bbox().left() - spatium);
3778 : }
3779 : }
3780 :
3781 0 : for (const SystemLockIndicator* sli : item->system()->systemLockIndicators()) {
3782 0 : if (sli != item && sli->systemLock()->endMB() == endMB) {
3783 0 : const double sliWidth = FontMetrics(sli->font()).boundingRect(sli->iconCode()).width();
3784 : // TODO: Rough spacing here
3785 0 : xOffset -= (sliWidth * 2) - spatium;
3786 : }
3787 0 : }
3788 :
3789 0 : x = std::min(x, xOffset - iconBox.right());
3790 :
3791 0 : ldata->setPos(PointF(x, -2.5 * spatium));
3792 :
3793 : // Ensure it goes behind notation and LayoutBreak
3794 0 : const_cast<SystemIndicatorIcon*>(item)->setZ(-100);
3795 0 : }
3796 :
3797 53 : void TLayout::layoutStaveSharingChange(const StaveSharingChange* item, IndicatorIcon::LayoutData* ldata)
3798 : {
3799 53 : if (!item->configuration()->canLayoutIcons()) {
3800 53 : return;
3801 : }
3802 :
3803 0 : const FontMetrics metrics(item->font());
3804 0 : const RectF iconBox = metrics.boundingRect(item->iconCode());
3805 :
3806 0 : Shape shape;
3807 0 : shape.add(iconBox, item);
3808 0 : ldata->setShape(shape);
3809 0 : ldata->setPos(PointF(0.0, -2.5 * item->spatium()));
3810 :
3811 0 : Autoplace::autoplaceSegmentElement(item, ldata, false);
3812 0 : }
3813 :
3814 206035 : static void _layoutLedgerLine(const LedgerLine* item, const LayoutContext& ctx, LedgerLine::LayoutData* ldata)
3815 : {
3816 206035 : double chordMag = item->chord()->mag();
3817 206035 : ldata->setMag(chordMag);
3818 206035 : ldata->lineWidth = ctx.conf().styleAbsolute(Sid::ledgerLineWidth) * chordMag;
3819 206035 : if (item->staff()) {
3820 206035 : const_cast<LedgerLine*>(item)->setColor(item->staff()->staffType(item->tick())->color());
3821 : }
3822 206035 : double w2 = ldata->lineWidth * .5;
3823 :
3824 206035 : if (item->vertical()) {
3825 0 : ldata->setBbox(-w2, 0, w2, item->len());
3826 : } else {
3827 206035 : ldata->setBbox(0, -w2, item->len(), 2 * w2);
3828 : }
3829 206035 : }
3830 :
3831 206035 : void TLayout::layoutLedgerLine(LedgerLine* item, LayoutContext& ctx)
3832 : {
3833 : LAYOUT_CALL_ITEM(item);
3834 206035 : _layoutLedgerLine(item, ctx, item->mutldata());
3835 206035 : }
3836 :
3837 0 : void TLayout::layoutLetRing(LetRing* item, LayoutContext& ctx)
3838 : {
3839 : LAYOUT_CALL_ITEM(item);
3840 0 : layoutLine(item, ctx);
3841 0 : }
3842 :
3843 31 : void TLayout::layoutLetRingSegment(LetRingSegment* item, LayoutContext& ctx)
3844 : {
3845 : LAYOUT_CALL_ITEM(item);
3846 31 : HarmonicMarkSegment::LayoutData* ldata = item->mutldata();
3847 :
3848 31 : const StaffType* stType = item->staffType();
3849 :
3850 31 : if (stType && stType->isHiddenElementOnTab(Sid::letRingShowTabCommon, Sid::letRingShowTabSimple)) {
3851 0 : ldata->setIsSkipDraw(true);
3852 0 : return;
3853 : }
3854 31 : ldata->setIsSkipDraw(false);
3855 :
3856 31 : layoutTextLineBaseSegment(item, ctx);
3857 :
3858 31 : Autoplace::autoplaceSpannerSegment(item, ldata);
3859 : }
3860 :
3861 3199 : void TLayout::layoutLineSegment(LineSegment* item, LayoutContext& ctx)
3862 : {
3863 : LAYOUT_CALL_ITEM(item);
3864 3199 : layoutItem(item, ctx);
3865 3199 : }
3866 :
3867 2374 : void TLayout::layoutLyrics(Lyrics* item, LayoutContext& ctx)
3868 : {
3869 : LAYOUT_CALL_ITEM(item);
3870 2374 : LyricsLayout::layout(item, ctx);
3871 2374 : }
3872 :
3873 341 : void TLayout::layoutLyricsLine(LyricsLine* item)
3874 : {
3875 : LAYOUT_CALL_ITEM(item);
3876 341 : LyricsLayout::layout(item);
3877 341 : }
3878 :
3879 364 : void TLayout::layoutLyricsLineSegment(LyricsLineSegment* item, LayoutContext& ctx)
3880 : {
3881 : LAYOUT_CALL_ITEM(item);
3882 364 : LyricsLayout::layout(item, ctx);
3883 364 : }
3884 :
3885 309 : void TLayout::layoutMarker(Marker* item, Marker::LayoutData* ldata, LayoutContext& ctx)
3886 : {
3887 : LAYOUT_CALL_ITEM(item);
3888 :
3889 309 : MarkerLayout::layoutMarker(item, ldata, ctx);
3890 309 : }
3891 :
3892 147 : void TLayout::layoutMeasureBase(MeasureBase* item, LayoutContext& ctx)
3893 : {
3894 147 : layoutItem(item, ctx);
3895 147 : }
3896 :
3897 41323 : void TLayout::layoutBaseMeasureBase(const MeasureBase* item, MeasureBase::LayoutData* ldata, const LayoutContext& ctx)
3898 : {
3899 41323 : LD_CONDITION(ldata->isSetBbox());
3900 :
3901 41323 : double x = ldata->bbox().width();
3902 48731 : for (EngravingItem* e : item->el()) {
3903 7408 : if (e->isLayoutBreak()) {
3904 2617 : TLayout::layoutItem(e, const_cast<LayoutContext&>(ctx));
3905 2617 : EngravingItem::LayoutData* eldata = e->mutldata();
3906 2617 : double spatium = item->spatium();
3907 2617 : double y = 0.0;
3908 2617 : if (toLayoutBreak(e)->isNoBreak()) {
3909 92 : x -= eldata->bbox().right() * .5;
3910 : } else {
3911 2525 : x -= eldata->bbox().right() + 0.5 * spatium;
3912 : }
3913 2617 : y = -2.5 * spatium;
3914 2617 : eldata->setPos(x, y);
3915 2617 : x += eldata->bbox().left() - 0.5 * spatium;
3916 4791 : } else if (e->isMarker() || e->isJump()) {
3917 : } else {
3918 4254 : layoutItem(e, const_cast<LayoutContext&>(ctx));
3919 : }
3920 : }
3921 41323 : }
3922 :
3923 4257 : void TLayout::layoutMeasureNumber(MeasureNumber* item, MeasureNumber::LayoutData* ldata, const LayoutContext& ctx)
3924 : {
3925 4257 : MeasureNumberLayout::layoutMeasureNumber(item, ldata, ctx);
3926 4257 : }
3927 :
3928 376 : void TLayout::layoutMeasureRepeat(const MeasureRepeat* item, MeasureRepeat::LayoutData* ldata, const LayoutContext& ctx)
3929 : {
3930 : LAYOUT_CALL_ITEM(item);
3931 :
3932 : //! NOTE The types are listed here explicitly to show what types there are (see Rest::add method)
3933 : //! and accordingly show what depends on.
3934 376 : for (EngravingItem* e : item->el()) {
3935 0 : switch (e->type()) {
3936 0 : case ElementType::DEAD_SLAPPED: {
3937 0 : DeadSlapped* ds = item_cast<DeadSlapped*>(e);
3938 : LD_INDEPENDENT;
3939 0 : layoutDeadSlapped(ds, ds->mutldata());
3940 0 : } break;
3941 0 : case ElementType::SYMBOL: {
3942 0 : Symbol* s = item_cast<Symbol*>(e);
3943 : // LD_X not clear yet
3944 0 : layoutSymbol(s, s->mutldata(), ctx);
3945 0 : } break;
3946 0 : case ElementType::IMAGE: {
3947 0 : Image* im = item_cast<Image*>(e);
3948 : LD_INDEPENDENT;
3949 0 : layoutImage(im, im->mutldata());
3950 0 : } break;
3951 0 : default:
3952 0 : UNREACHABLE;
3953 : }
3954 : }
3955 :
3956 376 : switch (item->numMeasures()) {
3957 278 : case 1:
3958 : {
3959 278 : ldata->setSymId(SymId::repeat1Bar);
3960 278 : if (ctx.conf().styleB(Sid::mrNumberSeries) && item->track() != muse::nidx) {
3961 0 : int placeInSeries = 2; // "1" would be the measure actually being repeated
3962 0 : staff_idx_t staffIdx = item->staffIdx();
3963 0 : const Measure* m = item->measure();
3964 0 : while (m && m->isOneMeasureRepeat(staffIdx) && m->prevIsOneMeasureRepeat(staffIdx)) {
3965 0 : placeInSeries++;
3966 0 : m = m->prevMeasure();
3967 : }
3968 0 : if (placeInSeries % ctx.conf().styleI(Sid::mrNumberEveryXMeasures) == 0) {
3969 0 : if (ctx.conf().styleB(Sid::mrNumberSeriesWithParentheses)) {
3970 0 : ldata->setNumberSym(String(u"(%1)").arg(placeInSeries));
3971 : } else {
3972 0 : ldata->setNumberSym(placeInSeries);
3973 : }
3974 : } else {
3975 0 : ldata->clearNumberSym();
3976 : }
3977 278 : } else if (ctx.conf().styleB(Sid::oneMeasureRepeatShow1)) {
3978 0 : ldata->setNumberSym(1);
3979 : } else {
3980 278 : ldata->clearNumberSym();
3981 : }
3982 278 : break;
3983 : }
3984 55 : case 2:
3985 55 : ldata->setSymId(SymId::repeat2Bars);
3986 55 : ldata->setNumberSym(item->numMeasures());
3987 55 : break;
3988 43 : case 4:
3989 43 : ldata->setSymId(SymId::repeat4Bars);
3990 43 : ldata->setNumberSym(item->numMeasures());
3991 43 : break;
3992 0 : default:
3993 0 : ldata->setSymId(SymId::noSym); // should never happen
3994 0 : ldata->clearNumberSym();
3995 0 : break;
3996 : }
3997 :
3998 376 : RectF bbox = item->symBbox(ldata->symId);
3999 :
4000 376 : if (item->track() != muse::nidx) { // if this is in score rather than a palette cell
4001 : // For unknown reasons, the symbol has some offset in almost all SMuFL fonts
4002 : // We compensate for it, to make sure the symbol is visually centered around the staff line
4003 376 : double offset = (-bbox.top() - bbox.bottom()) / 2.0;
4004 :
4005 376 : const StaffType* staffType = item->staffType();
4006 :
4007 : // Only need to set y position here; x position is handled in MeasureLayout::layoutMeasureElements()
4008 376 : ldata->setPos(0, std::floor(staffType->middleLine() / 2.0) * staffType->lineDistance().val() * item->spatium() + offset);
4009 : }
4010 :
4011 376 : ChordLayout::fillShape(item, ldata, ctx.conf());
4012 376 : }
4013 :
4014 0 : void TLayout::layoutMeasureRepeatExtender(const MeasureRepeat* item, MeasureRepeat::LayoutData* ldata, const LayoutContext& ctx)
4015 : {
4016 0 : ldata->extenderLineLeft = LineF();
4017 0 : ldata->extenderLineRight = LineF();
4018 :
4019 0 : Measure* parentMeasure = item->measure();
4020 0 : if (!parentMeasure) {
4021 0 : return;
4022 : }
4023 :
4024 0 : Measure* startMeasure = parentMeasure->prevMeasure();
4025 0 : if (!startMeasure || startMeasure->system() != parentMeasure->system()) {
4026 0 : return;
4027 : }
4028 :
4029 0 : Measure* endMeasure = parentMeasure->nextMeasure();
4030 0 : if (endMeasure) {
4031 0 : endMeasure = endMeasure->nextMeasure();
4032 : }
4033 0 : if (!endMeasure || endMeasure->system() != parentMeasure->system()) {
4034 0 : return;
4035 : }
4036 :
4037 : // Reuse the function that calculates start and end pos for mmRests,
4038 : // except start and end point are on different measures
4039 0 : staff_idx_t staffIdx = item->staffIdx();
4040 0 : double xStart = MeasureLayout::getMeasureStartEndPos(startMeasure, startMeasure->first(SegmentType::ChordRest), staffIdx,
4041 0 : /*needHeaderException*/ startMeasure->header(), /*modernMMrest*/ true, ctx).x1;
4042 0 : double xEnd = MeasureLayout::getMeasureStartEndPos(endMeasure, endMeasure->first(SegmentType::ChordRest), staffIdx,
4043 0 : /*needHeaderException*/ false, /*modernMRest*/ true, ctx).x2;
4044 :
4045 0 : double margin = ctx.conf().styleAbsolute(Sid::multiMeasureRestMargin);
4046 0 : xStart += margin;
4047 0 : xEnd -= margin;
4048 :
4049 0 : double xItemInSysCoords = item->x() + item->segment()->x() + parentMeasure->x();
4050 :
4051 0 : PointF startPoint(xStart + startMeasure->x() - xItemInSysCoords, 0.0);
4052 0 : PointF midPointL(-0.5 * item->spatium(), 0.0);
4053 0 : ldata->extenderLineLeft = LineF(startPoint, midPointL);
4054 :
4055 0 : PointF midPointR(item->symWidth(ldata->symId) + 0.5 * item->spatium(), 0.0);
4056 0 : PointF endPoint(xEnd + endMeasure->x() - xItemInSysCoords, 0.0);
4057 0 : ldata->extenderLineRight = LineF(midPointR, endPoint);
4058 : }
4059 :
4060 456 : void TLayout::layoutMMRest(const MMRest* item, MMRest::LayoutData* ldata, const LayoutContext& ctx)
4061 : {
4062 : LAYOUT_CALL_ITEM(item);
4063 :
4064 : //! NOTE The types are listed here explicitly to show what types there are (see Rest::add method)
4065 : //! and accordingly show what depends on.
4066 456 : for (EngravingItem* e : item->el()) {
4067 0 : switch (e->type()) {
4068 0 : case ElementType::DEAD_SLAPPED: {
4069 0 : DeadSlapped* ds = item_cast<DeadSlapped*>(e);
4070 : LD_INDEPENDENT;
4071 0 : layoutDeadSlapped(ds, ds->mutldata());
4072 0 : } break;
4073 0 : case ElementType::SYMBOL: {
4074 0 : Symbol* s = item_cast<Symbol*>(e);
4075 : // LD_X not clear yet
4076 0 : layoutSymbol(s, s->mutldata(), ctx);
4077 0 : } break;
4078 0 : case ElementType::IMAGE: {
4079 0 : Image* im = item_cast<Image*>(e);
4080 : LD_INDEPENDENT;
4081 0 : layoutImage(im, im->mutldata());
4082 0 : } break;
4083 0 : default:
4084 0 : UNREACHABLE;
4085 : }
4086 : }
4087 :
4088 456 : LD_CONDITION(ldata->restWidth.has_value());
4089 :
4090 : //! NOTE This is not look like layout data, perhaps this is should be set not here
4091 456 : ldata->number = item->measure()->mmRestCount();
4092 456 : ldata->setNumberSym(ldata->number);
4093 456 : ldata->yNumberPos = ctx.conf().styleAbsolute(Sid::mmRestNumberPos);
4094 :
4095 456 : const Staff* staff = item->staff();
4096 456 : if (staff->lines(item->tick()) == 1) {
4097 0 : ldata->yNumberPos = std::min(ldata->yNumberPos, -item->spatium());
4098 : }
4099 :
4100 456 : if (item->isOldStyle()) {
4101 66 : SymIdList restSyms;
4102 66 : double symsWidth = 0.0;
4103 :
4104 66 : int remaining = ldata->number;
4105 66 : double spacing = ctx.conf().styleAbsolute(Sid::mmRestOldStyleSpacing);
4106 : SymId sym;
4107 :
4108 132 : while (remaining > 0) {
4109 66 : if (remaining >= 4) {
4110 0 : sym = SymId::restLonga;
4111 0 : remaining -= 4;
4112 66 : } else if (remaining >= 2) {
4113 0 : sym = SymId::restDoubleWhole;
4114 0 : remaining -= 2;
4115 : } else {
4116 66 : sym = SymId::restWhole;
4117 66 : remaining -= 1;
4118 : }
4119 :
4120 66 : restSyms.push_back(sym);
4121 66 : symsWidth += item->symBbox(sym).width();
4122 :
4123 66 : if (remaining > 0) { // do not add spacing after last symbol
4124 0 : symsWidth += spacing;
4125 : }
4126 : }
4127 :
4128 66 : ldata->restSyms = restSyms;
4129 66 : ldata->symsWidth = symsWidth;
4130 66 : }
4131 :
4132 : // Only need to set y position here; x position is handled in MeasureLayout::layoutMeasureElements()
4133 456 : const StaffType* staffType = item->staffType();
4134 456 : double midline = staffType->middleLine() / 2.0;
4135 456 : if (item->isOldStyle()) {
4136 66 : midline = std::max(1.0, midline);
4137 : }
4138 456 : ldata->setPos(0, midline * staffType->lineDistance().val() * item->spatium());
4139 :
4140 456 : ChordLayout::fillShape(item, ldata, ctx.conf());
4141 456 : }
4142 :
4143 0 : void TLayout::layoutMMRestRange(MMRestRange* item, MMRestRange::LayoutData* ldata, const LayoutContext& ctx)
4144 : {
4145 0 : MeasureNumberLayout::layoutMMRestRange(item, ldata, ctx);
4146 0 : }
4147 :
4148 63625 : void TLayout::layoutNote(const Note* item, Note::LayoutData* ldata)
4149 : {
4150 : LAYOUT_CALL_ITEM(item);
4151 : LD_INDEPENDENT;
4152 :
4153 63625 : if (!ldata->isSetPos()) {
4154 736 : ldata->setPos(PointF());
4155 : }
4156 :
4157 63625 : bool useTablature = item->staff() && item->staff()->isTabStaff(item->chord()->tick());
4158 63625 : bool useJianpu = item->staff() && item->staff()->isJianpuStaff(item->chord()->tick());
4159 63625 : ldata->useTablature.set_value(useTablature);
4160 :
4161 63625 : RectF noteBBox;
4162 63625 : if (useTablature) {
4163 1359 : if (item->displayFret() == Note::DisplayFretOption::Hide) {
4164 0 : return;
4165 : }
4166 :
4167 1359 : const Staff* st = item->staff();
4168 1359 : const StaffType* tab = st->staffTypeForElement(item);
4169 : // not complete but we need systems to be laid out to add parenthesis
4170 1359 : if (item->fixed()) {
4171 0 : const_cast<Note*>(item)->setFretString(u"/");
4172 : } else {
4173 1359 : const_cast<Note*>(item)->setFretString(tab->fretString(std::fabs(item->fret()), item->string(), item->deadNote()));
4174 :
4175 1359 : if (item->negativeFretUsed() && !item->deadNote()) {
4176 40 : const_cast<Note*>(item)->setFretString(u"-" + item->fretString());
4177 : }
4178 :
4179 1359 : if (item->displayFret() == Note::DisplayFretOption::ArtificialHarmonic) {
4180 0 : const_cast<Note*>(item)->setFretString(String(u"%1 <%2>").arg(item->fretString(), String::number(item->harmonicFret())));
4181 1359 : } else if (item->displayFret() == Note::DisplayFretOption::NaturalHarmonic) {
4182 0 : const_cast<Note*>(item)->setFretString(String(u"<%1>").arg(String::number(item->harmonicFret())));
4183 : }
4184 : }
4185 :
4186 1359 : double w = item->tabHeadWidth(tab);
4187 1359 : double mags = item->magS();
4188 1359 : double y = item->deadNote() ? tab->deadFretBoxY() : tab->fretBoxY();
4189 1359 : double height = item->deadNote() ? tab->deadFretBoxH() : tab->fretBoxH();
4190 :
4191 1359 : noteBBox = RectF(0, y * mags, w, height * mags);
4192 62266 : } else if (useJianpu) {
4193 1764 : const Staff* st = item->staff();
4194 1764 : const StaffType* jianpu = st->staffTypeForElement(item);
4195 :
4196 1764 : KeySigEvent ks = st->keySigEvent(item->chord()->tick());
4197 : KeyMode mode;
4198 : int tonicTpc;
4199 1764 : jianpuKeyMapping(ks, mode, tonicTpc);
4200 1764 : String accName, stepName;
4201 1764 : tpc2Jianpu(item->tpc(), tonicTpc, mode, accName, stepName);
4202 1764 : Tie* tieBack = item->tieBackNonPartial();
4203 1764 : if (tieBack && tieBack->startNote()->tpc() == item->tpc()) {
4204 : // tied continuation: accidental was already shown on the tied-from note
4205 0 : accName = String();
4206 : }
4207 1764 : const_cast<Note*>(item)->setJianpuDigit(stepName);
4208 1764 : const_cast<Note*>(item)->setJianpuAccidental(jianpuAccidentalMark(accName));
4209 :
4210 1764 : double width = item->headWidth();
4211 1764 : double height = jianpu->jianpuBoxH() * item->magS();
4212 :
4213 1764 : noteBBox = RectF(0, -height * .5, width, height);
4214 1764 : } else {
4215 60502 : if (item->deadNote()) {
4216 218 : const_cast<Note*>(item)->setHeadGroup(NoteHeadGroup::HEAD_CROSS);
4217 60284 : } else if (item->harmonic()) {
4218 74 : const_cast<Note*>(item)->setHeadGroup(NoteHeadGroup::HEAD_DIAMOND);
4219 : }
4220 :
4221 60502 : SymId nh = item->noteHead();
4222 60502 : ldata->cachedNoteheadSym.set_value(nh);
4223 :
4224 60502 : if (item->isNoteName()) {
4225 137 : ldata->cachedSymNull.set_value(SymId::noteEmptyBlack);
4226 137 : NoteHeadType ht = item->headType() == NoteHeadType::HEAD_AUTO ? item->chord()->durationType().headType() : item->headType();
4227 137 : if (ht == NoteHeadType::HEAD_WHOLE) {
4228 8 : ldata->cachedSymNull.set_value(SymId::noteEmptyWhole);
4229 129 : } else if (ht == NoteHeadType::HEAD_HALF) {
4230 0 : ldata->cachedSymNull.set_value(SymId::noteEmptyHalf);
4231 : }
4232 : } else {
4233 60365 : ldata->cachedSymNull.set_value(SymId::noSym);
4234 : }
4235 60502 : noteBBox = item->symBbox(nh);
4236 : }
4237 :
4238 63625 : ldata->setBbox(noteBBox);
4239 :
4240 63625 : fillNoteShape(item, ldata);
4241 : }
4242 :
4243 128251 : void TLayout::fillNoteShape(const Note* item, Note::LayoutData* ldata)
4244 : {
4245 : LAYOUT_CALL_ITEM(item);
4246 128251 : RectF noteBBox = ldata->bbox();
4247 :
4248 128251 : Shape shape(Shape::Type::Composite);
4249 128251 : shape.add(noteBBox, item);
4250 :
4251 130426 : for (const NoteDot* dot : item->dots()) {
4252 2175 : if (!dot->addToSkyline()) {
4253 4 : continue;
4254 : }
4255 2171 : shape.add(item->symBbox(SymId::augmentationDot).translated(dot->pos()), dot);
4256 : }
4257 :
4258 128251 : Accidental* acc = item->accidental();
4259 128251 : if (acc && acc->addToSkyline()) {
4260 8183 : shape.add(acc->ldata()->shape().translated(acc->pos()));
4261 : }
4262 128251 : const LaissezVib* lv = item->laissezVib();
4263 128251 : const LaissezVibSegment* lvSeg = lv && !lv->segmentsEmpty() ? lv->frontSegment() : nullptr;
4264 128251 : if (lvSeg && lvSeg->addToSkyline()) {
4265 108 : const PointF cornerClosestToNote = lv->up() ? lvSeg->shape().bbox().bottomLeft() : lvSeg->shape().bbox().topLeft();
4266 108 : const PointF pos = lvSeg->ldata()->posRelativeToNote.value() - cornerClosestToNote;
4267 108 : shape.add(lvSeg->shape().translate(pos));
4268 : }
4269 129182 : for (auto e : item->el()) {
4270 931 : if (e->addToSkyline()) {
4271 896 : if (e->isFingering() && toFingering(e)->layoutType() != ElementType::NOTE) {
4272 783 : continue;
4273 : }
4274 113 : shape.add(e->ldata()->bbox().translated(e->pos()), e);
4275 : }
4276 : }
4277 :
4278 : // This method is also called from SingleLayout, where `part` may be nullptr
4279 128251 : Part* part = item->part();
4280 128251 : if (part && part->instrument()->hasStrings()) {
4281 14465 : GuitarBend* bend = item->bendFor();
4282 14465 : if (bend && bend->addToSkyline() && bend->bendType() == GuitarBendType::SLIGHT_BEND && !bend->segmentsEmpty()) {
4283 70 : GuitarBendSegment* bendSeg = toGuitarBendSegment(bend->frontSegment());
4284 : // Semi-hack: the relative position of note and bend
4285 : // isn't fully known yet, so we use an approximation
4286 70 : double sp = item->spatium();
4287 70 : PointF approxRelPos(noteBBox.width() + 0.25 * sp, -0.25 * sp);
4288 70 : shape.add(bendSeg->shape().translate(approxRelPos));
4289 : }
4290 14465 : GuitarBend* bendBack = item->bendBack();
4291 1228 : if (bendBack && bendBack->bendType() == GuitarBendType::SCOOP && !bendBack->segmentsEmpty()
4292 15693 : && item->findAncestor(ElementType::SYSTEM)) {
4293 3 : GuitarBendSegment* seg = toGuitarBendSegment(bendBack->frontSegment());
4294 3 : shape.add(seg->shape().translated(seg->pos() - item->systemPos()));
4295 : }
4296 : }
4297 :
4298 132747 : for (JianpuOctaveDot* octaveDot : item->jianpuOctaveDots()) {
4299 4496 : shape.add(octaveDot->ldata()->bbox().translated(octaveDot->pos()), octaveDot);
4300 : }
4301 :
4302 128251 : ldata->setShape(shape);
4303 128251 : }
4304 :
4305 1259 : void TLayout::layoutNoteDot(const NoteDot* item, NoteDot::LayoutData* ldata)
4306 : {
4307 : LAYOUT_CALL_ITEM(item);
4308 : LD_INDEPENDENT;
4309 :
4310 1259 : if (ldata->isValid()) {
4311 60 : return;
4312 : }
4313 :
4314 1199 : ldata->setBbox(item->symBbox(SymId::augmentationDot));
4315 : }
4316 :
4317 2248 : void TLayout::layoutJianpuOctaveDot(JianpuOctaveDot* item, LayoutContext& ctx)
4318 : {
4319 : LAYOUT_CALL_ITEM(item);
4320 2248 : JianpuOctaveDot::LayoutData* ldata = item->mutldata();
4321 :
4322 2248 : double mag = item->note()->mag();
4323 2248 : ldata->setMag(mag);
4324 2248 : ldata->radius = ctx.conf().styleAbsolute(Sid::jianpuOctaveDotRadius) * mag;
4325 :
4326 2248 : double diameter = ldata->radius * 2;
4327 2248 : ldata->setBbox(0, -ldata->radius, ldata->len, diameter);
4328 2248 : }
4329 :
4330 751 : void TLayout::layoutOrnament(const Ornament* item, Ornament::LayoutData* ldata, const LayoutConfiguration& conf)
4331 : {
4332 : LAYOUT_CALL_ITEM(item);
4333 751 : LD_CONDITION(ldata->symId.has_value());
4334 :
4335 751 : ldata->setShape(Shape());
4336 :
4337 751 : layoutArticulation(const_cast<Ornament*>(item), ldata);
4338 :
4339 751 : double _spatium = item->spatium();
4340 751 : double vertMargin = 0.35 * _spatium;
4341 : static constexpr double ornamentAccidentalMag = 0.6; // TODO: style?
4342 :
4343 4506 : for (size_t i = 0; i < item->accidentalsAboveAndBelow().size(); ++i) {
4344 1502 : bool above = (i == 0);
4345 1502 : Accidental* accidental = item->accidentalsAboveAndBelow()[i];
4346 1502 : if (!accidental) {
4347 1434 : continue;
4348 : }
4349 68 : Accidental::LayoutData* accLData = accidental->mutldata();
4350 :
4351 68 : LD_CONDITION(accLData->isSetBbox());
4352 :
4353 68 : accidental->computeMag();
4354 :
4355 68 : accLData->setMag(accLData->mag() * ornamentAccidentalMag);
4356 68 : accLData->syms.clear(); // Invalidate
4357 68 : layoutAccidental(accidental, accLData, conf);
4358 68 : Shape accidentalShape = accidental->shape();
4359 : double minVertDist = above
4360 108 : ? accidentalShape.minVerticalDistance(Shape(ldata->bbox()))
4361 68 : : Shape(ldata->bbox()).minVerticalDistance(accidentalShape);
4362 :
4363 108 : accLData->setPos(-0.5 * accLData->bbox().width() + 0.5 * ldata->bbox().width(),
4364 40 : above ? (-minVertDist - vertMargin) : (minVertDist + vertMargin));
4365 68 : }
4366 :
4367 751 : Shape sh(Shape::Type::Composite);
4368 751 : sh.add(ldata->bbox(), item); // from Articulation
4369 2253 : for (const Accidental* accidental : item->accidentalsAboveAndBelow()) {
4370 1502 : if (accidental && accidental->visible()) {
4371 68 : LD_CONDITION(accidental->ldata()->isSetShape());
4372 68 : LD_CONDITION(accidental->ldata()->isSetPos());
4373 68 : sh.add(accidental->shape().translate(accidental->pos()));
4374 : }
4375 : }
4376 :
4377 751 : ldata->setShape(sh);
4378 751 : }
4379 :
4380 16 : void TLayout::layoutOrnamentCueNote(Ornament* item, LayoutContext& ctx)
4381 : {
4382 : LAYOUT_CALL_ITEM(item);
4383 16 : if (!item->ownershipParent()) {
4384 0 : return;
4385 : }
4386 :
4387 16 : Chord* parentChord = toChord(item->chordRest());
4388 16 : Chord* cueNoteChord = item->cueNoteChord();
4389 :
4390 16 : if (!cueNoteChord) {
4391 0 : return;
4392 : }
4393 :
4394 16 : const std::vector<Note*>& notes = cueNoteChord->notes();
4395 16 : Note* cueNote = notes.empty() ? nullptr : notes.front();
4396 :
4397 16 : if (!cueNote) {
4398 0 : return;
4399 : }
4400 :
4401 64 : ChordLayout::layoutChords3({ cueNoteChord }, { cueNote }, item->staff(), ctx);
4402 32 : ChordLayout::layoutLedgerLines({ cueNoteChord }, ctx);
4403 32 : AccidentalsLayout::layoutAccidentals({ cueNoteChord }, ctx);
4404 16 : layoutChord(cueNoteChord, ctx);
4405 :
4406 16 : Shape noteShape = cueNoteChord->shape();
4407 16 : Shape parentChordShape = parentChord->shape();
4408 16 : double minDist = HorizontalSpacing::minHorizontalDistance(parentChordShape, noteShape, parentChord->spatium());
4409 : // Check for possible other chords in same segment
4410 16 : staff_idx_t startStaff = staff2track(parentChord->staffIdx());
4411 80 : for (staff_idx_t staff = startStaff; staff < startStaff + VOICES; ++staff) {
4412 64 : Segment* segment = parentChord->segment();
4413 64 : ChordRest* cr = segment->element(staff) ? toChordRest(segment->element(staff)) : nullptr;
4414 64 : if (cr) {
4415 16 : minDist = std::max(minDist, HorizontalSpacing::minHorizontalDistance(cr->shape(), noteShape, cr->spatium()));
4416 : }
4417 : }
4418 16 : cueNoteChord->mutldata()->setPosX(minDist);
4419 16 : }
4420 :
4421 0 : void TLayout::layoutOttava(Ottava* item, LayoutContext& ctx)
4422 : {
4423 : LAYOUT_CALL_ITEM(item);
4424 0 : layoutLine(item, ctx);
4425 0 : }
4426 :
4427 161 : void TLayout::layoutOttavaSegment(OttavaSegment* item, LayoutContext& ctx)
4428 : {
4429 : LAYOUT_CALL_ITEM(item);
4430 161 : OttavaSegment::LayoutData* ldata = item->mutldata();
4431 :
4432 161 : layoutTextLineBaseSegment(item, ctx);
4433 :
4434 161 : Autoplace::autoplaceSpannerSegment(item, ldata);
4435 161 : }
4436 :
4437 56 : void TLayout::layoutPageLockIndicator(const PageLockIndicator* item, PageLockIndicator::LayoutData* ldata)
4438 : {
4439 56 : if (!item->configuration()->canLayoutIcons()) {
4440 56 : return;
4441 : }
4442 0 : const Page* page = item->page();
4443 0 : const System* sys = item->system();
4444 :
4445 0 : IF_ASSERT_FAILED(page && sys) {
4446 0 : return;
4447 : }
4448 :
4449 0 : Shape shape;
4450 :
4451 0 : const double spatium = item->spatium();
4452 :
4453 0 : const FontMetrics metrics(item->font());
4454 0 : const RectF iconBox = metrics.boundingRect(item->iconCode());
4455 0 : shape.add(iconBox, item);
4456 :
4457 0 : const double borderWidth = iconBox.height() + spatium;
4458 0 : const double borderPadding = 2 * spatium;
4459 0 : const double iconInset = (borderWidth - iconBox.width()) / 2;
4460 0 : PointF posOnPage = PointF(page->width() - borderPadding - iconBox.width() - iconBox.left() - iconInset,
4461 0 : page->height() - borderPadding - iconInset);
4462 0 : PointF posOnSystem = posOnPage - sys->pos();
4463 0 : ldata->setPos(posOnSystem);
4464 :
4465 0 : if (item->selected()) {
4466 : // Outer range rectangle
4467 0 : RectF rangeRect = page->shape().bbox();
4468 0 : PointF rectPagePos = rangeRect.topLeft() - ldata->pos() - sys->pos();
4469 0 : rangeRect.translate(rectPagePos);
4470 0 : rangeRect.adjust(borderPadding, borderPadding, -borderPadding, -borderPadding);
4471 0 : ldata->rangeRect = rangeRect;
4472 :
4473 : // Inner rectangle cutout
4474 : // Leaves border with rounded corners when painted
4475 0 : RectF innerRect = rangeRect.adjusted(borderWidth, borderWidth, -borderWidth, -borderWidth);
4476 0 : ldata->innerRangeRect = innerRect;
4477 0 : shape.add(RectF(PointF(rangeRect.left(), rangeRect.top()), PointF(rangeRect.right(), innerRect.top()))); // top
4478 0 : shape.add(RectF(PointF(rangeRect.left(), innerRect.bottom()), PointF(rangeRect.right(), rangeRect.bottom()))); // bottom
4479 0 : shape.add(RectF(PointF(rangeRect.left(), innerRect.top()), PointF(innerRect.left(), innerRect.bottom()))); // left
4480 0 : shape.add(RectF(PointF(innerRect.right(), innerRect.top()), PointF(rangeRect.right(), innerRect.bottom()))); // right
4481 : }
4482 :
4483 0 : ldata->setShape(shape);
4484 :
4485 : // Ensure it goes behind notation and LayoutBreak
4486 0 : const_cast<PageLockIndicator*>(item)->setZ(-100);
4487 0 : }
4488 :
4489 0 : void TLayout::layoutPalmMute(PalmMute* item, LayoutContext& ctx)
4490 : {
4491 : LAYOUT_CALL_ITEM(item);
4492 0 : layoutLine(item, ctx);
4493 0 : }
4494 :
4495 14 : void TLayout::layoutPalmMuteSegment(PalmMuteSegment* item, LayoutContext& ctx)
4496 : {
4497 : LAYOUT_CALL_ITEM(item);
4498 14 : PalmMuteSegment::LayoutData* ldata = item->mutldata();
4499 :
4500 14 : const StaffType* stType = item->staffType();
4501 :
4502 14 : if (stType && stType->isHiddenElementOnTab(Sid::palmMuteShowTabCommon, Sid::palmMuteShowTabSimple)) {
4503 0 : ldata->setIsSkipDraw(true);
4504 0 : return;
4505 : }
4506 14 : ldata->setIsSkipDraw(false);
4507 :
4508 14 : layoutTextLineBaseSegment(item, ctx);
4509 :
4510 14 : Autoplace::autoplaceSpannerSegment(item, ldata);
4511 : }
4512 :
4513 0 : void TLayout::layoutParenthesis(Parenthesis* item, Parenthesis::LayoutData* ldata, const LayoutContext& ctx)
4514 : {
4515 0 : ParenthesisLayout::layoutParenthesis(item, ldata, ctx);
4516 0 : }
4517 :
4518 0 : void TLayout::layoutPedal(Pedal* item, LayoutContext& ctx)
4519 : {
4520 : LAYOUT_CALL_ITEM(item);
4521 0 : layoutLine(item, ctx);
4522 0 : }
4523 :
4524 119 : void TLayout::layoutPedalSegment(PedalSegment* item, LayoutContext& ctx)
4525 : {
4526 : LAYOUT_CALL_ITEM(item);
4527 119 : PedalSegment::LayoutData* ldata = item->mutldata();
4528 :
4529 119 : layoutTextLineBaseSegment(item, ctx);
4530 :
4531 119 : Text* endText = item->endText();
4532 119 : if (endText && !endText->empty() && ldata->npoints > 0) { // Rosette
4533 11 : PointF endPoint = item->pos2();
4534 11 : double endTextWidth = endText->ldata()->bbox().width();
4535 11 : double xEndText = endPoint.x() - endTextWidth;
4536 11 : if (const Text* startText = item->text()) {
4537 11 : xEndText = std::max(xEndText, startText->ldata()->bbox().width());
4538 : }
4539 11 : endText->mutldata()->setPosX(xEndText);
4540 :
4541 11 : double lineTextGap = item->absoluteFromSpatium(item->getProperty(Pid::GAP_BETWEEN_TEXT_AND_LINE).value<Spatium>());
4542 11 : PointF& endOfLine = ldata->points.at(ldata->npoints - 1);
4543 11 : endOfLine.setX(xEndText - lineTextGap);
4544 :
4545 : // Recalculate shape after moving end text
4546 11 : Shape sh = recalculateTextLineBaseSegmentShape(item);
4547 11 : ldata->setShape(sh);
4548 11 : }
4549 :
4550 119 : Autoplace::autoplaceSpannerSegment(item, ldata);
4551 119 : }
4552 :
4553 2 : void TLayout::layoutPickScrapeSegment(PickScrapeSegment* item, LayoutContext& ctx)
4554 : {
4555 : LAYOUT_CALL_ITEM(item);
4556 2 : PickScrapeSegment::LayoutData* ldata = item->mutldata();
4557 2 : layoutTextLineBaseSegment(item, ctx);
4558 2 : Autoplace::autoplaceSpannerSegment(item, ldata);
4559 2 : }
4560 :
4561 6 : void TLayout::layoutPlayCountText(PlayCountText* item, TextBase::LayoutData* ldata)
4562 : {
4563 : LAYOUT_CALL_ITEM(item);
4564 6 : BarLine* bl = item->barline();
4565 6 : Segment* seg = item->segment();
4566 :
4567 6 : TextLayout::layoutBaseTextBase(item, ldata);
4568 :
4569 : // Avoid incoming barlines from above
4570 6 : double xAdj = 0.0;
4571 6 : BarLine* blAbove = item->staffIdx() != 0 ? toBarLine(seg->element(staff2track(item->staffIdx() - 1))) : nullptr;
4572 6 : if (blAbove && blAbove->spanStaff()) {
4573 0 : xAdj = blAbove->width();
4574 : }
4575 :
4576 6 : const double barlineWidth = bl->width();
4577 6 : const double diff = item->width() - barlineWidth + xAdj;
4578 6 : ldata->moveX(-diff);
4579 6 : }
4580 :
4581 87 : void TLayout::layoutPlayTechAnnotation(const PlayTechAnnotation* item, PlayTechAnnotation::LayoutData* ldata)
4582 : {
4583 : LAYOUT_CALL_ITEM(item);
4584 87 : TextLayout::layoutBaseTextBase(item, ldata);
4585 :
4586 87 : if (item->isHandbellsSymbol()) {
4587 25 : Chord* chord = nullptr;
4588 25 : Segment* seg = item->segment();
4589 25 : track_idx_t sTrack = staff2track(item->staffIdx());
4590 25 : track_idx_t eTrack = sTrack + VOICES;
4591 25 : for (track_idx_t track = sTrack; track < eTrack; ++track) {
4592 25 : if (seg->element(track) && seg->element(track)->isChord()) {
4593 25 : chord = toChord(seg->element(track));
4594 25 : break;
4595 : }
4596 : }
4597 25 : double center = 0.0;
4598 25 : if (chord) {
4599 25 : Note* refNote = item->placeAbove() ? chord->upNote() : chord->downNote();
4600 25 : center = 0.5 * refNote->width() + refNote->x() + chord->x();
4601 : } else {
4602 0 : center = 0.5 * item->score()->noteHeadWidth();
4603 : }
4604 25 : ldata->setPosX(center - 0.5 * (ldata->bbox().right() - ldata->bbox().left()));
4605 : }
4606 :
4607 87 : if (item->autoplace()) {
4608 87 : const Segment* s = toSegment(item->ownershipParent());
4609 87 : const Measure* m = s->measure();
4610 87 : LD_CONDITION(ldata->isSetPos());
4611 87 : LD_CONDITION(m->ldata()->isSetPos());
4612 87 : LD_CONDITION(s->ldata()->isSetPos());
4613 : }
4614 :
4615 87 : Autoplace::autoplaceSegmentElement(item, ldata);
4616 87 : }
4617 :
4618 4 : void TLayout::layoutRasgueadoSegment(RasgueadoSegment* item, LayoutContext& ctx)
4619 : {
4620 : LAYOUT_CALL_ITEM(item);
4621 4 : RasgueadoSegment::LayoutData* ldata = item->mutldata();
4622 4 : const StaffType* stType = item->staffType();
4623 :
4624 4 : if (stType && stType->isHiddenElementOnTab(Sid::rasgueadoShowTabCommon, Sid::rasgueadoShowTabSimple)) {
4625 0 : ldata->setIsSkipDraw(true);
4626 0 : return;
4627 : }
4628 4 : ldata->setIsSkipDraw(false);
4629 :
4630 4 : layoutTextLineBaseSegment(item, ctx);
4631 :
4632 4 : Autoplace::autoplaceSpannerSegment(item, ldata);
4633 : }
4634 :
4635 127 : void TLayout::layoutRehearsalMark(const RehearsalMark* item, RehearsalMark::LayoutData* ldata)
4636 : {
4637 : LAYOUT_CALL_ITEM(item);
4638 127 : TextLayout::layoutBaseTextBase(item, ldata);
4639 :
4640 127 : const Segment* s = item->segment();
4641 :
4642 127 : LD_CONDITION(s->ldata()->isSetPos());
4643 :
4644 127 : if (!s->rtick().isZero()) {
4645 27 : return;
4646 : }
4647 :
4648 : // first CR of measure, alignment is hcenter or right (the usual cases)
4649 : // align with barline, point just after header, or start of measure depending on context
4650 100 : const Measure* m = s->measure();
4651 100 : const Segment* header = s->prev(); // possibly just a start repeat
4652 100 : while (header && header->isType(SegmentType::Duration)) {
4653 0 : header = header->prev();
4654 : }
4655 100 : double measureX = -s->x();
4656 100 : const Segment* repeat = m->findSegmentR(SegmentType::StartRepeatBarLine, Fraction(0, 1));
4657 :
4658 100 : if (repeat) {
4659 0 : LD_CONDITION(repeat->ldata()->isSetPos());
4660 : }
4661 :
4662 100 : double barlineX = repeat ? repeat->x() - s->x() : measureX;
4663 100 : const System* sys = m->system();
4664 100 : bool systemFirst = (sys && m->isFirstInSystem());
4665 :
4666 100 : if (!header || repeat || !systemFirst) {
4667 : // no header, or header with repeat, or header mid-system - align with barline
4668 32 : ldata->setPosX(barlineX);
4669 32 : checkRehearsalMarkVSBigTimeSig(item, ldata);
4670 32 : return;
4671 : }
4672 :
4673 : // header at start of system
4674 : // align to a point just after the header
4675 68 : EngravingItem* e = header->element(item->track());
4676 :
4677 68 : if (e) {
4678 68 : LD_CONDITION(e->ldata()->isSetBbox());
4679 : }
4680 :
4681 68 : LD_CONDITION(header->ldata()->isSetBbox());
4682 :
4683 68 : double w = e ? e->ldata()->bbox().width() : header->ldata()->bbox().width();
4684 68 : ldata->setPosX(header->x() + w - s->x());
4685 :
4686 : // special case for right aligned rehearsal marks at start of system
4687 : // left align with start of measure if that is further left
4688 68 : if (item->position() == AlignH::RIGHT) {
4689 0 : ldata->setPosX(std::min(ldata->pos().x(), measureX + ldata->bbox().width()));
4690 : }
4691 :
4692 68 : checkRehearsalMarkVSBigTimeSig(item, ldata);
4693 : }
4694 :
4695 100 : void TLayout::checkRehearsalMarkVSBigTimeSig(const RehearsalMark* item, TextBase::LayoutData* ldata)
4696 : {
4697 100 : if (item->score()->style().styleV(Sid::timeSigPlacement).value<TimeSigPlacement>() != TimeSigPlacement::ABOVE_STAVES) {
4698 100 : return;
4699 : }
4700 :
4701 0 : const Segment* s = item->segment();
4702 0 : TimeSig* bigTimeSig = nullptr;
4703 0 : for (const Segment* segment = s; segment && segment->tick() == s->tick(); segment = segment->prevActive()) {
4704 0 : if (!segment->isType(SegmentType::TimeSigTypes)) {
4705 0 : continue;
4706 : }
4707 0 : TimeSig* timeSig = toTimeSig(segment->element(item->track()));
4708 0 : if (timeSig && timeSig->visible()) {
4709 0 : bigTimeSig = timeSig;
4710 0 : break;
4711 : }
4712 : }
4713 :
4714 0 : if (!bigTimeSig) {
4715 0 : return;
4716 : }
4717 :
4718 0 : double timeSigRightEdge = bigTimeSig->ldata()->bbox().translated(bigTimeSig->ldata()->pos()).right()
4719 0 : + bigTimeSig->segment()->x() - s->x();
4720 0 : double rehMarkLeftEdge = ldata->bbox().translated(item->ldata()->pos()).left();
4721 0 : double distance = rehMarkLeftEdge - timeSigRightEdge;
4722 0 : const double margin = 0.5 * item->fontMetrics().xHeight();
4723 0 : if (distance < margin) {
4724 0 : ldata->moveX(margin - distance);
4725 : }
4726 : }
4727 :
4728 75939 : void TLayout::layoutRest(const Rest* item, Rest::LayoutData* ldata, const LayoutContext& ctx)
4729 : {
4730 75939 : RestLayout::layoutRest(item, ldata, ctx);
4731 75939 : }
4732 :
4733 0 : void TLayout::layoutShadowNote(ShadowNote* item, LayoutContext& ctx)
4734 : {
4735 : LAYOUT_CALL_ITEM(item);
4736 0 : if (!item->isValid()) {
4737 0 : item->setbbox(RectF());
4738 0 : return;
4739 : }
4740 :
4741 0 : double _spatium = item->spatium();
4742 0 : double mag = item->mag();
4743 0 : RectF noteheadBbox = item->symBbox(item->noteheadSymbol());
4744 0 : bool up = item->computeUp();
4745 0 : bool isJianpu = item->staff() && item->staff()->isJianpuStaff(item->tick());
4746 :
4747 : // Layout dots
4748 0 : double dotWidth = 0.0;
4749 0 : if (item->duration().dots() > 0) {
4750 0 : double noteheadWidth = noteheadBbox.width();
4751 0 : double d = ctx.conf().styleAbsolute(Sid::dotNoteDistance) * mag;
4752 0 : double dd = ctx.conf().styleAbsolute(Sid::dotDotDistance) * mag;
4753 0 : dotWidth = (noteheadWidth + d);
4754 0 : if (item->hasFlag() && up) {
4755 0 : dotWidth = std::max(dotWidth, noteheadWidth + item->symBbox(item->flagSym()).right());
4756 : }
4757 0 : for (int i = 0; i < item->duration().dots(); i++) {
4758 0 : dotWidth += dd * i;
4759 : }
4760 : }
4761 :
4762 0 : RectF newBbox;
4763 0 : newBbox.setRect(noteheadBbox.x(), noteheadBbox.y(), noteheadBbox.width() + dotWidth, noteheadBbox.height());
4764 :
4765 : // Layout stem and flag
4766 0 : if (item->hasStem()) {
4767 0 : double x = noteheadBbox.x();
4768 0 : double w = noteheadBbox.width();
4769 :
4770 0 : double stemWidth = ctx.conf().styleAbsolute(Sid::stemWidth);
4771 0 : double stemLength = (up ? -3.5 : 3.5) * _spatium;
4772 0 : double stemAnchor = item->symSmuflAnchor(item->noteheadSymbol(), up ? SmuflAnchorId::stemUpSE : SmuflAnchorId::stemDownNW).y();
4773 :
4774 0 : newBbox |= RectF(up ? x + w - stemWidth : x,
4775 : stemAnchor,
4776 : stemWidth,
4777 0 : stemLength - stemAnchor);
4778 :
4779 0 : if (item->hasFlag()) {
4780 0 : RectF flagBbox = item->symBbox(item->flagSym());
4781 0 : newBbox |= RectF(up ? x + w - stemWidth : x,
4782 0 : stemAnchor + stemLength + flagBbox.y(),
4783 : flagBbox.width(),
4784 0 : flagBbox.height());
4785 : }
4786 : }
4787 :
4788 0 : int lineIdx = item->lineIndex();
4789 :
4790 : // Layout ledger lines if needed
4791 0 : if (item->ledgerLinesVisible()) {
4792 0 : double extraLen = ctx.conf().styleAbsolute(Sid::ledgerLineLength) * mag;
4793 0 : double step = 0.5 * _spatium * item->staffType()->lineDistance().val();
4794 0 : double yOffset = item->staffOffsetY();
4795 0 : double x = noteheadBbox.x() - extraLen;
4796 0 : double w = noteheadBbox.width() + 2 * extraLen;
4797 :
4798 0 : double lw = ctx.conf().styleAbsolute(Sid::ledgerLineWidth);
4799 :
4800 : // jianpu will show the 5-line staff with more extra length
4801 0 : if (isJianpu) {
4802 0 : x -= 2 * extraLen;
4803 0 : w += 4 * extraLen;
4804 : }
4805 :
4806 0 : RectF r(x, -lw * .5, w, lw);
4807 0 : const int topLine = -2 + yOffset / step;
4808 0 : for (int i = topLine; i >= lineIdx; i -= 2) {
4809 0 : newBbox |= r.translated(PointF(0, step * (i - lineIdx)));
4810 : }
4811 0 : int l = item->staffType()->lines() * 2 + yOffset / step; // first ledger line below staff
4812 0 : for (int i = l; i <= lineIdx; i += 2) {
4813 0 : newBbox |= r.translated(PointF(0, step * (i - lineIdx)));
4814 : }
4815 : }
4816 :
4817 : // Layout accidental (Jianpu shows its accidental as part of the digit label instead)
4818 0 : SymId acc = Accidental::subtype2symbol(item->accidentalType());
4819 0 : if (!isJianpu && acc != SymId::noSym) {
4820 0 : RectF symRect = item->symBbox(acc);
4821 0 : double accWidth = symRect.width() + ctx.conf().styleAbsolute(Sid::accidentalNoteDistance) * mag;
4822 0 : double dh = 0.0;
4823 :
4824 0 : if (symRect.y() < newBbox.y()) {
4825 0 : dh = symRect.height() - (newBbox.y() - symRect.y());
4826 0 : newBbox.setY(symRect.y());
4827 0 : } else if (symRect.y() > newBbox.y()) {
4828 0 : dh = newBbox.height() - (symRect.y() - newBbox.y());
4829 : }
4830 :
4831 0 : newBbox.setX(newBbox.x() - accWidth);
4832 0 : newBbox.setWidth(newBbox.width() + accWidth);
4833 0 : newBbox.setHeight(newBbox.height() + dh);
4834 : }
4835 :
4836 : // Layout jianpu
4837 0 : RectF jianpuBbox(noteheadBbox.x(), 0, noteheadBbox.width(), 0);
4838 0 : if (isJianpu) {
4839 0 : const Staff* staff = item->staff();
4840 0 : const StaffType* st = staff->staffTypeForElement(item);
4841 0 : int dots = 0;
4842 :
4843 : // jianpu digit
4844 0 : if (item->isRest()) {
4845 0 : item->setJianpuDigit(String(u"0"));
4846 0 : item->setJianpuAccidental(String());
4847 : } else {
4848 0 : item->setJianpuDigit(String());
4849 0 : item->setJianpuAccidental(String());
4850 0 : Score* score = item->score();
4851 0 : Position pos;
4852 0 : bool error = !(score && score->getPosition(&pos, item->pos(), item->track()) && pos.segment);
4853 0 : NoteVal nval;
4854 0 : if (!error) {
4855 0 : nval = NoteInput::noteValForPosition(score, pos, item->accidentalType(), error);
4856 : }
4857 0 : if (!error) {
4858 0 : bool concertPitch = ctx.conf().styleB(Sid::concertPitch);
4859 0 : int tpc = nval.tpc(concertPitch);
4860 :
4861 0 : if (tpc != Tpc::TPC_INVALID) {
4862 0 : String accName, stepName;
4863 0 : KeySigEvent ks = staff->keySigEvent(item->tick());
4864 : KeyMode mode;
4865 : int tonicTpc;
4866 0 : jianpuKeyMapping(ks, mode, tonicTpc);
4867 0 : tpc2Jianpu(tpc, tonicTpc, mode, accName, stepName);
4868 0 : item->setJianpuDigit(stepName);
4869 0 : item->setJianpuAccidental(jianpuAccidentalMark(accName));
4870 :
4871 0 : Interval transpose = item->part()->instrument(item->tick())->transpose();
4872 0 : int epitch = nval.pitch - transpose.chromatic;
4873 0 : dots = -pitch2JianpuOctave(epitch, tpc, tonicTpc, mode);
4874 0 : }
4875 : }
4876 : }
4877 0 : double dotDistance = ctx.conf().styleAbsolute(Sid::jianpuOctaveDotDistance) * mag;
4878 0 : double lineDistance = ctx.conf().styleAbsolute(Sid::jianpuDiminutionBeamDistance) * mag;
4879 0 : jianpuBbox.setHeight(st->jianpuBoxH() * mag + dotDistance);
4880 0 : if (!up) {
4881 0 : jianpuBbox.setY(noteheadBbox.y() - jianpuBbox.height()); // Jianpu is above the head note
4882 : } else {
4883 0 : jianpuBbox.setY(newBbox.y() + newBbox.height()); // Jianpu is under the head note
4884 : }
4885 :
4886 : // jianpu diminution line
4887 0 : int lines = jianpuDiminutionLinesForDuration(item->duration());
4888 0 : item->setJianpuDiminutionLines(lines);
4889 :
4890 : // jianpu octave dots
4891 0 : item->setJianpuOctaveDots(dots);
4892 :
4893 : // Always has one extra distance
4894 0 : double extraHeight = (1 + abs(dots)) * dotDistance + lines * lineDistance;
4895 :
4896 0 : jianpuBbox.setHeight(jianpuBbox.height() + extraHeight);
4897 0 : if (!up) {
4898 0 : jianpuBbox.setY(jianpuBbox.y() - extraHeight); // Jianpu is above the head note
4899 : }
4900 0 : newBbox |= jianpuBbox;
4901 : }
4902 :
4903 0 : const std::set<SymId>& articulationIds = item->articulationIds();
4904 0 : if (articulationIds.empty()) {
4905 0 : item->setbbox(newBbox);
4906 0 : return;
4907 : }
4908 :
4909 : // Layout articulations
4910 0 : bool distantArticulations = false;
4911 0 : double minDist = 0.4 * _spatium;
4912 0 : if ((!up && lineIdx > 1) || (up && lineIdx < item->staffType()->bottomLine() - 1)) {
4913 0 : minDist = _spatium;
4914 : }
4915 :
4916 : // Extend bounding box for nearby articulations
4917 0 : for (const SymId& artic: item->articulationIds()) {
4918 0 : String articName = Articulation::symId2ArticulationName(artic);
4919 0 : const double symH = item->symHeight(artic);
4920 0 : if (articName == u"staccato" || articName == u"tenuto") {
4921 0 : if (!up) {
4922 0 : newBbox.setTop(newBbox.y() - minDist - symH);
4923 : } else {
4924 0 : newBbox.setBottom(newBbox.bottom() + minDist + symH);
4925 : }
4926 : } else {
4927 0 : distantArticulations = true;
4928 : }
4929 0 : }
4930 :
4931 : // If there are any other articulations, extend bounding box to include whole staff
4932 : // And extend further to include those articulations
4933 0 : if (distantArticulations) {
4934 0 : minDist = 0.4 * _spatium;
4935 0 : const double staffTop = -_spatium * .5 * lineIdx;
4936 0 : const double staffBottom = -_spatium * .5 * (item->staffType()->bottomLine() - lineIdx);
4937 0 : if (newBbox.y() > staffTop) {
4938 0 : newBbox.setTop(staffTop);
4939 : }
4940 0 : if (newBbox.bottom() < staffBottom) {
4941 0 : newBbox.setBottom(staffBottom);
4942 : }
4943 :
4944 0 : for (const SymId& artic: item->articulationIds()) {
4945 0 : String articName = Articulation::symId2ArticulationName(artic);
4946 0 : const double symH = item->symHeight(artic);
4947 0 : bool isMarcato = Articulation::symId2ArticulationName(artic).contains(u"marcato");
4948 0 : bool isLv = artic == SymId::articLaissezVibrerAbove || artic == SymId::articLaissezVibrerBelow;
4949 0 : if (articName == u"staccato" || articName == u"tenuto") {
4950 0 : continue;
4951 : } else {
4952 0 : if (!up || isMarcato) {
4953 0 : newBbox.setTop(newBbox.y() - minDist - symH);
4954 : } else {
4955 0 : newBbox.setBottom(newBbox.bottom() + minDist + symH);
4956 : }
4957 : }
4958 0 : if (isLv) {
4959 0 : const double width = item->symWidth(artic) - item->symWidth(item->noteheadSymbol()) / 2;
4960 0 : newBbox.setWidth(newBbox.width() + width);
4961 : }
4962 0 : }
4963 : }
4964 :
4965 0 : item->setbbox(newBbox);
4966 : }
4967 :
4968 492 : void TLayout::layoutLine(SLine* item, LayoutContext& ctx)
4969 : {
4970 : LAYOUT_CALL_ITEM(item);
4971 492 : item->computeStartElement();
4972 492 : item->computeEndElement();
4973 :
4974 : System* s1;
4975 : System* s2;
4976 492 : PointF p1(item->linePos(Grip::START, &s1));
4977 492 : PointF p2(item->linePos(Grip::END, &s2));
4978 :
4979 492 : const std::vector<System*>& systems = ctx.dom().systems();
4980 492 : system_idx_t sysIdx1 = muse::indexOf(systems, s1);
4981 492 : system_idx_t sysIdx2 = muse::indexOf(systems, s2);
4982 492 : int segmentsNeeded = 0;
4983 :
4984 492 : if (sysIdx1 == muse::nidx || sysIdx2 == muse::nidx) {
4985 0 : return;
4986 : }
4987 :
4988 986 : for (system_idx_t i = sysIdx1; i <= sysIdx2; ++i) {
4989 494 : if (systems.at(i)->vbox()) {
4990 0 : continue;
4991 : }
4992 494 : ++segmentsNeeded;
4993 : }
4994 :
4995 492 : int segCount = int(item->spannerSegments().size());
4996 :
4997 492 : if (segmentsNeeded != segCount) {
4998 326 : item->fixupSegments(segmentsNeeded, [item]() { return item->createLineSegment(); });
4999 163 : if (segmentsNeeded > segCount) {
5000 326 : for (int i = segCount; i < segmentsNeeded; ++i) {
5001 163 : LineSegment* lineSegm = item->segmentAt(i);
5002 : // set user offset to previous segment's offset
5003 163 : if (segCount > 0) {
5004 1 : lineSegm->setOffset(PointF(0, item->segmentAt(i - 1)->offset().y()));
5005 : } else {
5006 162 : lineSegm->setOffset(PointF(0, item->offset().y()));
5007 : }
5008 : }
5009 : }
5010 : }
5011 :
5012 492 : int segIdx = 0;
5013 986 : for (system_idx_t i = sysIdx1; i <= sysIdx2; ++i) {
5014 494 : System* system = systems.at(i);
5015 494 : if (system->vbox()) {
5016 0 : continue;
5017 : }
5018 494 : LineSegment* lineSegm = item->segmentAt(segIdx++);
5019 494 : lineSegm->setTrack(item->track()); // DEBUG
5020 494 : lineSegm->moveToSystem(system);
5021 :
5022 494 : if (sysIdx1 == sysIdx2) {
5023 : // single segment
5024 490 : lineSegm->setSpannerSegmentType(SpannerSegmentType::SINGLE);
5025 490 : double len = p2.x() - p1.x();
5026 : // enforcing a minimum length would be possible but inadvisable
5027 : // the line length calculations are tuned well enough that this should not be needed
5028 : //if (anchor() == Anchor::SEGMENT && !isPedal())
5029 : // len = std::max(1.0 * spatium(), len);
5030 490 : lineSegm->setPos(p1);
5031 490 : lineSegm->setPos2(PointF(len, p2.y() - p1.y()));
5032 4 : } else if (i == sysIdx1) {
5033 : // start segment
5034 2 : lineSegm->setSpannerSegmentType(SpannerSegmentType::BEGIN);
5035 2 : lineSegm->setPos(p1);
5036 2 : double x2 = system->endingXForOpenEndedLines();
5037 2 : lineSegm->setPos2(PointF(x2 - p1.x(), 0.0));
5038 2 : } else if (i > 0 && i != sysIdx2) {
5039 : // middle segment
5040 0 : lineSegm->setSpannerSegmentType(SpannerSegmentType::MIDDLE);
5041 0 : double x1 = system->firstNoteRestSegmentX(true);
5042 0 : double x2 = system->endingXForOpenEndedLines();
5043 0 : lineSegm->setPos(PointF(x1, p1.y()));
5044 0 : lineSegm->setPos2(PointF(x2 - x1, 0.0));
5045 2 : } else if (i == sysIdx2) {
5046 : // end segment
5047 2 : double minLen = 0.0;
5048 2 : double x1 = system->firstNoteRestSegmentX(true);
5049 2 : double len = std::max(minLen, p2.x() - x1);
5050 2 : lineSegm->setSpannerSegmentType(SpannerSegmentType::END);
5051 2 : lineSegm->setPos(PointF(p2.x() - len, p2.y()));
5052 2 : lineSegm->setPos2(PointF(len, 0.0));
5053 : }
5054 494 : layoutLineSegment(lineSegm, ctx);
5055 : }
5056 : }
5057 :
5058 492 : void TLayout::layoutNoteAnchoredLine(SLine* item, EngravingItem::LayoutData* ldata, LayoutContext& ctx)
5059 : {
5060 492 : double _spatium = item->spatium();
5061 492 : Note* startAnchor = toNote(item->startElement());
5062 492 : Note* endAnchor = toNote(item->endElement());
5063 492 : Chord* startChord = startAnchor->chord();
5064 492 : Chord* endChord = endAnchor->chord();
5065 492 : LineSegment* startSeg = toLineSegment(item->frontSegment());
5066 492 : LineSegment* endSeg = toLineSegment(item->backSegment());
5067 :
5068 : // Note: line segments are defined by
5069 : // initial point: ipos() (relative to system origin)
5070 : // ending point: pos2() (relative to initial point)
5071 :
5072 : // LINE ENDING POINTS TO NOTEHEAD CENTRES
5073 :
5074 : // assume line goes from centre of initial note centre to centre of ending note:
5075 : // move first segment origin and last segment ending point from notehead origin to notehead centre
5076 : // For TAB: begin at the right-edge of initial note rather than centre
5077 492 : PointF startOffset = (startChord->staff()->isTabStaff(startChord->tick()))
5078 492 : ? PointF(startAnchor->ldata()->bbox().right(), 0.0)
5079 492 : : PointF(startAnchor->headWidth() * 0.5, 0.0);
5080 :
5081 492 : PointF endOffset = PointF(endAnchor->headWidth() * 0.5, 0.0);
5082 :
5083 : // AVOID HORIZONTAL LINES
5084 :
5085 : // for microtonality read tuning, or check note accidental
5086 492 : double startTune = startAnchor->tuning();
5087 492 : double endTune = endAnchor->tuning();
5088 492 : AccidentalType startAcc = startAnchor->accidentalType();
5089 492 : AccidentalType endAcc = endAnchor->accidentalType();
5090 492 : if (muse::RealIsNull(startTune) && Accidental::isMicrotonal(startAcc)) {
5091 0 : startTune = Accidental::subtype2centOffset(startAcc);
5092 : }
5093 492 : if (muse::RealIsNull(endTune) && Accidental::isMicrotonal(endAcc)) {
5094 0 : endTune = Accidental::subtype2centOffset(endAcc);
5095 : }
5096 :
5097 492 : int upDown = (0 < (endAnchor->ppitch() - startAnchor->ppitch())) - ((endAnchor->ppitch() - startAnchor->ppitch()) < 0);
5098 : // same note, so compare tunings
5099 492 : if (upDown == 0) {
5100 5 : upDown = (0 < (endTune - startTune)) - ((endTune - startTune) < 0);
5101 : }
5102 :
5103 : // on TAB's, glissando are by necessity on the same string, this gives an horizontal glissando line;
5104 : // make bottom end point lower and top ending point higher
5105 492 : if (startChord->staff()->isTabStaff(startChord->tick())) {
5106 15 : double yOff = startChord->staff()->lineDistance(startChord->tick()) * 0.4 * _spatium;
5107 15 : startOffset.ry() += yOff * upDown;
5108 15 : endOffset.ry() -= yOff * upDown;
5109 : }
5110 : // if not TAB, angle glissando between notes on the same line
5111 : else {
5112 477 : if (startAnchor->line() == endAnchor->line()) {
5113 5 : startOffset.ry() += _spatium * 0.25 * upDown;
5114 5 : endOffset.ry() -= _spatium * 0.25 * upDown;
5115 : }
5116 : }
5117 :
5118 : // move initial point of first segment and adjust its length accordingly
5119 492 : startSeg->setPos(startSeg->ldata()->pos() + startOffset);
5120 492 : startSeg->setPos2(startSeg->ipos2() - startOffset);
5121 : // adjust ending point of last segment
5122 492 : endSeg->setPos2(endSeg->ipos2() + endOffset);
5123 :
5124 : // INTERPOLATION OF INTERMEDIATE POINTS
5125 :
5126 : // get total x-width and total y-height of all segments
5127 492 : double xTot = 0.0;
5128 986 : for (SpannerSegment* segm : item->spannerSegments()) {
5129 494 : xTot += segm->ipos2().x();
5130 : }
5131 492 : double startY = startSeg->ldata()->pos().y();
5132 492 : double yTot = endSeg->ldata()->pos().y() + endSeg->ipos2().y() - startY;
5133 492 : yTot -= yStaffDifference(endSeg->system(), startSeg->system(), track2staff(item->track2()));
5134 492 : double ratio = muse::divide(yTot, xTot, 1.0);
5135 : // interpolate y-coord of intermediate points across total width and height
5136 492 : double xCurr = 0.0;
5137 : double yCurr;
5138 494 : for (unsigned i = 0; i + 1 < item->spannerSegments().size(); i++) {
5139 2 : SpannerSegment* segm = item->segmentAt(i);
5140 2 : xCurr += segm->ipos2().x();
5141 2 : yCurr = startY + ratio * xCurr;
5142 2 : segm->rypos2() = yCurr - segm->ldata()->pos().y(); // position segm. end point at yCurr
5143 : // next segment shall start where this segment stopped
5144 2 : SpannerSegment* nextSeg = item->segmentAt(i + 1);
5145 2 : yCurr += yStaffDifference(nextSeg->system(), segm->system(), track2staff(item->track2()));
5146 2 : segm = nextSeg;
5147 2 : segm->rypos2() += segm->ldata()->pos().y() - yCurr; // adjust next segm. vertical length
5148 2 : segm->mutldata()->setPosY(yCurr); // position next segm. start point at yCurr
5149 : }
5150 :
5151 : // KEEP CLEAR OF ALL ELEMENTS OF THE CHORD
5152 : // Remove offset already applied
5153 492 : startOffset *= -1.0;
5154 492 : endOffset *= -1.0;
5155 : // Look at chord shapes (but don't consider lyrics)
5156 492 : Shape startCRShape = startChord->shape();
5157 492 : startCRShape.remove_if([](ShapeElement& s) {
5158 1227 : if (!s.item() || s.item()->isLyrics()) {
5159 6 : return true;
5160 : } else {
5161 1221 : return false;
5162 : }
5163 : });
5164 :
5165 492 : double startYAbove = startAnchor->ldata()->pos().y() + startAnchor->ldata()->bbox().topRight().y();
5166 492 : double startYBelow = startYAbove + startAnchor->ldata()->bbox().height();
5167 492 : startOffset.rx() += startCRShape.rightMostEdgeAtHeight(startYAbove, startYBelow) - startAnchor->pos().x();
5168 492 : if (!endChord->staff()->isTabStaff(endChord->tick())) {
5169 477 : double endYAbove = endAnchor->ldata()->pos().y() + endAnchor->ldata()->bbox().topLeft().y();
5170 477 : double endYBelow = endYAbove + endAnchor->ldata()->bbox().height();
5171 477 : double noteMiddle = endYAbove + endAnchor->ldata()->bbox().height() / 2;
5172 477 : if (upDown != 0) {
5173 472 : int llWidth = ctx.conf().styleS(Sid::ledgerLineWidth).val() * _spatium;
5174 : // Only check top/bottom half of note depending on line approach direction
5175 : // to avoid clearing acidentals the line won't collide with
5176 472 : endYAbove = upDown == 1 ? noteMiddle - llWidth : endYAbove;
5177 472 : endYBelow = upDown == 1 ? endYBelow : noteMiddle + llWidth;
5178 : }
5179 :
5180 477 : endOffset.rx() -= endAnchor->pos().x() - endChord->shape().leftMostEdgeAtHeight(endYAbove, endYBelow);
5181 : }
5182 : // Add note distance
5183 492 : const double lineNoteDist = 0.25 * item->spatium(); // TODO: style
5184 492 : startOffset.rx() += lineNoteDist;
5185 492 : endOffset.rx() -= lineNoteDist;
5186 :
5187 : // apply offsets: shorten first segment by x1 (and proportionally y) and adjust its length accordingly
5188 492 : startOffset.ry() = startSeg->ipos2().y() * muse::divide(startOffset.x(), startSeg->ipos2().x(), 1.0);
5189 492 : startSeg->setPos(startSeg->ldata()->pos() + startOffset);
5190 492 : startSeg->setPos2(startSeg->ipos2() - startOffset);
5191 : // adjust last segment length by x2 (and proportionally y)
5192 492 : endOffset.ry() = endSeg->ipos2().y() * muse::divide(endOffset.x(), endSeg->ipos2().x(), 1.0);
5193 492 : endSeg->setPos2(endSeg->ipos2() + endOffset);
5194 :
5195 986 : for (SpannerSegment* segm : item->spannerSegments()) {
5196 494 : TLayout::layoutItem(segm, ctx);
5197 : }
5198 :
5199 : // compute line bbox as the bbox of the last segment, relative to the end anchor note
5200 492 : PointF endAnchorPagePos = endAnchor->pagePos();
5201 492 : PointF endSystemPagePos;
5202 492 : IF_ASSERT_FAILED(endChord->segment()->system()) {
5203 0 : endSystemPagePos = endSeg->pos();
5204 : } else {
5205 492 : endSystemPagePos = endChord->segment()->system()->pagePos();
5206 : }
5207 :
5208 492 : PointF endAnchorSystPos = endAnchorPagePos - endSystemPagePos;
5209 492 : RectF r = RectF(endAnchorSystPos - endSeg->pos(), endAnchorSystPos - endSeg->pos() - endSeg->pos2()).normalized();
5210 492 : double lw = item->absoluteFromSpatium(item->lineWidth()) * .5;
5211 492 : ldata->setBbox(r.adjusted(-lw, -lw, lw, lw));
5212 492 : }
5213 :
5214 6 : void TLayout::layoutNoteLine(NoteLine* item, LayoutContext& ctx)
5215 : {
5216 : LAYOUT_CALL_ITEM(item);
5217 :
5218 6 : if (item->startElement()) {
5219 6 : item->setTick(item->startElement()->tick());
5220 : }
5221 :
5222 6 : if (item->endElement()) {
5223 6 : item->setTick2(item->endElement()->tick());
5224 : }
5225 :
5226 6 : TLayout::layoutLine(item, ctx);
5227 :
5228 6 : if (item->lineEndPlacement() == NoteLineEndPlacement::OFFSET_ENDS) {
5229 6 : layoutNoteAnchoredLine(item, item->mutldata(), ctx);
5230 6 : ChordLayout::addLineAttachPoints(item);
5231 : }
5232 6 : }
5233 :
5234 12 : void TLayout::layoutNoteLineSegment(NoteLineSegment* item, LayoutContext& ctx)
5235 : {
5236 : LAYOUT_CALL_ITEM(item);
5237 :
5238 12 : NoteLineSegment::LayoutData* ldata = item->mutldata();
5239 12 : layoutTextLineBaseSegment(item, ctx);
5240 :
5241 12 : if (item->staff()) {
5242 12 : ldata->setMag(item->staff()->staffMag(item->tick()));
5243 : }
5244 12 : }
5245 :
5246 0 : void TLayout::layoutSlur(Slur* item, LayoutContext& ctx)
5247 : {
5248 0 : SlurTieLayout::createSlurSegments(item, ctx);
5249 0 : }
5250 :
5251 2 : void TLayout::layoutSpacer(Spacer* item, LayoutContext&)
5252 : {
5253 : LAYOUT_CALL_ITEM(item);
5254 2 : Spacer::LayoutData* ldata = item->mutldata();
5255 :
5256 2 : double spatium = item->spatium();
5257 :
5258 2 : PainterPath path = PainterPath();
5259 2 : double w = spatium;
5260 2 : double b = w * .5;
5261 2 : double h = item->ownershipParent() ? item->absoluteGap() : item->absoluteFromSpatium(std::min(item->gap(), 4.0_sp)); // limit length for palette
5262 :
5263 2 : switch (item->spacerType()) {
5264 0 : case SpacerType::DOWN:
5265 0 : path.lineTo(w, 0.0);
5266 0 : path.moveTo(b, 0.0);
5267 0 : path.lineTo(b, h);
5268 0 : path.lineTo(0.0, h - b);
5269 0 : path.moveTo(b, h);
5270 0 : path.lineTo(w, h - b);
5271 0 : break;
5272 0 : case SpacerType::UP:
5273 0 : path.moveTo(b, 0.0);
5274 0 : path.lineTo(0.0, b);
5275 0 : path.moveTo(b, 0.0);
5276 0 : path.lineTo(w, b);
5277 0 : path.moveTo(b, 0.0);
5278 0 : path.lineTo(b, h);
5279 0 : path.moveTo(0.0, h);
5280 0 : path.lineTo(w, h);
5281 0 : break;
5282 2 : case SpacerType::FIXED:
5283 2 : path.lineTo(w, 0.0);
5284 2 : path.moveTo(b, 0.0);
5285 2 : path.lineTo(b, h);
5286 2 : path.moveTo(0.0, h);
5287 2 : path.lineTo(w, h);
5288 2 : break;
5289 : }
5290 2 : ldata->path = path;
5291 2 : double lw = spatium * 0.4;
5292 2 : RectF bb(0, 0, w, h);
5293 2 : bb.adjust(-lw, -lw, lw, lw);
5294 2 : ldata->setBbox(bb);
5295 :
5296 2 : item->setZ(0.0);
5297 2 : }
5298 :
5299 649 : void TLayout::layoutSpanner(Spanner* item, LayoutContext& ctx)
5300 : {
5301 : LAYOUT_CALL_ITEM(item);
5302 649 : layoutItem(item, ctx);
5303 649 : }
5304 :
5305 50291 : void TLayout::layoutStaffLines(StaffLines* item, LayoutContext& ctx)
5306 : {
5307 : LAYOUT_CALL_ITEM(item);
5308 50291 : layoutForWidth(item, item->measure()->width(), ctx);
5309 50291 : }
5310 :
5311 50291 : void TLayout::layoutForWidth(StaffLines* item, double w, LayoutContext& ctx)
5312 : {
5313 : LAYOUT_CALL_ITEM(item);
5314 50291 : StaffLines::LayoutData* ldata = item->mutldata();
5315 50291 : const Staff* s = item->staff();
5316 50291 : double _spatium = item->spatium();
5317 50291 : double dist = _spatium;
5318 50291 : item->setPos(PointF(0.0, 0.0));
5319 : int _lines;
5320 50291 : if (s) {
5321 50291 : ldata->setMag(s->staffMag(item->measure()->tick()));
5322 50291 : item->setVisible(!s->isLinesInvisible(item->measure()->tick()));
5323 50291 : item->setColor(s->color(item->measure()->tick()));
5324 50291 : const StaffType* st = s->staffType(item->measure()->tick());
5325 50291 : dist *= st->lineDistance().val();
5326 50291 : _lines = st->lines();
5327 50291 : ldata->setPosY(st->yoffset().val() * _spatium);
5328 : // if (_lines == 1)
5329 : // rypos() = 2 * _spatium;
5330 : } else {
5331 0 : _lines = 5;
5332 0 : item->setColor(item->configuration()->defaultColor());
5333 : }
5334 50291 : item->setLw(ctx.conf().styleS(Sid::staffLineWidth).val() * _spatium);
5335 50291 : double x1 = item->pos().x();
5336 50291 : double x2 = x1 + w;
5337 50291 : double y = item->pos().y();
5338 50291 : ldata->setBbox(x1, -item->lw() * .5 + y, w, (_lines - 1) * dist + item->lw());
5339 :
5340 50291 : std::vector<LineF> ll;
5341 301174 : for (int i = 0; i < _lines; ++i) {
5342 250883 : ll.push_back(LineF(x1, y, x2, y));
5343 250883 : y += dist;
5344 : }
5345 50291 : item->setLines(ll);
5346 50291 : }
5347 :
5348 0 : void TLayout::layoutStaffState(const StaffState* item, StaffState::LayoutData* ldata)
5349 : {
5350 : LAYOUT_CALL_ITEM(item);
5351 : LD_INDEPENDENT;
5352 :
5353 0 : if (ldata->isValid()) {
5354 0 : return;
5355 : }
5356 :
5357 0 : double _spatium = item->spatium();
5358 0 : ldata->lw = (_spatium * 0.3);
5359 0 : double h = _spatium * 4;
5360 0 : double w = _spatium * 2.5;
5361 : // double w1 = w * .6;
5362 :
5363 0 : PainterPath path;
5364 0 : switch (item->staffStateType()) {
5365 0 : case StaffStateType::INSTRUMENT:
5366 0 : path.lineTo(w, 0.0);
5367 0 : path.lineTo(w, h);
5368 0 : path.lineTo(0.0, h);
5369 0 : path.lineTo(0.0, 0.0);
5370 0 : path.moveTo(w * .5, h - _spatium * .5);
5371 0 : path.lineTo(w * .5, _spatium * 2);
5372 0 : path.moveTo(w * .5, _spatium * .8);
5373 0 : path.lineTo(w * .5, _spatium * 1.0);
5374 0 : break;
5375 :
5376 0 : case StaffStateType::TYPE:
5377 0 : path.lineTo(w, 0.0);
5378 0 : path.lineTo(w, h);
5379 0 : path.lineTo(0.0, h);
5380 0 : path.lineTo(0.0, 0.0);
5381 0 : break;
5382 :
5383 0 : case StaffStateType::VISIBLE:
5384 0 : path.lineTo(w, 0.0);
5385 0 : path.lineTo(w, h);
5386 0 : path.lineTo(0.0, h);
5387 0 : path.lineTo(0.0, 0.0);
5388 0 : break;
5389 :
5390 0 : case StaffStateType::INVISIBLE:
5391 0 : path.lineTo(w, 0.0);
5392 0 : path.lineTo(w, h);
5393 0 : path.lineTo(0.0, h);
5394 0 : path.lineTo(0.0, 0.0);
5395 0 : break;
5396 :
5397 0 : default:
5398 0 : LOGD("unknown layout break symbol");
5399 0 : break;
5400 : }
5401 :
5402 0 : ldata->path = path;
5403 :
5404 0 : RectF bb(0, 0, w, h);
5405 0 : bb.adjust(-ldata->lw, -ldata->lw, ldata->lw, ldata->lw);
5406 0 : ldata->setBbox(bb);
5407 0 : ldata->setPos(0.0, _spatium * -6.0);
5408 0 : }
5409 :
5410 1018 : void TLayout::layoutStaffText(const StaffText* item, StaffText::LayoutData* ldata)
5411 : {
5412 : LAYOUT_CALL_ITEM(item);
5413 1018 : TextLayout::layoutBaseTextBase(item, ldata);
5414 :
5415 1018 : if (item->autoplace()) {
5416 1018 : const Segment* s = toSegment(item->ownershipParent());
5417 1018 : const Measure* m = s->measure();
5418 1018 : LD_CONDITION(ldata->isSetPos());
5419 1018 : LD_CONDITION(m->ldata()->isSetPos());
5420 1018 : LD_CONDITION(s->ldata()->isSetPos());
5421 : }
5422 :
5423 1018 : Autoplace::autoplaceSegmentElement(item, ldata);
5424 :
5425 1018 : if (SoundFlag* flag = item->soundFlag()) {
5426 9 : layoutSoundFlag(flag, flag->mutldata());
5427 : }
5428 1018 : }
5429 :
5430 87 : void TLayout::layoutStaveSharingLabel(const StaveSharingLabel* item, TextBase::LayoutData* ldata)
5431 : {
5432 : LAYOUT_CALL_ITEM(item);
5433 87 : TextLayout::layoutBaseTextBase(item, ldata);
5434 87 : Autoplace::autoplaceSegmentElement(item, ldata);
5435 87 : }
5436 :
5437 19 : void TLayout::layoutStaffTypeChange(const StaffTypeChange* item, StaffTypeChange::LayoutData* ldata, const LayoutConfiguration& conf)
5438 : {
5439 : LAYOUT_CALL_ITEM(item);
5440 : LD_INDEPENDENT;
5441 :
5442 19 : double spatium = conf.spatium();
5443 19 : ldata->setBbox(RectF(-item->lw() * .5, -item->lw() * .5, spatium * 2.5 + item->lw(), spatium * 2.5 + item->lw()));
5444 19 : if (item->measure()) {
5445 19 : double y = -1.5 * spatium - ldata->bbox().height() + item->measure()->system()->staff(item->staffIdx())->y();
5446 19 : ldata->setPos(spatium * .8, y);
5447 : } else {
5448 0 : ldata->setPos(0.0, 0.0);
5449 : }
5450 19 : }
5451 :
5452 230250 : void TLayout::layoutStem(const Stem* item, Stem::LayoutData* ldata, const LayoutConfiguration& conf)
5453 : {
5454 : LAYOUT_CALL_ITEM(item);
5455 :
5456 230250 : const bool up = item->up();
5457 230250 : const double _up = up ? -1.0 : 1.0;
5458 :
5459 : //Note* note = up ? item->chord()->downNote() : item->chord()->upNote();
5460 : //! NOTE A lot of spam
5461 : // LD_CONDITION(note->ldata()->mirror.has_value());
5462 :
5463 230250 : double y1 = 0.0; // vertical displacement to match note attach point
5464 230250 : double y2 = _up * (item->length());
5465 :
5466 230250 : bool isTabStaff = false;
5467 230250 : if (item->chord()) {
5468 230250 : ldata->setMag(item->chord()->mag());
5469 :
5470 230250 : const Staff* staff = item->staff();
5471 230250 : const StaffType* staffType = staff ? staff->staffTypeForElement(item->chord()) : nullptr;
5472 230250 : isTabStaff = staffType && staffType->isTabStaff();
5473 :
5474 230250 : if (isTabStaff) {
5475 501 : if (staffType->stemThrough()) {
5476 : // if stems through staves, gets Y pos. of stem-side note relative to chord other side
5477 105 : const double staffLinesDistance = staffType->lineDistance().val() * item->spatium();
5478 105 : y1 = (item->chord()->downString() - item->chord()->upString()) * _up * staffLinesDistance;
5479 :
5480 : // if fret marks above lines, raise stem beginning by 1/2 line distance
5481 105 : if (!staffType->onLines()) {
5482 0 : y1 -= staffLinesDistance * 0.5;
5483 : }
5484 :
5485 : // shorten stem by 1/2 lineDist to clear the note and a little more to keep 'air' between stem and note
5486 105 : y1 += _up * staffLinesDistance * 0.7;
5487 : }
5488 : // in other TAB types, no correction
5489 : } else { // non-TAB
5490 : // move stem start to note attach point
5491 229749 : Note* note = up ? item->chord()->downNote() : item->chord()->upNote();
5492 229749 : if ((up && !note->ldata()->mirror.value(LD_ACCESS::BAD)) || (!up && note->ldata()->mirror.value(LD_ACCESS::BAD))) {
5493 124686 : y1 = note->stemUpSE().y();
5494 : } else {
5495 105063 : y1 = note->stemDownNW().y();
5496 : }
5497 :
5498 229749 : ldata->setPosY(note->ldata()->pos().y());
5499 : }
5500 :
5501 230250 : const Hook* hook = item->chord()->hook();
5502 230250 : const Beam* beam = item->chord()->beam();
5503 230250 : if (hook && !beam) {
5504 5837 : y2 += hook->smuflAnchor().y();
5505 : }
5506 :
5507 230250 : if (beam) {
5508 123133 : y2 -= _up * beam->absoluteFromSpatium(conf.styleS(Sid::beamWidth)) * .5 * beam->mag();
5509 : }
5510 : }
5511 :
5512 230250 : double lineWidthCorrection = item->lineWidthMag() * 0.5;
5513 230250 : double lineX = isTabStaff ? 0.0 : _up * lineWidthCorrection;
5514 :
5515 230250 : LineF line = LineF(lineX, y1, lineX, y2);
5516 230250 : ldata->line = line;
5517 :
5518 : // compute line and bounding rectangle
5519 230250 : RectF rect(line.p1(), line.p2() + PointF(0.0, ldata->beamCorrection));
5520 230250 : ldata->setBbox(rect.normalized().adjusted(-lineWidthCorrection, 0, lineWidthCorrection, 0));
5521 230250 : }
5522 :
5523 841 : void TLayout::layoutStemSlash(const StemSlash* item, StemSlash::LayoutData* ldata, const LayoutConfiguration& conf)
5524 : {
5525 : LAYOUT_CALL_ITEM(item);
5526 841 : IF_ASSERT_FAILED(item->ownershipParent()) {
5527 0 : return;
5528 : }
5529 :
5530 841 : const Chord* c = item->chord();
5531 841 : const Stem* stem = c->stem();
5532 841 : const Hook* hook = c->hook();
5533 841 : const Beam* beam = c->beam();
5534 :
5535 841 : LD_CONDITION(stem->ldata()->isSetPos());
5536 841 : LD_CONDITION(stem->ldata()->isSetBbox());
5537 : // Notes without a stem (including whole notes) don't draw a slash.
5538 :
5539 : static constexpr double heightReduction = 0.66;
5540 : static constexpr double angleIncrease = 1.2;
5541 : static constexpr double lengthIncrease = 1.1;
5542 841 : const double mag = c->mag();
5543 :
5544 841 : double up = c->up() ? -1 : 1;
5545 841 : double stemTipY = c->up()
5546 841 : ? stem->ldata()->bbox().translated(stem->pos()).top()
5547 9 : : stem->ldata()->bbox().translated(stem->pos()).bottom();
5548 841 : double leftHang = conf.noteHeadWidth() * mag / 2;
5549 841 : double angle = conf.styleD(Sid::stemSlashAngle) * M_PI / 180; // converting to radians
5550 841 : bool straight = conf.styleB(Sid::useStraightNoteFlags);
5551 841 : double graceNoteMag = mag;
5552 :
5553 841 : double startX = stem->ldata()->bbox().translated(stem->pos()).right() - leftHang;
5554 841 : double startY = stemTipY - up * graceNoteMag * conf.styleAbsolute(Sid::stemSlashPosition)
5555 841 : * (straight || !hook ? heightReduction : 1);
5556 :
5557 841 : double endX = 0;
5558 841 : double endY = 0;
5559 :
5560 841 : if (hook) {
5561 472 : auto musicFont = conf.styleSt(Sid::musicalSymbolFont);
5562 : // HACK: adjust slash angle for fonts with "fat" hooks. In future, we must use smufl cutOut
5563 507 : if (c->beams() >= 2 && !straight
5564 507 : && (musicFont == "Bravura" || musicFont == "Finale Maestro" || musicFont == "Gonville")) {
5565 0 : angle *= angleIncrease;
5566 : }
5567 472 : endX = hook->ldata()->bbox().translated(hook->ldata()->pos()).right(); // always ends at the right bbox margin of the hook
5568 472 : endY = startY + up * (endX - startX) * tan(angle);
5569 841 : } else if (beam) {
5570 225 : PointF p1 = beam->startAnchor();
5571 225 : PointF p2 = beam->endAnchor();
5572 225 : double beamAngle = p2.x() > p1.x() ? atan((p2.y() - p1.y()) / (p2.x() - p1.x())) : 0;
5573 225 : angle += up * beamAngle / 2;
5574 225 : double length = 2 * item->spatium();
5575 225 : bool obtuseAngle = (c->up() && beamAngle < 0) || (!c->up() && beamAngle > 0);
5576 225 : if (obtuseAngle) {
5577 120 : length *= lengthIncrease; // needs to be slightly longer
5578 : }
5579 225 : endX = startX + length * cos(angle);
5580 225 : endY = startY + up * length * sin(angle);
5581 : } else {
5582 144 : double rightHang = (conf.noteHeadWidth() * mag / 2) - stem->width(); // subtract the stem width so the slash is optically centered on the stem
5583 144 : endX = stem->ldata()->bbox().translated(stem->pos()).right() + rightHang;
5584 144 : endY = startY + up * (endX - startX) * tan(angle);
5585 : }
5586 :
5587 841 : ldata->line = LineF(PointF(startX, startY), PointF(endX, endY));
5588 841 : ldata->stemWidth = conf.styleAbsolute(Sid::stemSlashThickness) * graceNoteMag;
5589 :
5590 841 : RectF bbox = RectF(ldata->line.p1(), ldata->line.p2()).normalized();
5591 841 : bbox = bbox.adjusted(-ldata->stemWidth / 2, -ldata->stemWidth / 2, ldata->stemWidth, ldata->stemWidth);
5592 841 : ldata->setBbox(bbox);
5593 : }
5594 :
5595 131 : void TLayout::layoutSticking(const Sticking* item, Sticking::LayoutData* ldata)
5596 : {
5597 : LAYOUT_CALL_ITEM(item);
5598 131 : TextLayout::layoutBaseTextBase(item, ldata);
5599 :
5600 131 : if (item->autoplace() && item->ownershipParent()) {
5601 131 : const Segment* s = toSegment(item->ownershipParent());
5602 131 : const Measure* m = s->measure();
5603 131 : LD_CONDITION(ldata->isSetPos());
5604 131 : LD_CONDITION(m->ldata()->isSetPos());
5605 131 : LD_CONDITION(s->ldata()->isSetPos());
5606 : }
5607 :
5608 131 : Autoplace::autoplaceSegmentElement(item, ldata);
5609 131 : }
5610 :
5611 19 : void TLayout::layoutStringTunings(StringTunings* item, LayoutContext& ctx)
5612 : {
5613 : LAYOUT_CALL_ITEM(item);
5614 19 : item->updateText();
5615 :
5616 19 : TextLayout::layoutBaseTextBase(item, ctx);
5617 :
5618 19 : double spatium = item->spatium();
5619 :
5620 19 : if (item->noStringVisible()) {
5621 19 : Font font(item->font());
5622 :
5623 19 : RectF rect;
5624 19 : rect.setTopLeft({ 0, item->ldata()->bbox().y() - 2.5 * spatium });
5625 19 : rect.setSize({ spatium, 2.5 * spatium });
5626 :
5627 19 : item->setbbox(rect);
5628 19 : }
5629 :
5630 38 : for (TextBlock& block : item->mutldata()->blocks) {
5631 19 : for (TextFragment& fragment : block.fragments()) {
5632 0 : Font font = fragment.font(item);
5633 0 : if (font.type() == Font::Type::MusicSymbol) {
5634 : // HACK: the music symbol doesn't have a good baseline
5635 : // to go with text so we correct it here
5636 0 : const double baselineAdjustment = 0.35 * spatium * item->symbolScale();
5637 0 : fragment.pos.setY(fragment.pos.y() + baselineAdjustment);
5638 : }
5639 0 : }
5640 : }
5641 :
5642 19 : double secondStringXAlign = 0.0;
5643 19 : for (const TextFragment& fragment : item->fragmentList()) {
5644 0 : if (fragment.font(item).type() == Font::Type::MusicSymbol) {
5645 0 : secondStringXAlign = std::max(secondStringXAlign, fragment.pos.x());
5646 : }
5647 19 : }
5648 :
5649 38 : for (TextBlock& block : item->mutldata()->blocks) {
5650 19 : double xMove = 0.0;
5651 19 : for (TextFragment& fragment : block.fragments()) {
5652 0 : if (block.fragments().front() == fragment) { // skip first
5653 0 : continue;
5654 : }
5655 :
5656 0 : if (fragment.font(item).type() == Font::Type::MusicSymbol) {
5657 0 : xMove = secondStringXAlign - fragment.pos.x();
5658 : }
5659 0 : fragment.pos.setX(fragment.pos.x() + xMove);
5660 : }
5661 : }
5662 :
5663 19 : Segment* parentSegment = item->segment();
5664 19 : item->move(PointF(-parentSegment->x() + item->spatium(), 0.0));
5665 :
5666 19 : Autoplace::autoplaceSegmentElement(item, item->mutldata());
5667 19 : }
5668 :
5669 9 : void TLayout::layoutSoundFlag(const SoundFlag* item, SoundFlag::LayoutData* ldata)
5670 : {
5671 : LAYOUT_CALL_ITEM(item);
5672 :
5673 9 : const Score* score = item->score();
5674 9 : if (score && !score->showSoundFlags()) {
5675 0 : return;
5676 : }
5677 :
5678 9 : const EngravingItem* parent = item->ownershipParentItem();
5679 9 : if (!parent) {
5680 0 : return;
5681 : }
5682 :
5683 9 : double spatium = item->spatium();
5684 9 : double iconHeight = spatium * 3.0;
5685 :
5686 9 : RectF parentBbox = parent->ldata()->bbox();
5687 9 : RectF iconBBox = RectF(parentBbox.x(), parentBbox.y(), iconHeight, iconHeight);
5688 :
5689 9 : iconBBox.moveCenter(parentBbox.center());
5690 :
5691 : // <icon><space><text>
5692 9 : double space = spatium / 2.0;
5693 9 : iconBBox.setX(parentBbox.x() - iconBBox.width() - space);
5694 :
5695 9 : ldata->setBbox(iconBBox);
5696 : }
5697 :
5698 72 : void TLayout::layoutSymbol(const Symbol* item, Symbol::LayoutData* ldata, const LayoutContext& ctx)
5699 : {
5700 : LAYOUT_CALL_ITEM(item);
5701 72 : IF_ASSERT_FAILED(item->ownershipParent()) {
5702 0 : return;
5703 : }
5704 :
5705 : LD_INDEPENDENT;
5706 :
5707 72 : double expectedMag = item->staff() ? item->staff()->staffMag(item->tick()) : ldata->mag();
5708 :
5709 72 : if (ldata->isValid() && RealIsEqual(ldata->mag(), expectedMag)) {
5710 0 : return;
5711 : }
5712 :
5713 72 : if (item->staff()) {
5714 50 : ldata->setMag(expectedMag);
5715 : }
5716 144 : ldata->setBbox(item->scoreFont()
5717 72 : ? item->scoreFont()->bbox(item->sym(), item->magS() * item->symbolsSize())
5718 31 : : item->symBbox(item->sym()));
5719 72 : double w = ldata->bbox().width();
5720 72 : PointF p;
5721 72 : if (item->align() == AlignV::BOTTOM) {
5722 0 : p.setY(-ldata->bbox().height());
5723 72 : } else if (item->align() == AlignV::VCENTER) {
5724 0 : p.setY((-ldata->bbox().height()) * .5);
5725 72 : } else if (item->align() == AlignV::BASELINE) {
5726 72 : p.setY(-item->baseLine());
5727 : }
5728 72 : if (item->align() == AlignH::RIGHT) {
5729 0 : p.setX(-w);
5730 72 : } else if (item->align() == AlignH::HCENTER) {
5731 0 : p.setX(-(w * .5));
5732 : }
5733 72 : ldata->setPos(p);
5734 :
5735 : // see BSymbol::add
5736 72 : for (EngravingItem* e : item->leafs()) {
5737 0 : switch (e->type()) {
5738 0 : case ElementType::SYMBOL: {
5739 0 : Symbol* s = item_cast<Symbol*>(e);
5740 0 : layoutSymbol(s, s->mutldata(), ctx);
5741 0 : } break;
5742 0 : case ElementType::IMAGE: {
5743 0 : Image* im = item_cast<Image*>(e);
5744 0 : layoutImage(im, im->mutldata());
5745 0 : } break;
5746 0 : default:
5747 0 : UNREACHABLE;
5748 : break;
5749 : }
5750 : }
5751 : }
5752 :
5753 0 : void TLayout::layoutFSymbol(const FSymbol* item, FSymbol::LayoutData* ldata)
5754 : {
5755 : LAYOUT_CALL_ITEM(item);
5756 : LD_INDEPENDENT;
5757 0 : if (ldata->isValid()) {
5758 0 : return;
5759 : }
5760 :
5761 0 : ldata->setBbox(FontMetrics::boundingRect(item->font(), item->toString()));
5762 : }
5763 :
5764 28 : void TLayout::layoutSystemDivider(const SystemDivider* item, SystemDivider::LayoutData* ldata, const LayoutContext& ctx)
5765 : {
5766 : LAYOUT_CALL_ITEM(item);
5767 : LD_INDEPENDENT;
5768 28 : if (ldata->isValid()) {
5769 6 : return;
5770 : }
5771 :
5772 22 : layoutSymbol(item, ldata, ctx);
5773 : }
5774 :
5775 125 : void TLayout::layoutSystemText(const SystemText* item, SystemText::LayoutData* ldata)
5776 : {
5777 : LAYOUT_CALL_ITEM(item);
5778 125 : TextLayout::layoutBaseTextBase(item, ldata);
5779 :
5780 125 : if (item->autoplace() && item->ownershipParent()) {
5781 125 : const Segment* s = toSegment(item->ownershipParent());
5782 125 : const Measure* m = s->measure();
5783 125 : LD_CONDITION(ldata->isSetPos());
5784 125 : LD_CONDITION(m->ldata()->isSetPos());
5785 125 : LD_CONDITION(s->ldata()->isSetPos());
5786 : }
5787 :
5788 125 : Autoplace::autoplaceSegmentElement(item, ldata);
5789 125 : }
5790 :
5791 24 : void TLayout::layoutTabDurationSymbol(const TabDurationSymbol* item, TabDurationSymbol::LayoutData* ldata)
5792 : {
5793 : LAYOUT_CALL_ITEM(item);
5794 : LD_INDEPENDENT;
5795 :
5796 : static constexpr Spatium TAB_RESTSYMBDISPL = 2.0_sp;
5797 :
5798 24 : if (!item->tab()) {
5799 0 : ldata->setBbox(RectF());
5800 0 : return;
5801 : }
5802 24 : double spatium = item->spatium();
5803 : double hbb, wbb, xbb, ybb; // bbox sizes
5804 : double xpos, ypos; // position coords
5805 :
5806 24 : ldata->beamGrid = TabBeamGrid::NONE;
5807 24 : ldata->beamLength = 0.0;
5808 24 : ldata->beamLevel = 0;
5809 24 : Chord* chord = item->ownershipParent() && item->ownershipParent()->isChord() ? toChord(item->ownershipParent()) : nullptr;
5810 : // A grid replaces the individual duration signs. Only connect equal, undotted
5811 : // values; otherwise retain the signs rather than lose their rhythm information.
5812 15 : auto connects = [](const ChordRest* left, const ChordRest* right) {
5813 15 : if (!left || !right || !left->isChord() || !right->isChord()) {
5814 4 : return false;
5815 : }
5816 22 : const bool regularChords = !left->isGrace() && !right->isGrace()
5817 22 : && !toChord(left)->noStem() && !toChord(right)->noStem();
5818 22 : const bool sameContext = left->measure() == right->measure() && left->staffType() == right->staffType()
5819 22 : && left->tuplet() == right->tuplet();
5820 11 : const bool equalUndottedDurations = left->durationType() == right->durationType() && !left->dots() && !right->dots();
5821 11 : const bool leftContinues = left->beamMode() == BeamMode::BEGIN || left->beamMode() == BeamMode::MID;
5822 11 : const bool rightConnects = right->beamMode() == BeamMode::MID || right->beamMode() == BeamMode::END;
5823 11 : return regularChords && sameContext && equalUndottedDurations && leftContinues && rightConnects;
5824 : };
5825 24 : const int beamLevel = chord ? static_cast<int>(chord->durationType().type())
5826 24 : - static_cast<int>(item->tab()->tabDurationFont().zeroBeamLevel) : 0;
5827 22 : const bool joinsPrevious = beamLevel > 0 && (chord->beamMode() == BeamMode::MID || chord->beamMode() == BeamMode::END)
5828 46 : && connects(Navigation::prevChordRest(chord), chord);
5829 22 : const bool joinsNext = beamLevel > 0 && (chord->beamMode() == BeamMode::BEGIN || chord->beamMode() == BeamMode::MID)
5830 46 : && connects(chord, Navigation::nextChordRest(chord));
5831 : // if no chord (shouldn't happens...) or not a special beam mode, layout regular symbol
5832 24 : if (beamLevel < 1 || (!joinsPrevious && !joinsNext)) {
5833 16 : FontMetrics fm(item->tab()->durationFont());
5834 16 : hbb = item->tab()->durationBoxH();
5835 16 : wbb = fm.width(item->text());
5836 16 : xbb = 0.0;
5837 16 : xpos = 0.0;
5838 16 : ypos = item->tab()->durationFontYOffset();
5839 16 : ybb = item->tab()->durationBoxY() - ypos;
5840 : // with rests, move symbol down by half its displacement from staff
5841 16 : if (item->ownershipParent() && item->ownershipParent()->isRest()) {
5842 0 : ybb += TAB_RESTSYMBDISPL.toAbsolute(spatium);
5843 0 : ypos += TAB_RESTSYMBDISPL.toAbsolute(spatium);
5844 : }
5845 16 : }
5846 : // if on a chord with special beam mode, layout an 'English'-style duration grid
5847 : else {
5848 8 : const TablatureDurationFont& font = item->tab()->tabDurationFont();
5849 8 : hbb = font.gridStemHeight.toAbsolute(spatium); // bbox height is stem height
5850 8 : wbb = font.gridStemWidth.toAbsolute(spatium); // bbox width is stem width
5851 8 : xbb = -wbb * 0.5; // bbox is half at left and half at right of stem centre
5852 8 : ybb = -hbb; // bbox top is at top of stem height
5853 8 : xpos = 0.75 * spatium; // conventional centring of stem on fret marks
5854 8 : ypos = item->tab()->durationGridYOffset(); // stem start is at bottom
5855 8 : ldata->beamLevel = beamLevel;
5856 8 : ldata->beamGrid = joinsPrevious ? TabBeamGrid::MEDIALFINAL : TabBeamGrid::INITIAL;
5857 : }
5858 : // set this' mag from parent chord mag (include staff mag)
5859 24 : double mag = chord != nullptr ? chord->mag() : 1.0;
5860 24 : ldata->setMag(mag);
5861 24 : mag = item->magS(); // local mag * score mag
5862 : // set magnified bbox and position
5863 24 : ldata->setBbox(xbb * mag, ybb * mag, wbb * mag, hbb * mag);
5864 24 : ldata->setPos(xpos * mag, ypos * mag);
5865 : }
5866 :
5867 24 : void TLayout::layoutTabDurationSymbol2(const TabDurationSymbol* item, TabDurationSymbol::LayoutData* ldata)
5868 : {
5869 24 : if (ldata->beamGrid != TabBeamGrid::MEDIALFINAL) {
5870 20 : return;
5871 : }
5872 4 : const ChordRest* previous = Navigation::prevChordRest(toChord(item->ownershipParent()));
5873 4 : if (!previous || !previous->tabDur()) {
5874 0 : return;
5875 : }
5876 : // Horizontal spacing is now final. Drawing applies the symbol magnification again.
5877 4 : const double length = previous->tabDur()->pagePos().x() - item->pagePos().x();
5878 4 : ldata->beamLength = length / item->magS();
5879 4 : RectF bbox = ldata->bbox();
5880 4 : bbox.setLeft(std::min(bbox.left(), length));
5881 4 : ldata->setBbox(bbox);
5882 : }
5883 :
5884 134 : void TLayout::layoutTapping(Tapping* item, Tapping::LayoutData* ldata, LayoutContext& ctx)
5885 : {
5886 134 : TappingLayout::layoutTapping(item, ldata, ctx);
5887 134 : }
5888 :
5889 0 : void TLayout::layoutTappingHalfSlur(TappingHalfSlur* item)
5890 : {
5891 : UNUSED(item)
5892 0 : }
5893 :
5894 1194 : void TLayout::layoutTempoText(const TempoText* item, TempoText::LayoutData* ldata)
5895 : {
5896 : LAYOUT_CALL_ITEM(item);
5897 1194 : IF_ASSERT_FAILED(item->ownershipParent()) {
5898 0 : return;
5899 : }
5900 :
5901 1194 : ldata->disconnectSnappedItems();
5902 :
5903 1194 : TextLayout::layoutBaseTextBase(item, ldata);
5904 :
5905 1194 : if (!item->autoplace()) {
5906 0 : return;
5907 : }
5908 :
5909 : // tempo text on first chordrest of measure should align over time sig if present, unless time sig is above staff
5910 1194 : Segment* s = item->segment();
5911 :
5912 1194 : RehearsalMark* rehearsalMark = toRehearsalMark(s->findAnnotation(ElementType::REHEARSAL_MARK, item->track(), item->track()));
5913 1194 : RectF rehearsalMarkBbox = rehearsalMark ? rehearsalMark->ldata()->bbox().translated(rehearsalMark->pos()) : RectF();
5914 1194 : RectF thisBbox = ldata->bbox().translated(item->pos());
5915 :
5916 1194 : if (s->rtick().isZero()) {
5917 1174 : Segment* p = item->segment()->prev(SegmentType::TimeSig);
5918 1174 : if (p && !p->allElementsInvisible()) {
5919 932 : ldata->moveX(-(s->x() - p->x()));
5920 932 : EngravingItem* e = p->element(item->staffIdx() * VOICES);
5921 932 : if (e) {
5922 932 : ldata->moveX(p->hasTimeSigAboveStaves() ? e->x() + e->width() + e->spatium() : e->x());
5923 : }
5924 : }
5925 : }
5926 :
5927 1194 : if (rehearsalMark) {
5928 10 : const bool sameSide = item->placeAbove() == rehearsalMark->placeAbove();
5929 : const bool collision
5930 10 : = item->placeAbove() ? muse::RealIsEqualOrMore(rehearsalMarkBbox.bottom(), thisBbox.top()) : muse::RealIsEqualOrLess(
5931 10 : rehearsalMarkBbox.top(), thisBbox.bottom());
5932 :
5933 10 : if (sameSide && collision && item->getProperty(Pid::TEMPO_ALIGN_RIGHT_OF_REHEARSAL_MARK).toBool()) {
5934 10 : double rightEdge = rehearsalMarkBbox.right();
5935 10 : const double fontSizeScaleFactor = item->size() / 10.0;
5936 10 : const double padding = 0.5 * item->spatium() * fontSizeScaleFactor;
5937 10 : double curX = ldata->pos().x();
5938 10 : ldata->setPosX(std::max(curX, rightEdge + padding));
5939 : }
5940 : }
5941 1194 : Autoplace::autoplaceSegmentElement(item, ldata);
5942 : }
5943 :
5944 11 : Shape TLayout::recalculateTextLineBaseSegmentShape(const TextLineBaseSegment* item)
5945 : {
5946 : LAYOUT_CALL_ITEM(item);
5947 11 : const TextLineBaseSegment::LayoutData* ldata = item->ldata();
5948 11 : Shape shape;
5949 11 : if (!item->text()->empty()) {
5950 11 : shape.add(item->text()->ldata()->bbox().translated(item->text()->pos()), item->text());
5951 : }
5952 11 : if (!item->endText()->empty()) {
5953 11 : shape.add(item->endText()->ldata()->bbox().translated(item->endText()->pos()), item->endText());
5954 : }
5955 11 : double lw2 = 0.5 * item->absoluteFromSpatium(item->lineWidth());
5956 11 : bool isDottedLine = item->textLineBase()->lineStyle() == LineType::DOTTED;
5957 11 : if (item->twoLines()) { // hairpins
5958 0 : shape.add(TextLineBaseSegment::boundingBoxOfLine(ldata->points[0], ldata->points[1], lw2, isDottedLine), item);
5959 0 : shape.add(TextLineBaseSegment::boundingBoxOfLine(ldata->points[2], ldata->points[3], lw2, isDottedLine), item);
5960 : } else {
5961 22 : for (int i = 0; i < ldata->npoints - 1; ++i) {
5962 11 : shape.add(TextLineBaseSegment::boundingBoxOfLine(ldata->points[i], ldata->points[i + 1], lw2, isDottedLine), item,
5963 11 : !item->textLineBase()->lineVisible());
5964 : }
5965 : }
5966 11 : return shape;
5967 0 : }
5968 :
5969 16125 : void TLayout::layoutText(const Text* item, Text::LayoutData* ldata)
5970 : {
5971 : LAYOUT_CALL_ITEM(item);
5972 16125 : if (item->layoutParent() && item->layoutToParentWidth()) {
5973 11774 : LD_CONDITION(item->layoutParent()->ldata()->isSetBbox());
5974 : }
5975 :
5976 16125 : TextLayout::layoutBaseTextBase(item, ldata);
5977 16125 : }
5978 :
5979 0 : void TLayout::layoutTextLine(TextLine* item, LayoutContext& ctx)
5980 : {
5981 : LAYOUT_CALL_ITEM(item);
5982 0 : layoutLine(item, ctx);
5983 0 : }
5984 :
5985 55 : void TLayout::layoutTextLineSegment(TextLineSegment* item, LayoutContext& ctx)
5986 : {
5987 : LAYOUT_CALL_ITEM(item);
5988 55 : TextLineSegment::LayoutData* ldata = item->mutldata();
5989 55 : layoutTextLineBaseSegment(item, ctx);
5990 :
5991 55 : Autoplace::autoplaceSpannerSegment(item, ldata);
5992 55 : }
5993 :
5994 : // Extends lines to fill the corner between them.
5995 : // Assumes that l1p2 == l2p1 is the intersection between the lines.
5996 : // If checkAngle is false, assumes that the lines are perpendicular,
5997 : // and some calculations are saved.
5998 147 : static inline void extendLines(const PointF& l1p1, PointF& l1p2, PointF& l2p1, const PointF& l2p2, double lineWidth, bool checkAngle)
5999 : {
6000 147 : PointF l1UnitVector = (l1p2 - l1p1).normalized();
6001 147 : PointF l2UnitVector = (l2p1 - l2p2).normalized();
6002 :
6003 147 : double addedLength = lineWidth * 0.5;
6004 :
6005 147 : if (checkAngle) {
6006 0 : double angle = M_PI - acos(PointF::dotProduct(l1UnitVector, l2UnitVector));
6007 :
6008 0 : if (angle <= M_PI_2) {
6009 0 : addedLength *= tan(0.5 * angle);
6010 : }
6011 : }
6012 :
6013 147 : l1p2 += l1UnitVector * addedLength;
6014 147 : l2p1 += l2UnitVector * addedLength;
6015 147 : }
6016 :
6017 0 : static PolygonF createArrow(bool start, bool filled, PointF& startPoint, PointF& endPoint, const TextLineBase* tl)
6018 : {
6019 0 : double arrowWidth = 0.0;
6020 0 : double arrowHeight = 0.0;
6021 0 : if (filled) {
6022 0 : arrowWidth = tl->absoluteFromSpatium(start ? tl->beginFilledArrowWidth() : tl->endFilledArrowWidth());
6023 0 : arrowHeight = tl->absoluteFromSpatium(start ? tl->beginFilledArrowHeight() : tl->endFilledArrowHeight());
6024 : } else {
6025 0 : arrowWidth = tl->absoluteFromSpatium(start ? tl->beginLineArrowWidth() : tl->endLineArrowWidth());
6026 0 : arrowHeight = tl->absoluteFromSpatium(start ? tl->beginLineArrowHeight() : tl->endLineArrowHeight());
6027 : }
6028 :
6029 0 : PolygonF arrow;
6030 0 : if (start) {
6031 0 : arrow << PointF(0.0, -arrowHeight / 2) << PointF(-arrowWidth, 0.0) << PointF(0.0, arrowHeight / 2); // left
6032 : } else {
6033 0 : arrow << PointF(0.0, -arrowHeight / 2) << PointF(arrowWidth, 0.0) << PointF(0.0, arrowHeight / 2); // right
6034 : }
6035 :
6036 0 : PointF arrowAdjust = PointF(arrowWidth, 0.0);
6037 0 : arrowAdjust = (start ? 1.0 : -1.0) * arrowAdjust;
6038 0 : arrow.translate(arrowAdjust);
6039 :
6040 0 : const double yDiff = endPoint.y() - startPoint.y();
6041 0 : const double xDiff = endPoint.x() - startPoint.x();
6042 0 : const double rotate = atan(yDiff / xDiff) + (endPoint.x() < startPoint.x() ? M_PI : 0.0);
6043 :
6044 0 : Transform t;
6045 0 : t.rotateRadians(rotate);
6046 :
6047 0 : for (PointF& p : arrow) {
6048 0 : p = t.map(p);
6049 : }
6050 :
6051 0 : if (start) {
6052 0 : arrow.translate(startPoint);
6053 : } else {
6054 0 : arrow.translate(endPoint);
6055 : }
6056 :
6057 0 : const PointF lineVector = endPoint - startPoint;
6058 0 : const PointF unitVector = lineVector.normalized();
6059 0 : const double reduction = filled ? arrowWidth : tl->absoluteFromSpatium(tl->lineWidth()) / 2;
6060 0 : if (start) {
6061 0 : startPoint += reduction * unitVector;
6062 : } else {
6063 0 : endPoint -= reduction * unitVector;
6064 : }
6065 :
6066 0 : return arrow;
6067 0 : }
6068 :
6069 0 : void TLayout::layoutTextLineBase(TextLineBase* item, LayoutContext& ctx)
6070 : {
6071 0 : layoutLine(item, ctx);
6072 0 : }
6073 :
6074 1405 : void TLayout::layoutTextLineBaseSegment(TextLineBaseSegment* item, LayoutContext& ctx)
6075 : {
6076 1405 : TextLineBaseSegment::LayoutData* ldata = item->mutldata();
6077 1405 : ldata->npoints = 0;
6078 1405 : const TextLineBase* tl = item->textLineBase();
6079 1405 : const double spatium = tl->spatium();
6080 1405 : const bool isSingleOrBegin = item->isSingleBeginType();
6081 1405 : const bool isSingleOrEnd = item->isSingleEndType();
6082 1405 : const bool isDottedLine = tl->lineStyle() == LineType::DOTTED;
6083 1405 : const double lineWidth = tl->absoluteFromSpatium(tl->lineWidth());
6084 :
6085 1405 : if (item->spanner()->placeBelow()) {
6086 594 : ldata->setPosY(item->staff() ? item->staff()->staffHeight(item->tick()) : 0.0);
6087 : }
6088 :
6089 : // adjust Y pos to staffType offset
6090 1405 : ldata->moveY(item->staffOffsetY());
6091 :
6092 1405 : if (!tl->diagonal()) {
6093 1388 : item->setUserYoffset2(0);
6094 : }
6095 :
6096 438 : auto alignBaseLine = [lineWidth](Text* text, PointF& pp1, PointF& pp2) {
6097 438 : PointF widthCorrection(0.0, lineWidth / 2);
6098 438 : switch (text->align().vertical) {
6099 117 : case AlignV::TOP:
6100 117 : pp1 += widthCorrection;
6101 117 : pp2 += widthCorrection;
6102 117 : break;
6103 136 : case AlignV::VCENTER:
6104 136 : break;
6105 0 : case AlignV::BOTTOM:
6106 0 : pp1 -= widthCorrection;
6107 0 : pp2 -= widthCorrection;
6108 0 : break;
6109 185 : case AlignV::BASELINE:
6110 185 : pp1 -= widthCorrection;
6111 185 : pp2 -= widthCorrection;
6112 185 : break;
6113 : }
6114 438 : };
6115 :
6116 1405 : if (isSingleOrBegin) {
6117 1382 : item->text()->setXmlText(tl->beginText());
6118 1382 : item->text()->setFamily(tl->beginFontFamily());
6119 1382 : item->text()->setSize(tl->beginFontSize());
6120 1382 : item->text()->setSymbolScale(tl->beginTextMusicalSymbolsScale());
6121 1382 : item->text()->setSymbolSize(tl->beginTextMusicSymbolsSize());
6122 1382 : item->text()->setOffset(tl->beginTextOffset() * item->mag());
6123 1382 : item->text()->setAlign(tl->beginTextAlign());
6124 1382 : item->text()->setPosition(tl->beginTextPosition());
6125 1382 : item->text()->setFontStyle(tl->beginFontStyle());
6126 : } else {
6127 23 : item->text()->setXmlText(tl->continueText());
6128 23 : item->text()->setFamily(tl->continueFontFamily());
6129 23 : item->text()->setSize(tl->continueFontSize());
6130 23 : item->text()->setSymbolScale(tl->continueTextMusicalSymbolsScale());
6131 23 : item->text()->setSymbolSize(tl->continueTextMusicSymbolsSize());
6132 23 : item->text()->setOffset(tl->continueTextOffset() * item->mag());
6133 23 : item->text()->setAlign(tl->continueTextAlign());
6134 23 : item->text()->setPosition(tl->continueTextPosition());
6135 23 : item->text()->setFontStyle(tl->continueFontStyle());
6136 : }
6137 1405 : item->text()->setPlacement(PlacementV::ABOVE);
6138 1405 : item->text()->setTrack(item->track());
6139 1405 : item->text()->setColor(tl->lineColor());
6140 1405 : layoutText(item->text(), item->text()->mutldata());
6141 :
6142 1405 : if (isSingleOrEnd) {
6143 1380 : item->endText()->setXmlText(tl->endText());
6144 1380 : item->endText()->setFamily(tl->endFontFamily());
6145 1380 : item->endText()->setSize(tl->endFontSize());
6146 1380 : item->endText()->setSymbolScale(tl->endTextMusicalSymbolsScale());
6147 1380 : item->endText()->setSymbolSize(tl->endTextMusicSymbolsSize());
6148 1380 : item->endText()->setOffset(tl->endTextOffset() * item->mag());
6149 1380 : item->endText()->setAlign(tl->endTextAlign());
6150 1380 : item->endText()->setPosition(tl->endTextPosition());
6151 1380 : item->endText()->setFontStyle(tl->endFontStyle());
6152 1380 : item->endText()->setPlacement(PlacementV::ABOVE);
6153 1380 : item->endText()->setTrack(item->track());
6154 1380 : item->endText()->setColor(tl->lineColor());
6155 1380 : layoutText(item->endText(), item->endText()->mutldata());
6156 : } else {
6157 25 : item->endText()->setXmlText(u"");
6158 : }
6159 :
6160 1405 : if (!item->textLineBase()->textSizeSpatiumDependent()) {
6161 0 : item->text()->setSize(item->text()->size() * item->defaultSpatium() / item->spatium());
6162 0 : item->endText()->setSize(item->endText()->size() * item->defaultSpatium() / item->spatium());
6163 : }
6164 :
6165 1405 : PointF defaultPos = tl->defaultPos();
6166 1405 : ldata->move(defaultPos);
6167 :
6168 1405 : PointF pp1;
6169 1405 : PointF pp2(item->pos2());
6170 :
6171 : // line with no text or hooks - just use the basic rectangle for line
6172 1910 : if (item->text()->empty() && item->endText()->empty()
6173 494 : && (!isSingleOrBegin || tl->beginHookType() == HookType::NONE)
6174 1910 : && (!isSingleOrEnd || tl->endHookType() == HookType::NONE)) {
6175 455 : ldata->npoints = 2;
6176 455 : ldata->points[0] = pp1;
6177 455 : ldata->points[1] = pp2;
6178 455 : ldata->lineLength = sqrt(PointF::dotProduct(pp2 - pp1, pp2 - pp1));
6179 :
6180 455 : item->setbbox(TextLineBaseSegment::boundingBoxOfLine(pp1, pp2, lineWidth / 2, isDottedLine));
6181 504 : return;
6182 : }
6183 :
6184 950 : double l1 = 0.0;
6185 950 : double l2 = 0.0;
6186 950 : const double gapBetweenTextAndLine = spatium * tl->gapBetweenTextAndLine().val();
6187 :
6188 950 : const bool alignBeginText = tl->beginTextPlace() == TextPlace::LEFT || tl->beginTextPlace() == TextPlace::AUTO;
6189 950 : const bool alignContinueText = tl->continueTextPlace() == TextPlace::LEFT || tl->continueTextPlace() == TextPlace::AUTO;
6190 950 : const bool alignEndText = tl->endTextPlace() == TextPlace::LEFT || tl->endTextPlace() == TextPlace::AUTO;
6191 950 : const bool hasBeginText = !item->text()->empty() && isSingleOrBegin;
6192 950 : const bool hasContinueText = !item->text()->empty() && !isSingleOrBegin;
6193 950 : const bool hasEndText = !item->endText()->empty() && isSingleOrEnd;
6194 :
6195 950 : Shape shape;
6196 :
6197 950 : if (!item->text()->empty()) {
6198 900 : if ((isSingleOrBegin && alignBeginText) || (!isSingleOrBegin && alignContinueText)) {
6199 427 : l1 = gapBetweenTextAndLine;
6200 427 : switch (item->text()->position()) {
6201 427 : case AlignH::LEFT:
6202 427 : l1 += item->text()->ldata()->bbox().width();
6203 427 : break;
6204 0 : case AlignH::HCENTER:
6205 0 : l1 += item->text()->ldata()->bbox().width() / 2;
6206 0 : break;
6207 0 : default:
6208 0 : break;
6209 : }
6210 : }
6211 900 : shape.add(item->text()->ldata()->bbox().translated(item->text()->pos()), item);
6212 : }
6213 : // set end text position and extend bbox
6214 950 : if (!item->endText()->empty()) {
6215 30 : if (alignEndText) {
6216 30 : l2 = gapBetweenTextAndLine;
6217 30 : switch (item->endText()->position()) {
6218 4 : case AlignH::RIGHT:
6219 4 : l2 += item->endText()->ldata()->bbox().width();
6220 4 : break;
6221 11 : case AlignH::HCENTER:
6222 11 : l2 += item->endText()->ldata()->bbox().width() / 2;
6223 11 : break;
6224 15 : default:
6225 15 : break;
6226 : }
6227 : }
6228 30 : const double endTextX = std::max(pp2.x(), shape.bbox().right() + l2); // prevent end text from overlapping begin text
6229 30 : item->endText()->mutldata()->setPosX(endTextX);
6230 30 : shape.add(item->endText()->ldata()->bbox().translated(item->endText()->pos()), item);
6231 : }
6232 :
6233 950 : bool beginHookDrawnSeparately = false;
6234 950 : bool endHookDrawnSeparately = false;
6235 :
6236 950 : if (tl->lineVisible() || !ctx.conf().isPrintingMode()) {
6237 950 : pp1.rx() += l1;
6238 950 : pp2.rx() -= l2;
6239 :
6240 : // Make sure baseline of text and line are properly aligned (accounting for line thickness)
6241 950 : if ((hasBeginText && alignBeginText) || (hasContinueText && alignContinueText)) {
6242 427 : alignBaseLine(item->text(), pp1, pp2);
6243 523 : } else if (hasEndText && alignEndText) {
6244 11 : alignBaseLine(item->endText(), pp1, pp2);
6245 : }
6246 :
6247 950 : const double beginHookHeight = (tl->placeBelow() ? -1.0 : 1.0) * tl->beginHookHeight().val() * spatium;
6248 950 : const double endHookHeight = (tl->placeBelow() ? -1.0 : 1.0) * tl->endHookHeight().val() * spatium;
6249 :
6250 950 : double beginHookWidth = 0.0;
6251 950 : double endHookWidth = 0.0;
6252 :
6253 950 : if (tl->beginHookType() == HookType::HOOK_45) {
6254 10 : beginHookWidth = std::fabs(beginHookHeight * .4);
6255 10 : pp1.rx() += beginHookWidth;
6256 : }
6257 :
6258 950 : if (tl->endHookType() == HookType::HOOK_45) {
6259 8 : endHookWidth = std::fabs(endHookHeight * .4);
6260 8 : pp2.rx() -= endHookWidth;
6261 : }
6262 :
6263 : // don't draw backwards lines (or hooks) if text is longer than nominal line length
6264 950 : if (!item->text()->empty() && pp1.x() > pp2.x() && !tl->diagonal()) {
6265 49 : ldata->setShape(shape);
6266 49 : return;
6267 : }
6268 :
6269 : const bool beginArrow = isSingleOrBegin
6270 901 : && (tl->beginHookType() == HookType::ARROW_FILLED || tl->beginHookType() == HookType::ARROW);
6271 901 : const bool endArrow = isSingleOrEnd && (tl->endHookType() == HookType::ARROW_FILLED || tl->endHookType() == HookType::ARROW);
6272 :
6273 901 : if (beginArrow) {
6274 0 : const bool filled = tl->beginHookType() == HookType::ARROW_FILLED;
6275 0 : ldata->beginArrow = createArrow(true, filled, pp1, pp2, tl);
6276 0 : shape.add(ldata->beginArrow.boundingRect(), item);
6277 : }
6278 901 : if (endArrow) {
6279 0 : const bool filled = tl->endHookType() == HookType::ARROW_FILLED;
6280 0 : ldata->endArrow = createArrow(false, filled, pp1, pp2, tl);
6281 0 : shape.add(ldata->endArrow.boundingRect(), item);
6282 : }
6283 :
6284 1761 : auto hasHook = [](HookType type) -> bool {
6285 1761 : switch (type) {
6286 1087 : case HookType::HOOK_45:
6287 : case HookType::HOOK_90:
6288 : case HookType::HOOK_90T:
6289 1087 : return true;
6290 674 : case HookType::NONE:
6291 : case HookType::ARROW:
6292 : case HookType::ARROW_FILLED:
6293 : case HookType::ROSETTE:
6294 674 : return false;
6295 0 : default:
6296 0 : break;
6297 : }
6298 0 : return false;
6299 : };
6300 :
6301 901 : const bool beginHook = isSingleOrBegin && hasHook(tl->beginHookType());
6302 901 : const bool endHook = isSingleOrEnd && hasHook(tl->endHookType());
6303 :
6304 901 : if (beginHook) {
6305 : // We use the term "endpoint" for the point that does not touch the main line.
6306 499 : const PointF& beginHookEndpoint = ldata->points[ldata->npoints++]
6307 499 : = PointF(pp1.x() - beginHookWidth, pp1.y() + beginHookHeight);
6308 499 : if (tl->beginHookType() == HookType::HOOK_90T) {
6309 : // A T-hook needs to be drawn separately, so we add an extra point
6310 0 : beginHookDrawnSeparately = true;
6311 0 : ldata->points[ldata->npoints++] = PointF(pp1.x() - beginHookWidth, pp1.y() - beginHookHeight);
6312 499 : } else if (tl->lineStyle() != LineType::SOLID) {
6313 : // For non-solid lines, we also draw the hook separately,
6314 : // so that we can distribute the dashes/dots for each linepiece individually
6315 2 : beginHookDrawnSeparately = true;
6316 2 : PointF& beginHookStartpoint = ldata->points[ldata->npoints++] = pp1;
6317 :
6318 2 : if (tl->lineStyle() == LineType::DASHED) {
6319 : // For dashes lines, we extend the lines somewhat,
6320 : // so that the corner between them gets filled
6321 1 : bool checkAngle = tl->beginHookType() == HookType::HOOK_45 || tl->diagonal();
6322 1 : extendLines(beginHookEndpoint, beginHookStartpoint, pp1, pp2, lineWidth, checkAngle);
6323 : }
6324 : }
6325 : }
6326 :
6327 901 : ldata->points[ldata->npoints++] = pp1;
6328 901 : PointF& pp22 = ldata->points[ldata->npoints++] = pp2; // Keep a reference so that we can modify later
6329 :
6330 901 : if (endHook) {
6331 588 : const PointF endHookEndpoint = PointF(pp2.x() + endHookWidth, pp2.y() + endHookHeight);
6332 588 : if (tl->endHookType() == HookType::HOOK_90T) {
6333 : // A T-hook needs to be drawn separately, so we add an extra point
6334 45 : endHookDrawnSeparately = true;
6335 45 : ldata->points[ldata->npoints++] = PointF(pp2.x() + endHookWidth, pp2.y() - endHookHeight);
6336 543 : } else if (tl->lineStyle() != LineType::SOLID) {
6337 : // For non-solid lines, we also draw the hook separately,
6338 : // so that we can distribute the dashes/dots for each linepiece individually
6339 148 : endHookDrawnSeparately = true;
6340 148 : PointF& endHookStartpoint = ldata->points[ldata->npoints++] = pp2;
6341 :
6342 148 : if (tl->lineStyle() == LineType::DASHED) {
6343 : // For dashes lines, we extend the lines somewhat,
6344 : // so that the corner between them gets filled
6345 146 : bool checkAngle = tl->endHookType() == HookType::HOOK_45 || tl->diagonal();
6346 146 : extendLines(pp1, pp22, endHookStartpoint, endHookEndpoint, lineWidth, checkAngle);
6347 : }
6348 : }
6349 :
6350 588 : ldata->points[ldata->npoints++] = endHookEndpoint;
6351 : }
6352 :
6353 901 : ldata->lineLength = sqrt(PointF::dotProduct(pp22 - pp1, pp22 - pp1));
6354 : }
6355 :
6356 : // Calculate shape of line
6357 : {
6358 901 : int start = 0;
6359 901 : if (beginHookDrawnSeparately) {
6360 2 : PointF& p1 = ldata->points[start++];
6361 2 : PointF& p2 = ldata->points[start++];
6362 2 : shape.add(TextLineBaseSegment::boundingBoxOfLine(p1, p2, lineWidth / 2, isDottedLine), item);
6363 : }
6364 :
6365 901 : int end = ldata->npoints;
6366 901 : if (endHookDrawnSeparately) {
6367 193 : PointF& p1 = ldata->points[--end];
6368 193 : PointF& p2 = ldata->points[--end];
6369 193 : shape.add(TextLineBaseSegment::boundingBoxOfLine(p1, p2, lineWidth / 2, isDottedLine), item);
6370 : }
6371 :
6372 2694 : for (int i = start; i < end - 1; ++i) {
6373 1793 : shape.add(TextLineBaseSegment::boundingBoxOfLine(ldata->points[i], ldata->points[i + 1], lineWidth / 2,
6374 1793 : isDottedLine), item, !tl->lineVisible());
6375 : }
6376 : }
6377 :
6378 901 : ldata->setShape(shape);
6379 950 : }
6380 :
6381 2311 : void TLayout::layoutTie(Tie* item, LayoutContext&)
6382 : {
6383 : UNUSED(item);
6384 2311 : }
6385 :
6386 7433 : void TLayout::layoutTimeSig(const TimeSig* item, TimeSig::LayoutData* ldata, const LayoutContext& ctx)
6387 : {
6388 : LAYOUT_CALL_ITEM(item);
6389 : LD_INDEPENDENT;
6390 :
6391 7433 : ldata->setPos(0.0, 0.0);
6392 7433 : double spatium = item->spatium();
6393 :
6394 7433 : ldata->setBbox(RectF()); // prepare for an empty time signature
6395 :
6396 7433 : ldata->pointLargeLeftParen = PointF();
6397 7433 : ldata->pz = PointF();
6398 7433 : ldata->pn = PointF();
6399 7433 : ldata->pointLargeRightParen = PointF();
6400 :
6401 7433 : const MStyle& style = item->style();
6402 7433 : TimeSigPlacement timeSigPlacement = style.styleV(Sid::timeSigPlacement).value<TimeSigPlacement>();
6403 7433 : if (timeSigPlacement == TimeSigPlacement::NORMAL) {
6404 7399 : const_cast<TimeSig*>(item)->setSystemFlag(false);
6405 : } else {
6406 34 : const_cast<TimeSig*>(item)->setSystemFlag(true);
6407 : }
6408 :
6409 7433 : double lineDist = 0.0;
6410 7433 : int numOfLines = 0;
6411 7433 : TimeSigType sigType = item->timeSigType();
6412 7433 : const Staff* staff = item->staff();
6413 7433 : const Segment* seg = item->segment();
6414 7433 : const Measure* meas = seg ? seg->measure() : nullptr;
6415 7433 : const Fraction tick = meas ? meas->tick() : item->tick();
6416 :
6417 7433 : if (staff) {
6418 : // if staff is without time sig, format as if no text at all
6419 7433 : const StaffType* stVisibility = staff->staffType(item->tick());
6420 7433 : if (!stVisibility->genTimesig()) {
6421 : // reset position and box sizes to 0
6422 : // LOGD("staff: no time sig");
6423 111 : ldata->pointLargeLeftParen.rx() = 0.0;
6424 111 : ldata->pn.rx() = 0.0;
6425 111 : ldata->pz.rx() = 0.0;
6426 111 : ldata->pointLargeRightParen.rx() = 0.0;
6427 111 : ldata->setBbox(RectF());
6428 : // leave everything else as it is:
6429 : // draw() will anyway skip any drawing if staff type has no time sigs
6430 111 : return;
6431 : }
6432 7322 : numOfLines = staff->lines(tick);
6433 7322 : lineDist = staff->lineDistance(tick);
6434 : } else {
6435 : // assume dimensions of a standard staff
6436 0 : lineDist = 1.0;
6437 0 : numOfLines = 5;
6438 : }
6439 :
6440 : // if some symbol
6441 : // compute vert. displacement to center in the staff height
6442 : // determine middle staff position:
6443 :
6444 7322 : double yoff = spatium * (numOfLines - 1) * .5 * lineDist;
6445 :
6446 : // C and Ccut are placed at the middle of the staff: use yoff directly
6447 7322 : IEngravingFontPtr font = ctx.engravingFont();
6448 7322 : ScaleF scale = item->scale();
6449 7322 : SizeF mag(item->magS() * scale);
6450 7322 : TimeSigStyle timeSigStyle = item->timeSigStyle();
6451 :
6452 7322 : double numDist = item->numDist() * scale.height();
6453 :
6454 7322 : Shape shape;
6455 7322 : if (sigType == TimeSigType::FOUR_FOUR) {
6456 76 : ldata->pz = PointF(0.0, yoff);
6457 152 : SymId sym = timeSigStyle == TimeSigStyle::LARGE ? SymId::timeSigCommonLarge
6458 76 : : timeSigStyle == TimeSigStyle::NARROW ? SymId::timeSigCommonNarrow
6459 : : SymId::timeSigCommon;
6460 76 : RectF bbox = font->bbox(sym, mag);
6461 76 : shape.add(bbox.translated(ldata->pz), item);
6462 76 : ldata->ns.clear();
6463 76 : ldata->ns.push_back(sym);
6464 76 : ldata->ds.clear();
6465 7246 : } else if (sigType == TimeSigType::ALLA_BREVE) {
6466 56 : ldata->pz = PointF(0.0, yoff);
6467 112 : SymId sym = timeSigStyle == TimeSigStyle::LARGE ? SymId::timeSigCutCommonLarge
6468 56 : : timeSigStyle == TimeSigStyle::NARROW ? SymId::timeSigCutCommonNarrow
6469 : : SymId::timeSigCutCommon;
6470 56 : RectF bbox = font->bbox(sym, mag);
6471 56 : shape.add(bbox.translated(ldata->pz), item);
6472 56 : ldata->ns.clear();
6473 56 : ldata->ns.push_back(sym);
6474 56 : ldata->ds.clear();
6475 7190 : } else if (sigType == TimeSigType::CUT_BACH) {
6476 0 : ldata->pz = PointF(0.0, yoff);
6477 0 : SymId sym = timeSigStyle == TimeSigStyle::LARGE ? SymId::timeSigCut2Large
6478 0 : : timeSigStyle == TimeSigStyle::NARROW ? SymId::timeSigCut2Narrow
6479 : : SymId::timeSigCut2;
6480 0 : RectF bbox = font->bbox(sym, mag);
6481 0 : shape.add(bbox.translated(ldata->pz), item);
6482 0 : ldata->ns.clear();
6483 0 : ldata->ns.push_back(sym);
6484 0 : ldata->ds.clear();
6485 7190 : } else if (sigType == TimeSigType::CUT_TRIPLE) {
6486 0 : ldata->pz = PointF(0.0, yoff);
6487 0 : SymId sym = timeSigStyle == TimeSigStyle::LARGE ? SymId::timeSigCut3Large
6488 0 : : timeSigStyle == TimeSigStyle::NARROW ? SymId::timeSigCut3Narrow
6489 : : SymId::timeSigCut3;
6490 0 : RectF bbox = font->bbox(sym, mag);
6491 0 : shape.add(bbox.translated(ldata->pz), item);
6492 0 : ldata->ns.clear();
6493 0 : ldata->ns.push_back(sym);
6494 0 : ldata->ds.clear();
6495 : } else {
6496 7190 : if (item->numeratorString().isEmpty()) {
6497 21504 : ldata->ns = timeSigSymIdsFromString(item->numeratorString().isEmpty()
6498 14336 : ? String::number(item->sig().numerator())
6499 0 : : item->numeratorString(),
6500 7168 : timeSigStyle);
6501 :
6502 21504 : ldata->ds = timeSigSymIdsFromString(item->denominatorString().isEmpty()
6503 14336 : ? String::number(item->sig().denominator())
6504 0 : : item->denominatorString(),
6505 7168 : timeSigStyle);
6506 : } else {
6507 22 : ldata->ns = timeSigSymIdsFromString(item->numeratorString(), timeSigStyle);
6508 22 : ldata->ds = timeSigSymIdsFromString(item->denominatorString(), timeSigStyle);
6509 : }
6510 :
6511 7190 : RectF numRect = font->bbox(ldata->ns, mag);
6512 7190 : RectF denRect = font->bbox(ldata->ds, mag);
6513 :
6514 7190 : double pzY = yoff - (denRect.width() < 0.01 ? 0.0 : 0.5 * (numRect.height() + numDist));
6515 7190 : double pnY = yoff + 0.5 * (denRect.height() + numDist);
6516 :
6517 7190 : if (numRect.width() >= denRect.width()) {
6518 : // numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
6519 6184 : ldata->pz = PointF(0.0, pzY);
6520 : // denominator: horiz: centred around centre of numerator | vert: one space below centre line
6521 6184 : ldata->pn = PointF((numRect.width() - denRect.width()) * .5, pnY);
6522 : } else {
6523 : // numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
6524 1006 : ldata->pz = PointF((denRect.width() - numRect.width()) * .5, pzY);
6525 : // denominator: horiz: centred around centre of numerator | vert: one space below centre line
6526 1006 : ldata->pn = PointF(0.0, pnY);
6527 : }
6528 :
6529 : // centering of parenthesis so the middle of the parenthesis is at the divisor marking level
6530 7190 : int centerY = yoff / 2 + spatium;
6531 7190 : int widestPortion = numRect.width() > denRect.width() ? numRect.width() : denRect.width();
6532 7190 : ldata->pointLargeLeftParen = PointF(-spatium, centerY);
6533 7190 : ldata->pointLargeRightParen = PointF(widestPortion + spatium, centerY);
6534 :
6535 7190 : shape.add(numRect.translated(ldata->pz), item); // translate bounding boxes to actual string positions
6536 7190 : shape.add(denRect.translated(ldata->pn), item);
6537 7190 : if (item->largeParentheses()) {
6538 18 : shape.add(RectF(ldata->pointLargeLeftParen.x(), ldata->pointLargeLeftParen.y() - denRect.height(), spatium / 2,
6539 18 : numRect.height() + denRect.height()), item);
6540 18 : shape.add(RectF(ldata->pointLargeRightParen.x(), ldata->pointLargeRightParen.y() - denRect.height(), spatium / 2,
6541 18 : numRect.height() + denRect.height()), item);
6542 : }
6543 : }
6544 :
6545 7322 : ldata->setShape(shape);
6546 :
6547 7322 : ldata->setPosX(-shape.bbox().left());
6548 :
6549 7322 : if (item->isAboveStaves()) {
6550 0 : if (staff->hasSystemObjectsBelowBottomStaff()) {
6551 0 : double staffHeight = spatium * (numOfLines - 1) * lineDist;
6552 0 : ldata->setPosY(staffHeight - ldata->bbox().top());
6553 : } else {
6554 0 : ldata->setPosY(-ldata->bbox().bottom());
6555 : }
6556 7322 : } else if (item->isAcrossStaves()) {
6557 34 : double top = ldata->bbox().top();
6558 34 : ldata->setPosY(-top);
6559 : }
6560 :
6561 7322 : ldata->moveY(item->yPos());
6562 7322 : }
6563 :
6564 498 : void TLayout::layoutTimeTickAnchor(TimeTickAnchor* item, LayoutContext&)
6565 : {
6566 498 : TimeTickAnchor::LayoutData* ldata = item->mutldata();
6567 498 : const StaffType* st = item->staffType();
6568 498 : ldata->setPos(0.0, 0.0);
6569 498 : double width = item->segment()->width();
6570 498 : double height = item->staff()->staffHeight();
6571 498 : if (st && st->lines() == 1) {
6572 : // Special case for single line staves
6573 0 : const double lineDist = st->lineDistance().val() * item->spatium();
6574 0 : ldata->setPosY(-lineDist);
6575 0 : height = lineDist * 2;
6576 : }
6577 498 : ldata->setBbox(0.0, 0.0, width, height);
6578 498 : }
6579 :
6580 0 : void TLayout::layoutTremoloSingle(TremoloSingleChord* item, LayoutContext& ctx)
6581 : {
6582 : LAYOUT_CALL_ITEM(item);
6583 0 : TremoloLayout::layout(item, ctx);
6584 0 : }
6585 :
6586 0 : void TLayout::layoutTremoloTwo(TremoloTwoChord* item, LayoutContext& ctx)
6587 : {
6588 : LAYOUT_CALL_ITEM(item);
6589 0 : TremoloLayout::layout(item, ctx);
6590 0 : }
6591 :
6592 38 : void TLayout::layoutTremoloBar(const TremoloBar* item, TremoloBar::LayoutData* ldata)
6593 : {
6594 : LAYOUT_CALL_ITEM(item);
6595 : LD_INDEPENDENT;
6596 :
6597 38 : double spatium = item->spatium();
6598 :
6599 38 : ldata->setPos(0.0, -spatium * 3.0);
6600 :
6601 : /* we place the tremolo bars starting slightly before the
6602 : * notehead, and end it slightly after, drawing above the
6603 : * note. The values specified in Guitar Pro are very large, too
6604 : * large for the scale used in Musescore. We used the
6605 : * timeFactor and pitchFactor below to reduce these values down
6606 : * consistently to values that make sense to draw with the
6607 : * Musescore scale. */
6608 :
6609 38 : double timeFactor = item->userMag() / 1.0;
6610 38 : double pitchFactor = -spatium * .02;
6611 :
6612 38 : PolygonF polygon;
6613 142 : for (const PitchValue& v : item->points()) {
6614 104 : polygon << PointF(v.time * timeFactor, v.pitch * pitchFactor);
6615 : }
6616 38 : ldata->polygon = polygon;
6617 :
6618 38 : const double w = item->absoluteFromSpatium(item->lineWidth());
6619 38 : ldata->setBbox(ldata->polygon.boundingRect().adjusted(-w, -w, w, w));
6620 38 : }
6621 :
6622 116 : void TLayout::layoutTrillSegment(TrillSegment* item, LayoutContext& ctx)
6623 : {
6624 : LAYOUT_CALL_ITEM(item);
6625 116 : TrillSegment::LayoutData* ldata = item->mutldata();
6626 116 : Trill* trill = item->trill();
6627 116 : EngravingItem* startItem = trill->startElement();
6628 116 : Chord* startChord = startItem && startItem->isChord() ? toChord(startItem) : nullptr;
6629 116 : if (startChord) {
6630 : // Semi-hack: spanners don't have staffMove property, so we change
6631 : // the staffIdx itself to follow cross-staff chords if needed
6632 114 : item->setStaffIdx(startChord->vStaffIdx());
6633 : }
6634 :
6635 116 : if (item->staff()) {
6636 116 : ldata->setMag(item->staff()->staffMag(item->tick()));
6637 : }
6638 116 : if (item->spanner()->placeBelow()) {
6639 1 : ldata->setPosY(item->staff() ? item->staff()->staffHeight(item->tick()) : 0.0);
6640 : }
6641 116 : const double yOff = item->staffOffsetY();
6642 116 : ldata->moveY(yOff);
6643 :
6644 116 : PointF defaultPos = item->defaultPos();
6645 116 : ldata->move(defaultPos);
6646 :
6647 116 : bool accidentalGoesBelow = trill->trillType() == TrillType::DOWNPRALL_LINE;
6648 116 : Ornament* ornament = trill->ornament();
6649 116 : if (ornament) {
6650 116 : if (item->isSingleBeginType()) {
6651 115 : TLayout::layoutOrnament(ornament, ornament->mutldata(), ctx.conf());
6652 : }
6653 116 : trill->setAccidental(accidentalGoesBelow ? ornament->accidentalBelow() : ornament->accidentalAbove());
6654 116 : trill->setCueNoteChord(ornament->cueNoteChord());
6655 116 : ArticulationAnchor anchor = ornament->anchor();
6656 116 : if (anchor == ArticulationAnchor::AUTO) {
6657 113 : trill->setPlacement(trill->track() % 2 ? PlacementV::BELOW : PlacementV::ABOVE);
6658 : } else {
6659 3 : trill->setPlacement(anchor == ArticulationAnchor::TOP ? PlacementV::ABOVE : PlacementV::BELOW);
6660 : }
6661 116 : trill->setPropertyFlags(Pid::PLACEMENT, PropertyFlags::STYLED); // Ensures that the property isn't written (it is written by the ornamnent)
6662 : }
6663 :
6664 116 : if (item->isSingleType() || item->isBeginType()) {
6665 115 : switch (trill->trillType()) {
6666 101 : case TrillType::TRILL_LINE:
6667 101 : item->symbolLine(SymId::ornamentTrill, SymId::wiggleTrill);
6668 101 : break;
6669 6 : case TrillType::PRALLPRALL_LINE:
6670 6 : item->symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
6671 6 : break;
6672 4 : case TrillType::UPPRALL_LINE:
6673 4 : item->symbolLine(SymId::ornamentBottomLeftConcaveStroke,
6674 : SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
6675 4 : break;
6676 4 : case TrillType::DOWNPRALL_LINE:
6677 4 : item->symbolLine(SymId::ornamentLeftVerticalStroke,
6678 : SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
6679 4 : break;
6680 : }
6681 115 : Accidental* a = trill->accidental();
6682 115 : if (a) {
6683 10 : double vertMargin = 0.35 * item->spatium();
6684 10 : RectF box = item->symBbox(item->symbols().front());
6685 10 : double x = 0;
6686 10 : double y = 0;
6687 10 : x = 0.5 * (box.width() - a->width());
6688 : double minVertDist = accidentalGoesBelow
6689 20 : ? Shape(box).minVerticalDistance(a->shape())
6690 10 : : a->shape().minVerticalDistance(Shape(box));
6691 10 : y = (accidentalGoesBelow ? minVertDist + vertMargin : -minVertDist - vertMargin) + yOff;
6692 10 : a->setPos(x, y);
6693 10 : a->setOwnershipParent(item);
6694 : }
6695 : } else {
6696 1 : switch (trill->trillType()) {
6697 1 : case TrillType::TRILL_LINE:
6698 : case TrillType::PRALLPRALL_LINE:
6699 1 : item->symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
6700 1 : break;
6701 0 : case TrillType::UPPRALL_LINE:
6702 : case TrillType::DOWNPRALL_LINE:
6703 0 : item->symbolLine(SymId::ornamentZigZagLineNoRightEnd,
6704 : SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
6705 0 : break;
6706 : }
6707 : }
6708 :
6709 116 : Autoplace::autoplaceSpannerSegment(item, ldata);
6710 116 : }
6711 :
6712 232 : void TLayout::fillTrillSegmentShape(const TrillSegment* item, TrillSegment::LayoutData* ldata, const LayoutConfiguration& conf)
6713 : {
6714 : LAYOUT_CALL_ITEM(item);
6715 232 : std::shared_ptr<const IEngravingFont> font = conf.engravingFont();
6716 232 : Shape s = font->shape(item->symbols(), item->magS());
6717 232 : Accidental* accidental = item->trill()->accidental();
6718 232 : if (accidental && accidental->visible() && item->isSingleBeginType()) {
6719 20 : s.add(accidental->shape().translate(accidental->pos()));
6720 : }
6721 :
6722 232 : ldata->setShape(s);
6723 232 : }
6724 :
6725 0 : void TLayout::layoutTripletFeel(const TripletFeel* item, TripletFeel::LayoutData* ldata)
6726 : {
6727 : LAYOUT_CALL_ITEM(item);
6728 0 : layoutSystemText(item, ldata);
6729 0 : }
6730 :
6731 0 : void TLayout::layoutTrill(Trill* item, LayoutContext& ctx)
6732 : {
6733 : LAYOUT_CALL_ITEM(item);
6734 0 : layoutLine(static_cast<SLine*>(item), ctx);
6735 0 : }
6736 :
6737 41 : void TLayout::layoutTuplet(Tuplet* item, LayoutContext& ctx)
6738 : {
6739 : LAYOUT_CALL_ITEM(item);
6740 41 : TupletLayout::layout(item, ctx);
6741 41 : }
6742 :
6743 1829 : void TLayout::fillTupletShape(const Tuplet* item, Tuplet::LayoutData* ldata)
6744 : {
6745 : LAYOUT_CALL_ITEM(item);
6746 1829 : Shape s;
6747 1829 : if (item->hasBracket()) {
6748 3639 : auto tupletRect = [](const PointF& p1, const PointF& p2, double height, const Tuplet* item) {
6749 3639 : Shape shape;
6750 3639 : double xLength = p2.x() - p1.x();
6751 3639 : int nSteps = 2 * std::ceil(xLength / item->spatium());
6752 3639 : double xStep = xLength / nSteps;
6753 3639 : double yStep = (p2.y() - p1.y()) / nSteps;
6754 3639 : double rectHeight = (item->isUp() ? height : -height) - yStep;
6755 3639 : double xCur = p1.x();
6756 3639 : double yCur = p1.y() + yStep;
6757 26031 : for (int i = 0; i < nSteps; ++i) {
6758 22392 : shape.add(RectF(xCur, yCur, xStep, rectHeight).normalized(), item);
6759 22392 : xCur += xStep;
6760 22392 : yCur += yStep;
6761 : }
6762 3639 : return shape;
6763 0 : };
6764 :
6765 918 : double height = item->style().styleAbsolute(Sid::tupletBracketHookHeight);
6766 918 : s.add(tupletRect(item->bracketL[0], item->bracketL[1], height, item));
6767 918 : s.add(tupletRect(item->bracketL[1], item->bracketL[2], height, item));
6768 918 : if (item->number()) {
6769 885 : s.add(tupletRect(item->bracketR[0], item->bracketR[1], height, item));
6770 885 : s.add(tupletRect(item->bracketR[1], item->bracketR[2], height, item));
6771 : } else {
6772 33 : s.add(tupletRect(item->bracketL[2], item->bracketL[3], height, item));
6773 : }
6774 : }
6775 1829 : if (item->number()) {
6776 1549 : s.add(item->number()->ldata()->bbox().translated(item->number()->pos()));
6777 : }
6778 :
6779 1829 : ldata->setShape(s);
6780 1829 : }
6781 :
6782 28 : void TLayout::layoutVibratoSegment(VibratoSegment* item, LayoutContext&)
6783 : {
6784 : LAYOUT_CALL_ITEM(item);
6785 28 : VibratoSegment::LayoutData* ldata = item->mutldata();
6786 28 : if (item->staff()) {
6787 28 : ldata->setMag(item->staff()->staffMag(item->tick()));
6788 : }
6789 28 : if (item->spanner()->placeBelow()) {
6790 0 : ldata->setPosY(item->staff() ? item->staff()->staffHeight(item->tick()) : 0.0);
6791 : }
6792 :
6793 28 : PointF defaultPos = item->defaultPos();
6794 28 : ldata->move(defaultPos);
6795 :
6796 28 : ldata->moveY(item->staffOffsetY());
6797 :
6798 28 : switch (item->vibrato()->vibratoType()) {
6799 10 : case VibratoType::GUITAR_VIBRATO:
6800 10 : item->symbolLine(SymId::guitarVibratoStroke, SymId::guitarVibratoStroke);
6801 10 : break;
6802 4 : case VibratoType::GUITAR_VIBRATO_WIDE:
6803 4 : item->symbolLine(SymId::guitarWideVibratoStroke, SymId::guitarWideVibratoStroke);
6804 4 : break;
6805 11 : case VibratoType::VIBRATO_SAWTOOTH:
6806 11 : item->symbolLine(SymId::wiggleSawtooth, SymId::wiggleSawtooth);
6807 11 : break;
6808 3 : case VibratoType::VIBRATO_SAWTOOTH_WIDE:
6809 3 : item->symbolLine(SymId::wiggleSawtoothWide, SymId::wiggleSawtoothWide);
6810 3 : break;
6811 0 : default:
6812 0 : break;
6813 : }
6814 :
6815 28 : Autoplace::autoplaceSpannerSegment(item, ldata);
6816 28 : }
6817 :
6818 0 : void TLayout::layoutVibrato(Vibrato* item, LayoutContext& ctx)
6819 : {
6820 : LAYOUT_CALL_ITEM(item);
6821 0 : layoutLine(static_cast<SLine*>(item), ctx);
6822 :
6823 0 : if (ctx.conf().isPaletteMode()) {
6824 0 : return;
6825 : }
6826 0 : if (item->spannerSegments().empty()) {
6827 0 : LOGD("Vibrato: no segments");
6828 0 : return;
6829 : }
6830 : }
6831 :
6832 0 : void TLayout::layoutVolta(Volta* item, LayoutContext& ctx)
6833 : {
6834 : LAYOUT_CALL_ITEM(item);
6835 0 : layoutLine(item, ctx);
6836 0 : }
6837 :
6838 376 : void TLayout::layoutVoltaSegment(VoltaSegment* item, LayoutContext& ctx)
6839 : {
6840 : LAYOUT_CALL_ITEM(item);
6841 376 : VoltaSegment::LayoutData* ldata = item->mutldata();
6842 376 : layoutTextLineBaseSegment(item, ctx);
6843 376 : if (item->placeBelow()) {
6844 0 : if (TextBase* beginText = item->text()) {
6845 0 : PointF offset = beginText->offset();
6846 0 : offset.setY(-offset.y() + beginText->ldata()->bbox().height());
6847 0 : beginText->setOffset(offset);
6848 : }
6849 0 : if (TextBase* endText = item->endText()) {
6850 0 : PointF offset = endText->offset();
6851 0 : offset.setY(-offset.y() + endText->ldata()->bbox().height());
6852 0 : endText->setOffset(offset);
6853 : }
6854 : // recalculate shape after moving texts
6855 0 : Shape sh = recalculateTextLineBaseSegmentShape(item);
6856 0 : ldata->setShape(sh);
6857 0 : }
6858 376 : Autoplace::autoplaceSpannerSegment(item, ldata);
6859 376 : }
6860 :
6861 0 : void TLayout::layoutWhammyBarSegment(WhammyBarSegment* item, LayoutContext& ctx)
6862 : {
6863 : LAYOUT_CALL_ITEM(item);
6864 0 : WhammyBarSegment::LayoutData* ldata = item->mutldata();
6865 0 : layoutTextLineBaseSegment(item, ctx);
6866 :
6867 0 : Autoplace::autoplaceSpannerSegment(item, ldata);
6868 0 : }
6869 :
6870 : using LayoutSystemTypes = rtti::TypeList<LyricsLine, Slur, HammerOnPullOff, TappingHalfSlur, Volta>;
6871 :
6872 : class LayoutSystemVisitor : public rtti::Visitor<LayoutSystemVisitor>
6873 : {
6874 : public:
6875 : template<typename T>
6876 9440 : static bool doVisit(Spanner* item, System* system, LayoutContext& ctx, SpannerSegment** segOut)
6877 : {
6878 9440 : if (T::classof(item)) {
6879 1620 : *segOut = TLayout::layoutSystem(static_cast<T*>(item), system, ctx);
6880 1620 : return true;
6881 : }
6882 7820 : return false;
6883 : }
6884 : };
6885 :
6886 2693 : SpannerSegment* TLayout::layoutSystem(Spanner* item, System* system, LayoutContext& ctx)
6887 : {
6888 : LAYOUT_CALL_ITEM(item);
6889 2693 : SpannerSegment* seg = nullptr;
6890 2693 : bool found = LayoutSystemVisitor::visit(LayoutSystemTypes {}, item, system, ctx, &seg);
6891 2693 : if (!found) {
6892 1073 : SLine* line = dynamic_cast<SLine*>(item);
6893 1073 : if (line) {
6894 1073 : seg = layoutSystemSLine(line, system, ctx);
6895 : } else {
6896 0 : DO_ASSERT(found);
6897 : }
6898 : }
6899 2693 : return seg;
6900 : }
6901 :
6902 2749 : SpannerSegment* TLayout::getNextLayoutSystemSegment(Spanner* spanner, System* system,
6903 : std::function<SpannerSegment* ()> createSegment)
6904 : {
6905 : // Prefer a segment which has already been added to the system
6906 2749 : SpannerSegment* seg = nullptr;
6907 2749 : SpannerSegment* detached = nullptr;
6908 3988 : for (SpannerSegment* ss : spanner->spannerSegments()) {
6909 1243 : if (ss->system() == system) {
6910 4 : seg = ss;
6911 4 : break;
6912 : }
6913 1239 : if (!detached && !ss->system()) {
6914 1148 : detached = ss;
6915 : }
6916 : }
6917 2749 : if (!seg) {
6918 2745 : seg = detached;
6919 : }
6920 :
6921 2749 : if (!seg) {
6922 1597 : if ((seg = spanner->popUnusedSegment())) {
6923 0 : spanner->reuse(seg);
6924 : } else {
6925 1597 : seg = createSegment();
6926 1597 : assert(seg);
6927 1597 : spanner->add(seg);
6928 : }
6929 : }
6930 2749 : seg->moveToSystem(system);
6931 2749 : seg->setSpanner(spanner);
6932 2749 : seg->setTrack(spanner->track());
6933 2749 : seg->setVisible(spanner->visible());
6934 2749 : return seg;
6935 : }
6936 :
6937 : // layout spannersegment for system
6938 1449 : SpannerSegment* TLayout::layoutSystemSLine(SLine* line, System* system, LayoutContext& ctx)
6939 : {
6940 : LAYOUT_CALL_ITEM(line);
6941 1449 : Fraction stick = system->firstMeasure()->tick();
6942 1449 : Fraction etick = system->lastMeasure()->endTick();
6943 :
6944 1449 : LineSegment* lineSegm = toLineSegment(TLayout::getNextLayoutSystemSegment(line, system, [line]() {
6945 864 : return line->createLineSegment();
6946 : }));
6947 :
6948 : SpannerSegmentType sst;
6949 1449 : if (line->tick() >= stick) {
6950 : //
6951 : // this is the first call to layoutSystem,
6952 : // processing the first line segment
6953 : //
6954 1425 : line->computeStartElement();
6955 1425 : line->computeEndElement();
6956 1425 : if (line->tick2() < etick) {
6957 1210 : sst = SpannerSegmentType::SINGLE;
6958 215 : } else if (line->tick2() == etick) {
6959 194 : if (line->isPedal() && toPedal(line)->connect45HookToNext()) {
6960 0 : sst = SpannerSegmentType::BEGIN;
6961 : } else {
6962 194 : sst = SpannerSegmentType::SINGLE;
6963 : }
6964 : } else {
6965 21 : sst = SpannerSegmentType::BEGIN;
6966 : }
6967 1425 : if (line->hasVoiceAssignmentProperties()) {
6968 478 : line->setPlacementBasedOnVoiceAssignment(ctx.conf().styleV(Sid::dynamicsHairpinVoiceBasedPlacement).value<DirectionV>());
6969 : }
6970 24 : } else if (line->tick() < stick
6971 24 : && (line->tick2() > etick || (line->tick2() == etick && line->isPedal() && toPedal(line)->connect45HookToNext()))) {
6972 3 : sst = SpannerSegmentType::MIDDLE;
6973 : } else {
6974 : //
6975 : // this is the last call to layoutSystem
6976 : // processing the last line segment
6977 : //
6978 21 : sst = SpannerSegmentType::END;
6979 : }
6980 1449 : lineSegm->setSpannerSegmentType(sst);
6981 1449 : lineSegm->setPlacement(line->placement());
6982 :
6983 1449 : switch (sst) {
6984 1404 : case SpannerSegmentType::SINGLE: {
6985 : System* s;
6986 1404 : PointF p1 = line->linePos(Grip::START, &s);
6987 1404 : PointF p2 = line->linePos(Grip::END, &s);
6988 1404 : double len = p2.x() - p1.x();
6989 1404 : lineSegm->setPos(p1);
6990 1404 : lineSegm->setPos2(PointF(len, p2.y() - p1.y()));
6991 : }
6992 1404 : break;
6993 21 : case SpannerSegmentType::BEGIN: {
6994 : System* s;
6995 21 : PointF p1 = line->linePos(Grip::START, &s);
6996 21 : lineSegm->setPos(p1);
6997 21 : double x2 = system->endingXForOpenEndedLines();
6998 21 : lineSegm->setPos2(PointF(x2 - p1.x(), 0.0));
6999 : }
7000 21 : break;
7001 3 : case SpannerSegmentType::MIDDLE: {
7002 3 : double x1 = line->isVolta() ? voltaMidEndSegmentStartX(toVolta(line), system, ctx)
7003 3 : : system->firstNoteRestSegmentX(true);
7004 3 : double x2 = system->endingXForOpenEndedLines();
7005 : System* s;
7006 3 : PointF p1 = line->linePos(Grip::START, &s);
7007 3 : lineSegm->setPos(PointF(x1, p1.y()));
7008 3 : lineSegm->setPos2(PointF(x2 - x1, 0.0));
7009 : }
7010 3 : break;
7011 21 : case SpannerSegmentType::END: {
7012 : System* s;
7013 21 : PointF p2 = line->linePos(Grip::END, &s);
7014 21 : double x1 = line->isVolta() ? voltaMidEndSegmentStartX(toVolta(line), system, ctx)
7015 15 : : system->firstNoteRestSegmentX(true);
7016 21 : double len = p2.x() - x1;
7017 21 : lineSegm->setPos(PointF(p2.x() - len, p2.y()));
7018 21 : lineSegm->setPos2(PointF(len, 0.0));
7019 : }
7020 21 : break;
7021 : }
7022 :
7023 1449 : layoutLineSegment(lineSegm, ctx);
7024 :
7025 1449 : return lineSegm;
7026 : }
7027 :
7028 6 : double TLayout::voltaMidEndSegmentStartX(Volta* volta, System* system, LayoutContext& ctx)
7029 : {
7030 6 : Measure* firstMeasure = system->firstMeasure();
7031 6 : if (!firstMeasure) {
7032 0 : return 0.0;
7033 : }
7034 :
7035 6 : Segment* refSeg = firstMeasure->repeatStart()
7036 12 : ? firstMeasure->first(SegmentType::StartRepeatBarLine)
7037 6 : : ctx.conf().styleB(Sid::voltaAlignStartBeforeKeySig)
7038 6 : ? firstMeasure->first(SegmentType::KeySig)
7039 6 : : nullptr;
7040 :
7041 6 : if (!refSeg) {
7042 6 : return system->firstNoteRestSegmentX(true);
7043 : }
7044 :
7045 0 : if (refSeg->isStartRepeatBarLineType()) {
7046 0 : return refSeg->x() + firstMeasure->x();
7047 : }
7048 :
7049 0 : KeySig* keySig = toKeySig(refSeg->element(volta->track()));
7050 0 : if (!keySig || keySig->ldata()->keySymbols.empty()) {
7051 0 : return 0.0;
7052 : }
7053 :
7054 0 : KeySym keySym = keySig->ldata()->keySymbols.front();
7055 0 : double xCutout = ctx.engravingFont()->smuflAnchor(keySym.sym, SmuflAnchorId::cutOutNW, 1.0).x();
7056 0 : return keySig->x() + keySym.xPos.val() + xCutout + refSeg->x() + firstMeasure->x();
7057 : }
7058 :
7059 355 : SpannerSegment* TLayout::layoutSystem(LyricsLine* line, System* system, LayoutContext& ctx)
7060 : {
7061 : LAYOUT_CALL_ITEM(line);
7062 355 : if (!line->isPartialLyricsLine() && !line->lyrics()) {
7063 0 : return nullptr;
7064 : }
7065 :
7066 355 : Fraction stick = system->firstMeasure()->tick();
7067 355 : Fraction etick = system->lastMeasure()->endTick();
7068 :
7069 355 : LyricsLineSegment* lineSegm = toLyricsLineSegment(TLayout::getNextLayoutSystemSegment(line, system, [line]() {
7070 260 : return line->createLineSegment();
7071 : }));
7072 :
7073 : SpannerSegmentType sst;
7074 355 : if (line->tick() >= stick) {
7075 341 : TLayout::layoutLyricsLine(line);
7076 341 : if (line->ticks().isZero() && line->isEndMelisma()) {
7077 0 : return nullptr;
7078 : }
7079 341 : line->computeStartElement();
7080 341 : line->computeEndElement();
7081 341 : sst = line->tick2() <= etick ? SpannerSegmentType::SINGLE : SpannerSegmentType::BEGIN;
7082 14 : } else if (line->tick() < stick && line->tick2() > etick) {
7083 4 : sst = SpannerSegmentType::MIDDLE;
7084 : } else {
7085 10 : sst = SpannerSegmentType::END;
7086 : }
7087 355 : lineSegm->setSpannerSegmentType(sst);
7088 :
7089 355 : TLayout::layoutLyricsLineSegment(lineSegm, ctx);
7090 355 : if (!line->isPartialLyricsLine() && !line->lyrics()) {
7091 : // this line could have been removed in the process of laying out surrounding lyrics
7092 0 : return nullptr;
7093 : }
7094 :
7095 355 : return lineSegm;
7096 : }
7097 :
7098 376 : SpannerSegment* TLayout::layoutSystem(Volta* line, System* system, LayoutContext& ctx)
7099 : {
7100 : LAYOUT_CALL_ITEM(line);
7101 376 : return layoutSystemSLine(line, system, ctx);
7102 : }
7103 :
7104 889 : SpannerSegment* TLayout::layoutSystem(Slur* line, System* system, LayoutContext& ctx)
7105 : {
7106 : LAYOUT_CALL_ITEM(line);
7107 889 : return SlurTieLayout::layoutSystem(line, system, ctx);
7108 : }
|