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>
11 #include "note-spacing.hh"
12 #include "paper-column.hh"
13 #include "pointer-group-interface.hh"
16 #include "staff-spacing.hh"
17 #include "stream-event.hh"
19 #include "translator.icc"
29 Rhythmic_tuple (Grob_info i, Moment m)
34 static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
38 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
40 return Rhythmic_tuple::time_compare (a, b);
44 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
45 Rhythmic_tuple const &h2)
47 return (h1.end_ - h2.end_).main_part_.sign ();
50 /****************************************************************/
53 Acknowledge rhythmic elements, for initializing spacing fields in
56 class Spacing_engraver : public Engraver
58 PQueue<Rhythmic_tuple> playing_durations_;
59 vector<Rhythmic_tuple> now_durations_;
60 vector<Rhythmic_tuple> stopped_durations_;
63 Stream_event *start_section_;
65 TRANSLATOR_DECLARATIONS (Spacing_engraver);
68 DECLARE_ACKNOWLEDGER (staff_spacing);
69 DECLARE_ACKNOWLEDGER (note_spacing);
70 DECLARE_ACKNOWLEDGER (rhythmic_head);
71 DECLARE_TRANSLATOR_LISTENER (spacing_section);
73 void start_translation_timestep ();
74 void stop_translation_timestep ();
75 void process_music ();
77 virtual void finalize ();
79 void start_spanner ();
83 Spacing_engraver::Spacing_engraver ()
89 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
91 Spacing_engraver::listen_spacing_section (Stream_event *ev)
93 ASSIGN_EVENT_ONCE (start_section_, ev);
97 Spacing_engraver::process_music ()
99 if (start_section_ && spacing_)
107 Spacing_engraver::start_spanner ()
112 spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
113 spacing_->set_bound (LEFT,
114 unsmob_grob (get_property ("currentCommandColumn")));
118 Spacing_engraver::finalize ()
124 Spacing_engraver::stop_spanner ()
128 Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
130 spacing_->set_bound (RIGHT, p);
136 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
138 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
142 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
144 Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
148 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
150 if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
151 || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
155 only pay attention to durations that are not grace notes.
157 if (!now_.grace_part_)
159 Stream_event *r = i.event_cause ();
160 if (r && r->in_event_class ("rhythmic-event"))
162 Moment len = get_event_length (r);
163 Rhythmic_tuple t (i, now_mom () + len);
164 now_durations_.push_back (t);
170 Spacing_engraver::stop_translation_timestep ()
172 Paper_column *musical_column
173 = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
179 musical_column->set_object ("spacing", spacing_->self_scm ());
180 unsmob_grob (get_property ("currentCommandColumn"))
181 ->set_object ("spacing", spacing_->self_scm ());
183 SCM proportional = get_property ("proportionalNotationDuration");
184 if (unsmob_moment (proportional))
186 musical_column->set_property ("shortest-playing-duration", proportional);
187 musical_column->set_property ("shortest-starter-duration", proportional);
188 musical_column->set_property ("used", SCM_BOOL_T);
192 Moment shortest_playing;
193 shortest_playing.set_infinite (1);
194 for (vsize i = 0; i < playing_durations_.size (); i++)
196 Stream_event *ev = playing_durations_[i].info_.event_cause ();
199 Moment m = get_event_length (ev);
200 shortest_playing = min (shortest_playing, m);
204 starter.set_infinite (1);
206 for (vsize i = 0; i < now_durations_.size (); i++)
208 Moment m = get_event_length (now_durations_[i].info_.event_cause ());
211 starter = min (starter, m);
212 playing_durations_.insert (now_durations_[i]);
215 now_durations_.clear ();
217 shortest_playing = min (shortest_playing, starter);
219 assert (starter.to_bool ());
220 SCM sh = shortest_playing.smobbed_copy ();
221 SCM st = starter.smobbed_copy ();
223 musical_column->set_property ("shortest-playing-duration", sh);
224 musical_column->set_property ("shortest-starter-duration", st);
230 Spacing_engraver::start_translation_timestep ()
235 stopped_durations_.clear ();
237 while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
238 playing_durations_.delmin ();
239 while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
240 stopped_durations_.push_back (playing_durations_.get ());
243 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
244 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
245 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
247 ADD_TRANSLATOR (Spacing_engraver,
248 "make a SpacingSpanner and do "
249 "bookkeeping of shortest starting and playing notes ",
251 /* create */ "SpacingSpanner",
253 "currentMusicalColumn "
254 "currentCommandColumn "
255 "proportionalNotationDuration",