Skip to content

Commit

Permalink
Round overlap to half unit multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
brdvd committed Oct 21, 2024
1 parent 0973df6 commit 0b93ad0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/vrv/adjustbeamsfunctor.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class AdjustBeamsFunctor : public DocFunctor {
*/
int CalcLayerOverlap(const LayerElement *beamElement) const;

// Rounds the overlap to the closest multiple of a half unit
int AdjustOverlapToHalfUnit(int overlap, int unit) const;

public:
//
private:
Expand Down
27 changes: 17 additions & 10 deletions src/adjustbeamsfunctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,13 @@ FunctorCode AdjustBeamsFunctor::VisitClef(Clef *clef)
// calculate margins for the clef
const int leftMargin = m_directionBias * (currentBeamYLeft - clefBounds) - beams * beamWidth;
const int rightMargin = m_directionBias * (currentBeamYRight - clefBounds) - beams * beamWidth;
const int overlapMargin = std::min(leftMargin, rightMargin);
int overlapMargin = std::min(leftMargin, rightMargin);
if (overlapMargin >= 0) return FUNCTOR_CONTINUE;
// calculate offset required for the beam
overlapMargin *= -m_directionBias;
const int unit = m_doc->GetDrawingUnit(staff->m_drawingStaffSize);
const int unitChangeNumber = ((std::abs(overlapMargin) + unit / 6) / unit);
if (unitChangeNumber > 0) {
const int adjust = unitChangeNumber * unit * m_directionBias;
if (std::abs(adjust) > std::abs(m_overlapMargin)) m_overlapMargin = adjust;
}
const int adjust = this->AdjustOverlapToHalfUnit(overlapMargin, unit);
if (std::abs(adjust) > std::abs(m_overlapMargin)) m_overlapMargin = adjust;

return FUNCTOR_CONTINUE;
}
Expand Down Expand Up @@ -301,7 +299,7 @@ FunctorCode AdjustBeamsFunctor::VisitRest(Rest *rest)
// Calculate possible overlap for the rest with beams
const int beams = m_outerBeam->GetBeamPartDuration(rest, false) - DURATION_4;
const int beamWidth = m_outerBeam->m_beamWidth;
const int overlapMargin = rest->Intersects(m_outerBeam, SELF, beams * beamWidth, true) * m_directionBias;
int overlapMargin = rest->Intersects(m_outerBeam, SELF, beams * beamWidth, true) * m_directionBias;

// Adjust drawing location for the rest based on the overlap with beams.
// Adjustment should be an even number, so that the rest is positioned properly
Expand Down Expand Up @@ -335,9 +333,9 @@ FunctorCode AdjustBeamsFunctor::VisitRest(Rest *rest)
}
}

overlapMargin *= -m_directionBias;
const int unit = m_doc->GetDrawingUnit(staff->m_drawingStaffSize);
const int unitChangeNumber = std::abs(overlapMargin) / unit + 1;
const int adjust = unitChangeNumber * unit * m_directionBias;
const int adjust = this->AdjustOverlapToHalfUnit(overlapMargin, unit);
if (std::abs(adjust) > std::abs(m_overlapMargin)) m_overlapMargin = adjust;

return FUNCTOR_CONTINUE;
Expand Down Expand Up @@ -426,7 +424,16 @@ int AdjustBeamsFunctor::CalcLayerOverlap(const LayerElement *beamElement) const
else if (*minOverlap < 0) {
overlap = (*minOverlap - unit) * m_directionBias;
}
return overlap;
const int adjust = this->AdjustOverlapToHalfUnit(overlap, unit);
return adjust;
}

int AdjustBeamsFunctor::AdjustOverlapToHalfUnit(int overlap, int unit) const
{
const int overlapSign = (overlap >= 0) ? 1 : -1;
const int halfUnit = unit / 2;
const int halfUnitChangeNumber = (std::abs(overlap) + halfUnit / 2) / halfUnit;
return halfUnitChangeNumber * halfUnit * overlapSign;
}

} // namespace vrv

0 comments on commit 0b93ad0

Please sign in to comment.