Skip to content

Commit

Permalink
better layout for beams+rests
Browse files Browse the repository at this point in the history
  • Loading branch information
wschweer committed May 17, 2014
1 parent 31620b3 commit 7f765e7
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 41 deletions.
51 changes: 20 additions & 31 deletions libmscore/beam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ bool Beam::slopeZero(const QList<ChordRest*>& cl)
//
// return true if beam spans a rest
//
foreach(const ChordRest* cr, cl) {
for(const ChordRest* cr : cl) {
if (cr->type() != CHORD)
return true;
}
Expand Down Expand Up @@ -1008,50 +1008,33 @@ static Bm beamMetric1(bool up, char l1, char l2)
//---------------------------------------------------------
// adjust
// adjust stem len for notes between start-end
// return 1/4 spatium units
//---------------------------------------------------------

static int adjust(qreal _spatium4, int slant, const QList<ChordRest*>& cl)
{
int n = cl.size();
const Chord* c1 = 0;
const Chord* c2 = 0;
int i1, i2;
for (i1 = 0; i1 < n; ++i1) {
if (cl[i1]->type() == Element::CHORD) {
c1 = static_cast<Chord*>(cl[i1]);
break;
}
}
for (i2 = n-1; i2 >= 0; --i2) {
if (cl[i2]->type() == Element::CHORD) {
c2 = static_cast<Chord*>(cl[i2]);
break;
}
}
if (!(c1 && c2))
return 0;
const ChordRest* c1 = cl[0];
const ChordRest* c2 = cl[n-1];

QPointF p1(c1->stemPosBeam()); // canvas coordinates
qreal slope = (slant * _spatium4) / (c2->stemPosBeam().x() - p1.x());
int ml = -1000;
if (c1->up()) {
for (int i = i1+1; i <= i2; ++i) {
const Chord* c = static_cast<Chord*>(cl[i]);
if (c->type() != Element::CHORD)
continue;
for (int i = 1; i < n; ++i) {
const ChordRest* c = cl[i];
QPointF p3(c->stemPosBeam());
qreal yUp = p1.y() + (p3.x() - p1.x()) * slope;
int l = lrint((yUp - p3.y()) / _spatium4);
int l = lrint((yUp - p3.y()) / (_spatium4));
ml = qMax(ml, l);
}
}
else {
for (int i = i1+1; i <= i2; ++i) {
const Chord* c = static_cast<Chord*>(cl[i]);
if (c->type() != Element::CHORD)
continue;
for (int i = 1; i < n; ++i) {
const ChordRest* c = cl[i];
QPointF p3(c->stemPosBeam());
qreal yUp = p1.y() + (p3.x() - p1.x()) * slope;
int l = lrint((p3.y() - yUp) / _spatium4);
int l = lrint((p3.y() - yUp) / (_spatium4));
ml = qMax(ml, l);
}
}
Expand Down Expand Up @@ -1399,7 +1382,9 @@ void Beam::computeStemLen(const QList<ChordRest*>& cl, qreal& py1, int beamLevel
slope = 0.0;
else
slope = (bm.s * _spatium4) / dx;
py1 += ((c1->line(_up) - c1->line(!_up)) * 2 + bm.l) * _spStaff4;
int dy = (c1->line(_up) - c1->line(!_up)) * 2;

py1 += (dy + bm.l) * _spStaff4;
}

//---------------------------------------------------------
Expand Down Expand Up @@ -1584,9 +1569,13 @@ void Beam::layout2(QList<ChordRest*>crl, SpannerSegmentType, int frag)
BeamMode bm = Groups::endBeam(c);
bool b32 = (beamLevel >= 1) && (bm == BeamMode::BEGIN32);
bool b64 = (beamLevel >= 2) && (bm == BeamMode::BEGIN64);
if ((l >= beamLevel && (b32 || b64)) || (l < beamLevel)
|| ((c->type() == REST) && (l == beamLevel)))

if ((l >= beamLevel && (b32 || b64)) || (l < beamLevel)) {
if (i && crl[i-1]->type() == REST) {
--i;
}
break;
}
}

int bl = growDown ? beamLevel : -beamLevel;
Expand Down
8 changes: 4 additions & 4 deletions libmscore/chord.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ class Chord : public ChordRest {
// Chord has at least one Note
Note* upNote() const;
Note* downNote() const;
virtual int upLine() const;
virtual int downLine() const;
virtual int upString() const;
virtual int downString() const;

Expand All @@ -161,9 +159,11 @@ class Chord : public ChordRest {
int graceIndex() const { return _graceIndex; }
void setGraceIndex(int val) { _graceIndex = val; }

virtual QPointF stemPos() const; ///< page coordinates
virtual int upLine() const;
virtual int downLine() const;
virtual QPointF stemPos() const; ///< page coordinates
virtual QPointF stemPosBeam() const; ///< page coordinates
virtual qreal stemPosX() const;
QPointF stemPosBeam() const; ///< page coordinates
bool underBeam() const;
Hook* hook() const { return _hook; }

Expand Down
5 changes: 3 additions & 2 deletions libmscore/chordrest.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ class ChordRest : public DurationElement {
virtual qreal downPos() const = 0;
virtual qreal centerX() const = 0;

int line(bool up) const { return up ? upLine() : downLine(); }
int line() const { return _up ? upLine() : downLine(); }
virtual int upLine() const = 0;
virtual int downLine() const = 0;
virtual QPointF stemPos() const = 0;
virtual qreal stemPosX() const = 0;
virtual QPointF stemPosBeam() const = 0;

int line(bool up) const { return up ? upLine() : downLine(); }
int line() const { return _up ? upLine() : downLine(); }
bool up() const { return _up; }
void setUp(bool val) { _up = val; }

Expand Down
2 changes: 1 addition & 1 deletion libmscore/cmd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2002,7 +2002,7 @@ void Score::cmdMoveRest(Rest* rest, Direction dir)
else if (dir == Direction::DOWN)
pos.ry() += spatium();
undoChangeProperty(rest, P_USER_OFF, pos);
setLayoutAll(false);
setLayoutAll(rest->beam() != nullptr); // layout all if rest is beamed
}

//---------------------------------------------------------
Expand Down
20 changes: 18 additions & 2 deletions libmscore/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ qreal Rest::mag() const

int Rest::upLine() const
{
return lrint(bbox().top() * 2.0 / spatium());
return lrint((pos().y() + bbox().top() + spatium()) * 2 / spatium());
}

//---------------------------------------------------------
Expand All @@ -565,7 +565,7 @@ int Rest::upLine() const

int Rest::downLine() const
{
return lrint(bbox().bottom() * 2.0 / spatium());
return lrint((pos().y() + bbox().top() + spatium()) * 2 / spatium());
}

//---------------------------------------------------------
Expand All @@ -578,6 +578,22 @@ QPointF Rest::stemPos() const
return pagePos();
}

//---------------------------------------------------------
// stemPosBeam
// return stem position of note on beam side
// return canvas coordinates
//---------------------------------------------------------

QPointF Rest::stemPosBeam() const
{
QPointF p(pagePos());
if (_up)
p.ry() += bbox().top() + spatium() * 2;
else
p.ry() += bbox().bottom() - spatium() * 2;
return p;
}

//---------------------------------------------------------
// stemPosX
//---------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions libmscore/rest.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Rest : public ChordRest {
virtual int downLine() const;
virtual QPointF stemPos() const;
virtual qreal stemPosX() const;
virtual QPointF stemPosBeam() const;
};


Expand Down
Binary file added vtest/beams-1-ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vtest/beams-1.mscz
Binary file not shown.
Binary file added vtest/beams-2-ref.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added vtest/beams-2.mscz
Binary file not shown.
2 changes: 1 addition & 1 deletion vtest/gen
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ else
chord-layout-1 chord-layout-2 chord-layout-3 chord-layout-4 chord-layout-5\
chord-layout-6 chord-layout-7 chord-layout-8 chord-layout-9 chord-layout-10 chord-layout-11\
accidental-1 accidental-2 accidental-3 accidental-4 accidental-5 accidental-6 accidental-7\
tie-1 grace-1 harmony-1"
tie-1 grace-1 harmony-1 beams-1 beams-2"
fi

DPI=130
Expand Down

0 comments on commit 7f765e7

Please sign in to comment.