]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
release: 1.5.38
[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--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "musical-request.hh"
11 #include "paper-column.hh"
12 #include "engraver.hh"
13 #include "pqueue.hh"
14 #include "note-spacing.hh"
15 #include "staff-spacing.hh"
16 #include "group-interface.hh"
17 #include "spanner.hh"
18
19 struct Rhythmic_tuple
20 {
21   Grob_info info_;
22   Moment end_;
23   
24   Rhythmic_tuple ()
25     {
26     }
27   Rhythmic_tuple (Grob_info i, Moment m)
28     {
29       info_ = i;
30       end_ = m;
31     }
32   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);  
33 };
34
35 /**
36    Acknowledge rhythmic elements, for initializing spacing fields in
37    the columns.
38
39    should be the  last one of the toplevel context
40 */
41 class Spacing_engraver : public Engraver
42 {
43   PQueue<Rhythmic_tuple> playing_durations_;
44   Array<Rhythmic_tuple> now_durations_;
45   Array<Rhythmic_tuple> stopped_durations_;
46
47   Spanner * spacing_p_;
48   
49   TRANSLATOR_DECLARATIONS(Spacing_engraver);
50 protected:
51   virtual void acknowledge_grob (Grob_info);
52   virtual void start_translation_timestep ();
53   virtual void stop_translation_timestep ();
54   virtual void initialize ();
55   virtual void finalize ();
56 };
57
58 inline int
59 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
60 {
61   return Rhythmic_tuple::time_compare (a,b);
62 }
63
64 int
65 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
66                               Rhythmic_tuple const &h2)
67 {
68   return (h1.end_ - h2.end_).main_part_.sign ();
69 }
70
71 Spacing_engraver::Spacing_engraver ()
72 {
73   spacing_p_ = 0;
74 }
75
76 void
77 Spacing_engraver::initialize ()
78 {
79   spacing_p_  =new Spanner (get_property ("SpacingSpanner"));
80   spacing_p_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));  
81   announce_grob(spacing_p_, SCM_EOL);
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 (Note_spacing::has_interface (i.grob_l_) || Staff_spacing::has_interface (i.grob_l_))
97     {
98       Pointer_group_interface::add_grob (spacing_p_, ly_symbol2scm  ("wishes"), i.grob_l_);
99     }
100   
101   if (to_boolean (i.grob_l_->get_grob_property ("non-rhythmic")))
102     return;
103   
104   if (Rhythmic_req * r = dynamic_cast<Rhythmic_req*> (i.music_cause ()))
105     {
106       Rhythmic_tuple t (i, now_mom () + r->length_mom ());
107       now_durations_.push (t);
108     }
109 }
110
111 void
112 Spacing_engraver::stop_translation_timestep ()
113 {
114   Moment shortest_playing;
115   shortest_playing.set_infinite (1);
116   for (int i=0; i < playing_durations_.size (); i++)
117     {
118       Moment m = (playing_durations_[i].info_.music_cause ())->length_mom ();
119       if (m.to_bool ())
120         {
121           shortest_playing = shortest_playing <? m;
122         }
123     }
124   
125   Moment starter;
126   starter.set_infinite (1);
127
128   for (int i=0; i < now_durations_.size (); i++)
129     {
130       Moment m = now_durations_[i].info_.music_cause ()->length_mom ();
131       if (m.to_bool ())
132         {
133           starter = starter <? m;
134           playing_durations_.insert (now_durations_[i]);
135         }
136     }
137   now_durations_.clear ();
138   
139   shortest_playing = shortest_playing <? starter;
140   
141   Paper_column * sc
142     = dynamic_cast<Paper_column*> (unsmob_grob (get_property ("currentMusicalColumn")));
143
144   assert (starter.to_bool ());
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 */       "");