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