]> git.donarmstrong.com Git - lilypond.git/blob - lily/spacing-engraver.cc
Revert "Issue 4550 (2/2) Avoid "using namespace std;" in included files"
[lilypond.git] / lily / spacing-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21 #include "moment.hh"
22 #include "note-spacing.hh"
23 #include "paper-column.hh"
24 #include "pointer-group-interface.hh"
25 #include "pqueue.hh"
26 #include "spanner.hh"
27 #include "staff-spacing.hh"
28 #include "stream-event.hh"
29
30 #include "translator.icc"
31
32 using std::vector;
33
34 struct Rhythmic_tuple
35 {
36   Grob_info info_;
37   Moment end_;
38
39   Rhythmic_tuple ()
40   {
41   }
42   Rhythmic_tuple (Grob_info i, Moment m)
43   {
44     info_ = i;
45     end_ = m;
46   }
47   static int time_compare (Rhythmic_tuple const &, Rhythmic_tuple const &);
48 };
49
50 inline int
51 compare (Rhythmic_tuple const &a, Rhythmic_tuple const &b)
52 {
53   return Rhythmic_tuple::time_compare (a, b);
54 }
55
56 int
57 Rhythmic_tuple::time_compare (Rhythmic_tuple const &h1,
58                               Rhythmic_tuple const &h2)
59 {
60   return (h1.end_ - h2.end_).main_part_.sign ();
61 }
62
63 /****************************************************************/
64
65 /*
66   Acknowledge rhythmic elements, for initializing spacing fields in
67   the columns.
68 */
69 class Spacing_engraver : public Engraver
70 {
71   PQueue<Rhythmic_tuple> playing_durations_;
72   vector<Rhythmic_tuple> now_durations_;
73   vector<Rhythmic_tuple> stopped_durations_;
74   Moment now_;
75   Spanner *spacing_;
76   Stream_event *start_section_;
77
78   TRANSLATOR_DECLARATIONS (Spacing_engraver);
79
80 protected:
81   DECLARE_ACKNOWLEDGER (staff_spacing);
82   DECLARE_ACKNOWLEDGER (note_spacing);
83   DECLARE_ACKNOWLEDGER (rhythmic_head);
84   DECLARE_ACKNOWLEDGER (rhythmic_grob);
85   DECLARE_TRANSLATOR_LISTENER (spacing_section);
86
87   void start_translation_timestep ();
88   void stop_translation_timestep ();
89   void process_music ();
90   void add_starter_duration (Grob_info i);
91
92   virtual void finalize ();
93
94   void start_spanner ();
95   void stop_spanner ();
96 };
97
98 Spacing_engraver::Spacing_engraver ()
99 {
100   spacing_ = 0;
101   start_section_ = 0;
102 }
103
104 IMPLEMENT_TRANSLATOR_LISTENER (Spacing_engraver, spacing_section);
105 void
106 Spacing_engraver::listen_spacing_section (Stream_event *ev)
107 {
108   ASSIGN_EVENT_ONCE (start_section_, ev);
109 }
110
111 void
112 Spacing_engraver::process_music ()
113 {
114   if (start_section_ && spacing_)
115     stop_spanner ();
116
117   if (!spacing_)
118     start_spanner ();
119 }
120
121 void
122 Spacing_engraver::start_spanner ()
123 {
124   assert (!spacing_);
125
126   spacing_ = make_spanner ("SpacingSpanner", SCM_EOL);
127   spacing_->set_bound (LEFT,
128                        unsmob<Grob> (get_property ("currentCommandColumn")));
129 }
130
131 void
132 Spacing_engraver::finalize ()
133 {
134   stop_spanner ();
135 }
136
137 void
138 Spacing_engraver::stop_spanner ()
139 {
140   if (spacing_)
141     {
142       Grob *p = unsmob<Grob> (get_property ("currentCommandColumn"));
143
144       spacing_->set_bound (RIGHT, p);
145       spacing_ = 0;
146     }
147 }
148
149 void
150 Spacing_engraver::acknowledge_note_spacing (Grob_info i)
151 {
152   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
153 }
154
155 void
156 Spacing_engraver::acknowledge_staff_spacing (Grob_info i)
157 {
158   Pointer_group_interface::add_grob (spacing_, ly_symbol2scm ("wishes"), i.grob ());
159 }
160
161 void
162 Spacing_engraver::acknowledge_rhythmic_grob (Grob_info i)
163 {
164   add_starter_duration (i);
165 }
166
167 void
168 Spacing_engraver::acknowledge_rhythmic_head (Grob_info i)
169 {
170   add_starter_duration (i);
171 }
172
173 void
174 Spacing_engraver::add_starter_duration (Grob_info i)
175 {
176   if (i.grob ()->internal_has_interface (ly_symbol2scm ("lyric-syllable-interface"))
177       || i.grob ()->internal_has_interface (ly_symbol2scm ("multi-measure-interface")))
178     return;
179
180   /*
181     only pay attention to durations that are not grace notes.
182   */
183   if (!now_.grace_part_)
184     {
185       Stream_event *r = i.event_cause ();
186       if (r && r->in_event_class ("rhythmic-event"))
187         {
188           Moment len = get_event_length (r, now_);
189           Rhythmic_tuple t (i, now_mom () + len);
190           now_durations_.push_back (t);
191         }
192     }
193 }
194
195 void
196 Spacing_engraver::stop_translation_timestep ()
197 {
198   Paper_column *musical_column
199     = unsmob<Paper_column> (get_property ("currentMusicalColumn"));
200
201   if (!spacing_)
202     start_spanner ();
203
204   musical_column->set_object ("spacing", spacing_->self_scm ());
205   unsmob<Grob> (get_property ("currentCommandColumn"))
206   ->set_object ("spacing", spacing_->self_scm ());
207
208   SCM proportional = get_property ("proportionalNotationDuration");
209   if (unsmob<Moment> (proportional))
210     {
211       musical_column->set_property ("shortest-playing-duration", proportional);
212       musical_column->set_property ("shortest-starter-duration", proportional);
213       musical_column->set_property ("used", SCM_BOOL_T);
214       return;
215     }
216
217   Moment shortest_playing;
218   shortest_playing.set_infinite (1);
219   for (vsize i = 0; i < playing_durations_.size (); i++)
220     {
221       Stream_event *ev = playing_durations_[i].info_.event_cause ();
222       if (ev)
223         {
224           Moment m = get_event_length (ev);
225           shortest_playing = min (shortest_playing, m);
226         }
227     }
228   Moment starter;
229   starter.set_infinite (1);
230
231   for (vsize i = 0; i < now_durations_.size (); i++)
232     {
233       Moment m = get_event_length (now_durations_[i].info_.event_cause ());
234       if (m.to_bool ())
235         {
236           starter = min (starter, m);
237           playing_durations_.insert (now_durations_[i]);
238         }
239     }
240   now_durations_.clear ();
241
242   shortest_playing = min (shortest_playing, starter);
243
244   assert (starter.to_bool ());
245   SCM sh = shortest_playing.smobbed_copy ();
246   SCM st = starter.smobbed_copy ();
247
248   musical_column->set_property ("shortest-playing-duration", sh);
249   musical_column->set_property ("shortest-starter-duration", st);
250 }
251
252 void
253 Spacing_engraver::start_translation_timestep ()
254 {
255   start_section_ = 0;
256
257   now_ = now_mom ();
258   stopped_durations_.clear ();
259
260   while (playing_durations_.size () && playing_durations_.front ().end_ < now_)
261     playing_durations_.delmin ();
262   while (playing_durations_.size () && playing_durations_.front ().end_ == now_)
263     stopped_durations_.push_back (playing_durations_.get ());
264 }
265
266 ADD_ACKNOWLEDGER (Spacing_engraver, staff_spacing);
267 ADD_ACKNOWLEDGER (Spacing_engraver, note_spacing);
268 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_head);
269 ADD_ACKNOWLEDGER (Spacing_engraver, rhythmic_grob);
270
271 ADD_TRANSLATOR (Spacing_engraver,
272                 /* doc */
273                 "Make a @code{SpacingSpanner} and do bookkeeping of shortest"
274                 " starting and playing notes.",
275
276                 /* create */
277                 "SpacingSpanner ",
278
279                 /* read */
280                 "currentMusicalColumn "
281                 "currentCommandColumn "
282                 "proportionalNotationDuration ",
283
284                 /* write */
285                 ""
286                );