Skip to content

Commit

Permalink
Add LedgerLine::Dash class
Browse files Browse the repository at this point in the history
  • Loading branch information
lpugin committed Nov 12, 2024
1 parent 911d63c commit 144f374
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
28 changes: 27 additions & 1 deletion include/vrv/staff.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ class LedgerLine {
*/
void AddDash(int left, int right, int extension);

class Dash {
public:
int m_x1;
int m_x2;
ListOfConstObjects m_events;

// Constructor
Dash(int x1, int x2, const Object *object)
{
m_x1 = x1;
m_x2 = x2;
m_events.push_back(object);
}

// Merge function to merge another Dash object into this one
void MergeWith(const Dash &other)
{
// Keep the first int from this Dash object, and the second int from the other
this->m_x1 = std::min(other.m_x1, this->m_x1);
this->m_x2 = std::max(other.m_x2, this->m_x2);
// Append the list from other to this
this->m_events.insert(this->m_events.end(), other.m_events.begin(), other.m_events.end());
}
};

protected:
//
private:
Expand All @@ -53,7 +78,8 @@ class LedgerLine {
/**
* A list of dashes relative to the staff position.
*/
std::list<std::pair<int, int>> m_dashes;
// std::list<std::pair<int, int>> m_dashes;
std::list<Dash> m_dashes;

protected:
//
Expand Down
21 changes: 10 additions & 11 deletions src/calcledgerlinesfunctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,14 @@ void CalcLedgerLinesFunctor::AdjustLedgerLines(
// For each dash on the inner line (both cue and normal) we construct an adjustment with zero delta
// and sort them
std::vector<Adjustment> adjustments;
using DashType = std::pair<int, int>;
if (!lines.empty()) {
for (const DashType &dash : lines.at(0).m_dashes) {
adjustments.push_back({ dash.first, dash.second, false, 0 });
for (const LedgerLine::Dash &dash : lines.at(0).m_dashes) {
adjustments.push_back({ dash.m_x1, dash.m_x2, false, 0 });
}
}
if (!cueLines.empty()) {
for (const DashType &dash : cueLines.at(0).m_dashes) {
adjustments.push_back({ dash.first, dash.second, true, 0 });
for (const LedgerLine::Dash &dash : cueLines.at(0).m_dashes) {
adjustments.push_back({ dash.m_x1, dash.m_x2, true, 0 });
}
}

Expand Down Expand Up @@ -175,13 +174,13 @@ void CalcLedgerLinesFunctor::AdjustLedgerLines(
if (adjustment.delta > 0) {
ArrayOfLedgerLines &linesToAdjust = adjustment.isCue ? cueLines : lines;
for (LedgerLine &line : linesToAdjust) {
std::list<DashType>::iterator iterDash
= std::find_if(line.m_dashes.begin(), line.m_dashes.end(), [&adjustment](const DashType &dash) {
return ((dash.first >= adjustment.left) && (dash.second <= adjustment.right));
});
std::list<LedgerLine::Dash>::iterator iterDash = std::find_if(
line.m_dashes.begin(), line.m_dashes.end(), [&adjustment](const LedgerLine::Dash &dash) {
return ((dash.m_x1 >= adjustment.left) && (dash.m_x2 <= adjustment.right));
});
if (iterDash != line.m_dashes.end()) {
iterDash->first += adjustment.delta;
iterDash->second -= adjustment.delta;
iterDash->m_x1 += adjustment.delta;
iterDash->m_x2 -= adjustment.delta;
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/staff.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,23 +302,24 @@ void LedgerLine::AddDash(int left, int right, int extension)
{
assert(left < right);

std::list<std::pair<int, int>>::iterator iter;
std::list<LedgerLine::Dash>::iterator iter;

// First add the dash
for (iter = m_dashes.begin(); iter != m_dashes.end(); ++iter) {
if (iter->first > left) break;
if (iter->m_x1 > left) break;
}
m_dashes.insert(iter, { left, right });
m_dashes.insert(iter, LedgerLine::Dash(left, right, NULL));

// Merge dashes which overlap by more than 1.5 extensions
// => Dashes belonging to the same chord overlap at least by two extensions and will get merged
// => Overlapping dashes of adjacent notes will not get merged
std::list<std::pair<int, int>>::iterator previous = m_dashes.begin();
std::list<LedgerLine::Dash>::iterator previous = m_dashes.begin();
iter = m_dashes.begin();
++iter;
while (iter != m_dashes.end()) {
if (previous->second > iter->first + 1.5 * extension) {
previous->second = std::max(iter->second, previous->second);
if (previous->m_x1 > iter->m_x1 + 1.5 * extension) {
previous->MergeWith(*iter);
previous->m_x2 = std::max(iter->m_x2, previous->m_x2);
iter = m_dashes.erase(iter);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions src/view_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1376,8 +1376,8 @@ void View::DrawLedgerLines(DeviceContext *dc, Staff *staff, const ArrayOfLedgerL
dc->SetBrush(m_currentColor, AxSOLID);

for (const LedgerLine &line : lines) {
for (const std::pair<int, int> &dash : line.m_dashes) {
dc->DrawLine(ToDeviceContextX(x + dash.first), ToDeviceContextY(y), ToDeviceContextX(x + dash.second),
for (const LedgerLine::Dash &dash : line.m_dashes) {
dc->DrawLine(ToDeviceContextX(x + dash.m_x1), ToDeviceContextY(y), ToDeviceContextX(x + dash.m_x2),
ToDeviceContextY(y));
}
y += ySpace;
Expand Down

0 comments on commit 144f374

Please sign in to comment.