Skip to content

Commit

Permalink
Merge pull request #23743 from miiizen/12955-measure-nums
Browse files Browse the repository at this point in the history
Fix measure number layout order
  • Loading branch information
mike-spa authored Jul 26, 2024
2 parents 9703f42 + 2d08541 commit 73cc000
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/engraving/dom/measurenumber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace mu::engraving {
static const ElementStyle measureNumberStyle {
{ Sid::measureNumberVPlacement, Pid::PLACEMENT },
{ Sid::measureNumberHPlacement, Pid::HPLACEMENT },
{ Sid::measureNumberMinDistance, Pid::MIN_DISTANCE }
};

//---------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/engraving/dom/mmrestrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ namespace mu::engraving {
static const ElementStyle mmRestRangeStyle {
{ Sid::mmRestRangeBracketType, Pid::MMREST_RANGE_BRACKET_TYPE },
{ Sid::mmRestRangeVPlacement, Pid::PLACEMENT },
{ Sid::mmRestRangeHPlacement, Pid::HPLACEMENT }
{ Sid::mmRestRangeHPlacement, Pid::HPLACEMENT },
{ Sid::mmRestRangeMinDistance, Pid::MIN_DISTANCE }
};

MMRestRange::MMRestRange(Measure* parent)
Expand Down
65 changes: 42 additions & 23 deletions src/engraving/rendering/dev/measurelayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ void MeasureLayout::layoutMeasureNumber(Measure* m, LayoutContext& ctx)
m->add(t);
}
t->setXmlText(s);
TLayout::layoutMeasureNumber(t, t->mutldata());
TLayout::layoutMeasureNumber(t, t->mutldata(), ctx);
} else {
if (t) {
ctx.mutDom().doUndoRemoveElement(t);
Expand Down Expand Up @@ -1173,10 +1173,47 @@ void MeasureLayout::layoutMMRestRange(Measure* m, LayoutContext& ctx)
}
// setXmlText is reimplemented to take care of brackets
rr->setXmlText(s);
TLayout::layoutMMRestRange(rr, rr->mutldata());
TLayout::layoutMMRestRange(rr, rr->mutldata(), ctx);
}
}

MeasureLayout::MeasureStartEndPos MeasureLayout::getMeasureStartEndPos(const Measure* measure, const Segment* firstCrSeg,
const staff_idx_t staffIdx, const bool needsHeaderException,
const bool modernMMRest,
const LayoutContext& ctx)
{
if (!measure || !firstCrSeg) {
return MeasureStartEndPos(0.0, 0.0);
}

Segment* s1;
for (s1 = firstCrSeg->prevActive(); s1 && s1->allElementsInvisible(); s1 = s1->prevActive()) {
}
Segment* s2;
if (modernMMRest) {
for (s2 = firstCrSeg->nextActive(); s2; s2 = s2->nextActive()) {
if (!s2->isChordRestType() && s2->element(staffIdx * VOICES)) {
break;
}
}
} else {
// Ignore any stuff before the end bar line (such as courtesy clefs)
s2 = measure->findSegment(SegmentType::EndBarLine, measure->endTick());
}

double x1 = s1 ? s1->x() + s1->minRight() : 0;
double x2 = s2 ? s2->x() - s2->minLeft() : measure->width();

bool headerException = measure->header() && firstCrSeg->prev() && !firstCrSeg->prev()->isStartRepeatBarLineType()
&& needsHeaderException;
if (headerException) { //needs this exception on header bar
// Set x1 to the imaginary barline located the minimum barline->note distance to the left of the rest's segment
x1 = firstCrSeg->x() - ctx.conf().styleMM(Sid::barNoteDistance);
}

return MeasureStartEndPos(x1, x2);
}

//-----------------------------------------------------------------------------
// layoutMeasureElements()
// lays out all the element of a measure
Expand Down Expand Up @@ -1212,33 +1249,15 @@ void MeasureLayout::layoutMeasureElements(Measure* m, LayoutContext& ctx)
// x1 - left measure position of free space
// x2 - right measure position of free space

Segment* s1;
for (s1 = s.prevActive(); s1 && s1->allElementsInvisible(); s1 = s1->prevActive()) {
}
Segment* s2;
if (modernMMRest) {
for (s2 = s.nextActive(); s2; s2 = s2->nextActive()) {
if (!s2->isChordRestType() && s2->element(staffIdx * VOICES)) {
break;
}
}
} else {
// Ignore any stuff before the end bar line (such as courtesy clefs)
s2 = m->findSegment(SegmentType::EndBarLine, m->endTick());
}
const MeasureStartEndPos measureStartEnd = getMeasureStartEndPos(m, &s, staffIdx, e->isMMRest(), modernMMRest, ctx);

