forked from rism-digital/verovio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adjustaccidxfunctor.cpp
183 lines (151 loc) · 7.11 KB
/
adjustaccidxfunctor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/////////////////////////////////////////////////////////////////////////////
// Name: adjustaccidxfunctor.cpp
// Author: David Bauer
// Created: 2023
// Copyright (c) Authors and others. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include "adjustaccidxfunctor.h"
//----------------------------------------------------------------------------
#include "doc.h"
//----------------------------------------------------------------------------
namespace vrv {
//----------------------------------------------------------------------------
// AdjustAccidXFunctor
//----------------------------------------------------------------------------
AdjustAccidXFunctor::AdjustAccidXFunctor(Doc *doc) : DocFunctor(doc)
{
m_currentMeasure = NULL;
}
FunctorCode AdjustAccidXFunctor::VisitAlignment(Alignment *alignment)
{
for (const auto &graceAlignerPair : alignment->GetGraceAligners()) {
graceAlignerPair.second->Process(*this);
}
return FUNCTOR_CONTINUE;
}
FunctorCode AdjustAccidXFunctor::VisitAlignmentReference(AlignmentReference *alignmentReference)
{
m_adjustedAccids.clear();
std::vector<Accid *> accids = this->GetAccidentalsForAdjustment(alignmentReference);
if (accids.empty()) return FUNCTOR_SIBLINGS;
assert(m_doc);
StaffDef *staffDef = m_doc->GetCurrentScoreDef()->GetStaffDef(alignmentReference->GetN());
int staffSize = (staffDef && staffDef->HasScale()) ? staffDef->GetScale() : 100;
std::sort(accids.begin(), accids.end(), AccidSpaceSort());
// process accid layer alignment
for (Accid *accid : accids) {
this->SetAccidLayerAlignment(accid, alignmentReference);
}
// Detect accids which are an octave apart => they will be grouped together in the multiset
std::multiset<Accid *, AccidOctaveSort> octaveEquivalence;
std::copy(accids.begin(), accids.end(), std::inserter(octaveEquivalence, octaveEquivalence.begin()));
// Align the octaves
for (Accid *accid : accids) {
// Skip any accid that was already adjusted
if (m_adjustedAccids.count(accid) > 0) continue;
auto range = octaveEquivalence.equal_range(accid);
// Handle at least two octave accids without unisons
int octaveAccidCount = 0;
std::set<data_OCTAVE> octaves;
for (auto octaveIter = range.first; octaveIter != range.second; ++octaveIter) {
Note *note = vrv_cast<Note *>((*octaveIter)->GetFirstAncestor(NOTE));
octaves.insert(note->GetOct());
++octaveAccidCount;
}
if ((octaveAccidCount < 2) || ((int)octaves.size() < octaveAccidCount)) continue;
// Now adjust the octave accids and store the left most position
int minDrawingX = -VRV_UNSET;
for (auto octaveIter = range.first; octaveIter != range.second; ++octaveIter) {
this->AdjustAccidWithSpace(*octaveIter, alignmentReference, staffSize);
minDrawingX = std::min(minDrawingX, (*octaveIter)->GetDrawingX());
}
// Finally, align the accidentals whenever the adjustment is not too large
for (auto octaveIter = range.first; octaveIter != range.second; ++octaveIter) {
const int dist = (*octaveIter)->GetDrawingX() - minDrawingX;
if ((dist > 0) && (*octaveIter)->HasContentHorizontalBB()) {
const int accidWidth = (*octaveIter)->GetContentRight() - (*octaveIter)->GetContentLeft();
if (dist < accidWidth / 2) {
(*octaveIter)->SetDrawingXRel((*octaveIter)->GetDrawingXRel() - dist);
}
}
}
}
// Align accidentals for unison notes if any of them are present
for (Accid *accid : accids) {
if (accid->GetDrawingUnisonAccid() == NULL) continue;
accid->SetDrawingXRel(accid->GetDrawingUnisonAccid()->GetDrawingXRel());
}
const int count = (int)accids.size();
const int middle = (count / 2) + (count % 2);
// Zig-zag processing
for (int i = 0, j = count - 1; i < middle; ++i, --j) {
// top one - but skip if already adjusted (i.e. octaves)
if (m_adjustedAccids.count(accids.at(i)) == 0) {
this->AdjustAccidWithSpace(accids.at(i), alignmentReference, staffSize);
}
// Break with odd number of elements once the middle is reached
if (i == j) break;
// bottom one - but skip if already adjusted
if (m_adjustedAccids.count(accids.at(j)) == 0) {
this->AdjustAccidWithSpace(accids.at(j), alignmentReference, staffSize);
}
}
return FUNCTOR_SIBLINGS;
}
FunctorCode AdjustAccidXFunctor::VisitMeasure(Measure *measure)
{
m_currentMeasure = measure;
measure->m_measureAligner.Process(*this);
return FUNCTOR_CONTINUE;
}
std::vector<Accid *> AdjustAccidXFunctor::GetAccidentalsForAdjustment(AlignmentReference *alignmentReference) const
{
std::vector<Accid *> accidentals;
for (Object *child : alignmentReference->GetChildren()) {
if (child->Is(ACCID)) {
Accid *accid = vrv_cast<Accid *>(child);
if (accid->HasAccid()) accidentals.push_back(accid);
}
}
return accidentals;
}
void AdjustAccidXFunctor::SetAccidLayerAlignment(Accid *accid, const AlignmentReference *alignmentReference) const
{
if (accid->IsAlignedWithSameLayer()) return;
const ArrayOfConstObjects &children = alignmentReference->GetChildren();
Note *parentNote = vrv_cast<Note *>(accid->GetFirstAncestor(NOTE));
const bool hasUnisonOverlap = std::any_of(children.begin(), children.end(), [parentNote](const Object *object) {
if (!object->Is(NOTE)) return false;
const Note *otherNote = vrv_cast<const Note *>(object);
// in case notes are in unison but have different accidentals
return parentNote && parentNote->IsUnisonWith(otherNote, true) && !parentNote->IsUnisonWith(otherNote, false);
});
if (!hasUnisonOverlap) return;
Chord *chord = parentNote->IsChordTone();
// no chord, so align only parent note
if (!chord) {
accid->IsAlignedWithSameLayer(true);
return;
}
// we have chord ancestor, so need to align all of its accidentals
ListOfObjects accidentals = chord->FindAllDescendantsByType(ACCID);
std::for_each(accidentals.begin(), accidentals.end(), [](Object *object) {
Accid *accid = vrv_cast<Accid *>(object);
accid->IsAlignedWithSameLayer(true);
});
}
void AdjustAccidXFunctor::AdjustAccidWithSpace(Accid *accid, AlignmentReference *alignmentReference, int staffSize)
{
std::vector<Accid *> leftAccids;
const ArrayOfObjects &children = alignmentReference->GetChildren();
// bottom one
for (Object *child : children) {
// if accidental has unison overlap, ignore elements on other layers for overlap
if (accid->IsAlignedWithSameLayer() && (accid->GetFirstAncestor(LAYER) != child->GetFirstAncestor(LAYER)))
continue;
accid->AdjustX(dynamic_cast<LayerElement *>(child), m_doc, staffSize, leftAccids, m_adjustedAccids);
}
// Mark as adjusted (even if position was not altered)
m_adjustedAccids.insert(accid);
}
} // namespace vrv