]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
* flower
[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--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "paper-column.hh"
10 #include "engraver.hh"
11 #include "pqueue.hh"
12 #include "note-spacing.hh"
13 #include "staff-spacing.hh"
14 #include "group-interface.hh"
15 #include "spanner.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   Moment now_;
45   Spanner *spacing_;
46
47   TRANSLATOR_DECLARATIONS (Spacing_engraver);
48 protected:
49   virtual void acknowledge_grob (Grob_info);
50   virtual void start_translation_timestep ();
51   virtual void stop_translation_timestep ();
52   virtual void process_music ();
53   virtual void finalize ();
54 };
55
56 inline int
57 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
58 {
59   return Rhythmic_tuple::time_compare (a, b);
60 }
61
62 int
63 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
64                               Rhythmic_tuple const &h2)
65 {
66   return (h1.end_ - h2.end_).main_part_.sign ();
67 }
68
69 Spacing_engraver::Spacing_engraver ()
70 {
71   spacing_ = 0;
72 }
73
74 void
75 Spacing_engraver::process_music ()
76 {
77   if (!spacing_)
78     {
79       spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
80       spacing_->set_bound (LEFT, unsmob_grob (get_property ("currentCommandColumn")));
81
82     }
83 }
84
85 void
86 Spacing_engraver::finalize ()
87 {
88   if (spacing_)
89     {
90       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
91
92       spacing_->set_bound (RIGHT, p);
93       spacing_ = 0;
94     }
95 }
96
97 void
98 Spacing_engraver::acknowledge_grob (Grob_info i)
99 {
100   if (Note_spacing::has_interface (i.grob_) || Staff_spacing::has_interface (i.grob_))
101     {
102       Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob_);
103     }
104
105   if (i.grob_->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
106       || i.grob_->internal_has_interface (ly_symbol2scm ("multi-measure-event")))
107     return;
108
109   /*
110     only pay attention to durations that are not grace notes.
111   */
112   if (!now_.grace_part_)
113     {
114       Music *r = i.music_cause ();
115       if (r && r->is_mus_type ("rhythmic-event"))
116         {
117           Moment len = r->get_length ();
118           Rhythmic_tuple t (i, now_mom () + len);
119           now_durations_.push (t);
120         }
121     }
122 }
123
124 void
125 Spacing_engraver::stop_translation_timestep ()
126 {
127   Moment shortest_playing;
128   shortest_playing.set_infinite (1);
129   for (int i = 0; i < playing_durations_.size (); i++)
130     {
131       Music *mus = playing_durations_[i].info_.music_cause ();
132       if (mus)
133         {
134           Moment m = mus->get_length ();
135           shortest_playing = shortest_playing <? m;
136         }
137     }
138   Moment starter;
139   starter.set_infinite (1);
140
141   for (int i = 0; i < now_durations_.size (); i++)
142     {
143       Moment m = now_durations_[i].info_.music_cause ()->get_length ();
144       if (m.to_bool ())
145         {
146           starter = starter <? m;
147           playing_durations_.insert (now_durations_[i]);
148
149         }
150     }
151   now_durations_.clear ();
152
153   shortest_playing = shortest_playing <? starter;
154
155   Paper_column *sc
156     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
157
158   assert (starter.to_bool ());
159   SCM sh = shortest_playing.smobbed_copy ();
160   SCM st = starter.smobbed_copy ();
161
162   sc->set_property ("shortest-playing-duration", sh);
163   sc->set_property ("shortest-starter-duration", st);
164 }
165
166 void
167 Spacing_engraver::start_translation_timestep ()
168 {
169   now_ = now_mom ();
170   stopped_durations_.clear ();
171   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
172     playing_durations_.delmin ();
173   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
174     stopped_durations_.push (playing_durations_.get ());
175 }
176
177
178 ADD_TRANSLATOR (Spacing_engraver,
179                 /* descr */ "make a SpacingSpanner and do bookkeeping of shortest starting and playing notes  ",
180                 /* creats*/ "SpacingSpanner",
181                 /* accepts */ "",
182                 /* acks  */ "grob-interface",
183                 /* reads */ "",
184                 /* write */ "");