Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2023 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "singlelayout.h"
24 :
25 : #include "draw/fontmetrics.h"
26 :
27 : #include "types/types.h"
28 : #include "types/typesconv.h"
29 : #include "types/symnames.h"
30 :
31 : #include "dom/dummyparent.h"
32 :
33 : #include "dom/factory.h"
34 :
35 : #include "dom/engravingitem.h"
36 : #include "dom/score.h"
37 :
38 : #include "dom/accidental.h"
39 : #include "dom/actionicon.h"
40 : #include "dom/ambitus.h"
41 : #include "dom/articulation.h"
42 : #include "dom/bagpembell.h"
43 : #include "dom/barline.h"
44 : #include "dom/bend.h"
45 : #include "dom/bracket.h"
46 : #include "dom/breath.h"
47 : #include "dom/capo.h"
48 : #include "dom/chordline.h"
49 : #include "dom/clef.h"
50 : #include "dom/dynamic.h"
51 : #include "dom/expression.h"
52 : #include "dom/fermata.h"
53 : #include "dom/fingering.h"
54 : #include "dom/fret.h"
55 : #include "dom/glissando.h"
56 : #include "dom/gradualtempochange.h"
57 : #include "dom/guitarbend.h"
58 : #include "dom/hairpin.h"
59 : #include "dom/hammeronpulloff.h"
60 : #include "dom/harppedaldiagram.h"
61 : #include "dom/instrchange.h"
62 : #include "dom/jump.h"
63 : #include "dom/keysig.h"
64 : #include "dom/letring.h"
65 : #include "dom/line.h"
66 : #include "dom/lyrics.h"
67 : #include "dom/marker.h"
68 : #include "dom/measurenumber.h"
69 : #include "dom/measurerepeat.h"
70 : #include "dom/note.h"
71 : #include "dom/noteline.h"
72 : #include "dom/ornament.h"
73 : #include "dom/ottava.h"
74 : #include "dom/page.h"
75 : #include "dom/palmmute.h"
76 : #include "dom/pedal.h"
77 : #include "dom/playcounttext.h"
78 : #include "dom/playtechannotation.h"
79 : #include "dom/rehearsalmark.h"
80 : #include "dom/slur.h"
81 : #include "dom/soundflag.h"
82 : #include "dom/spacer.h"
83 : #include "dom/staff.h"
84 : #include "dom/stafftext.h"
85 : #include "dom/stavesharingchange.h"
86 : #include "dom/stafftypechange.h"
87 : #include "dom/sticking.h"
88 : #include "dom/stringtunings.h"
89 : #include "dom/symbol.h"
90 : #include "dom/systemtext.h"
91 : #include "dom/tapping.h"
92 : #include "dom/tempotext.h"
93 : #include "dom/text.h"
94 : #include "dom/textline.h"
95 : #include "dom/textlinebase.h"
96 : #include "dom/timesig.h"
97 : #include "dom/tremolobar.h"
98 : #include "dom/tremolosinglechord.h"
99 : #include "dom/tremolotwochord.h"
100 : #include "dom/trill.h"
101 : #include "dom/vibrato.h"
102 : #include "dom/volta.h"
103 : #include "dom/whammybar.h"
104 :
105 : #include "dom/utils.h"
106 :
107 : #include "rendering/score/tlayout.h"
108 : #include "rendering/score/textlayout.h"
109 : #include "rendering/score/tremololayout.h"
110 : #include "rendering/score/chordlayout.h"
111 : #include "rendering/score/slurtielayout.h"
112 :
113 : #include "log.h"
114 :
115 : using namespace muse;
116 : using namespace muse::draw;
117 : using namespace mu::engraving;
118 : using namespace mu::engraving::rendering::score;
119 : using namespace mu::engraving::rendering::single;
120 :
121 0 : void SingleLayout::layoutItem(EngravingItem* item)
122 : {
123 0 : Context ctx(item->score());
124 :
125 0 : switch (item->type()) {
126 0 : case ElementType::ACCIDENTAL: layout(toAccidental(item), ctx);
127 0 : break;
128 0 : case ElementType::ACTION_ICON: layout(toActionIcon(item), ctx);
129 0 : break;
130 0 : case ElementType::AMBITUS: layout(toAmbitus(item), ctx);
131 0 : break;
132 0 : case ElementType::ARPEGGIO: layout(toArpeggio(item), ctx);
133 0 : break;
134 0 : case ElementType::CHORD_BRACKET: layout(toChordBracket(item), ctx);
135 0 : break;
136 0 : case ElementType::ARTICULATION: layout(toArticulation(item), ctx);
137 0 : break;
138 0 : case ElementType::BAGPIPE_EMBELLISHMENT: layout(toBagpipeEmbellishment(item), ctx);
139 0 : break;
140 0 : case ElementType::BAR_LINE: layout(toBarLine(item), ctx);
141 0 : break;
142 0 : case ElementType::BEND: layout(toBend(item), ctx);
143 0 : break;
144 0 : case ElementType::BRACKET: layout(toBracket(item), ctx);
145 0 : break;
146 0 : case ElementType::BREATH: layout(toBreath(item), ctx);
147 0 : break;
148 0 : case ElementType::CAPO: layout(toCapo(item), ctx);
149 0 : break;
150 0 : case ElementType::CHORDLINE: layout(toChordLine(item), ctx);
151 0 : break;
152 0 : case ElementType::CLEF: layout(toClef(item), ctx);
153 0 : break;
154 0 : case ElementType::DYNAMIC: layout(toDynamic(item), ctx);
155 0 : break;
156 0 : case ElementType::EXPRESSION: layout(toExpression(item), ctx);
157 0 : break;
158 0 : case ElementType::FERMATA: layout(toFermata(item), ctx);
159 0 : break;
160 0 : case ElementType::FINGERING: layout(toFingering(item), ctx);
161 0 : break;
162 0 : case ElementType::FRET_DIAGRAM: layout(toFretDiagram(item), ctx);
163 0 : break;
164 0 : case ElementType::FSYMBOL: layout(toFSymbol(item), ctx);
165 0 : break;
166 0 : case ElementType::GLISSANDO: layout(toGlissando(item), ctx);
167 0 : break;
168 0 : case ElementType::GUITAR_BEND: layout(toGuitarBend(item), ctx);
169 0 : break;
170 0 : case ElementType::GRADUAL_TEMPO_CHANGE: layout(toGradualTempoChange(item), ctx);
171 0 : break;
172 0 : case ElementType::HAIRPIN: layout(toHairpin(item), ctx);
173 0 : break;
174 0 : case ElementType::HAMMER_ON_PULL_OFF: layout(toHammerOnPullOff(item), ctx);
175 0 : break;
176 0 : case ElementType::HARP_DIAGRAM: layout(toHarpPedalDiagram(item), ctx);
177 0 : break;
178 0 : case ElementType::IMAGE: layout(toImage(item), ctx);
179 0 : break;
180 0 : case ElementType::INSTRUMENT_CHANGE: layout(toInstrumentChange(item), ctx);
181 0 : break;
182 0 : case ElementType::JUMP: layout(toJump(item), ctx);
183 0 : break;
184 0 : case ElementType::KEYSIG: layout(toKeySig(item), ctx);
185 0 : break;
186 0 : case ElementType::LAYOUT_BREAK: layout(toLayoutBreak(item), ctx);
187 0 : break;
188 0 : case ElementType::LET_RING: layout(toLetRing(item), ctx);
189 0 : break;
190 0 : case ElementType::LYRICS: layout(toLyrics(item), ctx);
191 0 : break;
192 0 : case ElementType::MARKER: layout(toMarker(item), ctx);
193 0 : break;
194 0 : case ElementType::MEASURE_NUMBER: layout(toMeasureNumber(item), ctx);
195 0 : break;
196 0 : case ElementType::MEASURE_REPEAT: layout(toMeasureRepeat(item), ctx);
197 0 : break;
198 0 : case ElementType::NOTEHEAD: layout(toNoteHead(item), ctx);
199 0 : break;
200 0 : case ElementType::NOTELINE: layout(toNoteLine(item), ctx);
201 0 : break;
202 0 : case ElementType::OTTAVA: layout(toOttava(item), ctx);
203 0 : break;
204 0 : case ElementType::ORNAMENT: layout(toOrnament(item), ctx);
205 0 : break;
206 0 : case ElementType::PALM_MUTE: layout(toPalmMute(item), ctx);
207 0 : break;
208 0 : case ElementType::PEDAL: layout(toPedal(item), ctx);
209 0 : break;
210 0 : case ElementType::PLAY_COUNT_TEXT: layout(toPlayCountText(item), ctx);
211 0 : break;
212 0 : case ElementType::PLAYTECH_ANNOTATION: layout(toPlayTechAnnotation(item), ctx);
213 0 : break;
214 0 : case ElementType::REHEARSAL_MARK: layout(toRehearsalMark(item), ctx);
215 0 : break;
216 0 : case ElementType::SLUR: layout(toSlur(item), ctx);
217 0 : break;
218 0 : case ElementType::SPACER: layout(toSpacer(item), ctx);
219 0 : break;
220 0 : case ElementType::STAFF_TEXT: layout(toStaffText(item), ctx);
221 0 : break;
222 0 : case ElementType::STAVE_SHARING_CHANGE: layout(toStaveSharingChange(item), ctx);
223 0 : break;
224 0 : case ElementType::STAVE_SHARING_LABEL: layout(toStaveSharingLabel(item), ctx);
225 0 : break;
226 0 : case ElementType::STAFFTYPE_CHANGE: layout(toStaffTypeChange(item), ctx);
227 0 : break;
228 0 : case ElementType::STICKING: layout(toSticking(item), ctx);
229 0 : break;
230 0 : case ElementType::STRING_TUNINGS: layout(toStringTunings(item), ctx);
231 0 : break;
232 0 : case ElementType::SYMBOL: layout(toSymbol(item), ctx);
233 0 : break;
234 0 : case ElementType::SYSTEM_TEXT: layout(toSystemText(item), ctx);
235 0 : break;
236 0 : case ElementType::SOUND_FLAG: layout(item_cast<SoundFlag*>(item), ctx);
237 0 : break;
238 0 : case ElementType::TAPPING: layout(toTapping(item), ctx);
239 0 : break;
240 0 : case ElementType::TEMPO_TEXT: layout(toTempoText(item), ctx);
241 0 : break;
242 0 : case ElementType::TEXT: layout(toText(item), ctx);
243 0 : break;
244 0 : case ElementType::TEXTLINE: layout(toTextLine(item), ctx);
245 0 : break;
246 0 : case ElementType::TIMESIG: layout(toTimeSig(item), ctx);
247 0 : break;
248 0 : case ElementType::TREMOLO_SINGLECHORD: layout(item_cast<TremoloSingleChord*>(item), ctx);
249 0 : break;
250 0 : case ElementType::TREMOLO_TWOCHORD: layout(item_cast<TremoloTwoChord*>(item), ctx);
251 0 : break;
252 0 : case ElementType::TREMOLOBAR: layout(toTremoloBar(item), ctx);
253 0 : break;
254 0 : case ElementType::TRILL: layout(toTrill(item), ctx);
255 0 : break;
256 0 : case ElementType::VIBRATO: layout(toVibrato(item), ctx);
257 0 : break;
258 0 : case ElementType::VOLTA: layout(toVolta(item), ctx);
259 0 : break;
260 0 : case ElementType::WHAMMY_BAR: layout(toWhammyBar(item), ctx);
261 0 : break;
262 : // drumset
263 0 : case ElementType::CHORD: layout(toChord(item), ctx);
264 0 : break;
265 0 : case ElementType::STEM: layout(toStem(item), ctx);
266 0 : break;
267 0 : default:
268 0 : LOGE() << "Not handled: " << item->typeName();
269 0 : IF_ASSERT_FAILED(false) {
270 : LayoutContext tctx(item->score());
271 : TLayout::layoutItem(item, tctx);
272 : }
273 : break;
274 : }
275 0 : }
276 :
277 0 : void SingleLayout::layoutLineSegment(LineSegment* item, const Context& ctx)
278 : {
279 0 : switch (item->type()) {
280 0 : case ElementType::GRADUAL_TEMPO_CHANGE_SEGMENT: layout(toGradualTempoChangeSegment(item), ctx);
281 0 : break;
282 0 : case ElementType::HAIRPIN_SEGMENT: layout(toHairpinSegment(item), ctx);
283 0 : break;
284 0 : case ElementType::LET_RING_SEGMENT: layout(toLetRingSegment(item), ctx);
285 0 : break;
286 0 : case ElementType::OTTAVA_SEGMENT: layout(toOttavaSegment(item), ctx);
287 0 : break;
288 0 : case ElementType::PALM_MUTE_SEGMENT: layout(toPalmMuteSegment(item), ctx);
289 0 : break;
290 0 : case ElementType::PEDAL_SEGMENT: layout(toPedalSegment(item), ctx);
291 0 : break;
292 0 : case ElementType::TEXTLINE_SEGMENT: layout(toTextLineSegment(item), ctx);
293 0 : break;
294 0 : case ElementType::TRILL_SEGMENT: layout(toTrillSegment(item), ctx);
295 0 : break;
296 0 : case ElementType::VIBRATO_SEGMENT: layout(toVibratoSegment(item), ctx);
297 0 : break;
298 0 : case ElementType::VOLTA_SEGMENT: layout(toVoltaSegment(item), ctx);
299 0 : break;
300 0 : case ElementType::WHAMMY_BAR_SEGMENT: layout(toWhammyBarSegment(item), ctx);
301 0 : break;
302 0 : default:
303 0 : UNREACHABLE;
304 : break;
305 : }
306 0 : }
307 :
308 0 : const MStyle& SingleLayout::Context::style() const
309 : {
310 0 : return m_score->style();
311 : }
312 :
313 0 : std::shared_ptr<IEngravingFont> SingleLayout::Context::engravingFont() const
314 : {
315 0 : return m_score->engravingFont();
316 : }
317 :
318 0 : DummyParent* SingleLayout::Context::dummyParent() const
319 : {
320 0 : return m_score->dummy();
321 : }
322 :
323 0 : void SingleLayout::layout(Accidental* item, const Context&)
324 : {
325 0 : if (!item->ldata() || !item->ldata()->isValid()) {
326 0 : Accidental::LayoutData* ldata = item->mutldata();
327 0 : SymId symId = item->symId();
328 0 : Accidental::LayoutData::Sym s(symId, 0.0, 0.0);
329 0 : ldata->syms.push_back(s);
330 0 : ldata->setBbox(item->symBbox(symId));
331 : }
332 0 : }
333 :
334 0 : void SingleLayout::layout(ActionIcon* item, const Context&)
335 : {
336 0 : FontMetrics fontMetrics(item->iconFont());
337 0 : item->setbbox(fontMetrics.boundingRect(Char(item->icon())));
338 0 : }
339 :
340 0 : void SingleLayout::layout(Ambitus* item, const Context& ctx)
341 : {
342 0 : Ambitus::LayoutData* ldata = item->mutldata();
343 0 : double headWdt = item->headWidth();
344 0 : double spatium = item->spatium();
345 :
346 0 : double lineDist = spatium;
347 0 : constexpr int numOfLines = 3;
348 :
349 : //
350 : // NOTEHEADS Y POS
351 : //
352 : // if pitch == INVALID_PITCH or tpc == Tpc::TPC_INVALID, set to some default:
353 : // for use in palettes and when actual range cannot be calculated (new ambitus or no notes in staff)
354 : //
355 :
356 : // top notehead
357 0 : ldata->topPos.setY(0.0);
358 :
359 : // bottom notehead
360 0 : ldata->bottomPos.setY((numOfLines - 1) * lineDist);
361 :
362 : //
363 : // NOTEHEAD X POS
364 : //
365 : // Note: manages colliding accidentals
366 : //
367 :
368 0 : layout(item->topAccidental(), ctx);
369 0 : layout(item->bottomAccidental(), ctx);
370 :
371 0 : double accNoteDist = item->absoluteFromSpatium(ctx.style().styleS(Sid::accidentalNoteDistance));
372 0 : double xAccidOffTop = item->topAccidental()->width(LD_ACCESS::BAD) + accNoteDist;
373 0 : double xAccidOffBottom = item->bottomAccidental()->width(LD_ACCESS::BAD) + accNoteDist;
374 :
375 0 : switch (item->direction()) {
376 0 : case DirectionH::AUTO: // noteheads one above the other
377 : // left align noteheads and right align accidentals 'hanging' on the left
378 0 : ldata->topPos.setX(0.0);
379 0 : ldata->bottomPos.setX(0.0);
380 0 : item->topAccidental()->mutldata()->setPosX(-xAccidOffTop);
381 0 : item->bottomAccidental()->mutldata()->setPosX(-xAccidOffBottom);
382 0 : break;
383 0 : case DirectionH::LEFT: // top notehead at the left of bottom notehead
384 : // place top notehead at left margin; bottom notehead at right of top head;
385 : // top accid. 'hanging' on left of top head and bottom accid. 'hanging' at left of bottom head
386 0 : ldata->topPos.setX(0.0);
387 0 : ldata->bottomPos.setX(headWdt);
388 0 : item->topAccidental()->mutldata()->setPosX(-xAccidOffTop);
389 0 : item->bottomAccidental()->mutldata()->setPosX(headWdt - xAccidOffBottom);
390 0 : break;
391 0 : case DirectionH::RIGHT: // top notehead at the right of bottom notehead
392 : // bottom notehead at left margin; top notehead at right of bottomnotehead
393 : // top accid. 'hanging' on left of top head and bottom accid. 'hanging' at left of bottom head
394 0 : ldata->bottomPos.setX(0.0);
395 0 : ldata->topPos.setX(headWdt);
396 0 : item->bottomAccidental()->mutldata()->setPosX(-xAccidOffBottom);
397 0 : item->topAccidental()->mutldata()->setPosX(headWdt - xAccidOffTop);
398 0 : break;
399 : }
400 :
401 : // compute line from top note centre to bottom note centre
402 0 : LineF fullLine(ldata->topPos.x() + headWdt * 0.5,
403 : ldata->topPos.y(),
404 0 : ldata->bottomPos.x() + headWdt * 0.5,
405 0 : ldata->bottomPos.y());
406 : // shorten line on each side by offsets
407 0 : double yDelta = ldata->bottomPos.y() - ldata->topPos.y();
408 0 : if (!RealIsNull(yDelta)) {
409 0 : double off = Ambitus::LINEOFFSET_DEFAULT.toAbsolute(spatium);
410 0 : PointF p1 = fullLine.pointAt(off / yDelta);
411 0 : PointF p2 = fullLine.pointAt(1 - (off / yDelta));
412 0 : ldata->line = LineF(p1, p2);
413 : } else {
414 0 : ldata->line = fullLine;
415 : }
416 :
417 0 : RectF headRect(0, -0.5 * spatium, headWdt, 1 * spatium);
418 0 : ldata->setBbox(headRect.translated(ldata->topPos).united(headRect.translated(ldata->bottomPos))
419 0 : .united(item->topAccidental()->ldata()->bbox(LD_ACCESS::BAD)
420 0 : .translated(item->topAccidental()->ldata()->pos()))
421 0 : .united(item->bottomAccidental()->ldata()->bbox(LD_ACCESS::BAD)
422 0 : .translated(item->bottomAccidental()->ldata()->pos()))
423 : );
424 0 : }
425 :
426 0 : void SingleLayout::layout(Arpeggio* item, const Context& ctx)
427 : {
428 0 : Arpeggio::LayoutData* ldata = item->mutldata();
429 :
430 0 : auto symbolLine = [](const std::shared_ptr<const IEngravingFont>& f, Arpeggio::LayoutData* data, SymId end, SymId fill)
431 : {
432 0 : data->symbols.clear();
433 :
434 0 : double w = data->bottom - data->top;
435 0 : double w1 = f->advance(end, data->magS);
436 0 : double w2 = f->advance(fill, data->magS);
437 0 : int n = lrint((w - w1) / w2);
438 0 : for (int i = 0; i < n; ++i) {
439 0 : data->symbols.push_back(fill);
440 : }
441 0 : data->symbols.push_back(end);
442 0 : };
443 :
444 0 : ldata->arpeggioHeight = item->spatium() * 4;
445 0 : ldata->top = 0.0;
446 0 : ldata->bottom = ldata->arpeggioHeight;
447 :
448 0 : ldata->setMag(item->staff() ? item->staff()->staffMag(item->tick()) : item->mag());
449 0 : ldata->magS = ldata->mag() * (ctx.style().spatium() / ctx.style().defaultSpatium());
450 :
451 0 : std::shared_ptr<const IEngravingFont> font = ctx.engravingFont();
452 0 : switch (item->arpeggioType()) {
453 0 : case ArpeggioType::NORMAL: {
454 0 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUp, SymId::wiggleArpeggiatoUp);
455 : // string is rotated -90 degrees
456 0 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
457 0 : ldata->setBbox(RectF(0.0, -ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
458 0 : } break;
459 :
460 0 : case ArpeggioType::UP: {
461 0 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUpArrow, SymId::wiggleArpeggiatoUp);
462 : // string is rotated -90 degrees
463 0 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
464 0 : ldata->setBbox(RectF(0.0, -ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
465 0 : } break;
466 :
467 0 : case ArpeggioType::DOWN: {
468 0 : symbolLine(font, ldata, SymId::wiggleArpeggiatoUpArrow, SymId::wiggleArpeggiatoUp);
469 : // string is rotated +90 degrees (so that UpArrow turns into a DownArrow)
470 0 : ldata->symsBBox = font->bbox(ldata->symbols, ldata->magS);
471 0 : ldata->setBbox(RectF(0.0, ldata->symsBBox.x() + ldata->top, ldata->symsBBox.height(), ldata->symsBBox.width()));
472 0 : } break;
473 :
474 0 : case ArpeggioType::UP_STRAIGHT: {
475 0 : double x1 = item->spatium() * 0.5;
476 0 : ldata->symsBBox = font->bbox(SymId::arrowheadBlackUp, ldata->magS);
477 0 : double w = ldata->symsBBox.width();
478 0 : ldata->setBbox(RectF(x1 - w * 0.5, ldata->top, w, ldata->bottom));
479 0 : } break;
480 :
481 0 : case ArpeggioType::DOWN_STRAIGHT: {
482 0 : double x1 = item->spatium() * 0.5;
483 0 : ldata->symsBBox = font->bbox(SymId::arrowheadBlackDown, ldata->magS);
484 0 : double w = ldata->symsBBox.width();
485 0 : ldata->setBbox(RectF(x1 - w * 0.5, ldata->top, w, ldata->bottom));
486 0 : } break;
487 :
488 0 : case ArpeggioType::BRACKET: {
489 0 : double w = ctx.style().styleS(Sid::arpeggioHookLen).val() * item->spatium();
490 0 : ldata->setBbox(RectF(0.0, ldata->top, w, ldata->bottom));
491 0 : } break;
492 : }
493 0 : }
494 :
495 0 : void SingleLayout::layout(ChordBracket* item, const Context& ctx)
496 : {
497 0 : ChordBracket::LayoutData* ldata = item->mutldata();
498 :
499 0 : ldata->arpeggioHeight = item->spatium() * 2;
500 0 : ldata->top = 0.0;
501 0 : ldata->bottom = ldata->arpeggioHeight;
502 0 : ldata->setMag(1);
503 0 : ldata->magS = 1;
504 :
505 0 : double w = item->absoluteFromSpatium(ctx.style().styleS(Sid::chordBracketHookLen));
506 0 : ldata->setBbox(RectF(0.0, ldata->top, w, ldata->bottom));
507 0 : }
508 :
509 0 : void SingleLayout::layout(Articulation* item, const Context& ctx)
510 : {
511 0 : RectF bbox;
512 :
513 0 : if (item->textType() != ArticulationTextType::NO_TEXT) {
514 0 : if (!item->text()) {
515 0 : Text* text = new Text(item, TextStyleType::ARTICULATION);
516 0 : static const ElementStyle elementStyle = {};
517 0 : text->initElementStyle(&elementStyle);
518 0 : item->setText(text);
519 : }
520 :
521 0 : Text* text = item->text();
522 0 : text->setXmlText(TConv::text(item->textType()));
523 0 : text->setTrack(item->track());
524 0 : text->setOwnershipParent(item);
525 :
526 0 : layoutTextBase(item->text(), ctx, item->text()->mutldata());
527 0 : bbox = text->ldata()->bbox();
528 : } else {
529 0 : bbox = item->symBbox(item->symId());
530 0 : bbox.translate(-0.5 * bbox.width(), 0.0);
531 : }
532 :
533 0 : item->setbbox(bbox);
534 0 : }
535 :
536 0 : void SingleLayout::layout(BagpipeEmbellishment* item, const Context& ctx)
537 : {
538 0 : BagpipeEmbellishment::LayoutData* ldata = item->mutldata();
539 0 : const double mags = item->magS() * 0.75; // grace head magnification
540 0 : const double spatium = ctx.style().spatium();
541 :
542 0 : const BagpipeNoteList nl = item->resolveNoteList();
543 :
544 0 : ldata->clearBbox();
545 0 : ldata->isDrawBeam = nl.size() > 1;
546 0 : ldata->isDrawFlag = nl.size() == 1;
547 0 : ldata->spatium = spatium;
548 0 : ldata->headsym = SymId::noteheadBlack;
549 0 : ldata->flagsym = SymId::flag32ndUp;
550 0 : ldata->stemLineW = ldata->spatium * 0.1;
551 :
552 0 : BagpipeEmbellishment::LayoutData::BeamData& dataBeam = ldata->beamData;
553 0 : dataBeam.y = (-8 * spatium / 2);
554 0 : dataBeam.width = (0.3 * spatium);
555 :
556 0 : const RectF headBBox = ctx.engravingFont()->bbox(ldata->headsym, mags);
557 0 : const RectF flagBBox = ctx.engravingFont()->bbox(ldata->flagsym, mags);
558 0 : const double xcorr = spatium * 0.1; // correction to align flag with top of stem
559 :
560 0 : double headW = headBBox.width();
561 0 : const double headw = 1.2 * headW; // grace head width
562 0 : const double headp = 1.6 * headW; // horizontal head pitch
563 0 : const double xl = (1 - 1.6 * (nl.size() - 1)) * headW / 2; // calc x for stem of leftmost note
564 :
565 : // draw the notes including stem, (optional) flag and (optional) ledger line
566 0 : double x = xl;
567 0 : for (size_t i = 0; i < nl.size(); ++i) {
568 0 : BagpipeEmbellishment::LayoutData::NoteData& noteData = ldata->notesData[i];
569 0 : const int line = BagpipeEmbellishment::BAGPIPE_NOTEINFO_LIST[nl.at(i)].line;
570 0 : const double y1f = ((line - 6) * spatium / 2); // top of stem for note with flag
571 0 : const double y2 = (line * spatium / 2); // bottom of stem
572 0 : const double ycorr = (0.8 * spatium); // correction to align flag with top of stem
573 :
574 : // head
575 0 : noteData.headXY = PointF(x - headw, y2);
576 0 : ldata->addBbox(headBBox.translated(noteData.headXY));
577 :
578 : // stem
579 : // top of stems actually used
580 0 : double y1 = ldata->isDrawFlag ? y1f : dataBeam.y;
581 0 : noteData.stemLine = LineF(x - ldata->stemLineW * .5, y1, x - ldata->stemLineW * .5, y2);
582 0 : ldata->addBbox(RectF(x - ldata->stemLineW * .5 - headw, y1, ldata->stemLineW, y2 - y1));
583 :
584 : // flag
585 0 : if (ldata->isDrawFlag) {
586 0 : noteData.flagXY = PointF(x - ldata->stemLineW * .5 + xcorr, y1 + ycorr);
587 0 : ldata->addBbox(flagBBox.translated(noteData.flagXY));
588 : }
589 :
590 : // draw the ledger line for high A
591 0 : if (line == -2) {
592 0 : noteData.ledgerLine = LineF(x - headw * 1.5 - ldata->stemLineW * .5, y2, x + headw * .5 - ldata->stemLineW * .5, y2);
593 0 : ldata->addBbox(RectF(x - headw * 1.5 - ldata->stemLineW * .5, y2 - ldata->stemLineW * 2, headw * 2, ldata->stemLineW));
594 : }
595 :
596 : // move x to next note x position
597 0 : x += headp;
598 : }
599 :
600 0 : dataBeam.x1 = xl - ldata->stemLineW * .5;
601 0 : dataBeam.x2 = x - headp - ldata->stemLineW * .5;
602 0 : }
603 :
604 0 : void SingleLayout::layout(BarLine* item, const Context& ctx)
605 : {
606 0 : BarLine::LayoutData* ldata = item->mutldata();
607 0 : ldata->setPos(PointF());
608 0 : ldata->setMag(1.0);
609 :
610 0 : double spatium = item->spatium();
611 0 : ldata->y1 = (spatium * .5 * item->spanFrom());
612 0 : if (muse::RealIsEqual(ldata->y2, 0.0)) {
613 0 : ldata->y2 = (spatium * .5 * (8.0 + item->spanTo()));
614 : }
615 :
616 0 : auto layoutWidth = [](BarLine* item, const Context& ctx) {
617 0 : const double dotWidth = item->symWidth(SymId::repeatDot);
618 :
619 0 : double w = 0.0;
620 0 : switch (item->barLineType()) {
621 0 : case BarLineType::DOUBLE:
622 0 : w = ctx.style().styleAbsolute(Sid::doubleBarWidth) * 2.0 + ctx.style().styleAbsolute(Sid::doubleBarDistance);
623 0 : break;
624 0 : case BarLineType::DOUBLE_HEAVY:
625 0 : w = ctx.style().styleAbsolute(Sid::endBarWidth) * 2.0 + ctx.style().styleAbsolute(Sid::endBarDistance);
626 0 : break;
627 0 : case BarLineType::END_START_REPEAT:
628 0 : w = ctx.style().styleAbsolute(Sid::endBarWidth)
629 0 : + ctx.style().styleAbsolute(Sid::barWidth) * 2.0
630 0 : + ctx.style().styleAbsolute(Sid::endBarDistance) * 2.0
631 0 : + ctx.style().styleAbsolute(Sid::repeatBarlineDotSeparation) * 2.0
632 0 : + dotWidth * 2;
633 0 : break;
634 0 : case BarLineType::START_REPEAT:
635 : case BarLineType::END_REPEAT:
636 0 : w = ctx.style().styleAbsolute(Sid::endBarWidth)
637 0 : + ctx.style().styleAbsolute(Sid::barWidth)
638 0 : + ctx.style().styleAbsolute(Sid::endBarDistance)
639 0 : + ctx.style().styleAbsolute(Sid::repeatBarlineDotSeparation)
640 : + dotWidth;
641 0 : break;
642 0 : case BarLineType::END:
643 : case BarLineType::REVERSE_END:
644 0 : w = ctx.style().styleAbsolute(Sid::endBarWidth)
645 0 : + ctx.style().styleAbsolute(Sid::barWidth)
646 0 : + ctx.style().styleAbsolute(Sid::endBarDistance);
647 0 : break;
648 0 : case BarLineType::BROKEN:
649 0 : w = ctx.style().styleAbsolute(Sid::dashBarWidth);
650 0 : break;
651 0 : case BarLineType::NORMAL:
652 : case BarLineType::DOTTED:
653 0 : w = ctx.style().styleAbsolute(Sid::barWidth);
654 0 : break;
655 0 : case BarLineType::HEAVY:
656 0 : w = ctx.style().styleAbsolute(Sid::endBarWidth);
657 0 : break;
658 : }
659 0 : return w;
660 : };
661 :
662 0 : double w = layoutWidth(item, ctx) * ldata->mag();
663 0 : RectF bbox(0.0, ldata->y1, w, ldata->y2 - ldata->y1);
664 0 : ldata->setBbox(bbox);
665 0 : }
666 :
667 0 : void SingleLayout::layout(Bend* item, const Context&)
668 : {
669 0 : Bend::LayoutData* ldata = item->mutldata();
670 0 : double spatium = item->spatium();
671 0 : double lw = item->absoluteFromSpatium(item->lineWidth());
672 :
673 0 : ldata->noteWidth = 0.0;
674 0 : ldata->notePos = PointF();
675 :
676 0 : RectF bb;
677 :
678 0 : FontMetrics fm(item->font(spatium));
679 :
680 0 : size_t n = item->points().size();
681 0 : double x = ldata->noteWidth;
682 0 : double y = -spatium * .8;
683 : double x2, y2;
684 :
685 0 : double aw = spatium * .5;
686 0 : PolygonF arrowUp;
687 0 : arrowUp << PointF(0, 0) << PointF(aw * .5, aw) << PointF(-aw * .5, aw);
688 0 : PolygonF arrowDown;
689 0 : arrowDown << PointF(0, 0) << PointF(aw * .5, -aw) << PointF(-aw * .5, -aw);
690 :
691 0 : for (size_t pt = 0; pt < n; ++pt) {
692 0 : if (pt == (n - 1)) {
693 0 : break;
694 : }
695 0 : int pitch = item->points().at(pt).pitch;
696 0 : if (pt == 0 && pitch) {
697 0 : y2 = -ldata->notePos.y() - spatium * 2;
698 0 : x2 = x;
699 0 : bb.unite(RectF(x, y, x2 - x, y2 - y));
700 :
701 0 : bb.unite(arrowUp.translated(x2, y2 + spatium * .2).boundingRect());
702 :
703 0 : int idx = (pitch + 12) / 25;
704 0 : const char* l = Bend::label[idx];
705 0 : bb.unite(fm.boundingRect(String::fromAscii(l)));
706 0 : y = y2;
707 : }
708 0 : if (pitch == item->points().at(pt + 1).pitch) {
709 0 : if (pt == (n - 2)) {
710 0 : break;
711 : }
712 0 : x2 = x + spatium;
713 0 : y2 = y;
714 0 : bb.unite(RectF(x, y, x2 - x, y2 - y));
715 0 : } else if (pitch < item->points().at(pt + 1).pitch) {
716 : // up
717 0 : x2 = x + spatium * .5;
718 0 : y2 = -ldata->notePos.y() - spatium * 2;
719 0 : double dx = x2 - x;
720 0 : double dy = y2 - y;
721 :
722 0 : PainterPath path;
723 0 : path.moveTo(x, y);
724 0 : path.cubicTo(x + dx / 2, y, x2, y + dy / 4, x2, y2);
725 0 : bb.unite(path.boundingRect());
726 0 : bb.unite(arrowUp.translated(x2, y2 + spatium * .2).boundingRect());
727 :
728 0 : int idx = (item->points().at(pt + 1).pitch + 12) / 25;
729 0 : const char* l = Bend::label[idx];
730 0 : bb.unite(fm.boundingRect(String::fromAscii(l)));
731 0 : } else {
732 : // down
733 0 : x2 = x + spatium * .5;
734 0 : y2 = y + spatium * 3;
735 0 : double dx = x2 - x;
736 0 : double dy = y2 - y;
737 :
738 0 : PainterPath path;
739 0 : path.moveTo(x, y);
740 0 : path.cubicTo(x + dx / 2, y, x2, y + dy / 4, x2, y2);
741 0 : bb.unite(path.boundingRect());
742 :
743 0 : bb.unite(arrowDown.translated(x2, y2 - spatium * .2).boundingRect());
744 0 : }
745 0 : x = x2;
746 0 : y = y2;
747 : }
748 0 : bb.adjust(-lw, -lw, lw, lw);
749 0 : ldata->setBbox(bb);
750 0 : ldata->setPos(0.0, 0.0);
751 0 : }
752 :
753 0 : void SingleLayout::layout(Bracket* item, const Context& ctx)
754 : {
755 0 : Bracket::LayoutData* ldata = item->mutldata();
756 :
757 0 : ldata->bracketHeight = 3.5 * item->spatium() * 2;
758 0 : ldata->braceSymbol = item->braceSymbol();
759 :
760 0 : Shape shape;
761 :
762 0 : switch (item->bracketType()) {
763 0 : case BracketType::BRACE: {
764 0 : if (item->braceSymbol() == SymId::noSym) {
765 0 : ldata->braceSymbol = SymId::brace;
766 : }
767 0 : double h = ldata->bracketHeight;
768 0 : double w = item->symWidth(ldata->braceSymbol) * item->magx();
769 0 : ldata->setBbox(RectF(0, 0, w, h));
770 0 : ldata->shape.add(ldata->bbox());
771 0 : ldata->bracketWidth = w + ctx.style().styleAbsolute(Sid::akkoladeBarDistance);
772 : }
773 0 : break;
774 0 : case BracketType::NORMAL: {
775 0 : double spatium = item->spatium();
776 0 : double w = ctx.style().styleAbsolute(Sid::bracketWidth) * 0.5;
777 0 : double x = -w;
778 :
779 0 : double bd = spatium * 0.5;
780 0 : shape.add(RectF(x, -bd, w * 2, 2 * (item->ldata()->h2() + bd)));
781 0 : shape.add(item->symBbox(SymId::bracketTop).translated(PointF(-w, -bd)));
782 0 : shape.add(item->symBbox(SymId::bracketBottom).translated(PointF(-w, bd + 2 * item->ldata()->h2())));
783 :
784 0 : w += item->symWidth(SymId::bracketTop);
785 0 : double y = -item->symHeight(SymId::bracketTop) - bd;
786 0 : double h = (-y + item->ldata()->h2()) * 2;
787 0 : ldata->setBbox(x, y, w, h);
788 :
789 0 : ldata->bracketWidth = ctx.style().styleAbsolute(Sid::bracketWidth) + ctx.style().styleAbsolute(Sid::bracketDistance);
790 : }
791 0 : break;
792 0 : case BracketType::SQUARE:
793 : {
794 0 : double w = ctx.style().styleAbsolute(Sid::staffLineWidth) * .5;
795 0 : double x = -w;
796 0 : double y = -w;
797 0 : double h = (item->ldata()->h2() + w) * 2;
798 0 : w += (0.5 * item->spatium() + 3 * w);
799 0 : ldata->setBbox(x, y, w, h);
800 0 : shape.add(item->ldata()->bbox());
801 :
802 0 : ldata->bracketWidth = ctx.style().styleAbsolute(Sid::staffLineWidth) / 2 + 0.5 * item->spatium();
803 : }
804 0 : break;
805 0 : case BracketType::LINE: {
806 0 : double spatium = item->spatium();
807 0 : double w = 0.67 * ctx.style().styleAbsolute(Sid::bracketWidth) * 0.5;
808 0 : double x = -w;
809 0 : double bd = spatium * 0.25;
810 0 : double y = -bd;
811 0 : double h = (-y + item->ldata()->h2()) * 2;
812 0 : ldata->setBbox(x, y, w, h);
813 0 : shape.add(item->ldata()->bbox());
814 :
815 0 : ldata->bracketWidth = 0.67 * ctx.style().styleAbsolute(Sid::bracketWidth) + ctx.style().styleAbsolute(Sid::bracketDistance);
816 : }
817 0 : break;
818 0 : case BracketType::GROUP:
819 0 : layoutGroupBracket(item, ctx);
820 0 : break;
821 0 : case BracketType::NO_BRACKET:
822 0 : break;
823 : }
824 :
825 0 : ldata->shape = shape;
826 0 : }
827 :
828 0 : void SingleLayout::layoutGroupBracket(Bracket* item, const Context& ctx)
829 : {
830 0 : Bracket::LayoutData* ldata = item->mutldata();
831 0 : ldata->bracketHeight = 12 * item->spatium();
832 0 : double w = ctx.style().styleAbsolute(Sid::staffLineWidth) * 0.5;
833 0 : double x = 0.0;
834 0 : double y = -w;
835 0 : double h = (ldata->bracketHeight * 0.5 + w) * 2;
836 0 : double width = item->spatium();
837 0 : ldata->setBbox(RectF(x, y, width, h));
838 0 : ldata->shape.add(ldata->bbox());
839 :
840 0 : ldata->bracketWidth = width;
841 :
842 0 : if (!item->text()) {
843 0 : const_cast<Bracket*>(item)->setText(new Text(const_cast<Bracket*>(item)));
844 : }
845 :
846 0 : Text* text = item->text();
847 0 : text->setAlign(Align(AlignH::HCENTER, AlignV::VCENTER));
848 0 : text->setPosition(AlignH::HCENTER);
849 0 : text->setTextAngle(-90);
850 0 : text->setXmlText(item->bracketItem()->longName());
851 0 : layout(text, ctx);
852 0 : text->mutldata()->setPos(0.0, item->ldata()->bbox().height() / 2);
853 :
854 0 : double textPadding = 0.5 * item->spatium();
855 0 : RectF mask = text->ldata()->bbox().translated(text->pos()).padded(textPadding);
856 0 : ldata->setMask(mask);
857 0 : }
858 :
859 0 : void SingleLayout::layout(Breath* item, const Context&)
860 : {
861 0 : item->setbbox(item->symBbox(item->symId()));
862 0 : }
863 :
864 0 : void SingleLayout::layout(Capo* item, const Context& ctx)
865 : {
866 0 : layoutTextBase(item, ctx, item->mutldata());
867 0 : }
868 :
869 0 : void SingleLayout::layout(Chord* item, const Context& ctx)
870 : {
871 0 : LayoutContext tctx(ctx.dontUseScore());
872 0 : ChordLayout::computeUp(item, tctx);
873 0 : ChordLayout::layout(item, tctx);
874 0 : ChordLayout::layoutStem(item, tctx);
875 0 : ChordLayout::layoutLedgerLines({ item }, tctx);
876 0 : }
877 :
878 0 : void SingleLayout::layout(ChordLine* item, const Context& ctx)
879 : {
880 0 : ChordLine::LayoutData* ldata = item->mutldata();
881 0 : ldata->setMag(1.0);
882 0 : TLayout::layoutChordLinePath(item, ldata, item->spatium(), item->chordMag());
883 :
884 0 : ldata->setPos(0.0, 0.0);
885 :
886 0 : if (!item->isWavy()) {
887 0 : RectF r = ldata->path.boundingRect();
888 0 : int x1 = 0, y1 = 0, width = 0, height = 0;
889 :
890 0 : x1 = r.x();
891 0 : y1 = r.y();
892 0 : width = r.width();
893 0 : height = r.height();
894 0 : ldata->setBbox(x1, y1, width, height);
895 : } else {
896 0 : RectF r = ctx.engravingFont()->bbox(item->waveSym(), item->magS());
897 :
898 0 : ldata->setBbox(r);
899 : }
900 0 : }
901 :
902 0 : void SingleLayout::layout(Clef* item, const Context& ctx)
903 : {
904 0 : Clef::LayoutData* ldata = item->mutldata();
905 0 : constexpr int lines = 5;
906 0 : constexpr Spatium lineDist = 1.0_sp;
907 0 : double spatium = ctx.style().spatium();
908 0 : Spatium yoff = 0.0_sp;
909 :
910 0 : if (item->clefType() != ClefType::INVALID && item->clefType() != ClefType::MAX) {
911 0 : ldata->symId = ClefInfo::symId(item->clefType());
912 0 : yoff = lineDist * (5 - ClefInfo::line(item->clefType()));
913 : } else {
914 0 : ldata->symId = SymId::noSym;
915 : }
916 :
917 0 : switch (item->clefType()) {
918 0 : case ClefType::C_19C: // 19th C clef is like a G clef
919 0 : yoff = lineDist * 1.5;
920 0 : break;
921 0 : case ClefType::TAB: // TAB clef
922 : case ClefType::TAB4: // TAB clef 4 strings
923 : case ClefType::TAB_SERIF: // TAB clef alternate style
924 : case ClefType::TAB4_SERIF: // TAB clef alternate style
925 0 : yoff = lineDist * (lines - 1) * 0.5;
926 0 : break;
927 0 : case ClefType::PERC: // percussion clefs
928 : case ClefType::PERC2:
929 0 : yoff = lineDist * (lines - 1) * 0.5;
930 0 : break;
931 0 : default:
932 0 : break;
933 : }
934 :
935 0 : ldata->setPos(0.0, yoff.toAbsolute(spatium));
936 :
937 0 : RectF bbox = item->symBbox(ldata->symId);
938 0 : ldata->setBbox(bbox);
939 0 : }
940 :
941 0 : void SingleLayout::layout(Expression* item, const Context& ctx)
942 : {
943 0 : layoutTextBase(item, ctx, item->mutldata());
944 0 : }
945 :
946 0 : void SingleLayout::layout(Fermata* item, const Context&)
947 : {
948 0 : item->setPos(PointF());
949 0 : item->setOffset(PointF());
950 0 : RectF b(item->symBbox(item->symId()));
951 0 : item->setbbox(b.translated(-0.5 * b.width(), 0.0));
952 0 : }
953 :
954 0 : void SingleLayout::layout(Fingering* item, const Context& ctx)
955 : {
956 0 : layoutTextBase(item, ctx, item->mutldata());
957 0 : }
958 :
959 0 : void SingleLayout::layout(FretDiagram* item, const Context& ctx)
960 : {
961 0 : FretDiagram::LayoutData* ldata = item->mutldata();
962 0 : double spatium = item->spatium();
963 0 : ldata->stringLineWidth = (spatium * 0.08);
964 0 : ldata->nutLineWidth = ((item->fretOffset() || !item->showNut()) ? ldata->stringLineWidth : spatium * 0.2);
965 0 : ldata->stringDist = (ctx.style().styleAbsolute(Sid::fretStringSpacing));
966 0 : ldata->fretDist = (ctx.style().styleAbsolute(Sid::fretFretSpacing));
967 0 : ldata->markerSize = (ldata->stringDist * 0.8);
968 :
969 0 : double w = ldata->stringDist * (item->strings() - 1) + ldata->markerSize;
970 0 : double h = (item->frets() + 1) * ldata->fretDist + ldata->markerSize;
971 0 : double y = -(ldata->markerSize * 0.5 + ldata->fretDist);
972 0 : double x = -(ldata->markerSize * 0.5);
973 :
974 : // Allocate space for fret offset number
975 0 : if (item->fretOffset() > 0) {
976 0 : Font scaledFont(item->fretNumFont());
977 0 : scaledFont.setPointSizeF(item->fretNumFont().pointSizeF() * item->userMag());
978 :
979 0 : FontMetrics fm2(scaledFont);
980 0 : double numw = fm2.width(String::number(item->fretOffset() + 1));
981 0 : double xdiff = numw + ldata->stringDist * .4;
982 0 : w += xdiff;
983 0 : x += (item->numPos() == 0) == (item->orientation() == engraving::Orientation::VERTICAL) ? -xdiff : 0;
984 0 : }
985 :
986 0 : if (item->orientation() == engraving::Orientation::HORIZONTAL) {
987 0 : std::swap(w, h);
988 0 : std::swap(x, y);
989 : }
990 :
991 0 : ldata->setBbox(x, y, w, h);
992 0 : }
993 :
994 0 : void SingleLayout::layout(FSymbol* item, const Context&)
995 : {
996 0 : item->setbbox(FontMetrics::boundingRect(item->font(), item->toString()));
997 0 : item->setOffset(0.0, 0.0);
998 0 : item->setPos(0.0, 0.0);
999 0 : }
1000 :
1001 0 : void SingleLayout::layout(Dynamic* item, const Context& ctx)
1002 : {
1003 0 : layoutTextBase(item, ctx, item->mutldata());
1004 0 : }
1005 :
1006 0 : void SingleLayout::layout(Glissando* item, const Context& ctx)
1007 : {
1008 0 : double spatium = item->spatium();
1009 :
1010 0 : if (item->spannerSegments().empty()) {
1011 0 : item->add(item->createLineSegment());
1012 : }
1013 0 : LineSegment* s = item->frontSegment();
1014 0 : s->setPos(PointF(-spatium * Glissando::GLISS_PALETTE_WIDTH / 2, spatium * Glissando::GLISS_PALETTE_HEIGHT / 2));
1015 0 : s->setPos2(PointF(spatium * Glissando::GLISS_PALETTE_WIDTH, -spatium * Glissando::GLISS_PALETTE_HEIGHT));
1016 0 : layout(static_cast<GlissandoSegment*>(s), ctx);
1017 0 : }
1018 :
1019 0 : void SingleLayout::layout(GlissandoSegment* item, const Context&)
1020 : {
1021 0 : if (item->pos2().x() <= 0) {
1022 0 : item->setbbox(RectF());
1023 0 : return;
1024 : }
1025 :
1026 0 : RectF r = RectF(0.0, 0.0, item->pos2().x(), item->pos2().y()).normalized();
1027 0 : double lw = item->absoluteFromSpatium(item->lineWidth()) * .5;
1028 0 : item->setbbox(r.adjusted(-lw, -lw, lw, lw));
1029 : }
1030 :
1031 0 : void SingleLayout::layout(GradualTempoChange* item, const Context& ctx)
1032 : {
1033 0 : layoutLine(item, ctx);
1034 0 : }
1035 :
1036 0 : void SingleLayout::layout(GradualTempoChangeSegment* item, const Context& ctx)
1037 : {
1038 0 : layoutTextLineBaseSegment(item, ctx);
1039 0 : item->setOffset(PointF());
1040 0 : }
1041 :
1042 0 : void SingleLayout::layout(GuitarBend*, const Context&)
1043 : {
1044 0 : NOT_IMPLEMENTED;
1045 : //! NOTE: Bends can be removed from disallowed elements in NotationInteraction::isOutgoingDragElementAllowed once this has been implemented
1046 0 : }
1047 :
1048 0 : void SingleLayout::layout(GuitarBendSegment*, const Context&)
1049 : {
1050 0 : NOT_IMPLEMENTED;
1051 0 : }
1052 :
1053 0 : void SingleLayout::layout(Hairpin* item, const Context& ctx)
1054 : {
1055 0 : item->setPos(0.0, 0.0);
1056 0 : layoutLine(item, ctx);
1057 0 : }
1058 :
1059 0 : void SingleLayout::layout(HammerOnPullOff* item, const Context& ctx)
1060 : {
1061 0 : double spatium = item->spatium();
1062 0 : HammerOnPullOffSegment* s = nullptr;
1063 0 : if (item->spannerSegments().empty()) {
1064 0 : s = new HammerOnPullOffSegment(item);
1065 0 : s->setTrack(item->track());
1066 0 : item->add(s);
1067 : } else {
1068 0 : s = toHammerOnPullOffSegment(item->frontSegment());
1069 : }
1070 :
1071 0 : s->setSpannerSegmentType(SpannerSegmentType::SINGLE);
1072 :
1073 0 : s->setPos(PointF());
1074 0 : s->ups(Grip::START).p = PointF(0, 0);
1075 0 : s->ups(Grip::END).p = PointF(spatium * 6, 0);
1076 0 : s->setExtraHeight(0.0);
1077 :
1078 0 : SlurTieLayout::computeBezier(s);
1079 :
1080 0 : layout(s, ctx);
1081 :
1082 0 : item->setbbox(s->ldata()->bbox());
1083 0 : }
1084 :
1085 0 : void SingleLayout::layout(HammerOnPullOffSegment* item, const Context& ctx)
1086 : {
1087 0 : const std::vector<HammerOnPullOffText*>& hopoTexts = item->hopoText();
1088 0 : if (item->hopoText().empty()) {
1089 0 : item->addHopoText(new HammerOnPullOffText(item));
1090 : }
1091 :
1092 0 : HammerOnPullOffText* hopoText = hopoTexts.front();
1093 0 : hopoText->setXmlText("H/P");
1094 :
1095 0 : Align align;
1096 0 : align.vertical = AlignV::BASELINE;
1097 0 : align.horizontal = AlignH::HCENTER;
1098 0 : hopoText->setAlign(align);
1099 0 : hopoText->setPosition(AlignH::HCENTER);
1100 0 : layoutTextBase(hopoText, ctx, hopoText->mutldata());
1101 :
1102 0 : RectF bbox = item->ldata()->bbox();
1103 0 : double x = 0.5 * (bbox.left() + bbox.right());
1104 0 : double y = bbox.top() - 0.5 * item->spatium();
1105 0 : hopoText->mutldata()->setPos(x, y);
1106 :
1107 0 : Shape itemShape = item->mutldata()->shape();
1108 0 : itemShape.add(hopoText->shape().translated(hopoText->pos()));
1109 0 : item->mutldata()->setShape(itemShape);
1110 0 : }
1111 :
1112 0 : void SingleLayout::layout(HairpinSegment* item, const Context& ctx)
1113 : {
1114 0 : const double spatium = item->spatium();
1115 0 : HairpinSegment::LayoutData* ldata = item->mutldata();
1116 :
1117 0 : HairpinType type = item->hairpin()->hairpinType();
1118 0 : if (item->hairpin()->isLineType()) {
1119 0 : item->setTwoLines(false);
1120 0 : layoutTextLineBaseSegment(item, ctx);
1121 0 : item->setDrawCircledTip(false);
1122 0 : item->setCircledTipRadius(0.0);
1123 : } else {
1124 0 : item->setTwoLines(true);
1125 :
1126 0 : item->hairpin()->setBeginTextAlign({ AlignH::LEFT, AlignV::VCENTER });
1127 0 : item->hairpin()->setEndTextAlign({ AlignH::RIGHT, AlignV::VCENTER });
1128 :
1129 0 : double x1 = 0.0;
1130 0 : layoutTextLineBaseSegment(item, ctx);
1131 0 : if (!item->text()->empty()) {
1132 0 : x1 = item->text()->width() + spatium * .5;
1133 : }
1134 :
1135 0 : Transform t;
1136 0 : double h1 = item->hairpin()->hairpinHeight().val() * spatium * .5;
1137 0 : double h2 = item->hairpin()->hairpinContHeight().val() * spatium * .5;
1138 :
1139 0 : double x = item->pos2().x();
1140 0 : if (!item->endText()->empty()) {
1141 0 : x -= (item->endText()->width() + spatium * .5); // 0.5 spatium distance
1142 : }
1143 0 : if (x < spatium) { // minimum size of hairpin
1144 0 : x = spatium;
1145 : }
1146 0 : double y = item->pos2().y();
1147 0 : double len = sqrt(x * x + y * y);
1148 0 : t.rotateRadians(asin(y / len));
1149 :
1150 0 : item->setDrawCircledTip(item->hairpin()->hairpinCircledTip());
1151 0 : item->setCircledTipRadius(item->drawCircledTip() ? 0.6 * spatium * .5 : 0.0);
1152 :
1153 0 : LineF l1, l2;
1154 :
1155 0 : switch (type) {
1156 0 : case HairpinType::CRESC_HAIRPIN: {
1157 0 : switch (item->spannerSegmentType()) {
1158 0 : case SpannerSegmentType::SINGLE:
1159 : case SpannerSegmentType::BEGIN: {
1160 0 : l1.setLine(x1 + item->circledTipRadius() * 2.0, 0.0, len, h1);
1161 0 : l2.setLine(x1 + item->circledTipRadius() * 2.0, 0.0, len, -h1);
1162 0 : PointF circledTip;
1163 0 : circledTip.setX(x1 + item->circledTipRadius());
1164 0 : circledTip.setY(0.0);
1165 0 : item->setCircledTip(circledTip);
1166 0 : } break;
1167 :
1168 0 : case SpannerSegmentType::MIDDLE:
1169 : case SpannerSegmentType::END:
1170 0 : item->setDrawCircledTip(false);
1171 0 : l1.setLine(x1, h2, len, h1);
1172 0 : l2.setLine(x1, -h2, len, -h1);
1173 0 : break;
1174 : }
1175 : }
1176 0 : break;
1177 0 : case HairpinType::DIM_HAIRPIN: {
1178 0 : switch (item->spannerSegmentType()) {
1179 0 : case SpannerSegmentType::SINGLE:
1180 : case SpannerSegmentType::END: {
1181 0 : l1.setLine(x1, h1, len - item->circledTipRadius() * 2, 0.0);
1182 0 : l2.setLine(x1, -h1, len - item->circledTipRadius() * 2, 0.0);
1183 0 : PointF circledTip;
1184 0 : circledTip.setX(len - item->circledTipRadius());
1185 0 : circledTip.setY(0.0);
1186 0 : item->setCircledTip(circledTip);
1187 0 : } break;
1188 0 : case SpannerSegmentType::BEGIN:
1189 : case SpannerSegmentType::MIDDLE:
1190 0 : item->setDrawCircledTip(false);
1191 0 : l1.setLine(x1, h1, len, +h2);
1192 0 : l2.setLine(x1, -h1, len, -h2);
1193 0 : break;
1194 : }
1195 : }
1196 0 : break;
1197 0 : default:
1198 0 : break;
1199 : }
1200 :
1201 : // Do Coord rotation
1202 0 : l1 = t.map(l1);
1203 0 : l2 = t.map(l2);
1204 0 : if (item->drawCircledTip()) {
1205 0 : item->setCircledTip(t.map(item->circledTip()));
1206 : }
1207 :
1208 0 : ldata->points[0] = l1.p1();
1209 0 : ldata->points[1] = l1.p2();
1210 0 : ldata->points[2] = l2.p1();
1211 0 : ldata->points[3] = l2.p2();
1212 0 : ldata->npoints = 4;
1213 :
1214 0 : RectF r = RectF(l1.p1(), l1.p2()).normalized().united(RectF(l2.p1(), l2.p2()).normalized());
1215 0 : if (!item->text()->empty()) {
1216 0 : r.unite(item->text()->ldata()->bbox());
1217 : }
1218 0 : if (!item->endText()->empty()) {
1219 0 : r.unite(item->endText()->ldata()->bbox().translated(x + item->endText()->ldata()->bbox().width(), 0.0));
1220 : }
1221 0 : double w = item->absoluteFromSpatium(ctx.style().styleS(Sid::hairpinLineWidth));
1222 0 : item->setbbox(r.adjusted(-w * .5, -w * .5, w, w));
1223 : }
1224 :
1225 0 : item->setPos(PointF());
1226 0 : item->setOffset(PointF());
1227 0 : }
1228 :
1229 0 : void SingleLayout::layout(HarpPedalDiagram* item, const Context& ctx)
1230 : {
1231 0 : item->updateDiagramText();
1232 0 : layoutTextBase(item, ctx, item->mutldata());
1233 0 : }
1234 :
1235 0 : void SingleLayout::layout(Image* item, const Context&)
1236 : {
1237 0 : item->init();
1238 :
1239 0 : SizeF imageSize = item->size();
1240 :
1241 0 : Image::LayoutData* ldata = item->mutldata();
1242 0 : ldata->setPos(PointF());
1243 0 : ldata->setBbox(RectF(PointF(), item->size2pixel(imageSize)));
1244 0 : }
1245 :
1246 0 : void SingleLayout::layout(InstrumentChange* item, const Context& ctx)
1247 : {
1248 0 : layoutTextBase(item, ctx, item->mutldata());
1249 0 : }
1250 :
1251 0 : void SingleLayout::layout(Jump* item, const Context& ctx)
1252 : {
1253 0 : layoutTextBase(item, ctx, item->mutldata());
1254 0 : }
1255 :
1256 0 : void SingleLayout::layout(KeySig* item, const Context& ctx)
1257 : {
1258 0 : KeySig::LayoutData* ldata = item->mutldata();
1259 0 : double spatium = item->spatium();
1260 0 : double step = spatium * 0.5;
1261 :
1262 0 : ldata->setBbox(RectF());
1263 0 : ldata->keySymbols.clear();
1264 :
1265 : // determine current clef for this staff
1266 0 : ClefType clef = ClefType::G;
1267 :
1268 0 : int key = int(item->key());
1269 :
1270 0 : if (item->isCustom() && !item->isAtonal()) {
1271 0 : Spatium accidentalGap = ctx.style().styleS(Sid::keysigAccidentalDistance);
1272 : // add standard key accidentals first, if necessary
1273 0 : for (int i = 1; i <= std::abs(key) && std::abs(key) <= 7; ++i) {
1274 0 : bool drop = false;
1275 0 : for (const CustDef& cd: item->customKeyDefs()) {
1276 0 : int degree = item->degInKey(cd.degree);
1277 : // if custom keysig accidental takes place, don't create tonal accidental
1278 0 : if ((degree * 2 + 2) % 7 == (key < 0 ? 8 - i : i) % 7) {
1279 0 : drop = true;
1280 0 : break;
1281 : }
1282 : }
1283 0 : if (!drop) {
1284 0 : KeySym ks;
1285 0 : int lineIndexOffset = key > 0 ? -1 : 6;
1286 0 : ks.sym = key > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
1287 0 : ks.line = ClefInfo::lines(clef)[lineIndexOffset + i];
1288 0 : if (ldata->keySymbols.size() > 0) {
1289 0 : KeySym& previous = ldata->keySymbols.back();
1290 0 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(previous.sym), spatium);
1291 0 : ks.xPos = previous.xPos + previousWidth + accidentalGap;
1292 : } else {
1293 0 : ks.xPos = 0_sp;
1294 : }
1295 : // TODO octave metters?
1296 0 : ldata->keySymbols.push_back(ks);
1297 : }
1298 : }
1299 0 : for (const CustDef& cd : item->customKeyDefs()) {
1300 0 : SymId sym = item->symInKey(cd.sym, cd.degree);
1301 0 : int degree = item->degInKey(cd.degree);
1302 0 : bool flat = std::string(SymNames::nameForSymId(sym).ascii()).find("Flat") != std::string::npos;
1303 0 : int accIdx = (degree * 2 + 1) % 7; // C D E F ... index to F C G D index
1304 0 : accIdx = flat ? 13 - accIdx : accIdx;
1305 0 : int line = ClefInfo::lines(clef)[accIdx] + cd.octAlt * 7;
1306 0 : Spatium xpos = cd.xAlt;
1307 0 : if (ldata->keySymbols.size() > 0) {
1308 0 : KeySym& previous = ldata->keySymbols.back();
1309 0 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(previous.sym), spatium);
1310 0 : xpos += previous.xPos + previousWidth + accidentalGap;
1311 : }
1312 : // if translated symbol if out of range, add key accidental followed by untranslated symbol
1313 0 : if (sym == SymId::noSym) {
1314 0 : KeySym ks;
1315 0 : ks.line = line;
1316 0 : ks.xPos = xpos;
1317 : // for quadruple sharp use two double sharps
1318 0 : if (cd.sym == SymId::accidentalTripleSharp) {
1319 0 : ks.sym = SymId::accidentalDoubleSharp;
1320 0 : sym = SymId::accidentalDoubleSharp;
1321 : } else {
1322 0 : ks.sym = key > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
1323 0 : sym = cd.sym;
1324 : }
1325 0 : ldata->keySymbols.push_back(ks);
1326 0 : xpos += Spatium(key < 0 ? 0.7 : 1); // flats closer
1327 : }
1328 : // create symbol; natural only if is user defined
1329 0 : if (sym != SymId::accidentalNatural || sym == cd.sym) {
1330 0 : KeySym ks;
1331 0 : ks.sym = sym;
1332 0 : ks.line = line;
1333 0 : ks.xPos = xpos;
1334 0 : ldata->keySymbols.push_back(ks);
1335 : }
1336 : }
1337 : } else {
1338 0 : if (std::abs(key) <= 7) {
1339 0 : const signed char* lines = ClefInfo::lines(clef);
1340 0 : SymId sym = key > 0 ? SymId::accidentalSharp : SymId::accidentalFlat;
1341 0 : Spatium accidentalGap = ctx.style().styleS(Sid::keysigAccidentalDistance);
1342 0 : Spatium previousWidth = Spatium::fromAbsolute(item->symWidth(sym), spatium);
1343 0 : int lineIndexOffset = key > 0 ? 0 : 7;
1344 0 : for (int i = 0; i < std::abs(key); ++i) {
1345 0 : int line = lines[lineIndexOffset + i];
1346 0 : KeySym ks;
1347 0 : ks.sym = sym;
1348 0 : Spatium x = 0.0_sp;
1349 0 : if (ldata->keySymbols.size() > 0) {
1350 0 : const KeySym& previous = ldata->keySymbols.back();
1351 0 : x = previous.xPos + previousWidth + accidentalGap;
1352 0 : bool isAscending = line < previous.line;
1353 0 : SmuflAnchorId currentCutout = isAscending ? SmuflAnchorId::cutOutSW : SmuflAnchorId::cutOutNW;
1354 0 : SmuflAnchorId previousCutout = isAscending ? SmuflAnchorId::cutOutNE : SmuflAnchorId::cutOutSE;
1355 0 : PointF cutout = item->symSmuflAnchor(sym, currentCutout);
1356 0 : double currentCutoutY = line * step + cutout.y();
1357 0 : double previousCutoutY = previous.line * step + item->symSmuflAnchor(previous.sym, previousCutout).y();
1358 0 : if ((isAscending && currentCutoutY < previousCutoutY) || (!isAscending && currentCutoutY > previousCutoutY)) {
1359 0 : x -= Spatium::fromAbsolute(cutout.x(), spatium);
1360 : }
1361 : }
1362 0 : ks.xPos = x;
1363 0 : ks.line = line;
1364 0 : ldata->keySymbols.push_back(ks);
1365 : }
1366 : } else {
1367 0 : LOGD() << "illegal key:" << key;
1368 : }
1369 : }
1370 :
1371 : // compute bbox
1372 0 : for (const KeySym& ks : ldata->keySymbols) {
1373 0 : double x = ks.xPos.toAbsolute(spatium);
1374 0 : double y = ks.line * step;
1375 0 : ldata->addBbox(item->symBbox(ks.sym).translated(x, y));
1376 : }
1377 0 : }
1378 :
1379 0 : void SingleLayout::layout(LayoutBreak* item, const Context&)
1380 : {
1381 0 : FontMetrics metrics(item->font());
1382 0 : RectF bbox = metrics.boundingRect(item->iconCode());
1383 0 : item->mutldata()->setShape(Shape(bbox, item));
1384 0 : }
1385 :
1386 0 : void SingleLayout::layout(LetRing* item, const Context& ctx)
1387 : {
1388 0 : layoutLine(item, ctx);
1389 0 : }
1390 :
1391 0 : void SingleLayout::layout(LetRingSegment* item, const Context& ctx)
1392 : {
1393 0 : layoutTextLineBaseSegment(item, ctx);
1394 0 : }
1395 :
1396 0 : void SingleLayout::layout(Lyrics* item, const Context& ctx)
1397 : {
1398 0 : layoutTextBase(toTextBase(item), ctx, item->mutldata());
1399 0 : }
1400 :
1401 0 : void SingleLayout::layout(NoteHead* item, const Context& ctx)
1402 : {
1403 0 : layout(static_cast<Symbol*>(item), ctx);
1404 0 : }
1405 :
1406 0 : void SingleLayout::layout(NoteLine*, const Context&)
1407 : {
1408 0 : NOT_IMPLEMENTED;
1409 : //! NOTE: NoteLines can be removed from disallowed elements in NotationInteraction::isOutgoingDragElementAllowed once this has been implemented
1410 0 : }
1411 :
1412 0 : void SingleLayout::layout(Marker* item, const Context& ctx)
1413 : {
1414 0 : layoutTextBase(item, ctx, item->mutldata());
1415 0 : }
1416 :
1417 0 : void SingleLayout::layout(MeasureNumber* item, const Context& ctx)
1418 : {
1419 0 : item->setPos(PointF());
1420 0 : item->setOffset(PointF());
1421 :
1422 0 : layout1TextBase(item, ctx, item->mutldata());
1423 0 : }
1424 :
1425 0 : void SingleLayout::layout(MeasureRepeat* item, const Context& ctx)
1426 : {
1427 0 : MeasureRepeat::LayoutData* ldata = item->mutldata();
1428 0 : switch (item->numMeasures()) {
1429 0 : case 1:
1430 : {
1431 0 : ldata->setSymId(SymId::repeat1Bar);
1432 0 : if (ctx.style().styleB(Sid::oneMeasureRepeatShow1)) {
1433 0 : ldata->setNumberSym(1);
1434 : } else {
1435 0 : ldata->clearNumberSym();
1436 : }
1437 0 : break;
1438 : }
1439 0 : case 2:
1440 0 : ldata->setSymId(SymId::repeat2Bars);
1441 0 : ldata->setNumberSym(item->numMeasures());
1442 0 : break;
1443 0 : case 4:
1444 0 : ldata->setSymId(SymId::repeat4Bars);
1445 0 : ldata->setNumberSym(item->numMeasures());
1446 0 : break;
1447 0 : default:
1448 0 : ldata->setSymId(SymId::noSym); // should never happen
1449 0 : ldata->clearNumberSym();
1450 0 : break;
1451 : }
1452 :
1453 0 : RectF bbox = item->symBbox(ldata->symId);
1454 0 : ldata->setBbox(bbox);
1455 0 : }
1456 :
1457 0 : void SingleLayout::layout(Ornament* item, const Context& ctx)
1458 : {
1459 0 : layout(toArticulation(item), ctx);
1460 0 : }
1461 :
1462 0 : void SingleLayout::layout(Ottava* item, const Context& ctx)
1463 : {
1464 0 : layoutLine(item, ctx);
1465 0 : }
1466 :
1467 0 : void SingleLayout::layout(OttavaSegment* item, const Context& ctx)
1468 : {
1469 0 : layoutTextLineBaseSegment(item, ctx);
1470 0 : item->setOffset(PointF());
1471 0 : }
1472 :
1473 0 : void SingleLayout::layout(PalmMute* item, const Context& ctx)
1474 : {
1475 0 : layoutLine(item, ctx);
1476 0 : }
1477 :
1478 0 : void SingleLayout::layout(PalmMuteSegment* item, const Context& ctx)
1479 : {
1480 0 : layoutTextLineBaseSegment(item, ctx);
1481 0 : }
1482 :
1483 0 : void SingleLayout::layout(Pedal* item, const Context& ctx)
1484 : {
1485 0 : layoutLine(item, ctx);
1486 0 : }
1487 :
1488 0 : void SingleLayout::layout(PedalSegment* item, const Context& ctx)
1489 : {
1490 0 : layoutTextLineBaseSegment(item, ctx);
1491 0 : item->setOffset(PointF());
1492 0 : }
1493 :
1494 0 : void SingleLayout::layout(PlayCountText* item, const Context& ctx)
1495 : {
1496 0 : layoutTextBase(item, ctx, item->mutldata());
1497 0 : }
1498 :
1499 0 : void SingleLayout::layout(PlayTechAnnotation* item, const Context& ctx)
1500 : {
1501 0 : layoutTextBase(item, ctx, item->mutldata());
1502 0 : }
1503 :
1504 0 : void SingleLayout::layout(RehearsalMark* item, const Context& ctx)
1505 : {
1506 0 : layoutTextBase(item, ctx, item->mutldata());
1507 0 : }
1508 :
1509 0 : void SingleLayout::layout(Slur* item, const Context&)
1510 : {
1511 0 : double spatium = item->spatium();
1512 0 : SlurSegment* s = nullptr;
1513 0 : if (item->spannerSegments().empty()) {
1514 0 : s = new SlurSegment(item);
1515 0 : s->setTrack(item->track());
1516 0 : item->add(s);
1517 : } else {
1518 0 : s = item->frontSegment();
1519 : }
1520 :
1521 0 : s->setSpannerSegmentType(SpannerSegmentType::SINGLE);
1522 :
1523 0 : s->setPos(PointF());
1524 0 : s->ups(Grip::START).p = PointF(0, 0);
1525 0 : s->ups(Grip::END).p = PointF(spatium * 6, 0);
1526 0 : s->setExtraHeight(0.0);
1527 :
1528 0 : SlurTieLayout::computeBezier(s);
1529 :
1530 0 : item->setbbox(s->ldata()->bbox());
1531 0 : }
1532 :
1533 0 : void SingleLayout::layout(Spacer* item, const Context&)
1534 : {
1535 0 : Spacer::LayoutData* ldata = item->mutldata();
1536 :
1537 0 : double spatium = item->spatium();
1538 :
1539 0 : PainterPath path = PainterPath();
1540 0 : double w = spatium;
1541 0 : double b = w * .5;
1542 0 : double h = item->ownershipParent() ? item->absoluteGap() : item->absoluteFromSpatium(std::min(item->gap(), 4.0_sp)); // limit length for palette
1543 :
1544 0 : switch (item->spacerType()) {
1545 0 : case SpacerType::DOWN:
1546 0 : path.lineTo(w, 0.0);
1547 0 : path.moveTo(b, 0.0);
1548 0 : path.lineTo(b, h);
1549 0 : path.lineTo(0.0, h - b);
1550 0 : path.moveTo(b, h);
1551 0 : path.lineTo(w, h - b);
1552 0 : break;
1553 0 : case SpacerType::UP:
1554 0 : path.moveTo(b, 0.0);
1555 0 : path.lineTo(0.0, b);
1556 0 : path.moveTo(b, 0.0);
1557 0 : path.lineTo(w, b);
1558 0 : path.moveTo(b, 0.0);
1559 0 : path.lineTo(b, h);
1560 0 : path.moveTo(0.0, h);
1561 0 : path.lineTo(w, h);
1562 0 : break;
1563 0 : case SpacerType::FIXED:
1564 0 : path.lineTo(w, 0.0);
1565 0 : path.moveTo(b, 0.0);
1566 0 : path.lineTo(b, h);
1567 0 : path.moveTo(0.0, h);
1568 0 : path.lineTo(w, h);
1569 0 : break;
1570 : }
1571 0 : ldata->path = path;
1572 0 : double lw = spatium * 0.4;
1573 0 : RectF bb(0, 0, w, h);
1574 0 : bb.adjust(-lw, -lw, lw, lw);
1575 0 : ldata->setBbox(bb);
1576 :
1577 0 : item->setZ(0.0);
1578 0 : }
1579 :
1580 0 : void SingleLayout::layout(StaffText* item, const Context& ctx)
1581 : {
1582 0 : layoutTextBase(item, ctx, item->mutldata());
1583 :
1584 0 : if (item->hasSoundFlag()) {
1585 0 : RectF bbox = item->ldata()->bbox();
1586 0 : double iconHeight = bbox.height();
1587 0 : RectF iconBBox = RectF(bbox.x(), bbox.y(), iconHeight, iconHeight);
1588 0 : item->soundFlag()->mutldata()->setBbox(iconBBox);
1589 :
1590 0 : double xMove = iconBBox.width() + iconBBox.width() / 2.0;
1591 0 : bbox.setWidth(bbox.width() + xMove);
1592 0 : item->setbbox(bbox);
1593 :
1594 0 : layout(item->soundFlag(), ctx);
1595 :
1596 0 : for (TextBlock& block : item->mutldata()->blocks) {
1597 0 : auto& fragments = block.fragments();
1598 0 : for (std::list<TextFragment>::iterator it = fragments.begin(); it != fragments.end(); ++it) {
1599 0 : it->pos.setX(it->pos.x() + xMove);
1600 : }
1601 : }
1602 : }
1603 0 : }
1604 :
1605 0 : void SingleLayout::layout(StaveSharingChange* item, const Context&)
1606 : {
1607 0 : const FontMetrics metrics(item->font());
1608 0 : const RectF iconBox = metrics.boundingRect(item->iconCode());
1609 :
1610 0 : Shape shape;
1611 0 : shape.add(iconBox, item);
1612 0 : item->mutldata()->setShape(shape);
1613 0 : item->mutldata()->setPos(PointF());
1614 0 : }
1615 :
1616 0 : void SingleLayout::layout(StaveSharingLabel* item, const Context& ctx)
1617 : {
1618 0 : layoutTextBase(item, ctx, item->mutldata());
1619 0 : }
1620 :
1621 0 : void SingleLayout::layout(StaffTypeChange* item, const Context& ctx)
1622 : {
1623 0 : double spatium = ctx.style().spatium();
1624 0 : item->setbbox(RectF(-item->lw() * .5, -item->lw() * .5, spatium * 2.5 + item->lw(), spatium * 2.5 + item->lw()));
1625 0 : item->setPos(0.0, 0.0);
1626 0 : }
1627 :
1628 0 : void SingleLayout::layout(StringTunings* item, const Context& ctx)
1629 : {
1630 0 : layoutTextBase(item, ctx, item->mutldata());
1631 :
1632 0 : double spatium = item->spatium();
1633 0 : for (TextBlock& block : item->mutldata()->blocks) {
1634 0 : for (TextFragment& fragment : block.fragments()) {
1635 0 : Font font = fragment.font(item);
1636 0 : if (font.type() != Font::Type::MusicSymbol) {
1637 : // HACK: the music symbol doesn't have a good baseline
1638 : // to go with text so we correct text here
1639 0 : const double baselineAdjustment = 0.35 * spatium * item->symbolScale();
1640 0 : fragment.pos.setY(fragment.pos.y() - baselineAdjustment);
1641 : }
1642 0 : }
1643 : }
1644 0 : }
1645 :
1646 0 : void SingleLayout::layout(Symbol* item, const Context&)
1647 : {
1648 0 : item->setbbox(item->scoreFont() ? item->scoreFont()->bbox(item->sym(), item->magS()) : item->symBbox(item->sym()));
1649 0 : item->setOffset(0.0, 0.0);
1650 0 : item->setPos(0.0, 0.0);
1651 0 : }
1652 :
1653 0 : void SingleLayout::layout(SystemText* item, const Context& ctx)
1654 : {
1655 0 : layoutTextBase(item, ctx, item->mutldata());
1656 0 : }
1657 :
1658 0 : void SingleLayout::layout(Tapping* item, const Context& ctx)
1659 : {
1660 0 : TappingText* text = item->text();
1661 :
1662 0 : if (!text) {
1663 0 : text = new TappingText(item);
1664 : }
1665 :
1666 0 : item->setText(text);
1667 0 : text->setTrack(item->track());
1668 0 : DO_ASSERT(item->hand() != TappingHand::INVALID);
1669 0 : text->setXmlText(item->hand() == TappingHand::LEFT ? "l.h. tap" : "r.h. tap");
1670 0 : text->setAlign(Align(AlignH::HCENTER, AlignV::BASELINE));
1671 :
1672 0 : layoutTextBase(text, ctx, text->mutldata());
1673 :
1674 0 : item->setbbox(text->ldata()->bbox());
1675 0 : }
1676 :
1677 0 : void SingleLayout::layout(SoundFlag* item, const Context& ctx)
1678 : {
1679 : UNUSED(item);
1680 : UNUSED(ctx);
1681 0 : }
1682 :
1683 0 : void SingleLayout::layout(Stem* item, const Context& ctx)
1684 : {
1685 0 : LayoutContext tctx(ctx.dontUseScore());
1686 0 : TLayout::layoutStem(item, item->mutldata(), tctx.conf());
1687 0 : }
1688 :
1689 0 : void SingleLayout::layout(Sticking* item, const Context& ctx)
1690 : {
1691 0 : layoutTextBase(toTextBase(item), ctx, item->mutldata());
1692 0 : }
1693 :
1694 0 : void SingleLayout::layout(TempoText* item, const Context& ctx)
1695 : {
1696 0 : layoutTextBase(item, ctx, item->mutldata());
1697 0 : }
1698 :
1699 0 : void SingleLayout::layout(TextLine* item, const Context& ctx)
1700 : {
1701 0 : layoutLine(item, ctx);
1702 0 : }
1703 :
1704 0 : void SingleLayout::layout(TextLineSegment* item, const Context& ctx)
1705 : {
1706 0 : layoutTextLineBaseSegment(item, ctx);
1707 0 : item->setOffset(PointF());
1708 0 : }
1709 :
1710 0 : void SingleLayout::layout(TimeSig* item, const Context& ctx)
1711 : {
1712 0 : TimeSig::LayoutData* ldata = item->mutldata();
1713 :
1714 0 : double spatium = item->spatium();
1715 :
1716 0 : ldata->setPos(0.0, 0.0);
1717 0 : ldata->setBbox(RectF()); // prepare for an empty time signature
1718 :
1719 0 : constexpr double lineDist = 1.0;
1720 0 : constexpr int numOfLines = 5;
1721 0 : TimeSigType sigType = item->timeSigType();
1722 :
1723 : // if some symbol
1724 : // compute vert. displacement to center in the staff height
1725 : // determine middle staff position:
1726 :
1727 0 : double yoff = spatium * (numOfLines - 1) * 0.5 * lineDist;
1728 :
1729 : // C and Ccut are placed at the middle of the staff: use yoff directly
1730 0 : IEngravingFontPtr font = ctx.engravingFont();
1731 0 : SizeF mag(item->magS() * item->scale());
1732 :
1733 0 : if (sigType == TimeSigType::FOUR_FOUR) {
1734 0 : ldata->pz = PointF(0.0, yoff);
1735 0 : RectF bbox = font->bbox(SymId::timeSigCommon, mag);
1736 0 : ldata->setBbox(bbox.translated(ldata->pz));
1737 0 : ldata->ns.clear();
1738 0 : ldata->ns.push_back(SymId::timeSigCommon);
1739 0 : ldata->ds.clear();
1740 0 : } else if (sigType == TimeSigType::ALLA_BREVE) {
1741 0 : ldata->pz = PointF(0.0, yoff);
1742 0 : RectF bbox = font->bbox(SymId::timeSigCutCommon, mag);
1743 0 : ldata->setBbox(bbox.translated(ldata->pz));
1744 0 : ldata->ns.clear();
1745 0 : ldata->ns.push_back(SymId::timeSigCutCommon);
1746 0 : ldata->ds.clear();
1747 0 : } else if (sigType == TimeSigType::CUT_BACH) {
1748 0 : ldata->pz = PointF(0.0, yoff);
1749 0 : RectF bbox = font->bbox(SymId::timeSigCut2, mag);
1750 0 : ldata->setBbox(bbox.translated(ldata->pz));
1751 0 : ldata->ns.clear();
1752 0 : ldata->ns.push_back(SymId::timeSigCut2);
1753 0 : ldata->ds.clear();
1754 0 : } else if (sigType == TimeSigType::CUT_TRIPLE) {
1755 0 : ldata->pz = PointF(0.0, yoff);
1756 0 : RectF bbox = font->bbox(SymId::timeSigCut3, mag);
1757 0 : ldata->setBbox(bbox.translated(ldata->pz));
1758 0 : ldata->ns.clear();
1759 0 : ldata->ns.push_back(SymId::timeSigCut3);
1760 0 : ldata->ds.clear();
1761 : } else {
1762 0 : if (item->numeratorString().isEmpty()) {
1763 0 : ldata->ns = timeSigSymIdsFromString(item->numeratorString().isEmpty()
1764 0 : ? String::number(item->sig().numerator())
1765 0 : : item->numeratorString());
1766 :
1767 0 : ldata->ds = timeSigSymIdsFromString(item->denominatorString().isEmpty()
1768 0 : ? String::number(item->sig().denominator())
1769 0 : : item->denominatorString());
1770 : } else {
1771 0 : ldata->ns = timeSigSymIdsFromString(item->numeratorString());
1772 0 : ldata->ds = timeSigSymIdsFromString(item->denominatorString());
1773 : }
1774 :
1775 0 : RectF numRect = font->bbox(ldata->ns, mag);
1776 0 : RectF denRect = font->bbox(ldata->ds, mag);
1777 :
1778 : // position numerator and denominator; vertical displacement:
1779 : // number of lines is odd: 0.0 (strings are directly above and below the middle line)
1780 : // number of lines even: 0.05 (strings are moved up/down to leave 1/10sp between them)
1781 :
1782 0 : double displ = (numOfLines & 1) ? 0.0 : (0.05 * spatium);
1783 :
1784 : //align on the wider
1785 0 : double pzY = yoff - (denRect.width() < 0.01 ? 0.0 : (displ + numRect.height() * 0.5));
1786 0 : double pnY = yoff + displ + denRect.height() * 0.5;
1787 :
1788 0 : if (numRect.width() >= denRect.width()) {
1789 : // numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
1790 0 : ldata->pz = PointF(0.0, pzY);
1791 : // denominator: horiz: centred around centre of numerator | vert: one space below centre line
1792 0 : ldata->pn = PointF((numRect.width() - denRect.width()) * 0.5, pnY);
1793 : } else {
1794 : // numerator: one space above centre line, unless denomin. is empty (if so, directly centre in the middle)
1795 0 : ldata->pz = PointF((denRect.width() - numRect.width()) * 0.5, pzY);
1796 : // denominator: horiz: centred around centre of numerator | vert: one space below centre line
1797 0 : ldata->pn = PointF(0.0, pnY);
1798 : }
1799 :
1800 : // centering of parenthesis so the middle of the parenthesis is at the divisor marking level
1801 0 : int centerY = yoff / 2 + spatium;
1802 0 : int widestPortion = numRect.width() > denRect.width() ? numRect.width() : denRect.width();
1803 0 : ldata->pointLargeLeftParen = PointF(-spatium, centerY);
1804 0 : ldata->pointLargeRightParen = PointF(widestPortion + spatium, centerY);
1805 :
1806 0 : ldata->setBbox(numRect.translated(ldata->pz)); // translate bounding boxes to actual string positions
1807 0 : ldata->addBbox(denRect.translated(ldata->pn));
1808 0 : if (item->largeParentheses()) {
1809 0 : ldata->addBbox(RectF(ldata->pointLargeLeftParen.x(), ldata->pointLargeLeftParen.y() - denRect.height(), spatium / 2,
1810 0 : numRect.height() + denRect.height()));
1811 0 : ldata->addBbox(RectF(ldata->pointLargeRightParen.x(), ldata->pointLargeRightParen.y() - denRect.height(), spatium / 2,
1812 0 : numRect.height() + denRect.height()));
1813 : }
1814 : }
1815 0 : }
1816 :
1817 0 : void SingleLayout::layout(TremoloSingleChord* item, const Context&)
1818 : {
1819 0 : item->computeShape(); // set bbox
1820 0 : item->setPath(item->basePath());
1821 0 : }
1822 :
1823 0 : void SingleLayout::layout(TremoloTwoChord* item, const Context&)
1824 : {
1825 0 : item->computeShape(); // set bbox
1826 0 : item->setPath(item->basePath());
1827 0 : }
1828 :
1829 0 : void SingleLayout::layout(TremoloBar* item, const Context&)
1830 : {
1831 0 : TremoloBar::LayoutData* ldata = item->mutldata();
1832 0 : double spatium = item->spatium();
1833 :
1834 0 : ldata->setPos(PointF());
1835 :
1836 0 : double timeFactor = item->userMag() / 1.0;
1837 0 : double pitchFactor = -spatium * 0.02;
1838 :
1839 0 : PolygonF polygon;
1840 0 : for (const PitchValue& v : item->points()) {
1841 0 : polygon << PointF(v.time * timeFactor, v.pitch * pitchFactor);
1842 : }
1843 0 : ldata->polygon = polygon;
1844 :
1845 0 : const double w = item->absoluteFromSpatium(item->lineWidth());
1846 0 : ldata->setBbox(ldata->polygon.boundingRect().adjusted(-w, -w, w, w));
1847 0 : }
1848 :
1849 0 : void SingleLayout::layout(Trill* item, const Context& ctx)
1850 : {
1851 0 : layoutLine(static_cast<SLine*>(item), ctx);
1852 0 : }
1853 :
1854 0 : void SingleLayout::layout(TrillSegment* item, const Context& ctx)
1855 : {
1856 0 : TrillSegment::LayoutData* ldata = item->mutldata();
1857 0 : if (item->spanner()->placeBelow()) {
1858 0 : ldata->setPosY(0.0);
1859 : }
1860 :
1861 0 : bool accidentalGoesBelow = item->trill()->trillType() == TrillType::DOWNPRALL_LINE;
1862 0 : Trill* trill = item->trill();
1863 0 : Ornament* ornament = trill->ornament();
1864 0 : if (ornament) {
1865 0 : if (item->isSingleBeginType()) {
1866 0 : layout(ornament, ctx);
1867 : }
1868 0 : trill->setAccidental(accidentalGoesBelow ? ornament->accidentalBelow() : ornament->accidentalAbove());
1869 0 : trill->setCueNoteChord(ornament->cueNoteChord());
1870 0 : ArticulationAnchor anchor = ornament->anchor();
1871 0 : if (anchor == ArticulationAnchor::AUTO) {
1872 0 : trill->setPlacement(trill->track() % 2 ? PlacementV::BELOW : PlacementV::ABOVE);
1873 : } else {
1874 0 : trill->setPlacement(anchor == ArticulationAnchor::TOP ? PlacementV::ABOVE : PlacementV::BELOW);
1875 : }
1876 0 : trill->setPropertyFlags(Pid::PLACEMENT, PropertyFlags::STYLED); // Ensures that the property isn't written (it is written by the ornamnent)
1877 : }
1878 :
1879 0 : if (item->isSingleType() || item->isBeginType()) {
1880 0 : switch (item->trill()->trillType()) {
1881 0 : case TrillType::TRILL_LINE:
1882 0 : item->symbolLine(SymId::ornamentTrill, SymId::wiggleTrill);
1883 0 : break;
1884 0 : case TrillType::PRALLPRALL_LINE:
1885 0 : item->symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
1886 0 : break;
1887 0 : case TrillType::UPPRALL_LINE:
1888 0 : item->symbolLine(SymId::ornamentBottomLeftConcaveStroke,
1889 : SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
1890 0 : break;
1891 0 : case TrillType::DOWNPRALL_LINE:
1892 0 : item->symbolLine(SymId::ornamentLeftVerticalStroke,
1893 : SymId::ornamentZigZagLineNoRightEnd, SymId::ornamentZigZagLineWithRightEnd);
1894 0 : break;
1895 : }
1896 0 : Accidental* a = item->trill()->accidental();
1897 0 : if (a) {
1898 0 : double vertMargin = 0.35 * item->spatium();
1899 0 : RectF box = item->symBbox(item->symbols().front());
1900 0 : double x = 0;
1901 0 : double y = 0;
1902 0 : x = 0.5 * (box.width() - a->width());
1903 : double minVertDist = accidentalGoesBelow
1904 0 : ? Shape(box).minVerticalDistance(a->shape())
1905 0 : : a->shape().minVerticalDistance(Shape(box));
1906 0 : y = accidentalGoesBelow ? minVertDist + vertMargin : -minVertDist - vertMargin;
1907 0 : a->setPos(x, y);
1908 0 : a->setOwnershipParent(item);
1909 : }
1910 : } else {
1911 0 : item->symbolLine(SymId::wiggleTrill, SymId::wiggleTrill);
1912 : }
1913 :
1914 0 : item->setOffset(PointF());
1915 0 : }
1916 :
1917 0 : void SingleLayout::layout(Vibrato* item, const Context& ctx)
1918 : {
1919 0 : layoutLine(static_cast<SLine*>(item), ctx);
1920 0 : }
1921 :
1922 0 : void SingleLayout::layout(VibratoSegment* item, const Context&)
1923 : {
1924 0 : VibratoSegment::LayoutData* ldata = item->mutldata();
1925 0 : if (item->spanner()->placeBelow()) {
1926 0 : ldata->setPosY(0.0);
1927 : }
1928 :
1929 0 : switch (item->vibrato()->vibratoType()) {
1930 0 : case VibratoType::GUITAR_VIBRATO:
1931 0 : item->symbolLine(SymId::guitarVibratoStroke, SymId::guitarVibratoStroke);
1932 0 : break;
1933 0 : case VibratoType::GUITAR_VIBRATO_WIDE:
1934 0 : item->symbolLine(SymId::guitarWideVibratoStroke, SymId::guitarWideVibratoStroke);
1935 0 : break;
1936 0 : case VibratoType::VIBRATO_SAWTOOTH:
1937 0 : item->symbolLine(SymId::wiggleSawtooth, SymId::wiggleSawtooth);
1938 0 : break;
1939 0 : case VibratoType::VIBRATO_SAWTOOTH_WIDE:
1940 0 : item->symbolLine(SymId::wiggleSawtoothWide, SymId::wiggleSawtoothWide);
1941 0 : break;
1942 0 : default:
1943 0 : break;
1944 : }
1945 :
1946 0 : item->setOffset(PointF());
1947 0 : }
1948 :
1949 0 : void SingleLayout::layout(Volta* item, const Context& ctx)
1950 : {
1951 0 : layoutLine(item, ctx);
1952 0 : }
1953 :
1954 0 : void SingleLayout::layout(WhammyBar* item, const Context& ctx)
1955 : {
1956 0 : layoutLine(item, ctx);
1957 0 : }
1958 :
1959 0 : void SingleLayout::layout(VoltaSegment* item, const Context& ctx)
1960 : {
1961 0 : layoutTextLineBaseSegment(item, ctx);
1962 0 : item->setOffset(PointF());
1963 :
1964 0 : double spatium = ctx.style().spatium();
1965 0 : double hookHeight = item->absoluteFromSpatium(item->volta()->beginHookHeight());
1966 0 : if (item->text()) {
1967 0 : Text* text = item->text();
1968 0 : RectF textBBox = text->ldata()->bbox().translated(text->pos());
1969 0 : text->mutldata()->moveY(hookHeight - textBBox.bottom());
1970 0 : text->mutldata()->moveX(0.5 * spatium);
1971 : }
1972 0 : }
1973 :
1974 0 : void SingleLayout::layout(WhammyBarSegment* item, const Context& ctx)
1975 : {
1976 0 : layoutTextLineBaseSegment(item, ctx);
1977 0 : item->setOffset(PointF());
1978 0 : }
1979 :
1980 0 : void SingleLayout::layout(Text* item, const Context& ctx)
1981 : {
1982 0 : layoutTextBase(toTextBase(item), ctx, item->mutldata());
1983 0 : }
1984 :
1985 0 : void SingleLayout::layoutTextBase(const TextBase* item, const Context& ctx, TextBase::LayoutData* ldata)
1986 : {
1987 0 : ldata->setPos(PointF());
1988 0 : const_cast<TextBase*>(item)->setOffset(PointF());
1989 :
1990 0 : if (item->placeBelow()) {
1991 0 : ldata->setPosY(0.0);
1992 : }
1993 :
1994 0 : layout1TextBase(item, ctx, ldata);
1995 0 : }
1996 :
1997 0 : static void textHorizontalLayout(const TextBase* item, Shape& shape, double maxBlockWidth, TextBase::LayoutData* ldata)
1998 : {
1999 : // Position and alignment
2000 0 : for (size_t i = 0; i < ldata->blocks.size(); ++i) {
2001 0 : TextBlock& textBlock = ldata->blocks[i];
2002 0 : double xAdj = -textBlock.boundingRect().left();
2003 :
2004 : // Set position relative to reference point
2005 0 : AlignH position = item->position();
2006 0 : if (position == AlignH::HCENTER) {
2007 0 : xAdj -= maxBlockWidth * .5;
2008 0 : } else if (position == AlignH::RIGHT) {
2009 0 : xAdj -= maxBlockWidth;
2010 : }
2011 :
2012 0 : double diff = maxBlockWidth - textBlock.boundingRect().width();
2013 0 : if (muse::RealIsNull(diff)) {
2014 : // This is the longest line, don't align
2015 0 : for (TextFragment& f : textBlock.fragments()) {
2016 0 : f.pos.rx() += xAdj;
2017 : }
2018 0 : textBlock.shape().translate(PointF(xAdj, 0.0));
2019 0 : shape.add(textBlock.shape().translated(PointF(0.0, textBlock.y())));
2020 0 : continue;
2021 0 : }
2022 : // Align relative to the longest line
2023 0 : AlignH alignH = item->align().horizontal;
2024 0 : if (alignH == AlignH::HCENTER) {
2025 0 : xAdj += diff * 0.5;
2026 0 : } else if (alignH == AlignH::RIGHT) {
2027 0 : xAdj += diff;
2028 : }
2029 :
2030 0 : for (TextFragment& fragment : textBlock.fragments()) {
2031 0 : fragment.pos.rx() += xAdj;
2032 : }
2033 0 : textBlock.shape().translate(PointF(xAdj, 0.0));
2034 0 : shape.add(textBlock.shape().translated(PointF(0.0, textBlock.y())));
2035 : }
2036 0 : }
2037 :
2038 0 : void SingleLayout::layout1TextBase(const TextBase* item, const Context&, TextBase::LayoutData* ldata)
2039 : {
2040 0 : if (ldata->layoutInvalid) {
2041 0 : item->createBlocks(ldata);
2042 : }
2043 :
2044 0 : double y = 0;
2045 :
2046 0 : double maxBlockWidth = -DBL_MAX;
2047 : // adjust the bounding box for the text item
2048 0 : for (size_t i = 0; i < ldata->rows(); ++i) {
2049 0 : TextBlock& t = ldata->blocks[i];
2050 0 : TextLayout::layoutTextBlock(&t, item);
2051 0 : const RectF* r = &t.boundingRect();
2052 :
2053 0 : if (r->height() == 0) {
2054 0 : r = &ldata->blocks.at(0).boundingRect();
2055 : }
2056 0 : y += t.lineSpacing();
2057 0 : t.setY(y);
2058 0 : maxBlockWidth = std::max(maxBlockWidth, t.boundingRect().width());
2059 : }
2060 :
2061 0 : Shape shape;
2062 0 : textHorizontalLayout(item, shape, maxBlockWidth, ldata);
2063 :
2064 0 : RectF bb = shape.bbox();
2065 :
2066 0 : double yoff = 0;
2067 0 : double h = 0;
2068 :
2069 0 : ldata->setPos(PointF());
2070 :
2071 0 : if (item->align() == AlignV::BOTTOM) {
2072 0 : yoff += h - bb.bottom();
2073 0 : } else if (item->align() == AlignV::VCENTER) {
2074 0 : yoff += (h - (bb.top() + bb.bottom())) * .5;
2075 0 : } else if (item->align() == AlignV::BASELINE) {
2076 0 : yoff += h * .5 - ldata->blocks.front().lineSpacing();
2077 : } else {
2078 0 : yoff += -bb.top();
2079 : }
2080 :
2081 0 : for (TextBlock& t : ldata->blocks) {
2082 0 : t.setY(t.y() + yoff);
2083 : }
2084 :
2085 0 : shape.translateY(yoff);
2086 0 : ldata->setShape(shape);
2087 :
2088 0 : if (item->hasFrame()) {
2089 0 : item->layoutFrame(ldata);
2090 : }
2091 :
2092 0 : if (!muse::RealIsNull(item->textAngle())) {
2093 0 : Transform t;
2094 0 : t.rotate(item->textAngle());
2095 0 : ldata->setShape(shape.transform(t));
2096 0 : ldata->highResShape.mut_value().transform(t);
2097 : }
2098 0 : }
2099 :
2100 0 : void SingleLayout::layoutLine(SLine* item, const Context& ctx)
2101 : {
2102 0 : if (item->spannerSegments().empty()) {
2103 0 : item->setLen(ctx.style().spatium() * 8);
2104 : }
2105 :
2106 0 : LineSegment* lineSegm = item->frontSegment();
2107 0 : layoutLineSegment(lineSegm, ctx);
2108 :
2109 0 : item->setbbox(lineSegm->ldata()->bbox(LD_ACCESS::BAD));
2110 0 : }
2111 :
2112 : // Extends lines to fill the corner between them.
2113 : // Assumes that l1p2 == l2p1 is the intersection between the lines.
2114 : // If checkAngle is false, assumes that the lines are perpendicular,
2115 : // and some calculations are saved.
2116 0 : static inline void extendLines(const PointF& l1p1, PointF& l1p2, PointF& l2p1, const PointF& l2p2, double lineWidth, bool checkAngle)
2117 : {
2118 0 : PointF l1UnitVector = (l1p2 - l1p1).normalized();
2119 0 : PointF l2UnitVector = (l2p1 - l2p2).normalized();
2120 :
2121 0 : double addedLength = lineWidth * 0.5;
2122 :
2123 0 : if (checkAngle) {
2124 0 : double angle = M_PI - acos(PointF::dotProduct(l1UnitVector, l2UnitVector));
2125 :
2126 0 : if (angle <= M_PI_2) {
2127 0 : addedLength *= tan(0.5 * angle);
2128 : }
2129 : }
2130 :
2131 0 : l1p2 += l1UnitVector * addedLength;
2132 0 : l2p1 += l2UnitVector * addedLength;
2133 0 : }
2134 :
2135 0 : static PolygonF createArrow(bool start, bool filled, PointF& startPoint, PointF& endPoint, const TextLineBase* tl)
2136 : {
2137 0 : double arrowWidth = 0.0;
2138 0 : double arrowHeight = 0.0;
2139 0 : if (filled) {
2140 0 : arrowWidth = tl->absoluteFromSpatium(start ? tl->beginFilledArrowWidth() : tl->endFilledArrowWidth());
2141 0 : arrowHeight = tl->absoluteFromSpatium(start ? tl->beginFilledArrowHeight() : tl->endFilledArrowHeight());
2142 : } else {
2143 0 : arrowWidth = tl->absoluteFromSpatium(start ? tl->beginLineArrowWidth() : tl->endLineArrowWidth());
2144 0 : arrowHeight = tl->absoluteFromSpatium(start ? tl->beginLineArrowHeight() : tl->endLineArrowHeight());
2145 : }
2146 :
2147 0 : PolygonF arrow;
2148 0 : if (start) {
2149 0 : arrow << PointF(0.0, -arrowHeight / 2) << PointF(-arrowWidth, 0.0) << PointF(0.0, arrowHeight / 2); // left
2150 : } else {
2151 0 : arrow << PointF(0.0, -arrowHeight / 2) << PointF(arrowWidth, 0.0) << PointF(0.0, arrowHeight / 2); // right
2152 : }
2153 :
2154 0 : PointF arrowAdjust = PointF(arrowWidth, 0.0);
2155 0 : arrowAdjust = (start ? 1.0 : -1.0) * arrowAdjust;
2156 0 : arrow.translate(arrowAdjust);
2157 :
2158 0 : const double yDiff = endPoint.y() - startPoint.y();
2159 0 : const double xDiff = endPoint.x() - startPoint.x();
2160 0 : const double rotate = atan(yDiff / xDiff) + (endPoint.x() < startPoint.x() ? M_PI : 0.0);
2161 :
2162 0 : Transform t;
2163 0 : t.rotateRadians(rotate);
2164 :
2165 0 : for (PointF& p : arrow) {
2166 0 : p = t.map(p);
2167 : }
2168 :
2169 0 : if (start) {
2170 0 : arrow.translate(startPoint);
2171 : } else {
2172 0 : arrow.translate(endPoint);
2173 : }
2174 :
2175 0 : const PointF lineVector = endPoint - startPoint;
2176 0 : const PointF unitVector = lineVector.normalized();
2177 0 : const double reduction = filled ? arrowWidth : tl->absoluteFromSpatium(tl->lineWidth()) / 2;
2178 0 : if (start) {
2179 0 : startPoint += reduction * unitVector;
2180 : } else {
2181 0 : endPoint -= reduction * unitVector;
2182 : }
2183 :
2184 0 : return arrow;
2185 0 : }
2186 :
2187 0 : void SingleLayout::layoutTextLineBaseSegment(TextLineBaseSegment* item, const Context& ctx)
2188 : {
2189 0 : TextLineBaseSegment::LayoutData* ldata = item->mutldata();
2190 0 : ldata->npoints = 0;
2191 0 : const TextLineBase* tl = item->textLineBase();
2192 0 : const double spatium = tl->spatium();
2193 0 : const bool isSingleOrBegin = item->isSingleBeginType();
2194 0 : const bool isSingleOrEnd = item->isSingleEndType();
2195 0 : const bool isDottedLine = tl->lineStyle() == LineType::DOTTED;
2196 0 : const double lineWidth = tl->absoluteFromSpatium(tl->lineWidth());
2197 :
2198 0 : if (!tl->diagonal()) {
2199 0 : item->setUserYoffset2(0);
2200 : }
2201 :
2202 0 : auto alignBaseLine = [lineWidth](Text* text, PointF& pp1, PointF& pp2) {
2203 0 : PointF widthCorrection(0.0, lineWidth / 2);
2204 0 : switch (text->align().vertical) {
2205 0 : case AlignV::TOP:
2206 0 : pp1 += widthCorrection;
2207 0 : pp2 += widthCorrection;
2208 0 : break;
2209 0 : case AlignV::VCENTER:
2210 0 : break;
2211 0 : case AlignV::BOTTOM:
2212 0 : pp1 -= widthCorrection;
2213 0 : pp2 -= widthCorrection;
2214 0 : break;
2215 0 : case AlignV::BASELINE:
2216 0 : pp1 -= widthCorrection;
2217 0 : pp2 -= widthCorrection;
2218 0 : break;
2219 : }
2220 0 : };
2221 :
2222 0 : if (isSingleOrBegin) {
2223 0 : item->text()->setXmlText(tl->beginText());
2224 0 : item->text()->setFamily(tl->beginFontFamily());
2225 0 : item->text()->setSize(tl->beginFontSize());
2226 0 : item->text()->setSymbolScale(tl->beginTextMusicalSymbolsScale());
2227 0 : item->text()->setSymbolSize(tl->beginTextMusicSymbolsSize());
2228 0 : item->text()->setOffset(tl->beginTextOffset() * item->mag());
2229 0 : item->text()->setAlign(tl->beginTextAlign());
2230 0 : item->text()->setPosition(tl->beginTextPosition());
2231 0 : item->text()->setFontStyle(tl->beginFontStyle());
2232 : } else {
2233 0 : item->text()->setXmlText(tl->continueText());
2234 0 : item->text()->setFamily(tl->continueFontFamily());
2235 0 : item->text()->setSize(tl->continueFontSize());
2236 0 : item->text()->setSymbolScale(tl->continueTextMusicalSymbolsScale());
2237 0 : item->text()->setSymbolSize(tl->continueTextMusicSymbolsSize());
2238 0 : item->text()->setOffset(tl->continueTextOffset() * item->mag());
2239 0 : item->text()->setAlign(tl->continueTextAlign());
2240 0 : item->text()->setPosition(tl->continueTextPosition());
2241 0 : item->text()->setFontStyle(tl->continueFontStyle());
2242 : }
2243 0 : item->text()->setPlacement(PlacementV::ABOVE);
2244 0 : item->text()->setTrack(item->track());
2245 0 : item->text()->setColor(tl->lineColor());
2246 0 : layout(item->text(), ctx);
2247 :
2248 0 : if (isSingleOrEnd) {
2249 0 : item->endText()->setXmlText(tl->endText());
2250 0 : item->endText()->setFamily(tl->endFontFamily());
2251 0 : item->endText()->setSize(tl->endFontSize());
2252 0 : item->endText()->setSymbolScale(tl->endTextMusicalSymbolsScale());
2253 0 : item->endText()->setSymbolSize(tl->endTextMusicSymbolsSize());
2254 0 : item->endText()->setOffset(tl->endTextOffset() * item->mag());
2255 0 : item->endText()->setAlign(tl->endTextAlign());
2256 0 : item->endText()->setPosition(tl->endTextPosition());
2257 0 : item->endText()->setFontStyle(tl->endFontStyle());
2258 0 : item->endText()->setPlacement(PlacementV::ABOVE);
2259 0 : item->endText()->setTrack(item->track());
2260 0 : item->endText()->setColor(tl->lineColor());
2261 0 : layout(item->endText(), ctx);
2262 : } else {
2263 0 : item->endText()->setXmlText(u"");
2264 : }
2265 :
2266 0 : if (!item->textLineBase()->textSizeSpatiumDependent()) {
2267 0 : item->text()->setSize(item->text()->size() * item->defaultSpatium() / item->spatium());
2268 0 : item->endText()->setSize(item->endText()->size() * item->defaultSpatium() / item->spatium());
2269 : }
2270 :
2271 0 : PointF pp1;
2272 0 : PointF pp2(item->pos2());
2273 :
2274 : // line with no text or hooks - just use the basic rectangle for line
2275 0 : if (item->text()->empty() && item->endText()->empty()
2276 0 : && (!isSingleOrBegin || tl->beginHookType() == HookType::NONE)
2277 0 : && (!isSingleOrEnd || tl->endHookType() == HookType::NONE)) {
2278 0 : ldata->npoints = 2;
2279 0 : ldata->points[0] = pp1;
2280 0 : ldata->points[1] = pp2;
2281 0 : ldata->lineLength = sqrt(PointF::dotProduct(pp2 - pp1, pp2 - pp1));
2282 :
2283 0 : item->setbbox(TextLineBaseSegment::boundingBoxOfLine(pp1, pp2, lineWidth / 2, isDottedLine));
2284 0 : return;
2285 : }
2286 :
2287 0 : double l1 = 0.0;
2288 0 : double l2 = 0.0;
2289 0 : const double gapBetweenTextAndLine = spatium * tl->gapBetweenTextAndLine().val();
2290 :
2291 0 : const bool alignBeginText = tl->beginTextPlace() == TextPlace::LEFT || tl->beginTextPlace() == TextPlace::AUTO;
2292 0 : const bool alignContinueText = tl->continueTextPlace() == TextPlace::LEFT || tl->continueTextPlace() == TextPlace::AUTO;
2293 0 : const bool alignEndText = tl->endTextPlace() == TextPlace::LEFT || tl->endTextPlace() == TextPlace::AUTO;
2294 0 : const bool hasBeginText = !item->text()->empty() && isSingleOrBegin;
2295 0 : const bool hasContinueText = !item->text()->empty() && !isSingleOrBegin;
2296 0 : const bool hasEndText = !item->endText()->empty() && isSingleOrEnd;
2297 :
2298 0 : Shape shape;
2299 :
2300 0 : if (!item->text()->empty()) {
2301 0 : if ((isSingleOrBegin && alignBeginText) || (!isSingleOrBegin && alignContinueText)) {
2302 0 : l1 = gapBetweenTextAndLine;
2303 0 : switch (item->text()->position()) {
2304 0 : case AlignH::LEFT:
2305 0 : l1 += item->text()->ldata()->bbox().width();
2306 0 : break;
2307 0 : case AlignH::HCENTER:
2308 0 : l1 += item->text()->ldata()->bbox().width() / 2;
2309 0 : break;
2310 0 : default:
2311 0 : break;
2312 : }
2313 : }
2314 0 : shape.add(item->text()->ldata()->bbox().translated(item->text()->pos()), item);
2315 : }
2316 : // set end text position and extend bbox
2317 0 : if (!item->endText()->empty()) {
2318 0 : if (alignEndText) {
2319 0 : l2 = gapBetweenTextAndLine;
2320 0 : switch (item->endText()->position()) {
2321 0 : case AlignH::RIGHT:
2322 0 : l2 += item->endText()->ldata()->bbox().width();
2323 0 : break;
2324 0 : case AlignH::HCENTER:
2325 0 : l2 += item->endText()->ldata()->bbox().width() / 2;
2326 0 : break;
2327 0 : default:
2328 0 : break;
2329 : }
2330 : }
2331 0 : const double endTextX = std::max(pp2.x(), shape.bbox().right() + l2); // prevent end text from overlapping begin text
2332 0 : item->endText()->mutldata()->setPosX(endTextX);
2333 0 : shape.add(item->endText()->ldata()->bbox().translated(item->endText()->pos()), item);
2334 : }
2335 :
2336 0 : bool beginHookDrawnSeparately = false;
2337 0 : bool endHookDrawnSeparately = false;
2338 :
2339 0 : if (tl->lineVisible()) {
2340 0 : pp1.rx() += l1;
2341 0 : pp2.rx() -= l2;
2342 :
2343 : // Make sure baseline of text and line are properly aligned (accounting for line thickness)
2344 0 : if ((hasBeginText && alignBeginText) || (hasContinueText && alignContinueText)) {
2345 0 : alignBaseLine(item->text(), pp1, pp2);
2346 0 : } else if (hasEndText && alignEndText) {
2347 0 : alignBaseLine(item->endText(), pp1, pp2);
2348 : }
2349 :
2350 0 : const double beginHookHeight = (tl->placeBelow() ? -1.0 : 1.0) * tl->beginHookHeight().val() * spatium;
2351 0 : const double endHookHeight = (tl->placeBelow() ? -1.0 : 1.0) * tl->endHookHeight().val() * spatium;
2352 :
2353 0 : double beginHookWidth = 0.0;
2354 0 : double endHookWidth = 0.0;
2355 :
2356 0 : if (tl->beginHookType() == HookType::HOOK_45) {
2357 0 : beginHookWidth = std::fabs(beginHookHeight * .4);
2358 0 : pp1.rx() += beginHookWidth;
2359 : }
2360 :
2361 0 : if (tl->endHookType() == HookType::HOOK_45) {
2362 0 : endHookWidth = std::fabs(endHookHeight * .4);
2363 0 : pp2.rx() -= endHookWidth;
2364 : }
2365 :
2366 : // don't draw backwards lines (or hooks) if text is longer than nominal line length
2367 0 : if (!item->text()->empty() && pp1.x() > pp2.x() && !tl->diagonal()) {
2368 0 : ldata->setShape(shape);
2369 0 : return;
2370 : }
2371 :
2372 : const bool beginArrow = isSingleOrBegin
2373 0 : && (tl->beginHookType() == HookType::ARROW_FILLED || tl->beginHookType() == HookType::ARROW);
2374 0 : const bool endArrow = isSingleOrEnd && (tl->endHookType() == HookType::ARROW_FILLED || tl->endHookType() == HookType::ARROW);
2375 :
2376 0 : if (beginArrow) {
2377 0 : const bool filled = tl->beginHookType() == HookType::ARROW_FILLED;
2378 0 : ldata->beginArrow = createArrow(true, filled, pp1, pp2, tl);
2379 0 : shape.add(ldata->beginArrow.boundingRect(), item);
2380 : }
2381 0 : if (endArrow) {
2382 0 : const bool filled = tl->endHookType() == HookType::ARROW_FILLED;
2383 0 : ldata->endArrow = createArrow(false, filled, pp1, pp2, tl);
2384 0 : shape.add(ldata->endArrow.boundingRect(), item);
2385 : }
2386 :
2387 0 : auto hasHook = [](HookType type) -> bool {
2388 0 : switch (type) {
2389 0 : case HookType::HOOK_45:
2390 : case HookType::HOOK_90:
2391 : case HookType::HOOK_90T:
2392 0 : return true;
2393 0 : case HookType::NONE:
2394 : case HookType::ARROW:
2395 : case HookType::ARROW_FILLED:
2396 : case HookType::ROSETTE:
2397 0 : return false;
2398 0 : default:
2399 0 : break;
2400 : }
2401 0 : return false;
2402 : };
2403 :
2404 0 : const bool beginHook = isSingleOrBegin && hasHook(tl->beginHookType());
2405 0 : const bool endHook = isSingleOrEnd && hasHook(tl->endHookType());
2406 :
2407 0 : if (beginHook) {
2408 : // We use the term "endpoint" for the point that does not touch the main line.
2409 0 : const PointF& beginHookEndpoint = ldata->points[ldata->npoints++]
2410 0 : = PointF(pp1.x() - beginHookWidth, pp1.y() + beginHookHeight);
2411 0 : if (tl->beginHookType() == HookType::HOOK_90T) {
2412 : // A T-hook needs to be drawn separately, so we add an extra point
2413 0 : beginHookDrawnSeparately = true;
2414 0 : ldata->points[ldata->npoints++] = PointF(pp1.x() - beginHookWidth, pp1.y() - beginHookHeight);
2415 0 : } else if (tl->lineStyle() != LineType::SOLID) {
2416 : // For non-solid lines, we also draw the hook separately,
2417 : // so that we can distribute the dashes/dots for each linepiece individually
2418 0 : beginHookDrawnSeparately = true;
2419 0 : PointF& beginHookStartpoint = ldata->points[ldata->npoints++] = pp1;
2420 :
2421 0 : if (tl->lineStyle() == LineType::DASHED) {
2422 : // For dashes lines, we extend the lines somewhat,
2423 : // so that the corner between them gets filled
2424 0 : bool checkAngle = tl->beginHookType() == HookType::HOOK_45 || tl->diagonal();
2425 0 : extendLines(beginHookEndpoint, beginHookStartpoint, pp1, pp2, lineWidth, checkAngle);
2426 : }
2427 : }
2428 : }
2429 :
2430 0 : ldata->points[ldata->npoints++] = pp1;
2431 0 : PointF& pp22 = ldata->points[ldata->npoints++] = pp2; // Keep a reference so that we can modify later
2432 :
2433 0 : if (endHook) {
2434 0 : const PointF endHookEndpoint = PointF(pp2.x() + endHookWidth, pp2.y() + endHookHeight);
2435 0 : if (tl->endHookType() == HookType::HOOK_90T) {
2436 : // A T-hook needs to be drawn separately, so we add an extra point
2437 0 : endHookDrawnSeparately = true;
2438 0 : ldata->points[ldata->npoints++] = PointF(pp2.x() + endHookWidth, pp2.y() - endHookHeight);
2439 0 : } else if (tl->lineStyle() != LineType::SOLID) {
2440 : // For non-solid lines, we also draw the hook separately,
2441 : // so that we can distribute the dashes/dots for each linepiece individually
2442 0 : endHookDrawnSeparately = true;
2443 0 : PointF& endHookStartpoint = ldata->points[ldata->npoints++] = pp2;
2444 :
2445 0 : if (tl->lineStyle() == LineType::DASHED) {
2446 : // For dashes lines, we extend the lines somewhat,
2447 : // so that the corner between them gets filled
2448 0 : bool checkAngle = tl->endHookType() == HookType::HOOK_45 || tl->diagonal();
2449 0 : extendLines(pp1, pp22, endHookStartpoint, endHookEndpoint, lineWidth, checkAngle);
2450 : }
2451 : }
2452 :
2453 0 : ldata->points[ldata->npoints++] = endHookEndpoint;
2454 : }
2455 :
2456 0 : ldata->lineLength = sqrt(PointF::dotProduct(pp22 - pp1, pp22 - pp1));
2457 : }
2458 :
2459 : // Calculate shape of line
2460 : {
2461 0 : int start = 0;
2462 0 : if (beginHookDrawnSeparately) {
2463 0 : PointF& p1 = ldata->points[start++];
2464 0 : PointF& p2 = ldata->points[start++];
2465 0 : shape.add(TextLineBaseSegment::boundingBoxOfLine(p1, p2, lineWidth / 2, isDottedLine), item);
2466 : }
2467 :
2468 0 : int end = ldata->npoints;
2469 0 : if (endHookDrawnSeparately) {
2470 0 : PointF& p1 = ldata->points[--end];
2471 0 : PointF& p2 = ldata->points[--end];
2472 0 : shape.add(TextLineBaseSegment::boundingBoxOfLine(p1, p2, lineWidth / 2, isDottedLine), item);
2473 : }
2474 :
2475 0 : for (int i = start; i < end - 1; ++i) {
2476 0 : shape.add(TextLineBaseSegment::boundingBoxOfLine(ldata->points[i], ldata->points[i + 1], lineWidth / 2,
2477 0 : isDottedLine), item, !tl->lineVisible());
2478 : }
2479 : }
2480 :
2481 0 : ldata->setShape(shape);
2482 0 : }
|