/*
- rest-collision-reg.cc -- implement Rest_collision_engraver
+ rest-collision-engraver.cc -- implement Rest_collision_engraver
source file of the GNU LilyPond music typesetter
- (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+ (c) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
*/
-#include "debug.hh"
-#include "rest-collision.hh"
+#include <list>
+
+#include "duration.hh"
#include "engraver.hh"
-#include "collision.hh"
+#include "item.hh"
+#include "moment.hh"
#include "note-column.hh"
+#include "rest-collision.hh"
+#include "stream-event.hh"
+#include "warn.hh"
class Rest_collision_engraver : public Engraver
{
- Item* rest_collision_p_;
-
- Link_array<Score_element> note_column_l_arr_;
+ Item *rest_collision_;
+ vsize rest_count_;
+ list<pair<Grob*, Moment> > note_columns_;
protected:
- virtual void acknowledge_element (Score_element_info);
- virtual void process_acknowledged ();
- virtual void do_pre_move_processing();
+ DECLARE_ACKNOWLEDGER (note_column);
+ void process_acknowledged ();
+ void stop_translation_timestep ();
+ void start_translation_timestep ();
public:
- VIRTUAL_COPY_CONS(Translator);
- Rest_collision_engraver();
-
+ TRANSLATOR_DECLARATIONS (Rest_collision_engraver);
};
-ADD_THIS_TRANSLATOR(Rest_collision_engraver);
-
-Rest_collision_engraver::Rest_collision_engraver()
+Rest_collision_engraver::Rest_collision_engraver ()
{
- rest_collision_p_ =0;
+ rest_collision_ = 0;
+ rest_count_ = 0;
}
void
Rest_collision_engraver::process_acknowledged ()
{
- if (rest_collision_p_ || note_column_l_arr_.size () < 2)
+ if (rest_collision_
+ || note_columns_.empty ()
+ || !rest_count_
+ || (note_columns_.size () == rest_count_
+ && rest_count_ < 2))
return;
- rest_collision_p_ = new Item (get_property ("basicRestCollisionProperties"));
- Rest_collision::set_interface (rest_collision_p_);
- announce_element (Score_element_info (rest_collision_p_, 0));
- for (int i=0; i< note_column_l_arr_.size (); i++)
- Rest_collision::add_column ( rest_collision_p_,note_column_l_arr_[i]);
+ rest_collision_ = make_item ("RestCollision", SCM_EOL);
+
+ list<pair<Grob*, Moment> >::iterator i;
+ for (i = note_columns_.begin (); i != note_columns_.end (); i++)
+ Rest_collision::add_column (rest_collision_, i->first);
}
void
-Rest_collision_engraver::acknowledge_element (Score_element_info i)
+Rest_collision_engraver::acknowledge_note_column (Grob_info i)
{
- if (Note_column::has_interface (i.elem_l_))
- note_column_l_arr_.push (i.elem_l_);
+ Moment end = now_mom ();
+ if (Note_column::has_rests (i.grob ()))
+ rest_count_++;
+ else
+ {
+ // We only keep track of ending moments for columns with notes.
+ // It is safe to add a column with notes to multiple RestCollisions, but
+ // it might not be safe to add a column with rests to multiple RestCollisions.
+ Grob *stem = Note_column::get_stem (i.grob ());
+ Stream_event *ev = stem ? stem->event_cause () : 0;
+ Duration *dur_ptr = ev ? unsmob_duration (ev->get_property ("duration")) : 0;
+ if (dur_ptr)
+ {
+ if (end.grace_part_)
+ end.grace_part_ += dur_ptr->get_length ();
+ else
+ end.main_part_ += dur_ptr->get_length ();
+ }
+ }
+ note_columns_.push_back (pair<Grob*, Moment> (i.grob (), end));
}
void
-Rest_collision_engraver::do_pre_move_processing()
+Rest_collision_engraver::stop_translation_timestep ()
{
- if (rest_collision_p_)
+ rest_collision_ = 0;
+ rest_count_ = 0;
+}
+
+void
+Rest_collision_engraver::start_translation_timestep ()
+{
+ list<pair<Grob*, Moment> >::iterator i = note_columns_.begin ();
+ while (i != note_columns_.end ())
{
- typeset_element (rest_collision_p_);
- rest_collision_p_ = 0;
+ if (i->second <= now_mom ())
+ i = note_columns_.erase (i);
+ else
+ i++;
}
- note_column_l_arr_.clear ();
}
+
+#include "translator.icc"
+
+ADD_ACKNOWLEDGER (Rest_collision_engraver, note_column);
+ADD_TRANSLATOR (Rest_collision_engraver,
+ /* doc */
+ "Handle collisions of rests.",
+
+ /* create */
+ "RestCollision ",
+
+ /* read */
+ "",
+
+ /* write */
+ ""
+ );