2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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/>.
20 #include "engraver.hh"
22 #include "note-spacing.hh"
23 #include "paper-column.hh"
24 #include "pointer-group-interface.hh"
27 #include "staff-spacing.hh"
28 #include "stream-event.hh"
30 #include "translator.icc"
40 Rhythmic_tuple (Grob_info i, Moment m)
45 static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
49 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
51 return Rhythmic_tuple::time_compare (a, b);
55 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
56 Rhythmic_tuple const &h2)
58 return (h1.end_ - h2.end_).main_part_.sign ();
61 /****************************************************************/
64 Acknowledge rhythmic elements, for initializing spacing fields in
67 class Spacing_engraver : public Engraver
69 PQueue<Rhythmic_tuple> playing_durations_;
70 vector<Rhythmic_tuple> now_durations_;
71 vector<Rhythmic_tuple> stopped_durations_;
74 Stream_event *start_section_;
76 TRANSLATOR_DECLARATIONS (Spacing_engraver);
79 void acknowledge_staff_spacing (Grob_info);
80 void acknowledge_note_spacing (Grob_info);
81 void acknowledge_rhythmic_head (Grob_info);
82 void acknowledge_rhythmic_grob (Grob_info);
83 void listen_spacing_section (Stream_event *);
85 void start_translation_timestep ();
86 void stop_translation_timestep ();
87 void process_music ();
88 void add_starter_duration (Grob_info i);
90 virtual void finalize ();
92 void start_spanner ();
96 Spacing_engraver::Spacing_engraver ()
103 Spacing_engraver::listen_spacing_section (Stream_event *ev)
105 ASSIGN_EVENT_ONCE (start_section_, ev);
109 Spacing_engraver::process_music ()
111 if (start_section_ && spacing_)
119 Spacing_engraver::start_spanner ()
123 spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
124 spacing_->set_bound (LEFT,
125 unsmob<Grob> (get_property ("currentCommandColumn")));
129 Spacing_engraver::finalize ()
135 Spacing_engraver::stop_spanner ()
139 Grob *p = unsmob<Grob> (get_property ("currentCommandColumn"));
141 spacing_->set_bound (RIGHT, p);
147 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
149 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
153 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
155 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
159 Spacing_engraver::acknowledge_rhythmic_grob (Grob_info i)
161 add_starter_duration (i);
165 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
167 add_starter_duration (i);
171 Spacing_engraver::add_starter_duration (Grob_info i)
173 if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
174 || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
178 only pay attention to durations that are not grace notes.
180 if (!now_.grace_part_)
182 Stream_event *r = i.event_cause ();
183 if (r && r->in_event_class ("rhythmic-event"))
185 Moment len = get_event_length (r, now_);
186 Rhythmic_tuple t (i, now_mom () + len);
187 now_durations_.push_back (t);
193 Spacing_engraver::stop_translation_timestep ()
195 Paper_column *musical_column
196 = unsmob<Paper_column> (get_property ("currentMusicalColumn"));
201 musical_column->set_object ("spacing", spacing_->self_scm ());
202 unsmob<Grob> (get_property ("currentCommandColumn"))
203 ->set_object ("spacing", spacing_->self_scm ());
205 SCM proportional = get_property ("proportionalNotationDuration");
206 if (unsmob<Moment> (proportional))
208 musical_column->set_property ("shortest-playing-duration", proportional);
209 musical_column->set_property ("shortest-starter-duration", proportional);
210 musical_column->set_property ("used", SCM_BOOL_T);
214 Moment shortest_playing;
215 shortest_playing.set_infinite (1);
216 for (vsize i = 0; i < playing_durations_.size (); i++)
218 Stream_event *ev = playing_durations_[i].info_.event_cause ();
221 Moment m = get_event_length (ev);
222 shortest_playing = min (shortest_playing, m);
226 starter.set_infinite (1);
228 for (vsize i = 0; i < now_durations_.size (); i++)
230 Moment m = get_event_length (now_durations_[i].info_.event_cause ());
233 starter = min (starter, m);
234 playing_durations_.insert (now_durations_[i]);
237 now_durations_.clear ();
239 shortest_playing = min (shortest_playing, starter);
241 assert (starter.to_bool ());
242 SCM sh = shortest_playing.smobbed_copy ();
243 SCM st = starter.smobbed_copy ();
245 musical_column->set_property ("shortest-playing-duration", sh);
246 musical_column->set_property ("shortest-starter-duration", st);
250 Spacing_engraver::start_translation_timestep ()
255 stopped_durations_.clear ();
257 while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
258 playing_durations_.delmin ();
259 while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
260 stopped_durations_.push_back (playing_durations_.get ());
265 Spacing_engraver::boot ()
267 ADD_LISTENER (Spacing_engraver, spacing_section);
268 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
269 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
270 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
271 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_grob);
274 ADD_TRANSLATOR (Spacing_engraver,
276 "Make a @code{SpacingSpanner} and do bookkeeping of shortest"
277 " starting and playing notes.",
283 "currentMusicalColumn "
284 "currentCommandColumn "
285 "proportionalNotationDuration ",