]> git.donarmstrong.com Git - lilypond.git/blob - lily/beam-collision-engraver.cc
Adds Beam_collision_engraver to the Score context.
[lilypond.git] / lily / beam-collision-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2011 Mike Solomon <mike@apollinemike.com>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "beam.hh"
21 #include "engraver.hh"
22 #include "item.hh"
23 #include "note-head.hh"
24 #include "pointer-group-interface.hh"
25 #include "stem.hh"
26
27 class Beam_collision_engraver : public Engraver
28 {
29 protected:
30   vector<Grob_info> beams_;
31   vector<Grob_info> covered_grobs_;
32
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);
40
41   virtual void finalize ();
42
43 private:
44   bool covered_grob_has_interface (Grob *covered_grob, Grob *beam);
45
46 public:
47   TRANSLATOR_DECLARATIONS (Beam_collision_engraver);
48 };
49
50 Beam_collision_engraver::Beam_collision_engraver () {}
51
52 bool
53 Beam_collision_engraver::covered_grob_has_interface (Grob *covered_grob, Grob *beam)
54 {
55   SCM interfaces = beam->get_property ("collision-interfaces");
56
57   for (SCM l = interfaces; scm_is_pair (l); l = scm_cdr (l))
58     {
59       if (covered_grob->internal_has_interface (scm_car (l)))
60         return true;
61     }
62
63   return false;
64 }
65
66 void
67 Beam_collision_engraver::finalize ()
68 {
69   if (!covered_grobs_.size ())
70     return;
71
72   vector_sort (covered_grobs_, Grob_info::less);
73   vector_sort (beams_, Grob_info::less);
74   vsize start = 0;
75
76   for (vsize i = 0; i < beams_.size (); i++)
77     {
78       Grob *beam_grob = beams_[i].grob ();
79
80       extract_grob_set (beam_grob, "normal-stems", stems);
81       Interval_t<int> vertical_span;
82       for (vsize j = 0; j < stems.size (); j++)
83         {
84           int vag = Grob::get_vertical_axis_group_index (stems[j]);
85           if (vag >= 0)
86             vertical_span.add_point (vag);
87         }
88       Context *beam_context = beams_[i].context ();
89
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])
93         start++;
94
95       // Stop when the grob's beginning comes after the beam's end.
96       for (vsize j = start; j < covered_grobs_.size (); j++)
97         {
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))
101             continue;
102           Context *covered_grob_context = covered_grobs_[j].context ();
103
104           Interval_t<int> covered_grob_spanned_rank = covered_grob->spanned_rank_interval ();
105           if (covered_grob_spanned_rank[LEFT] > beam_spanned_rank_[RIGHT])
106             break;
107           /*
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.
112           */
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))
119             {
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")))
123                   continue;
124
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)
128                     continue;
129
130               Pointer_group_interface::add_grob (beam_grob, ly_symbol2scm ("covered-grobs"), covered_grob);
131             }
132         }
133     }
134 }
135
136 void
137 Beam_collision_engraver::acknowledge_note_head (Grob_info i)
138 {
139   covered_grobs_.push_back (i);
140 }
141
142 void
143 Beam_collision_engraver::acknowledge_stem (Grob_info i)
144 {
145   covered_grobs_.push_back (i);
146 }
147
148 void
149 Beam_collision_engraver::acknowledge_accidental (Grob_info i)
150 {
151   if (i.grob ()->internal_has_interface (ly_symbol2scm ("inline-accidental-interface")))
152     covered_grobs_.push_back (i);
153 }
154
155 void
156 Beam_collision_engraver::acknowledge_clef (Grob_info i)
157 {
158   covered_grobs_.push_back (i);
159 }
160
161 void
162 Beam_collision_engraver::acknowledge_key_signature (Grob_info i)
163 {
164   covered_grobs_.push_back (i);
165 }
166
167 void
168 Beam_collision_engraver::acknowledge_time_signature (Grob_info i)
169 {
170   covered_grobs_.push_back (i);
171 }
172
173 void
174 Beam_collision_engraver::acknowledge_beam (Grob_info i)
175 {
176   beams_.push_back (i);
177   covered_grobs_.push_back (i);
178 }
179
180 #include "translator.icc"
181
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);
189
190 ADD_TRANSLATOR (Beam_collision_engraver,
191                 /* doc */
192                 "Help beams avoid colliding with notes and clefs in other voices.",
193
194                 /* create */
195                 "",
196
197                 /* read */
198                 "",
199
200                 /* write */
201                 ""
202                );