]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
* lily/engraver.cc (internal_make_item): centralize item/spanner
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "event.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   Moment now_;
47   Spanner * spacing_;
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 process_music ();
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_ = 0;
74 }
75
76 void
77 Spacing_engraver::process_music ()
78 {
79   if (!spacing_)
80     {
81       spacing_  =make_spanner ("SpacingSpanner");
82       spacing_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));  
83       announce_grob(spacing_, SCM_EOL);
84     }
85 }
86 void
87 Spacing_engraver::finalize ()
88 {
89   Grob * p = unsmob_grob (get_property ("currentCommandColumn"));
90   spacing_->set_bound (RIGHT, p);
91   typeset_grob (spacing_);
92   spacing_ =0;
93 }
94
95 void
96 Spacing_engraver::acknowledge_grob (Grob_info i)
97 {
98   if (Note_spacing::has_interface (i.grob_) || Staff_spacing::has_interface (i.grob_))
99     {
100       Pointer_group_interface::add_grob (spacing_, ly_symbol2scm  ("wishes"), i.grob_);
101     }
102   
103   if (i.grob_->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface")))
104     return;
105
106   /*
107     only pay attention to durations that are not grace notes. 
108    */
109   if (!now_.grace_part_)
110     {
111       Music *r = i.music_cause ();
112       if (r && r->is_mus_type ("rhythmic-event"))
113         {
114           Moment len = r->get_length ();
115           Rhythmic_tuple t (i, now_mom () + len);
116           now_durations_.push (t);
117         }
118     }
119 }
120
121 void
122 Spacing_engraver::stop_translation_timestep ()
123 {
124   Moment shortest_playing;
125   shortest_playing.set_infinite (1);
126   for (int i=0; i < playing_durations_.size (); i++)
127     {
128       Music * mus = playing_durations_[i].info_.music_cause ();
129       if (mus)
130         {
131           Moment m = mus->get_length ();
132           shortest_playing = shortest_playing <? m;
133         }
134     }
135   Moment starter;
136   starter.set_infinite (1);
137
138   for (int i=0; i < now_durations_.size (); i++)
139     {
140       Moment m = now_durations_[i].info_.music_cause ()->get_length ();
141       if (m.to_bool ())
142         {
143           starter = starter <? m;
144           playing_durations_.insert (now_durations_[i]);
145
146         }
147     }
148   now_durations_.clear ();
149   
150   shortest_playing = shortest_playing <? starter;
151   
152   Paper_column * sc
153     = dynamic_cast<Paper_column*> (unsmob_grob (get_property ("currentMusicalColumn")));
154
155   assert (starter.to_bool ());
156   SCM sh = shortest_playing.smobbed_copy ();
157   SCM st = starter.smobbed_copy ();
158
159   sc->set_grob_property ("shortest-playing-duration", sh);  
160   sc->set_grob_property ("shortest-starter-duration", st);
161 }
162
163 void
164 Spacing_engraver::start_translation_timestep ()
165 {
166   now_ = now_mom ();
167   stopped_durations_.clear ();
168   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
169     playing_durations_.delmin ();
170   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
171     stopped_durations_.push (playing_durations_.get ());
172 }
173
174
175
176
177 ENTER_DESCRIPTION(Spacing_engraver,
178 /* descr */       "make a SpacingSpanner and do bookkeeping of shortest starting and playing notes  ",
179 /* creats*/       "SpacingSpanner",
180 /* accepts */     "",
181 /* acks  */      "grob-interface",
182 /* reads */       "",
183 /* write */       "");