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 ()
110 spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
111 spacing_->set_bound (LEFT,
112 unsmob_grob (get_property ("currentCommandColumn")));
116 Spacing_engraver::finalize ()
122 Spacing_engraver::stop_spanner ()
126 Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
128 spacing_->set_bound (RIGHT, p);
134 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
136 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
140 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
142 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
146 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
148 if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
149 || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
153 only pay attention to durations that are not grace notes.
155 if (!now_.grace_part_)
157 Stream_event *r = i.event_cause ();
158 if (r && r->in_event_class ("rhythmic-event"))
160 Moment len = get_event_length (r);
161 Rhythmic_tuple t (i, now_mom () + len);
162 now_durations_.push_back (t);
168 Spacing_engraver::stop_translation_timestep ()
170 Paper_column *musical_column
171 = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
176 musical_column->set_object ("spacing", spacing_->self_scm ());
177 unsmob_grob (get_property ("currentCommandColumn"))
178 ->set_object ("spacing", spacing_->self_scm ());
181 SCM proportional = get_property ("proportionalNotationDuration");
182 if (unsmob_moment (proportional))
184 musical_column->set_property ("shortest-playing-duration", proportional);
185 musical_column->set_property ("shortest-starter-duration", proportional);
189 Moment shortest_playing;
190 shortest_playing.set_infinite (1);
191 for (vsize i = 0; i < playing_durations_.size (); i++)
193 Stream_event *ev = playing_durations_[i].info_.event_cause ();
196 Moment m = get_event_length (ev);
197 shortest_playing = min (shortest_playing, m);
201 starter.set_infinite (1);
203 for (vsize i = 0; i < now_durations_.size (); i++)
205 Moment m = get_event_length (now_durations_[i].info_.event_cause ());
208 starter = min (starter, m);
209 playing_durations_.insert (now_durations_[i]);
212 now_durations_.clear ();
214 shortest_playing = min (shortest_playing, starter);
216 assert (starter.to_bool ());
217 SCM sh = shortest_playing.smobbed_copy ();
218 SCM st = starter.smobbed_copy ();
220 musical_column->set_property ("shortest-playing-duration", sh);
221 musical_column->set_property ("shortest-starter-duration", st);
227 Spacing_engraver::start_translation_timestep ()
232 stopped_durations_.clear ();
234 while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
235 playing_durations_.delmin ();
236 while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
237 stopped_durations_.push_back (playing_durations_.get ());
240 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
241 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
242 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
244 ADD_TRANSLATOR (Spacing_engraver,
245 "make a SpacingSpanner and do "
246 "bookkeeping of shortest starting and playing notes ",
248 /* create */ "SpacingSpanner",
250 "spacing-section-event ",
252 "currentMusicalColumn "
253 "currentCommandColumn "
254 "proportionalNotationDuration",