double x1 = s1 ? s1->x() + s1->minRight() : 0;
double x2 = s2 ? s2->x() - s2->minLeft() : m->width();
const double x1 = measureStartEnd.x1;
const double x2 = measureStartEnd.x2;

if (e->isMMRest()) {
MMRest* mmrest = toMMRest(e);
// center multimeasure rest
double d = ctx.conf().styleMM(Sid::multiMeasureRestMargin);
bool headerException = m->header() && s.prev() && !s.prev()->isStartRepeatBarLineType();
if (headerException) { //needs this exception on header bar
// Set x1 to the imaginary barline located the minimum barline->note distance to the left of the rest's segment
x1 = s.x() - ctx.conf().styleMM(Sid::barNoteDistance);
}
double w = x2 - x1 - 2 * d;
MMRest::LayoutData* mmrestLD = mmrest->mutldata();
mmrestLD->restWidth = w;
Expand Down
10 changes: 10 additions & 0 deletions src/engraving/rendering/dev/measurelayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ namespace mu::engraving::rendering::dev {
class MeasureLayout
{
public:
struct MeasureStartEndPos {
MeasureStartEndPos(double x1, double x2)
: x1(x1), x2(x2) {}
double x1;
double x2;
};

MeasureLayout() = default;

static void layout2(Measure* item, LayoutContext& ctx);
Expand Down Expand Up @@ -66,6 +73,9 @@ class MeasureLayout

static void layoutTimeTickAnchors(Measure* m, LayoutContext& ctx);

static MeasureStartEndPos getMeasureStartEndPos(const Measure* measure, const Segment* firstCrSeg, const staff_idx_t staffIdx,
const bool needsHeaderException, const bool modernMMRest, const LayoutContext& ctx);

private:

static void createMMRest(LayoutContext& ctx, Measure* firstMeasure, Measure* lastMeasure, const Fraction& len);
Expand Down
18 changes: 16 additions & 2 deletions src/engraving/rendering/dev/systemlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,9 @@ void SystemLayout::layoutSystemElements(System* system, LayoutContext& ctx)
// create skylines
//-------------------------------------------------------------

std::vector<MeasureNumber*> measureNumbers;
std::vector<MMRestRange*> mmrRanges;

for (size_t staffIdx = 0; staffIdx < ctx.dom().nstaves(); ++staffIdx) {
SysStaff* ss = system->staff(staffIdx);
Skyline& skyline = ss->skyline();
Expand All @@ -814,10 +817,10 @@ void SystemLayout::layoutSystemElements(System* system, LayoutContext& ctx)
continue;
}
if (mno && mno->addToSkyline()) {
ss->skyline().add(mno->ldata()->bbox().translated(m->pos() + mno->pos() + mno->staffOffset()), mno);
measureNumbers.push_back(mno);
}
if (mmrr && mmrr->addToSkyline()) {
ss->skyline().add(mmrr->ldata()->bbox().translated(m->pos() + mmrr->pos()), mmrr);
mmrRanges.push_back(mmrr);
}
if (m->staffLines(staffIdx)->addToSkyline()) {
ss->skyline().add(m->staffLines(staffIdx)->ldata()->bbox().translated(m->pos()), m->staffLines(staffIdx));
Expand Down Expand Up @@ -1381,6 +1384,17 @@ void SystemLayout::layoutSystemElements(System* system, LayoutContext& ctx)
}
}
}

for (MeasureNumber* mno : measureNumbers) {
Autoplace::autoplaceMeasureElement(mno, mno->mutldata());
system->staff(mno->staffIdx())->skyline().add(mno->ldata()->bbox().translated(mno->measure()->pos() + mno->pos()
+ mno->staffOffset()), mno);
}

for (MMRestRange* mmrr : mmrRanges) {
Autoplace::autoplaceMeasureElement(mmrr, mmrr->mutldata());
system->staff(mmrr->staffIdx())->skyline().add(mmrr->ldata()->bbox().translated(mmrr->measure()->pos() + mmrr->pos()), mmrr);
}
}

void SystemLayout::doLayoutTies(System* system, std::vector<Segment*> sl, const Fraction& stick, const Fraction& etick, LayoutContext& ctx)
Expand Down
46 changes: 14 additions & 32 deletions src/engraving/rendering/dev/tlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
#include "tremololayout.h"
#include "tupletlayout.h"
#include "horizontalspacing.h"
#include "measurelayout.h"

