2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2011 Mike Solomon <mike@apollinemike.com>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "engraver.hh"
23 #include "note-head.hh"
24 #include "pointer-group-interface.hh"
27 class Beam_collision_engraver : public Engraver
30 vector<Grob_info> beams_;
31 vector<Grob_info> covered_grobs_;
33 DECLARE_ACKNOWLEDGER (note_head);
34 DECLARE_ACKNOWLEDGER (stem);
35 DECLARE_ACKNOWLEDGER (accidental);
36 DECLARE_ACKNOWLEDGER (clef);
37 DECLARE_ACKNOWLEDGER (key_signature);
38 DECLARE_ACKNOWLEDGER (time_signature);
39 DECLARE_ACKNOWLEDGER (beam);
41 virtual void finalize ();
44 bool covered_grob_has_interface (Grob *covered_grob, Grob *beam);
47 TRANSLATOR_DECLARATIONS (Beam_collision_engraver);
50 Beam_collision_engraver::Beam_collision_engraver () {}
53 Beam_collision_engraver::covered_grob_has_interface (Grob *covered_grob, Grob *beam)
55 SCM interfaces = beam->get_property ("collision-interfaces");
57 for (SCM l = interfaces; scm_is_pair (l); l = scm_cdr (l))
59 if (covered_grob->internal_has_interface (scm_car (l)))
67 Beam_collision_engraver::finalize ()
69 if (!covered_grobs_.size ())
72 vector_sort (covered_grobs_, Grob_info::less);
73 vector_sort (beams_, Grob_info::less);
76 for (vsize i = 0; i < beams_.size (); i++)
78 Grob *beam_grob = beams_[i].grob ();
80 extract_grob_set (beam_grob, "normal-stems", stems);
81 Interval_t<int> vertical_span;
82 for (vsize j = 0; j < stems.size (); j++)
84 int vag = Grob::get_vertical_axis_group_index (stems[j]);
86 vertical_span.add_point (vag);
88 Context *beam_context = beams_[i].context ();
90 Interval_t<int> beam_spanned_rank_ = beam_grob->spanned_rank_interval ();
91 // Start considering grobs at the first grob whose end falls at or after the beam's beginning.
92 while (covered_grobs_[start].grob ()->spanned_rank_interval ()[RIGHT] < beam_spanned_rank_[LEFT])
95 // Stop when the grob's beginning comes after the beam's end.
96 for (vsize j = start; j < covered_grobs_.size (); j++)
98 Grob *covered_grob = covered_grobs_[j].grob ();
99 int vag = Grob::get_vertical_axis_group_index (covered_grob);
100 if (!vertical_span.contains (vag))
102 Context *covered_grob_context = covered_grobs_[j].context ();
104 Interval_t<int> covered_grob_spanned_rank = covered_grob->spanned_rank_interval ();
105 if (covered_grob_spanned_rank[LEFT] > beam_spanned_rank_[RIGHT])
108 Only consider grobs whose end falls at or after the beam's beginning.
109 If the grob is a beam, it cannot start before beams_[i].
110 Also, if the user wants to check for collisions only in the beam's voice,
111 then make sure the beam and the covered_grob are in the same voice.
113 if ((covered_grob_spanned_rank[RIGHT] >= beam_spanned_rank_[LEFT])
114 && !(to_boolean (beam_grob->get_property ("collision-voice-only"))
115 && (covered_grob_context != beam_context))
116 && !(Beam::has_interface (covered_grob)
117 && (covered_grob_spanned_rank[LEFT] <= beam_spanned_rank_[LEFT]))
118 && covered_grob_has_interface (covered_grob, beam_grob))
120 // Do not consider note heads attached to the beam.
121 if (Stem::has_interface (covered_grob))
122 if (unsmob_grob (covered_grob->get_object ("beam")))
125 if (Grob *stem = unsmob_grob (covered_grob->get_object ("stem")))
126 if (Grob *beam = unsmob_grob (stem->get_object ("beam")))
127 if (beam == beam_grob)
130 Pointer_group_interface::add_grob (beam_grob, ly_symbol2scm ("covered-grobs"), covered_grob);
137 Beam_collision_engraver::acknowledge_note_head (Grob_info i)
139 covered_grobs_.push_back (i);
143 Beam_collision_engraver::acknowledge_stem (Grob_info i)
145 covered_grobs_.push_back (i);
149 Beam_collision_engraver::acknowledge_accidental (Grob_info i)
151 if (i.grob ()->internal_has_interface (ly_symbol2scm ("inline-accidental-interface")))
152 covered_grobs_.push_back (i);
156 Beam_collision_engraver::acknowledge_clef (Grob_info i)
158 covered_grobs_.push_back (i);
162 Beam_collision_engraver::acknowledge_key_signature (Grob_info i)
164 covered_grobs_.push_back (i);
168 Beam_collision_engraver::acknowledge_time_signature (Grob_info i)
170 covered_grobs_.push_back (i);
174 Beam_collision_engraver::acknowledge_beam (Grob_info i)
176 beams_.push_back (i);
177 covered_grobs_.push_back (i);
180 #include "translator.icc"
182 ADD_ACKNOWLEDGER (Beam_collision_engraver, note_head);
183 ADD_ACKNOWLEDGER (Beam_collision_engraver, stem);
184 ADD_ACKNOWLEDGER (Beam_collision_engraver, accidental);
185 ADD_ACKNOWLEDGER (Beam_collision_engraver, clef);
186 ADD_ACKNOWLEDGER (Beam_collision_engraver, key_signature);
187 ADD_ACKNOWLEDGER (Beam_collision_engraver, time_signature);
188 ADD_ACKNOWLEDGER (Beam_collision_engraver, beam);
190 ADD_TRANSLATOR (Beam_collision_engraver,
192 "Help beams avoid colliding with notes and clefs in other voices.",