Skip to content

Commit

Permalink
Improve layout of two-note tremolos of chords without stem (+ collect…
Browse files Browse the repository at this point in the history
…_artifacts)
  • Loading branch information
Harmoniker1 committed Jan 26, 2020
1 parent fcf873c commit 9b948ee
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
42 changes: 25 additions & 17 deletions libmscore/chord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,38 +1450,40 @@ qreal Chord::minAbsStemLength() const

// two-note tremolo
else {
const qreal tremoloMinHeight = ((_tremolo->lines() - 1) * td + sw) * spatium();
return tremoloMinHeight + beamLvl * beamDist + 2 * td * spatium();
if (_tremolo->chord1()->up() == _tremolo->chord2()->up()) {
const qreal tremoloMinHeight = ((_tremolo->lines() - 1) * td + sw) * spatium();
return tremoloMinHeight + beamLvl * beamDist + 2 * td * spatium();
}
return 0.0;
}
}

//---------------------------------------------------------
// adjustStemLenWithTwoNotesTremolo
// extendedStemLenWithTwoNotesTremolo
// Extend stem of one of the chords to make the tremolo less steep
//---------------------------------------------------------

static void adjustStemLenWithTwoNotesTremolo(Tremolo* _tremolo, qreal _spatium)
std::array<double, 2> extendedStemLenWithTwoNoteTremolo(Tremolo* _tremolo, qreal _spatium,
qreal stemLen1, qreal stemLen2)
{
Chord* c1 = _tremolo->chord1();
Chord* c2 = _tremolo->chord2();
const qreal sgn1 = c1->up() ? -1.0 : 1.0;
const qreal sgn2 = c2->up() ? -1.0 : 1.0;
if (c1->up() == c2->up()) {
const qreal sgn = c1->up() ? -1.0 : 1.0;
const qreal stemTipDistance =
(c2->stemPos().y() + sgn * c2->stem()->len()) - (c1->stemPos().y() + sgn * c1->stem()->len());
(c2->stemPos().y() + stemLen2) - (c1->stemPos().y() + stemLen1);
const bool stem1Higher = stemTipDistance > 0.0;
if (std::abs(stemTipDistance) > 1.0 * _spatium) {
if ( ( c1->up() && !stem1Higher)
|| (!c1->up() && stem1Higher)) {
const qreal chord1StemLen = c1->stem()->len();
c1->stem()->setLen(chord1StemLen + std::abs(stemTipDistance) - 1.0 * _spatium);
}
else if (( c1->up() && stem1Higher)
|| (!c1->up() && !stem1Higher)) {
const qreal chord2StemLen = c2->stem()->len();
c2->stem()->setLen(chord2StemLen + std::abs(stemTipDistance) - 1.0 * _spatium);
}
|| (!c1->up() && stem1Higher))
return { stemLen1 + sgn1 * (std::abs(stemTipDistance) - 1.0 * _spatium), stemLen2 };
else /* if (( c1->up() && stem1Higher)
|| (!c1->up() && !stem1Higher)) */
return { stemLen1, stemLen2 + sgn2 * (std::abs(stemTipDistance) - 1.0 * _spatium) };
}
}
return { stemLen1, stemLen2 };
}

//---------------------------------------------------------
Expand Down Expand Up @@ -1516,8 +1518,14 @@ void Chord::layoutStem1()
// if there is a two-note tremolo attached, and it is too steep,
// extend stem of one of the chords.
// this should be done after the stem lengths of two notes are both calculated
if (_tremolo && this == _tremolo->chord2())
adjustStemLenWithTwoNotesTremolo(_tremolo, spatium());
if (_tremolo && this == _tremolo->chord2()) {
Stem* stem1 = _tremolo->chord1()->stem();
Stem* stem2 = _tremolo->chord2()->stem();
std::array<double, 2> extendedLen = extendedStemLenWithTwoNoteTremolo(_tremolo, spatium(),
stem1->stemLen(), stem2->stemLen());
stem1->setLen(extendedLen[0]);
stem2->setLen(extendedLen[1]);
}
}
else {
if (_stem)
Expand Down
14 changes: 9 additions & 5 deletions libmscore/tremolo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void Tremolo::layoutOneNoteTremolo(qreal x, qreal y, qreal _spatium)
const qreal sw = score()->styleS(Sid::tremoloStrokeWidth).val();

qreal t;
// nearest distance between note and tremolo stroke should be no less than 3
// nearest distance between note and tremolo stroke should be no less than 3.0
if (chord()->hook() || chord()->beam()) {
t = up ? -3.0 - (2.0 * (lines() - 1)) * td - 2.0 * sw : 3.0;
}
Expand Down Expand Up @@ -356,6 +356,8 @@ void Tremolo::layout()
layoutOneNoteTremolo(x, y, _spatium);
}

extern std::array<double, 2> extendedStemLenWithTwoNoteTremolo(Tremolo*, qreal, qreal, qreal);

//---------------------------------------------------------
// layoutTwoNotesTremolo
//---------------------------------------------------------
Expand Down Expand Up @@ -399,8 +401,10 @@ void Tremolo::layoutTwoNotesTremolo(qreal x, qreal y, qreal h, qreal _spatium)
}
else {
firstChordStaffY = _chord1->pagePos().y() - _chord1->y(); // y coordinate of the staff of the first chord
y1 = _chord1->stemPosBeam().y() - firstChordStaffY + _chord1->defaultStemLength();
y2 = _chord2->stemPosBeam().y() - firstChordStaffY + _chord2->defaultStemLength();
const std::array<double, 2> extendedLen
= extendedStemLenWithTwoNoteTremolo(this, spatium(), _chord1->defaultStemLength(), _chord2->defaultStemLength());
y1 = _chord1->stemPos().y() - firstChordStaffY + extendedLen[0];
y2 = _chord2->stemPos().y() - firstChordStaffY + extendedLen[1];
}

// improve the case when one stem is up and another is down
Expand All @@ -420,10 +424,10 @@ void Tremolo::layoutTwoNotesTremolo(qreal x, qreal y, qreal h, qreal _spatium)
}

// compute the x coordinates of the inner edge of the stems
qreal x2 = _chord2->stemPosBeam().x();
qreal x2 = _chord2->stemPos().x();
if (_chord2->up() && stem2)
x2 -= stem2->lineWidth();
qreal x1 = _chord1->stemPosBeam().x();
qreal x1 = _chord1->stemPos().x();
if (!_chord1->up() && stem1)
x1 += stem1->lineWidth();

Expand Down

0 comments on commit 9b948ee

Please sign in to comment.