]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
d544410227cff4d0ef252a933a8a8c0ebf94ea48
[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--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10 #include "note-spacing.hh"
11 #include "paper-column.hh"
12 #include "pointer-group-interface.hh"
13 #include "pqueue.hh"
14 #include "spanner.hh"
15 #include "staff-spacing.hh"
16 #include "stream-event.hh"
17
18 #include "translator.icc"
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 inline int
37 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
38 {
39   return Rhythmic_tuple::time_compare (a, b);
40 }
41
42 int
43 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
44                               Rhythmic_tuple const &h2)
45 {
46   return (h1.end_ - h2.end_).main_part_.sign ();
47 }
48
49 /****************************************************************/
50
51 /*
52   Acknowledge rhythmic elements, for initializing spacing fields in
53   the columns.
54 */
55 class Spacing_engraver : public Engraver
56 {
57   PQueue<Rhythmic_tuple> playing_durations_;
58   vector<Rhythmic_tuple> now_durations_;
59   vector<Rhythmic_tuple> stopped_durations_;
60   Moment now_;
61   Spanner *spacing_;
62   Stream_event *start_section_;
63   
64   TRANSLATOR_DECLARATIONS (Spacing_engraver);
65
66 protected:
67   DECLARE_ACKNOWLEDGER (staff_spacing);
68   DECLARE_ACKNOWLEDGER (note_spacing);
69   DECLARE_ACKNOWLEDGER (rhythmic_head);
70   DECLARE_TRANSLATOR_LISTENER (spacing_section);
71
72   void start_translation_timestep ();
73   void stop_translation_timestep ();
74   void process_music ();
75   
76   virtual void finalize ();
77
78   void start_spanner ();
79   void stop_spanner ();
80 };
81
82 Spacing_engraver::Spacing_engraver ()
83 {
84   spacing_ = 0;
85   start_section_ = 0;
86 }
87
88 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
89 void
90 Spacing_engraver::listen_spacing_section (Stream_event *ev)
91 {
92   start_section_ = ev;
93 }
94
95 void
96 Spacing_engraver::process_music ()
97 {
98   if (start_section_ && spacing_)
99     stop_spanner ();
100   
101   if (!spacing_)
102     start_spanner ();
103 }
104
105 void
106 Spacing_engraver::start_spanner ()
107 {
108   assert (!spacing_);
109
110   spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
111   spacing_->set_bound (LEFT,
112                        unsmob_grob (get_property ("currentCommandColumn")));
113 }
114
115 void
116 Spacing_engraver::finalize ()
117 {
118   stop_spanner ();
119 }
120
121 void
122 Spacing_engraver::stop_spanner ()
123 {
124   if (spacing_)
125     {
126       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
127
128       spacing_->set_bound (RIGHT, p);
129       spacing_ = 0;
130     }
131 }
132
133 void
134 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
135 {
136   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
137 }
138
139 void
140 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
141 {
142   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
143 }
144
145 void
146 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
147 {
148   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
149       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
150     return;
151
152   /*
153     only pay attention to durations that are not grace notes.
154   */
155   if (!now_.grace_part_)
156     {
157       Stream_event *r = i.event_cause ();
158       if (r && r->in_event_class ("rhythmic-event"))
159         {
160           Moment len = get_event_length (r);
161           Rhythmic_tuple t (i, now_mom () + len);
162           now_durations_.push_back (t);
163         }
164     }
165 }
166
167 void
168 Spacing_engraver::stop_translation_timestep ()
169 {
170   Paper_column *musical_column
171     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
172
173   musical_column->set_object ("spacing", spacing_->self_scm ());
174   unsmob_grob (get_property ("currentCommandColumn"))
175     ->set_object ("spacing", spacing_->self_scm ());
176
177   SCM proportional = get_property ("proportionalNotationDuration");
178   if (unsmob_moment (proportional))
179     {
180       musical_column->set_property ("shortest-playing-duration", proportional);
181       musical_column->set_property ("shortest-starter-duration", proportional);
182       return;
183     }
184
185   Moment shortest_playing;
186   shortest_playing.set_infinite (1);
187   for (vsize i = 0; i < playing_durations_.size (); i++)
188     {
189       Stream_event *ev = playing_durations_[i].info_.event_cause ();
190       if (ev)
191         {
192           Moment m = get_event_length (ev);
193           shortest_playing = min (shortest_playing, m);
194         }
195     }
196   Moment starter;
197   starter.set_infinite (1);
198
199   for (vsize i = 0; i < now_durations_.size (); i++)
200     {
201       Moment m = get_event_length (now_durations_[i].info_.event_cause ());
202       if (m.to_bool ())
203         {
204           starter = min (starter, m);
205           playing_durations_.insert (now_durations_[i]);
206         }
207     }
208   now_durations_.clear ();
209
210   shortest_playing = min (shortest_playing, starter);
211
212   assert (starter.to_bool ());
213   SCM sh = shortest_playing.smobbed_copy ();
214   SCM st = starter.smobbed_copy ();
215
216   musical_column->set_property ("shortest-playing-duration", sh);
217   musical_column->set_property ("shortest-starter-duration", st);
218 }
219
220
221
222 void
223 Spacing_engraver::start_translation_timestep ()
224 {
225   start_section_ = 0;
226
227   now_ = now_mom ();
228   stopped_durations_.clear ();
229   
230   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
231     playing_durations_.delmin ();
232   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
233     stopped_durations_.push_back (playing_durations_.get ());
234 }
235
236 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
237 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
238 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
239
240 ADD_TRANSLATOR (Spacing_engraver,
241                 "make a SpacingSpanner and do "
242                 "bookkeeping of shortest starting and playing notes  ",
243
244                 /* create */ "SpacingSpanner",
245                 /* accept */
246                 "spacing-section-event ",
247                 /* read */
248                 "currentMusicalColumn "
249                 "currentCommandColumn "
250                 "proportionalNotationDuration",
251                 
252                 /* write */ "");