using namespace muse;
using namespace muse::draw;
Expand Down Expand Up @@ -314,7 +315,7 @@ void TLayout::layoutItem(EngravingItem* item, LayoutContext& ctx)
layoutMarker(item_cast<const Marker*>(item), static_cast<Marker::LayoutData*>(ldata));
break;
case ElementType::MEASURE_NUMBER:
layoutMeasureNumber(item_cast<const MeasureNumber*>(item), static_cast<MeasureNumber::LayoutData*>(ldata));
layoutMeasureNumber(item_cast<const MeasureNumber*>(item), static_cast<MeasureNumber::LayoutData*>(ldata), ctx);
break;
case ElementType::MEASURE_REPEAT:
layoutMeasureRepeat(item_cast<const MeasureRepeat*>(item), static_cast<MeasureRepeat::LayoutData*>(ldata), ctx);
Expand All @@ -323,7 +324,7 @@ void TLayout::layoutItem(EngravingItem* item, LayoutContext& ctx)
layoutMMRest(item_cast<const MMRest*>(item), static_cast<MMRest::LayoutData*>(ldata), ctx);
break;
case ElementType::MMREST_RANGE:
layoutMMRestRange(item_cast<const MMRestRange*>(item), static_cast<MMRestRange::LayoutData*>(ldata));
layoutMMRestRange(item_cast<const MMRestRange*>(item), static_cast<MMRestRange::LayoutData*>(ldata), ctx);
break;
case ElementType::NOTE:
layoutNote(item_cast<const Note*>(item), static_cast<Note::LayoutData*>(ldata));
Expand Down Expand Up @@ -4025,15 +4026,15 @@ void TLayout::layoutBaseMeasureBase(const MeasureBase* item, MeasureBase::Layout
}
}

void TLayout::layoutMeasureNumber(const MeasureNumber* item, MeasureNumber::LayoutData* ldata)
void TLayout::layoutMeasureNumber(const MeasureNumber* item, MeasureNumber::LayoutData* ldata, const LayoutContext& ctx)
{
LAYOUT_CALL_ITEM(item);
LD_CONDITION(item->measure()->ldata()->isSetBbox()); // layoutMeasureNumberBase

layoutMeasureNumberBase(item, ldata);
layoutMeasureNumberBase(item, ldata, ctx);
}

