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) 2021 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 "lyrics.h"
24 :
25 : #include "../editing/navigation.h"
26 :
27 : #include "chord.h"
28 : #include "chordrest.h"
29 : #include "dummyparent.h"
30 : #include "factory.h"
31 : #include "measure.h"
32 : #include "note.h"
33 : #include "score.h"
34 : #include "segment.h"
35 : #include "system.h"
36 : #include "utils.h"
37 :
38 : using namespace mu;
39 :
40 : namespace mu::engraving {
41 : //---------------------------------------------------------
42 : // LyricsLine
43 : //---------------------------------------------------------
44 :
45 : static const ElementStyle lyricsLineElementStyle {
46 : { Sid::lyricsDashLineThickness, Pid::LINE_WIDTH }
47 : };
48 :
49 257 : LyricsLine::LyricsLine(DummyParentOr<Lyrics> parent)
50 257 : : SLine(ElementType::LYRICSLINE, parent)
51 : {
52 257 : setDiagonal(false);
53 257 : initElementStyle(&lyricsLineElementStyle);
54 257 : m_nextLyrics = 0;
55 257 : }
56 :
57 13 : LyricsLine::LyricsLine(const ElementType& type, DummyParentOr<Lyrics> parent, ElementFlags f)
58 13 : : SLine(type, parent, f)
59 : {
60 13 : setDiagonal(false);
61 13 : initElementStyle(&lyricsLineElementStyle);
62 13 : m_nextLyrics = 0;
63 13 : }
64 :
65 0 : LyricsLine::LyricsLine(const LyricsLine& g)
66 0 : : SLine(g)
67 : {
68 0 : m_nextLyrics = 0;
69 0 : }
70 :
71 : //---------------------------------------------------------
72 : // createLineSegment
73 : //---------------------------------------------------------
74 :
75 260 : LineSegment* LyricsLine::createLineSegment()
76 : {
77 260 : LyricsLineSegment* seg = new LyricsLineSegment(this);
78 260 : seg->setTrack(track());
79 260 : seg->setColor(color());
80 260 : return seg;
81 : }
82 :
83 : //---------------------------------------------------------
84 : // removeUnmanaged
85 : // same as Spanner::removeUnmanaged(), but in addition, remove from hosting Lyrics
86 : //---------------------------------------------------------
87 :
88 248 : void LyricsLine::removeUnmanaged()
89 : {
90 248 : Spanner::removeUnmanaged();
91 248 : if (lyrics()) {
92 248 : lyrics()->remove(this);
93 : }
94 248 : }
95 :
96 : //---------------------------------------------------------
97 : // setProperty
98 : //---------------------------------------------------------
99 :
100 658 : bool LyricsLine::setProperty(Pid propertyId, const engraving::PropertyValue& v)
101 : {
102 658 : switch (propertyId) {
103 2 : case Pid::SPANNER_TICKS:
104 : {
105 : // if parent lyrics has a melisma, change its length too
106 4 : if (ownershipParent() && ownershipParent()->isLyrics()
107 4 : && isEndMelisma()) {
108 2 : Fraction newTicks = toLyrics(ownershipParent())->ticks() + v.value<Fraction>() - ticks();
109 2 : ownershipParent()->undoChangeProperty(Pid::LYRIC_TICKS, newTicks);
110 : }
111 2 : setTicks(v.value<Fraction>());
112 : }
113 2 : break;
114 656 : default:
115 656 : if (!SLine::setProperty(propertyId, v)) {
116 0 : return false;
117 : }
118 656 : break;
119 : }
120 658 : triggerLayout();
121 658 : return true;
122 : }
123 :
124 105 : PropertyValue LyricsLine::propertyDefault(Pid id) const
125 : {
126 105 : switch (id) {
127 4 : case Pid::LINE_WIDTH:
128 4 : return styleValue(Pid::LINE_WIDTH, getPropertyStyle(Pid::LINE_WIDTH));
129 101 : default:
130 101 : return SLine::propertyDefault(id);
131 : }
132 : }
133 :
134 636 : Sid LyricsLine::getPropertyStyle(Pid propertyId) const
135 : {
136 636 : if (propertyId == Pid::LINE_WIDTH) {
137 634 : return isEndMelisma() ? Sid::lyricsLineThickness : Sid::lyricsDashLineThickness;
138 : }
139 :
140 2 : return SLine::getPropertyStyle(propertyId);
141 : }
142 :
143 354 : void LyricsLine::doComputeEndElement()
144 : {
145 354 : if (!isEndMelisma()) {
146 240 : Spanner::doComputeEndElement();
147 240 : return;
148 : }
149 :
150 114 : setEndElement(score()->findChordRestEndingBeforeTickInTrack(tick2(), track()));
151 :
152 114 : if (!endElement()) {
153 1 : setEndElement(score()->findChordRestEndingBeforeTickInStaff(tick2(), track2staff(track())));
154 : }
155 : }
156 :
157 : //=========================================================
158 : // LyricsLineSegment
159 : //=========================================================
160 :
161 0 : void LyricsLineSegment::rebaseAnchors(EditData&, Grip)
162 : {
163 : // Don't rebase lyric line anchors on drag
164 0 : return;
165 : }
166 :
167 260 : LyricsLineSegment::LyricsLineSegment(LyricsLine* sp)
168 260 : : LineSegment(ElementType::LYRICSLINE_SEGMENT, sp, ElementFlag::ON_STAFF)
169 : {
170 260 : }
171 :
172 5 : LyricsLineSegment::LyricsLineSegment(const ElementType& type, LyricsLine* sp, ElementFlags f)
173 5 : : LineSegment(type, sp, f)
174 : {
175 5 : }
176 :
177 352 : double LyricsLineSegment::baseLineShift() const
178 : {
179 352 : if (lyricsLine()->isEndMelisma()) {
180 118 : return -0.5 * absoluteFromSpatium(lineWidth());
181 : }
182 :
183 234 : Lyrics* segLyrics = lyrics();
184 234 : return -style().styleD(Sid::lyricsDashYposRatio) * segLyrics->fontMetrics().xHeight();
185 : }
186 :
187 3 : PropertyValue LyricsLineSegment::getProperty(Pid propertyId) const
188 : {
189 3 : if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
190 1 : return d.delegate ? d.delegate->getProperty(propertyId) : propertyDefault(propertyId);
191 : }
192 :
193 2 : return LineSegment::getProperty(propertyId);
194 : }
195 :
196 6 : bool LyricsLineSegment::setProperty(Pid propertyId, const PropertyValue& val)
197 : {
198 6 : if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
199 1 : return d.delegate ? d.delegate->setProperty(propertyId, val) : false;
200 : }
201 :
202 5 : return LineSegment::setProperty(propertyId, val);
203 : }
204 :
205 3 : PropertyValue LyricsLineSegment::propertyDefault(Pid propertyId) const
206 : {
207 3 : if (const PropertyDelegate d = resolvePropertyDelegate(propertyId); d.hasDelegate) {
208 1 : return d.delegate ? d.delegate->propertyDefault(propertyId) : PropertyValue();
209 : }
210 :
211 2 : return LineSegment::propertyDefault(propertyId);
212 : }
213 :
214 27 : PropertyDelegate LyricsLineSegment::resolvePropertyDelegate(Pid propertyId) const
215 : {
216 27 : if (propertyId == Pid::GENERATED) {
217 0 : return PropertyDelegate(lyricsLine());
218 : }
219 :
220 27 : return LineSegment::resolvePropertyDelegate(propertyId);
221 : }
222 :
223 : //=========================================================
224 : // PartialLyricsLine
225 : //=========================================================
226 :
227 : static const ElementStyle partialLyricsLineElementStyle {
228 : { Sid::lyricsDashLineThickness, Pid::LINE_WIDTH },
229 : { Sid::lyricsPlacement, Pid::PLACEMENT },
230 : };
231 :
232 13 : PartialLyricsLine::PartialLyricsLine(DummyParent* parent)
233 13 : : LyricsLine(ElementType::PARTIAL_LYRICSLINE, parent)
234 : {
235 13 : setGenerated(false);
236 13 : initElementStyle(&partialLyricsLineElementStyle);
237 13 : }
238 :
239 0 : PartialLyricsLine::PartialLyricsLine(const PartialLyricsLine& other)
240 0 : : LyricsLine(other)
241 : {
242 0 : m_isEndMelisma = other.m_isEndMelisma;
243 0 : m_centerBetweenStaves = other.m_centerBetweenStaves;
244 0 : }
245 :
246 5 : LineSegment* PartialLyricsLine::createLineSegment()
247 : {
248 5 : PartialLyricsLineSegment* seg = new PartialLyricsLineSegment(this);
249 5 : seg->setTrack(track());
250 5 : seg->setColor(color());
251 5 : return seg;
252 : }
253 :
254 107 : PropertyValue PartialLyricsLine::getProperty(Pid propertyId) const
255 : {
256 107 : switch (propertyId) {
257 12 : case Pid::VERSE:
258 12 : return m_verse;
259 8 : case Pid::CENTER_BETWEEN_STAVES:
260 8 : return centerBetweenStaves();
261 87 : default:
262 87 : return LyricsLine::getProperty(propertyId);
263 : }
264 : }
265 :
266 44 : bool PartialLyricsLine::setProperty(Pid propertyId, const PropertyValue& val)
267 : {
268 44 : switch (propertyId) {
269 2 : case Pid::VERSE:
270 2 : setVerse(val.toInt());
271 2 : break;
272 4 : case Pid::CENTER_BETWEEN_STAVES:
273 4 : setCenterBetweenStaves(val.value<AutoOnOff>());
274 4 : break;
275 38 : default:
276 38 : return LyricsLine::setProperty(propertyId, val);
277 : }
278 :
279 6 : triggerLayout();
280 :
281 6 : return true;
282 : }
283 :
284 90 : PropertyValue PartialLyricsLine::propertyDefault(Pid propertyId) const
285 : {
286 90 : switch (propertyId) {
287 6 : case Pid::VERSE:
288 6 : return 0;
289 7 : case Pid::CENTER_BETWEEN_STAVES:
290 14 : return AutoOnOff::AUTO;
291 77 : default:
292 77 : return LyricsLine::propertyDefault(propertyId);
293 : }
294 : }
295 :
296 0 : void PartialLyricsLine::reset()
297 : {
298 0 : undoResetProperty(Pid::CENTER_BETWEEN_STAVES);
299 0 : LyricsLine::reset();
300 0 : }
301 :
302 35 : Sid PartialLyricsLine::getPropertyStyle(Pid propertyId) const
303 : {
304 35 : switch (propertyId) {
305 18 : case Pid::PLACEMENT:
306 18 : return Sid::lyricsPlacement;
307 17 : default:
308 17 : return LyricsLine::getPropertyStyle(propertyId);
309 : }
310 : }
311 :
312 1 : void PartialLyricsLine::undoChangeProperty(Pid id, const PropertyValue& v, PropertyFlags ps)
313 : {
314 1 : if (id == Pid::VERSE && verse() != v.toInt()) {
315 1 : ChordRest* endCR = endElement()
316 1 : && endElement()->isChordRest() ? toChordRest(endElement()) : nullptr;
317 1 : Lyrics* endLyrics = nullptr;
318 1 : if (endCR) {
319 1 : for (Lyrics* lyr : endCR->lyrics()) {
320 1 : if (lyr->verse() == verse()) {
321 1 : endLyrics = lyr;
322 1 : break;
323 : }
324 : }
325 : }
326 :
327 1 : LyricsLine::undoChangeProperty(id, v, ps);
328 1 : if (endLyrics && endLyrics->verse() != v.toInt()) {
329 1 : endLyrics->undoChangeProperty(id, v, ps);
330 : }
331 1 : return;
332 : }
333 :
334 0 : LyricsLine::undoChangeProperty(id, v, ps);
335 : }
336 :
337 13 : void PartialLyricsLine::doComputeEndElement()
338 : {
339 13 : LyricsLine::doComputeEndElement();
340 :
341 13 : if (startElement() && endElement() && startElement()->tick() > endElement()->tick()) {
342 0 : setEndElement(startElement());
343 : }
344 13 : }
345 :
346 : //=========================================================
347 : // PartialLyricsLineSegment
348 : //=========================================================
349 :
350 : static const ElementStyle partialLyricsLineSegmentElementStyle {
351 : { Sid::lyricsMinTopDistance, Pid::MIN_DISTANCE },
352 : };
353 :
354 5 : PartialLyricsLineSegment::PartialLyricsLineSegment(PartialLyricsLine* line)
355 5 : : LyricsLineSegment(ElementType::PARTIAL_LYRICSLINE_SEGMENT, line, ElementFlag::ON_STAFF)
356 : {
357 5 : setGenerated(false);
358 5 : initElementStyle(&partialLyricsLineSegmentElementStyle);
359 5 : }
360 :
361 9 : double PartialLyricsLineSegment::lineSpacing() const
362 : {
363 9 : const Lyrics* lyrics = lyricsLine()->findAdjacentLyricsOrDefault();
364 :
365 9 : if (!lyrics) {
366 0 : return 0.0;
367 : }
368 :
369 9 : return lyrics->lineSpacing();
370 : }
371 :
372 9 : double PartialLyricsLineSegment::baseLineShift() const
373 : {
374 9 : if (lyricsLine()->isEndMelisma()) {
375 5 : return -0.5 * absoluteFromSpatium(lineWidth());
376 : }
377 :
378 4 : const Lyrics* lyrics = lyricsLine()->findAdjacentLyricsOrDefault();
379 4 : if (!lyrics) {
380 0 : return 0.0;
381 : }
382 :
383 4 : return -style().styleD(Sid::lyricsDashYposRatio) * lyrics->fontMetrics().xHeight();
384 : }
385 :
386 30 : PropertyDelegate PartialLyricsLineSegment::resolvePropertyDelegate(Pid pid) const
387 : {
388 30 : switch (pid) {
389 3 : case Pid::VERSE:
390 : case Pid::CENTER_BETWEEN_STAVES:
391 3 : return PropertyDelegate(lyricsLine());
392 27 : default:
393 27 : return LyricsLineSegment::resolvePropertyDelegate(pid);
394 : }
395 : }
396 :
397 13 : Lyrics* PartialLyricsLine::findLyricsInPreviousRepeatSeg() const
398 : {
399 13 : const std::vector<Measure*> measures = findPreviousRepeatMeasures(findStartMeasure());
400 :
401 19 : for (const Measure* measure : measures) {
402 13 : Lyrics* prev = Navigation::lastLyricsInMeasure(measure->last(SegmentType::ChordRest), staffIdx(), verse(), placement());
403 :
404 13 : if (!prev) {
405 6 : continue;
406 : }
407 :
408 7 : return prev;
409 : }
410 :
411 6 : return nullptr;
412 13 : }
413 :
414 13 : Lyrics* PartialLyricsLine::findAdjacentLyricsOrDefault() const
415 : {
416 13 : Lyrics* prev = findLyricsInPreviousRepeatSeg();
417 13 : if (prev) {
418 7 : return prev;
419 : }
420 :
421 6 : Lyrics* next = nextLyrics();
422 6 : if (next) {
423 0 : return next;
424 : }
425 :
426 : // If there are no adjacent lyrics, create dummy lyrics using the odd lyrics text style to get font information
427 6 : Lyrics* dummyLyr = Factory::createLyrics(score()->dummy());
428 6 : dummyLyr->setTextStyleType(TextStyleType::LYRICS_ODD);
429 6 : return dummyLyr;
430 : }
431 : }
|