]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
release: 1.5.24
[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 #include "note-spacing.hh"
17 #include "staff-spacing.hh"
18 #include "group-interface.hh"
19
20 struct Rhythmic_tuple
21 {
22   Grob_info info_;
23   Moment end_;
24   
25   Rhythmic_tuple ()
26     {
27     }
28   Rhythmic_tuple (Grob_info i, Moment m)
29     {
30       info_ = i;
31       end_ = m;
32     }
33   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);  
34 };
35
36 /**
37    Acknowledge rhythmic elements, for initializing spacing fields in
38    the columns.
39
40    should be the  last one of the toplevel context
41 */
42 class Spacing_engraver : public Engraver
43 {
44   PQueue<Rhythmic_tuple> playing_durations_;
45   Array<Rhythmic_tuple> now_durations_;
46   Array<Rhythmic_tuple> stopped_durations_;
47
48   Spanner * spacing_p_;
49   
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 ();
57 };
58
59 inline int
60 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
61 {
62   return Rhythmic_tuple::time_compare (a,b);
63 }
64
65 int
66 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
67                               Rhythmic_tuple const &h2)
68 {
69   return (h1.end_ - h2.end_).main_part_.sign ();
70 }
71
72 Spacing_engraver::Spacing_engraver ()
73 {
74   spacing_p_ = 0;
75 }
76
77 void
78 Spacing_engraver::initialize ()
79 {
80   spacing_p_  =new Spanner (get_property ("SpacingSpanner"));
81   Spacing_spanner::set_interface (spacing_p_);
82   spacing_p_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));  
83   announce_grob (spacing_p_, 0);
84 }
85
86 void
87 Spacing_engraver::finalize ()
88 {
89   Grob * p = unsmob_grob (get_property ("currentCommandColumn"));
90   spacing_p_->set_bound (RIGHT, p);
91   typeset_grob (spacing_p_);
92   spacing_p_ =0;
93 }
94
95 void
96 Spacing_engraver::acknowledge_grob (Grob_info i)
97 {
98   if (Note_spacing::has_interface (i.grob_l_) || Staff_spacing::has_interface (i.grob_l_))
99     {
100       Pointer_group_interface::add_element (spacing_p_, ly_symbol2scm  ("wishes"), i.grob_l_);
101     }
102   
103   if (to_boolean (i.grob_l_->get_grob_property ("non-rhythmic")))
104     return;
105   
106   if (Rhythmic_req * r = dynamic_cast<Rhythmic_req*> (i.music_cause ()))
107     {
108       Rhythmic_tuple t (i, now_mom () + r->length_mom ());
109       now_durations_.push (t);
110     }
111 }
112
113 void
114 Spacing_engraver::stop_translation_timestep ()
115 {
116   Moment shortest_playing;
117   shortest_playing.set_infinite (1);
118   for (int i=0; i < playing_durations_.size (); i++)
119     {
120       Moment m = (playing_durations_[i].info_.music_cause ())->length_mom ();
121       if (m.to_bool ())
122         {
123           shortest_playing = shortest_playing <? m;
124         }
125     }
126   
127   Moment starter, inf;
128   inf.set_infinite (1);
129   starter=inf;
130   for (int i=0; i < now_durations_.size (); i++)
131     {
132       Moment m = now_durations_[i].info_.music_cause ()->length_mom ();
133       if (m.to_bool ())
134         starter = starter <? m;
135
136       playing_durations_.insert (now_durations_[i]);
137     }
138   now_durations_.clear ();
139   
140   shortest_playing = shortest_playing <? starter;
141   
142   Paper_column * sc
143     = dynamic_cast<Paper_column*> (unsmob_grob (get_property ("currentMusicalColumn")));
144
145   SCM sh = shortest_playing.smobbed_copy ();
146   SCM st = starter.smobbed_copy ();
147
148   sc->set_grob_property ("shortest-playing-duration", sh);  
149   sc->set_grob_property ("shortest-starter-duration", st);
150 }
151
152 void
153 Spacing_engraver::start_translation_timestep ()
154 {
155   Moment now = now_mom ();
156   stopped_durations_.clear ();
157   while (playing_durations_.size () && playing_durations_.front ().end_ < now)
158     playing_durations_.delmin ();
159   while (playing_durations_.size () && playing_durations_.front ().end_ == now)
160     stopped_durations_.push (playing_durations_.get ());
161 }
162
163
164
165
166 ENTER_DESCRIPTION(Spacing_engraver,
167 /* descr */       "make a SpacingSpanner and do bookkeeping of shortest starting and playing notes  ",
168 /* creats*/       "SpacingSpanner",
169 /* acks  */       "grob-interface",
170 /* reads */       "",
171 /* write */       "");