]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
0b3b58673159f08145eec098644ca9d7950323db
[lilypond.git] / lily / spacing-engraver.cc
1 /*   
2   spacing-engraver.cc --  implement Spacing_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "musical-request.hh"
11 #include "paper-column.hh"
12
13 #include "spacing-spanner.hh"
14 #include "engraver.hh"
15 #include "pqueue.hh"
16
17 struct Rhythmic_tuple
18 {
19   Grob_info info_;
20   Moment end_;
21   
22   Rhythmic_tuple ()
23     {
24     }
25   Rhythmic_tuple (Grob_info i, Moment m)
26     {
27       info_ = i;
28       end_ = m;
29     }
30   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);  
31 };
32
33 /**
34    Acknowledge rhythmic elements, for initializing spacing fields in
35    the columns.
36
37    should be the  last one of the toplevel context
38 */
39 class Spacing_engraver : public Engraver
40 {
41   PQueue<Rhythmic_tuple> playing_durations_;
42   Array<Rhythmic_tuple> now_durations_;
43   Array<Rhythmic_tuple> stopped_durations_;
44
45   Spanner * spacing_p_;
46 protected:
47   VIRTUAL_COPY_CONS (Translator);
48   virtual void acknowledge_grob (Grob_info);
49   virtual void start_translation_timestep ();
50   virtual void stop_translation_timestep ();
51   virtual void initialize ();
52   virtual void finalize ();
53 public:
54   Spacing_engraver ();
55 };
56
57 inline int
58 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
59 {
60   return Rhythmic_tuple::time_compare (a,b);
61 }
62
63 int
64 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
65                               Rhythmic_tuple const &h2)
66 {
67   return (h1.end_ - h2.end_).sign ();
68 }
69
70 Spacing_engraver::Spacing_engraver ()
71 {
72   spacing_p_ = 0;
73 }
74
75 void
76 Spacing_engraver::initialize ()
77 {
78   spacing_p_  =new Spanner (get_property ("SpacingSpanner"));
79   Spacing_spanner::set_interface (spacing_p_);
80   spacing_p_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));  
81   announce_grob (spacing_p_, 0);
82 }
83
84 void
85 Spacing_engraver::finalize ()
86 {
87   Grob * p = unsmob_grob (get_property ("currentCommandColumn"));
88   spacing_p_->set_bound (RIGHT, p);
89   typeset_grob (spacing_p_);
90   spacing_p_ =0;
91 }
92
93 void
94 Spacing_engraver::acknowledge_grob (Grob_info i)
95 {
96   if (to_boolean (i.elem_l_->get_grob_property ("grace")))
97     return;
98
99   if (to_boolean (i.elem_l_->get_grob_property ("non-rhythmic")))
100     return;
101   
102   if (Rhythmic_req * r = dynamic_cast<Rhythmic_req*> (i.req_l_))
103     {
104       Rhythmic_tuple t (i, now_mom () + r->length_mom ());
105       now_durations_.push (t);
106     }
107 }
108
109 void
110 Spacing_engraver::stop_translation_timestep ()
111 {
112   Moment shortest_playing;
113   shortest_playing.set_infinite (1);
114   for (int i=0; i < playing_durations_.size (); i++)
115     {
116       Moment m = (playing_durations_[i].info_.req_l_)->length_mom ();
117       if (m)
118         {
119           shortest_playing = shortest_playing <? m;
120         }
121     }
122   
123   Moment starter, inf;
124   inf.set_infinite (1);
125   starter=inf;
126   for (int i=0; i < now_durations_.size (); i++)
127     {
128       Moment m = now_durations_[i].info_.req_l_->length_mom ();
129       if (m)
130         starter = starter <? m;
131
132       playing_durations_.insert (now_durations_[i]);
133     }
134   now_durations_.clear ();
135   
136   shortest_playing = shortest_playing <? starter;
137   
138   Paper_column * sc
139     = dynamic_cast<Paper_column*> (unsmob_grob (get_property ("currentMusicalColumn")));
140
141   SCM sh = shortest_playing.smobbed_copy ();
142   SCM st = starter.smobbed_copy ();
143
144   sc->set_grob_property ("shortest-playing-duration", sh);  
145   sc->set_grob_property ("shortest-starter-duration", st);
146 }
147
148 void
149 Spacing_engraver::start_translation_timestep ()
150 {
151   Moment now = now_mom ();
152   stopped_durations_.clear ();
153   while (playing_durations_.size () && playing_durations_.front ().end_ < now)
154     playing_durations_.delmin ();
155   while (playing_durations_.size () && playing_durations_.front ().end_ == now)
156     stopped_durations_.push (playing_durations_.get ());
157 }
158
159 ADD_THIS_TRANSLATOR (Spacing_engraver);
160
161