2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2011--2012 Mike Solomon <mike@mikesolomon.org>
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 (octavate_eight);
38 DECLARE_ACKNOWLEDGER (key_signature);
39 DECLARE_ACKNOWLEDGER (time_signature);
40 DECLARE_ACKNOWLEDGER (beam);
41 DECLARE_ACKNOWLEDGER (flag);
43 virtual void finalize ();
46 bool covered_grob_has_interface (Grob *covered_grob, Grob *beam);
49 TRANSLATOR_DECLARATIONS (Beam_collision_engraver);
52 Beam_collision_engraver::Beam_collision_engraver () {}
55 Beam_collision_engraver::covered_grob_has_interface (Grob *covered_grob, Grob *beam)
57 SCM interfaces = beam->get_property ("collision-interfaces");
59 for (SCM l = interfaces; scm_is_pair (l); l = scm_cdr (l))
61 if (covered_grob->internal_has_interface (scm_car (l)))
69 Beam_collision_engraver::finalize ()
71 if (!covered_grobs_.size ())
74 vector_sort (covered_grobs_, Grob_info::less);
75 vector_sort (beams_, Grob_info::less);
78 for (vsize i = 0; i < beams_.size (); i++)
80 Grob *beam_grob = beams_[i].grob ();
82 extract_grob_set (beam_grob, "normal-stems", stems);
83 Interval_t<int> vertical_span;
84 for (vsize j = 0; j < stems.size (); j++)
86 int vag = Grob::get_vertical_axis_group_index (stems[j]);
88 vertical_span.add_point (vag);
90 Context *beam_context = beams_[i].context ();
92 Interval_t<int> beam_spanned_rank_ = beam_grob->spanned_rank_interval ();
93 // Start considering grobs at the first grob whose end falls at or after the beam's beginning.
94 while (covered_grobs_[start].grob ()->spanned_rank_interval ()[RIGHT] < beam_spanned_rank_[LEFT])
97 // Stop when the grob's beginning comes after the beam's end.
98 for (vsize j = start; j < covered_grobs_.size (); j++)
100 Grob *covered_grob = covered_grobs_[j].grob ();
101 int vag = Grob::get_vertical_axis_group_index (covered_grob);
102 if (!vertical_span.contains (vag))
104 Context *covered_grob_context = covered_grobs_[j].context ();
106 Interval_t<int> covered_grob_spanned_rank = covered_grob->spanned_rank_interval ();
107 if (covered_grob_spanned_rank[LEFT] > beam_spanned_rank_[RIGHT])
110 Only consider grobs whose end falls at or after the beam's beginning.
111 If the grob is a beam, it cannot start before beams_[i].
112 Also, if the user wants to check for collisions only in the beam's voice,
113 then make sure the beam and the covered_grob are in the same voice.
115 if ((covered_grob_spanned_rank[RIGHT] >= beam_spanned_rank_[LEFT])
116 && !(to_boolean (beam_grob->get_property ("collision-voice-only"))
117 && (covered_grob_context != beam_context))
118 && !(Beam::has_interface (covered_grob)
119 && (covered_grob_spanned_rank[LEFT] <= beam_spanned_rank_[LEFT]))
120 && covered_grob_has_interface (covered_grob, beam_grob))
122 // Do not consider note heads attached to the beam.
123 if (Stem::has_interface (covered_grob))
124 if (unsmob_grob (covered_grob->get_object ("beam")))
127 if (Grob *stem = unsmob_grob (covered_grob->get_object ("stem")))
128 if (Grob *beam = unsmob_grob (stem->get_object ("beam")))
129 if (beam == beam_grob)
132 Pointer_group_interface::add_grob (beam_grob, ly_symbol2scm ("covered-grobs"), covered_grob);
139 Beam_collision_engraver::acknowledge_note_head (Grob_info i)
141 covered_grobs_.push_back (i);
145 Beam_collision_engraver::acknowledge_stem (Grob_info i)
147 covered_grobs_.push_back (i);
151 Beam_collision_engraver::acknowledge_accidental (Grob_info i)
153 if (i.grob ()->internal_has_interface (ly_symbol2scm ("inline-accidental-interface")))
154 covered_grobs_.push_back (i);
158 Beam_collision_engraver::acknowledge_clef (Grob_info i)
160 covered_grobs_.push_back (i);
164 Beam_collision_engraver::acknowledge_key_signature (Grob_info i)
166 covered_grobs_.push_back (i);
170 Beam_collision_engraver::acknowledge_octavate_eight (Grob_info i)
172 covered_grobs_.push_back (i);
176 Beam_collision_engraver::acknowledge_time_signature (Grob_info i)
178 covered_grobs_.push_back (i);
182 Beam_collision_engraver::acknowledge_flag (Grob_info i)
184 covered_grobs_.push_back (i);
188 Beam_collision_engraver::acknowledge_beam (Grob_info i)
190 beams_.push_back (i);
191 covered_grobs_.push_back (i);
194 #include "translator.icc"
196 ADD_ACKNOWLEDGER (Beam_collision_engraver, note_head);
197 ADD_ACKNOWLEDGER (Beam_collision_engraver, stem);
198 ADD_ACKNOWLEDGER (Beam_collision_engraver, accidental);
199 ADD_ACKNOWLEDGER (Beam_collision_engraver, clef);
200 ADD_ACKNOWLEDGER (Beam_collision_engraver, key_signature);
201 ADD_ACKNOWLEDGER (Beam_collision_engraver, time_signature);
202 ADD_ACKNOWLEDGER (Beam_collision_engraver, octavate_eight);
203 ADD_ACKNOWLEDGER (Beam_collision_engraver, flag);
204 ADD_ACKNOWLEDGER (Beam_collision_engraver, beam);
206 ADD_TRANSLATOR (Beam_collision_engraver,
208 "Help beams avoid colliding with notes and clefs in other voices.",