void TLayout::layoutMeasureNumberBase(const MeasureNumberBase* item, MeasureNumberBase::LayoutData* ldata)
void TLayout::layoutMeasureNumberBase(const MeasureNumberBase* item, MeasureNumberBase::LayoutData* ldata, const LayoutContext& ctx)
{
IF_ASSERT_FAILED(item->explicitParent()) {
return;
Expand Down Expand Up @@ -4078,34 +4079,15 @@ void TLayout::layoutMeasureNumberBase(const MeasureNumberBase* item, MeasureNumb
// x1 - left measure position of free space
// x2 - right measure position of free space

const Measure* mea = item->measure();
const Measure* measure = item->measure();

// find first chordrest
Segment* chordRest = mea->first(SegmentType::ChordRest);
const Segment* crSeg = measure->first(SegmentType::ChordRest);

Segment* s1 = chordRest->prevActive();
// unfortunately, using !s1->header() does not work
while (s1 && (s1->isChordRestType()
|| s1->isBreathType()
|| s1->isClefType()
|| s1->isBarLineType()
|| !s1->element(item->staffIdx() * VOICES))) {
s1 = s1->prevActive();
}

Segment* s2 = chordRest->next();
// unfortunately, using !s1->trailer() does not work
while (s2 && (s2->isChordRestType()
|| s2->isBreathType()
|| s2->isClefType()
|| s2->isBarLineType()
|| !s2->element(item->staffIdx() * VOICES))) {
s2 = s2->nextActive();
}

// if s1/s2 does not exist, it means there is no header/trailer segment. Align with start/end of measure.
double x1 = s1 ? s1->x() + s1->minRight() : 0;
double x2 = s2 ? s2->x() - s2->minLeft() : mea->ldata()->bbox().width();
const MeasureLayout::MeasureStartEndPos measureStartEnd = MeasureLayout::getMeasureStartEndPos(measure, crSeg,
item->staffIdx(), true, false, ctx);
const double x1 = measureStartEnd.x1;
const double x2 = measureStartEnd.x2;

ldata->setPosX((x1 + x2) * 0.5);
} else if (item->hPlacement() == PlacementH::RIGHT) {
Expand Down Expand Up @@ -4272,12 +4254,12 @@ void TLayout::layoutMMRest(const MMRest* item, MMRest::LayoutData* ldata, const
ChordLayout::fillShape(item, ldata, ctx.conf());
}

void TLayout::layoutMMRestRange(const MMRestRange* item, MMRestRange::LayoutData* ldata)
void TLayout::layoutMMRestRange(const MMRestRange* item, MMRestRange::LayoutData* ldata, const LayoutContext& ctx)
{
LAYOUT_CALL_ITEM(item);
LD_CONDITION(item->measure()->ldata()->isSetBbox()); // layoutMeasureNumberBase

layoutMeasureNumberBase(item, ldata);
layoutMeasureNumberBase(item, ldata, ctx);
}

void TLayout::layoutNote(const Note* item, Note::LayoutData* ldata)
Expand Down
6 changes: 3 additions & 3 deletions src/engraving/rendering/dev/tlayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ class TLayout
static void layoutMarker(const Marker* item, Marker::LayoutData* ldata);
static void layoutMeasureBase(MeasureBase* item, LayoutContext& ctx); // factory
static void layoutBaseMeasureBase(const MeasureBase* item, MeasureBase::LayoutData* ldata, const LayoutContext& ctx); // base class
static void layoutMeasureNumber(const MeasureNumber* item, MeasureNumber::LayoutData* ldata);
static void layoutMeasureNumberBase(const MeasureNumberBase* item, MeasureNumberBase::LayoutData* ldata); // base class
static void layoutMeasureNumber(const MeasureNumber* item, MeasureNumber::LayoutData* ldata, const LayoutContext& ctx);
static void layoutMeasureNumberBase(const MeasureNumberBase* item, MeasureNumberBase::LayoutData* ldata, const LayoutContext& ctx); // base class
static void layoutMeasureRepeat(const MeasureRepeat* item, MeasureRepeat::LayoutData* ldata, const LayoutContext& ctx);
static void layoutMMRest(const MMRest* item, MMRest::LayoutData* ldata, const LayoutContext& ctx);
static void layoutMMRestRange(const MMRestRange* item, MMRestRange::LayoutData* ldata);
static void layoutMMRestRange(const MMRestRange* item, MMRestRange::LayoutData* ldata, const LayoutContext& ctx);

static void layoutNote(const Note* item, Note::LayoutData* ldata);
static void fillNoteShape(const Note* item, Note::LayoutData* ldata);
Expand Down
2 changes: 2 additions & 0 deletions src/engraving/style/styledef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ const std::array<StyleDef::StyleValue, size_t(Sid::STYLES)> StyleDef::styleValue
styleDef(measureNumberOffsetType, int(OffsetType::SPATIUM)),
styleDef(measureNumberVPlacement, PlacementV::ABOVE),
styleDef(measureNumberHPlacement, PlacementH::LEFT),
styleDef(measureNumberMinDistance, Spatium(0.5)),
styleDef(measureNumberAlign, Align(AlignH::LEFT, AlignV::BASELINE)),
styleDef(measureNumberFrameType, int(FrameType::NO_FRAME)),
styleDef(measureNumberFramePadding, 0.2),
Expand Down Expand Up @@ -1074,6 +1075,7 @@ const std::array<StyleDef::StyleValue, size_t(Sid::STYLES)> StyleDef::styleValue
styleDef(mmRestRangeFrameRound, 0),
styleDef(mmRestRangeFrameFgColor, PropertyValue::fromValue(Color::BLACK)),
styleDef(mmRestRangeFrameBgColor, PropertyValue::fromValue(Color::transparent)),
styleDef(mmRestRangeMinDistance, Spatium(0.5)),

styleDef(translatorFontFace, "Edwin"),
styleDef(translatorFontSize, 10.0),
Expand Down
2 changes: 2 additions & 0 deletions src/engraving/style/styledef.h
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ enum class Sid {
measureNumberOffsetType,
measureNumberVPlacement,
measureNumberHPlacement,
measureNumberMinDistance,
measureNumberAlign,
measureNumberFrameType,
measureNumberFramePadding,
Expand Down Expand Up @@ -1086,6 +1087,7 @@ enum class Sid {
mmRestRangeFrameRound,
mmRestRangeFrameFgColor,
mmRestRangeFrameBgColor,
mmRestRangeMinDistance,

translatorFontFace,
translatorFontSize,
Expand Down
Binary file added vtest/scores/measure-number.mscz
Binary file not shown.
Binary file added vtest/scores/mmrest-19.mscz
Binary file not shown.

0 comments on commit 73cc000

Please sign in to comment.