2 spacing-engraver.cc -- implement Spacing_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "note-spacing.hh"
11 #include "paper-column.hh"
12 #include "pointer-group-interface.hh"
15 #include "staff-spacing.hh"
16 #include "stream-event.hh"
18 #include "translator.icc"
28 Rhythmic_tuple (Grob_info i, Moment m)
33 static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
37 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
39 return Rhythmic_tuple::time_compare (a, b);
43 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
44 Rhythmic_tuple const &h2)
46 return (h1.end_ - h2.end_).main_part_.sign ();
49 /****************************************************************/
52 Acknowledge rhythmic elements, for initializing spacing fields in
55 class Spacing_engraver : public Engraver
57 PQueue<Rhythmic_tuple> playing_durations_;
58 vector<Rhythmic_tuple> now_durations_;
59 vector<Rhythmic_tuple> stopped_durations_;
62 Stream_event *start_section_;
64 TRANSLATOR_DECLARATIONS (Spacing_engraver);
67 DECLARE_ACKNOWLEDGER (staff_spacing);
68 DECLARE_ACKNOWLEDGER (note_spacing);
69 DECLARE_ACKNOWLEDGER (rhythmic_head);
70 DECLARE_TRANSLATOR_LISTENER (spacing_section);
72 void start_translation_timestep ();
73 void stop_translation_timestep ();
74 void process_music ();
76 virtual void finalize ();
78 void start_spanner ();
82 Spacing_engraver::Spacing_engraver ()
88 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
90 Spacing_engraver::listen_spacing_section (Stream_event *ev)
96 Spacing_engraver::process_music ()
98 if (start_section_ && spacing_)
106 Spacing_engraver::start_spanner ()
111 spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
112 spacing_->set_bound (LEFT,
113 unsmob_grob (get_property ("currentCommandColumn")));
117 Spacing_engraver::finalize ()
123 Spacing_engraver::stop_spanner ()
127 Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
129 spacing_->set_bound (RIGHT, p);
135 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
137 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
141 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
143 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
147 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
149 if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
150 || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
154 only pay attention to durations that are not grace notes.
156 if (!now_.grace_part_)
158 Stream_event *r = i.event_cause ();
159 if (r && r->in_event_class ("rhythmic-event"))
161 Moment len = get_event_length (r);
162 Rhythmic_tuple t (i, now_mom () + len);
163 now_durations_.push_back (t);
169 Spacing_engraver::stop_translation_timestep ()
171 Paper_column *musical_column
172 = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
178 musical_column->set_object ("spacing", spacing_->self_scm ());
179 unsmob_grob (get_property ("currentCommandColumn"))
180 ->set_object ("spacing", spacing_->self_scm ());
182 SCM proportional = get_property ("proportionalNotationDuration");
183 if (unsmob_moment (proportional))
185 musical_column->set_property ("shortest-playing-duration", proportional);
186 musical_column->set_property ("shortest-starter-duration", proportional);
190 Moment shortest_playing;
191 shortest_playing.set_infinite (1);
192 for (vsize i = 0; i < playing_durations_.size (); i++)
194 Stream_event *ev = playing_durations_[i].info_.event_cause ();
197 Moment m = get_event_length (ev);
198 shortest_playing = min (shortest_playing, m);
202 starter.set_infinite (1);
204 for (vsize i = 0; i < now_durations_.size (); i++)
206 Moment m = get_event_length (now_durations_[i].info_.event_cause ());
209 starter = min (starter, m);
210 playing_durations_.insert (now_durations_[i]);
213 now_durations_.clear ();
215 shortest_playing = min (shortest_playing, starter);
217 assert (starter.to_bool ());
218 SCM sh = shortest_playing.smobbed_copy ();
219 SCM st = starter.smobbed_copy ();
221 musical_column->set_property ("shortest-playing-duration", sh);
222 musical_column->set_property ("shortest-starter-duration", st);
228 Spacing_engraver::start_translation_timestep ()
233 stopped_durations_.clear ();
235 while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
236 playing_durations_.delmin ();
237 while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
238 stopped_durations_.push_back (playing_durations_.get ());
241 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
242 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
243 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
245 ADD_TRANSLATOR (Spacing_engraver,
246 "make a SpacingSpanner and do "
247 "bookkeeping of shortest starting and playing notes ",
249 /* create */ "SpacingSpanner",
251 "spacing-section-event ",
253 "currentMusicalColumn "
254 "currentCommandColumn "
255 "proportionalNotationDuration",