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