Skip to content

Commit

Permalink
Enable support for different scoreDef order
Browse files Browse the repository at this point in the history
  • Loading branch information
lpugin committed Dec 13, 2023
1 parent 0d09076 commit f1871ca
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
12 changes: 12 additions & 0 deletions include/vrv/verticalaligner.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class SystemAligner : public Object {
SystemAligner();
virtual ~SystemAligner();

/**
* Override the method of adding AlignmentReference children
*/
bool IsSupportedChild(Object *object) override;

/**
* Do not copy children for HorizontalAligner
*/
Expand Down Expand Up @@ -67,6 +72,13 @@ class SystemAligner : public Object {
*/
StaffAlignment *GetStaffAlignment(int idx, Staff *staff, const Doc *doc);

/**
* Reorder the staff alignment as given in the staffNs.
* Reordering will fail if the number of staffAlignment does not correspond, or
* if some staffAlignment pointers are missing for a corresponding staffN
*/
void ReorderBy(const std::vector<int> &staffNs);

/**
* Get the StaffAlignment for the staffN.
* Return NULL if not found.
Expand Down
4 changes: 4 additions & 0 deletions src/alignfunctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,10 @@ FunctorCode AlignVerticallyFunctor::VisitSystemEnd(System *system)
m_cumulatedShift = 0;
m_staffIdx = 0;

// StaffAlignment are added following the staff element in the measures
// We can now reorder them according to the scoreDef order
system->m_systemAligner.ReorderBy(system->GetDrawingScoreDef()->GetStaffNs());

system->m_systemAligner.Process(*this);

return FUNCTOR_SIBLINGS;
Expand Down
41 changes: 37 additions & 4 deletions src/verticalaligner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,14 @@ void SystemAligner::Reset()
ArrayOfObjects &children = this->GetChildrenForModification();
m_bottomAlignment = new StaffAlignment();
m_bottomAlignment->SetStaff(NULL, NULL, this->GetAboveSpacingType(NULL));
m_bottomAlignment->SetParent(this);
m_bottomAlignment->SetParentSystem(this->GetSystem());
children.push_back(m_bottomAlignment);
this->AddChild(m_bottomAlignment);
}

bool SystemAligner::IsSupportedChild(Object *child)
{
assert(dynamic_cast<StaffAlignment *>(child));
return true;
}

StaffAlignment *SystemAligner::GetStaffAlignment(int idx, Staff *staff, const Doc *doc)
Expand All @@ -77,16 +82,44 @@ StaffAlignment *SystemAligner::GetStaffAlignment(int idx, Staff *staff, const Do
// We create the StaffAlignment
StaffAlignment *alignment = new StaffAlignment();
alignment->SetStaff(staff, doc, this->GetAboveSpacingType(staff));
alignment->SetParent(this);
alignment->SetParentSystem(this->GetSystem());
children.push_back(alignment);
this->AddChild(alignment);

// put back the bottomAlignment
children.push_back(m_bottomAlignment);

return alignment;
}

void SystemAligner::ReorderBy(const std::vector<int> &staffNs)
{
std::vector<int> order = staffNs;
// First check that staffNs are unique
std::sort(order.begin(), order.end());
order.erase(std::unique(order.begin(), order.end()), order.end());
// If not, we should return because the re-ordering below will corrupt the data
if (order.size() != staffNs.size()) return;

ArrayOfObjects &children = this->GetChildrenForModification();

// Since we have a bottom alignment, the number is +1
if (children.size() != staffNs.size() + 1) return;

ListOfObjects orderedAlignments;
for (auto staffN : staffNs) {
StaffAlignment *alignment = this->GetStaffAlignmentForStaffN(staffN);
if (!alignment) return;
// Something is wrong in the data, we keep the order as it is
orderedAlignments.push_back(alignment);
}
int i = 0;
// We know that
for (auto alignment : orderedAlignments) {
children.at(i) = alignment;
++i;
}
}

StaffAlignment *SystemAligner::GetStaffAlignmentForStaffN(int staffN)
{
return const_cast<StaffAlignment *>(std::as_const(*this).GetStaffAlignmentForStaffN(staffN));
Expand Down

0 comments on commit f1871ca

Please sign in to comment.