lilypond-1.5.37
[lilypond.git] / lily / spacing-engraver.cc
blobf4aa7269a9bb3dac4bb1c492d84c8237ce1c346c
1 /*
2 spacing-engraver.cc -- implement Spacing_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
10 #include "musical-request.hh"
11 #include "paper-column.hh"
13 #include "spacing-spanner.hh"
14 #include "engraver.hh"
15 #include "pqueue.hh"
16 #include "note-spacing.hh"
17 #include "staff-spacing.hh"
18 #include "group-interface.hh"
20 struct Rhythmic_tuple
22 Grob_info info_;
23 Moment end_;
25 Rhythmic_tuple ()
28 Rhythmic_tuple (Grob_info i, Moment m)
30 info_ = i;
31 end_ = m;
33 static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
36 /**
37 Acknowledge rhythmic elements, for initializing spacing fields in
38 the columns.
40 should be the last one of the toplevel context
42 class Spacing_engraver : public Engraver
44 PQueue<Rhythmic_tuple> playing_durations_;
45 Array<Rhythmic_tuple> now_durations_;
46 Array<Rhythmic_tuple> stopped_durations_;
48 Spanner * spacing_p_;
50 TRANSLATOR_DECLARATIONS(Spacing_engraver);
51 protected:
52 virtual void acknowledge_grob (Grob_info);
53 virtual void start_translation_timestep ();
54 virtual void stop_translation_timestep ();
55 virtual void initialize ();
56 virtual void finalize ();
59 inline int
60 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
62 return Rhythmic_tuple::time_compare (a,b);
65 int
66 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
67 Rhythmic_tuple const &h2)
69 return (h1.end_ - h2.end_).main_part_.sign ();
72 Spacing_engraver::Spacing_engraver ()
74 spacing_p_ = 0;
77 void
78 Spacing_engraver::initialize ()
80 spacing_p_ =new Spanner (get_property ("SpacingSpanner"));
81 spacing_p_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
82 announce_grob(spacing_p_, SCM_EOL);
85 void
86 Spacing_engraver::finalize ()
88 Grob * p = unsmob_grob (get_property ("currentCommandColumn"));
89 spacing_p_->set_bound (RIGHT, p);
90 typeset_grob (spacing_p_);
91 spacing_p_ =0;
94 void
95 Spacing_engraver::acknowledge_grob (Grob_info i)
97 if (Note_spacing::has_interface (i.grob_l_) || Staff_spacing::has_interface (i.grob_l_))
99 Pointer_group_interface::add_grob (spacing_p_, ly_symbol2scm ("wishes"), i.grob_l_);
102 if (to_boolean (i.grob_l_->get_grob_property ("non-rhythmic")))
103 return;
105 if (Rhythmic_req * r = dynamic_cast<Rhythmic_req*> (i.music_cause ()))
107 Rhythmic_tuple t (i, now_mom () + r->length_mom ());
108 now_durations_.push (t);
112 void
113 Spacing_engraver::stop_translation_timestep ()
115 Moment shortest_playing;
116 shortest_playing.set_infinite (1);
117 for (int i=0; i < playing_durations_.size (); i++)
119 Moment m = (playing_durations_[i].info_.music_cause ())->length_mom ();
120 if (m.to_bool ())
122 shortest_playing = shortest_playing <? m;
126 Moment starter, inf;
127 inf.set_infinite (1);
128 starter=inf;
129 for (int i=0; i < now_durations_.size (); i++)
131 Moment m = now_durations_[i].info_.music_cause ()->length_mom ();
132 if (m.to_bool ())
133 starter = starter <? m;
135 playing_durations_.insert (now_durations_[i]);
137 now_durations_.clear ();
139 shortest_playing = shortest_playing <? starter;
141 Paper_column * sc
142 = dynamic_cast<Paper_column*> (unsmob_grob (get_property ("currentMusicalColumn")));
144 SCM sh = shortest_playing.smobbed_copy ();
145 SCM st = starter.smobbed_copy ();
147 sc->set_grob_property ("shortest-playing-duration", sh);
148 sc->set_grob_property ("shortest-starter-duration", st);
151 void
152 Spacing_engraver::start_translation_timestep ()
154 Moment now = now_mom ();
155 stopped_durations_.clear ();
156 while (playing_durations_.size () && playing_durations_.front ().end_ < now)
157 playing_durations_.delmin ();
158 while (playing_durations_.size () && playing_durations_.front ().end_ == now)
159 stopped_durations_.push (playing_durations_.get ());
165 ENTER_DESCRIPTION(Spacing_engraver,
166 /* descr */ "make a SpacingSpanner and do bookkeeping of shortest starting and playing notes ",
167 /* creats*/ "SpacingSpanner",
168 /* acks */ "grob-interface",
169 /* reads */ "",
170 /* write */ "");