]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
* lily/*.cc, lily/include/*.hh: eliminate dummy arguments from
[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_TRANSLATOR_LISTENER (spacing_section);
72
73   void start_translation_timestep ();
74   void stop_translation_timestep ();
75   void process_music ();
76   
77   virtual void finalize ();
78
79   void start_spanner ();
80   void stop_spanner ();
81 };
82
83 Spacing_engraver::Spacing_engraver ()
84 {
85   spacing_ = 0;
86   start_section_ = 0;
87 }
88
89 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
90 void
91 Spacing_engraver::listen_spacing_section (Stream_event *ev)
92 {
93   ASSIGN_EVENT_ONCE (start_section_, ev);
94 }
95
96 void
97 Spacing_engraver::process_music ()
98 {
99   if (start_section_ && spacing_)
100     stop_spanner ();
101   
102   if (!spacing_)
103     start_spanner ();
104 }
105
106 void
107 Spacing_engraver::start_spanner ()
108 {
109   assert (!spacing_);
110
111
112   spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
113   spacing_->set_bound (LEFT,
114                        unsmob_grob (get_property ("currentCommandColumn")));
115 }
116
117 void
118 Spacing_engraver::finalize ()
119 {
120   stop_spanner ();
121 }
122
123 void
124 Spacing_engraver::stop_spanner ()
125 {
126   if (spacing_)
127     {
128       Grob *p = unsmob_grob (get_property ("currentCommandColumn"));
129
130       spacing_->set_bound (RIGHT, p);
131       spacing_ = 0;
132     }
133 }
134
135 void
136 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
137 {
138   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
139 }
140
141 void
142 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
143 {
144   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
145 }
146
147 void
148 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
149 {
150   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
151       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
152     return;
153
154   /*
155     only pay attention to durations that are not grace notes.
156   */
157   if (!now_.grace_part_)
158     {
159       Stream_event *r = i.event_cause ();
160       if (r && r->in_event_class ("rhythmic-event"))
161         {
162           Moment len = get_event_length (r);
163           Rhythmic_tuple t (i, now_mom () + len);
164           now_durations_.push_back (t);
165         }
166     }
167 }
168
169 void
170 Spacing_engraver::stop_translation_timestep ()
171 {
172   Paper_column *musical_column
173     = dynamic_cast<Paper_column *> (unsmob_grob (get_property ("currentMusicalColumn")));
174
175
176   if (!spacing_)
177     start_spanner ();
178
179   musical_column->set_object ("spacing", spacing_->self_scm ());
180   unsmob_grob (get_property ("currentCommandColumn"))
181     ->set_object ("spacing", spacing_->self_scm ());
182   
183   SCM proportional = get_property ("proportionalNotationDuration");
184   if (unsmob_moment (proportional))
185     {
186       musical_column->set_property ("shortest-playing-duration", proportional);
187       musical_column->set_property ("shortest-starter-duration", proportional);
188       return;
189     }
190
191   Moment shortest_playing;
192   shortest_playing.set_infinite (1);
193   for (vsize i = 0; i < playing_durations_.size (); i++)
194     {
195       Stream_event *ev = playing_durations_[i].info_.event_cause ();
196       if (ev)
197         {
198           Moment m = get_event_length (ev);
199           shortest_playing = min (shortest_playing, m);
200         }
201     }
202   Moment starter;
203   starter.set_infinite (1);
204
205   for (vsize i = 0; i < now_durations_.size (); i++)
206     {
207       Moment m = get_event_length (now_durations_[i].info_.event_cause ());
208       if (m.to_bool ())
209         {
210           starter = min (starter, m);
211           playing_durations_.insert (now_durations_[i]);
212         }
213     }
214   now_durations_.clear ();
215
216   shortest_playing = min (shortest_playing, starter);
217
218   assert (starter.to_bool ());
219   SCM sh = shortest_playing.smobbed_copy ();
220   SCM st = starter.smobbed_copy ();
221
222   musical_column->set_property ("shortest-playing-duration", sh);
223   musical_column->set_property ("shortest-starter-duration", st);
224 }
225
226
227
228 void
229 Spacing_engraver::start_translation_timestep ()
230 {
231   start_section_ = 0;
232
233   now_ = now_mom ();
234   stopped_durations_.clear ();
235   
236   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
237     playing_durations_.delmin ();
238   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
239     stopped_durations_.push_back (playing_durations_.get ());
240 }
241
242 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
243 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
244 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
245
246 ADD_TRANSLATOR (Spacing_engraver,
247                 "make a SpacingSpanner and do "
248                 "bookkeeping of shortest starting and playing notes  ",
249
250                 /* create */ "SpacingSpanner",
251                 /* read */
252                 "currentMusicalColumn "
253                 "currentCommandColumn "
254                 "proportionalNotationDuration",
255                 
256                 /* write */ "");