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