]> git.donarmstrong.com Git - lilypond.git/blob - lily/figured-bass-engraver.cc
Issue 4957: parser.yy: loc_on_music -> loc_on_copy
[lilypond.git] / lily / figured-bass-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "engraver.hh"
22
23 #include "align-interface.hh"
24 #include "axis-group-interface.hh"
25 #include "context.hh"
26 #include "grob-array.hh"
27 #include "item.hh"
28 #include "pointer-group-interface.hh"
29 #include "spanner.hh"
30 #include "stream-event.hh"
31 #include "text-interface.hh"
32
33 #include "translator.icc"
34
35 struct Figure_group
36 {
37   Spanner *group_;
38   Spanner *continuation_line_;
39
40   SCM number_;
41   SCM alteration_;
42   SCM augmented_;
43   SCM diminished_;
44   SCM augmented_slash_;
45   SCM text_;
46
47   Item *figure_item_;
48   Stream_event *current_event_;
49
50   Figure_group ()
51   {
52     figure_item_ = 0;
53     continuation_line_ = 0;
54     reset_figure ();
55     group_ = 0;
56     current_event_ = 0;
57   }
58   /* Reset (or init) all figure information to FALSE */
59   void reset_figure ()
60   {
61     number_ = SCM_BOOL_F;
62     alteration_ = SCM_BOOL_F;
63     augmented_ = SCM_BOOL_F;
64     diminished_ = SCM_BOOL_F;
65     augmented_slash_ = SCM_BOOL_F;
66     text_ = SCM_BOOL_F;
67   }
68   /* Mark the members of the struct as used for the GUILE Garbage Collection */
69   void gc_mark () const
70   {
71     scm_gc_mark (number_);
72     scm_gc_mark (alteration_);
73     scm_gc_mark (augmented_);
74     scm_gc_mark (diminished_);
75     scm_gc_mark (augmented_slash_);
76     scm_gc_mark (text_);
77   }
78   bool group_is_equal_to (Stream_event *evt) const
79   {
80     return
81       ly_is_equal (number_, evt->get_property ("figure"))
82       && ly_is_equal (alteration_, evt->get_property ("alteration"))
83       && ly_is_equal (augmented_, evt->get_property ("augmented"))
84       && ly_is_equal (diminished_, evt->get_property ("diminished"))
85       && ly_is_equal (augmented_slash_, evt->get_property ("augmented-slash"))
86       && ly_is_equal (text_, evt->get_property ("text"));
87   }
88   bool is_continuation () const
89   {
90     return
91       current_event_
92       && group_is_equal_to (current_event_);
93   }
94   void assign_from_event (Stream_event *currevt, Item *item)
95   {
96     number_ = current_event_->get_property ("figure");
97     alteration_ = currevt->get_property ("alteration");
98     augmented_ = currevt->get_property ("augmented");
99     diminished_ = currevt->get_property ("diminished");
100     augmented_slash_ = currevt->get_property ("augmented-slash");
101     text_ = currevt->get_property ("text");
102     figure_item_ = item;
103   }
104 };
105
106 struct Figured_bass_engraver : public Engraver
107 {
108   TRANSLATOR_DECLARATIONS (Figured_bass_engraver);
109   void clear_spanners ();
110   void add_brackets ();
111   void create_grobs ();
112
113   void center_continuations (vector<Spanner *> const &consecutive_lines);
114   void center_repeated_continuations ();
115 protected:
116   vector<Figure_group> groups_;
117   Spanner *alignment_;
118   vector<Stream_event *> new_events_;
119   bool continuation_;
120   bool new_event_found_;
121
122   Moment stop_moment_;
123   bool have_rest_;
124
125   void listen_rest (Stream_event *);
126   void listen_bass_figure (Stream_event *);
127
128   virtual void derived_mark () const;
129
130   void start_translation_timestep ();
131   void stop_translation_timestep ();
132   void process_music ();
133 };
134
135 Figured_bass_engraver::Figured_bass_engraver ()
136 {
137   alignment_ = 0;
138   continuation_ = false;
139   have_rest_ = 0;
140   new_event_found_ = false;
141 }
142
143 void
144 Figured_bass_engraver::derived_mark () const
145 {
146   for (vsize i = 0; i < groups_.size (); i++)
147     {
148       groups_[i].gc_mark ();
149     }
150 }
151
152 void
153 Figured_bass_engraver::start_translation_timestep ()
154 {
155   if (now_mom ().main_part_ < stop_moment_.main_part_
156       || now_mom ().grace_part_ < Rational (0))
157     return;
158
159   have_rest_ = 0;
160   new_events_.clear ();
161   for (vsize i = 0; i < groups_.size (); i++)
162     groups_[i].current_event_ = 0;
163
164   continuation_ = false;
165 }
166
167 void
168 Figured_bass_engraver::stop_translation_timestep ()
169 {
170   if (groups_.empty ()
171       || now_mom ().main_part_ < stop_moment_.main_part_
172       || now_mom ().grace_part_ < Rational (0))
173     return;
174
175   bool found = false;
176   for (vsize i = 0; !found && i < groups_.size (); i++)
177     found = found || groups_[i].current_event_;
178
179   if (!found)
180     clear_spanners ();
181 }
182
183 void
184 Figured_bass_engraver::listen_rest (Stream_event *)
185 {
186   have_rest_ = true;
187 }
188
189 void
190 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
191 {
192   new_event_found_ = true;
193   Moment stop = now_mom () + get_event_length (ev, now_mom ());
194   stop_moment_ = max (stop_moment_, stop);
195
196   // Handle no-continuation here, don't even add it to the already existing
197   // spanner... This fixes some layout issues (figure will be placed separately)
198   bool no_continuation = to_boolean (ev->get_property ("no-continuation"));
199   if (to_boolean (get_property ("useBassFigureExtenders")) && !no_continuation)
200     {
201       for (vsize i = 0; i < groups_.size (); i++)
202         {
203           if (!groups_[i].current_event_
204               && groups_[i].group_is_equal_to (ev))
205             {
206               groups_[i].current_event_ = ev;
207               continuation_ = true;
208               return;
209             }
210         }
211     }
212   new_events_.push_back (ev);
213 }
214
215 void
216 Figured_bass_engraver::center_continuations (vector<Spanner *> const &consecutive_lines)
217 {
218   vector<Grob *> left_figs;
219   for (vsize j = consecutive_lines.size (); j--;)
220     left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
221
222   SCM ga = Grob_array::make_array ();
223   unsmob<Grob_array> (ga)->set_array (left_figs);
224
225   for (vsize j = consecutive_lines.size (); j--;)
226     consecutive_lines[j]->set_object ("figures",
227                                       unsmob<Grob_array> (ga)->smobbed_copy ());
228 }
229
230 void
231 Figured_bass_engraver::center_repeated_continuations ()
232 {
233   vector<Spanner *> consecutive_lines;
234   for (vsize i = 0; i <= groups_.size (); i++)
235     {
236       if (i < groups_.size ()
237           && groups_[i].continuation_line_
238           && (consecutive_lines.empty ()
239               || (consecutive_lines[0]->get_bound (LEFT)->get_column ()
240                   == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
241                   && consecutive_lines[0]->get_bound (RIGHT)->get_column ()
242                   == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
243         consecutive_lines.push_back (groups_[i].continuation_line_);
244       else
245         {
246           center_continuations (consecutive_lines);
247           consecutive_lines.clear ();
248         }
249     }
250 }
251
252 void
253 Figured_bass_engraver::clear_spanners ()
254 {
255   if (!alignment_)
256     return;
257
258   announce_end_grob (alignment_, SCM_EOL);
259   alignment_ = 0;
260
261   if (to_boolean (get_property ("figuredBassCenterContinuations")))
262     center_repeated_continuations ();
263
264   for (vsize i = 0; i < groups_.size (); i++)
265     {
266       if (groups_[i].group_)
267         {
268           announce_end_grob (groups_[i].group_, SCM_EOL);
269           groups_[i].group_ = 0;
270         }
271
272       if (groups_[i].continuation_line_)
273         {
274           announce_end_grob (groups_[i].continuation_line_, SCM_EOL);
275           groups_[i].continuation_line_ = 0;
276         }
277     }
278
279   /* Check me, groups_.clear () ? */
280 }
281
282 void
283 Figured_bass_engraver::process_music ()
284 {
285   bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
286   if (alignment_ && !use_extenders)
287     clear_spanners ();
288
289   // If we have a rest, or we have no new or continued events, clear all spanners
290   bool ignore_rest = to_boolean (get_property ("ignoreFiguredBassRest"));
291   if ((ignore_rest && have_rest_)
292       || (!continuation_ && new_events_.empty ()))
293     {
294       clear_spanners ();
295       groups_.clear ();
296       return;
297     }
298
299   if (!new_event_found_)
300     return;
301
302   new_event_found_ = false;
303
304   /*
305     Don't need to sync alignments, if we're not using extenders.
306    */
307   if (!use_extenders)
308     {
309       clear_spanners ();
310     }
311
312   if (!continuation_)
313     {
314       clear_spanners ();
315       groups_.clear ();
316     }
317
318   vsize k = 0;
319   for (vsize i = 0; i < new_events_.size (); i++)
320     {
321       while (k < groups_.size ()
322              && groups_[k].current_event_)
323         k++;
324
325       if (k >= groups_.size ())
326         {
327           Figure_group group;
328           groups_.push_back (group);
329         }
330
331       groups_[k].reset_figure ();
332       groups_[k].current_event_ = new_events_[i];
333       groups_[k].figure_item_ = 0;
334       k++;
335     }
336
337   for (vsize i = 0; i < groups_.size (); i++)
338     {
339       if (!groups_[i].is_continuation ())
340         {
341           groups_[i].reset_figure ();
342         }
343     }
344
345   if (use_extenders)
346     {
347       vector<int> junk_continuations;
348       for (vsize i = 0; i < groups_.size (); i++)
349         {
350           Figure_group &group = groups_[i];
351
352           if (group.is_continuation ())
353             {
354               if (!group.continuation_line_)
355                 {
356                   Spanner *line
357                     = make_spanner ("BassFigureContinuation", SCM_EOL);
358                   Item *item = group.figure_item_;
359                   group.continuation_line_ = line;
360                   line->set_bound (LEFT, item);
361
362                   /*
363                     Don't add as child. This will cache the wrong
364                     (pre-break) stencil when callbacks are triggered.
365                   */
366                   line->set_parent (group.group_, Y_AXIS);
367                   Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
368
369                   group.figure_item_ = 0;
370                 }
371             }
372           else if (group.continuation_line_)
373             junk_continuations.push_back (i);
374         }
375
376       /*
377         Ugh, repeated code.
378        */
379       vector<Spanner *> consecutive;
380       if (to_boolean (get_property ("figuredBassCenterContinuations")))
381         {
382           for (vsize i = 0; i <= junk_continuations.size (); i++)
383             {
384               if (i < junk_continuations.size ()
385                   && (i == 0 || junk_continuations[i - 1] == junk_continuations[i] - 1))
386                 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
387               else
388                 {
389                   center_continuations (consecutive);
390                   consecutive.clear ();
391                   if (i < junk_continuations.size ())
392                     consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
393                 }
394             }
395         }
396       for (vsize i = 0; i < junk_continuations.size (); i++)
397         groups_[junk_continuations[i]].continuation_line_ = 0;
398     }
399
400   create_grobs ();
401   add_brackets ();
402 }
403
404 void
405 Figured_bass_engraver::create_grobs ()
406 {
407   Grob *muscol
408     = unsmob<Item> (get_property ("currentMusicalColumn"));
409   if (!alignment_)
410     {
411       alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
412       alignment_->set_bound (LEFT, muscol);
413     }
414   alignment_->set_bound (RIGHT, muscol);
415
416   SCM proc = get_property ("figuredBassFormatter");
417   for (vsize i = 0; i < groups_.size (); i++)
418     {
419       Figure_group &group = groups_[i];
420
421       if (group.current_event_)
422         {
423           Item *item
424             = make_item ("BassFigure", group.current_event_->self_scm ());
425           group.assign_from_event (group.current_event_, item);
426
427           if (!group.group_)
428             {
429               group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
430               group.group_->set_bound (LEFT, muscol);
431               Align_interface::add_element (alignment_, group.group_);
432             }
433
434           if (scm_is_true (scm_memq (group.number_, get_property ("implicitBassFigures"))))
435             {
436               item->set_property ("transparent", SCM_BOOL_T);
437               item->set_property ("implicit", SCM_BOOL_T);
438             }
439
440           SCM text = group.text_;
441           if (!Text_interface::is_markup (text)
442               && ly_is_procedure (proc))
443             {
444               text = scm_call_3 (proc, group.number_, group.current_event_->self_scm (),
445                                  context ()->self_scm ());
446             }
447
448           item->set_property ("text", text);
449
450           Axis_group_interface::add_element (group.group_, item);
451         }
452
453       if (group.continuation_line_)
454         {
455           /*
456             UGH should connect to the bass staff, and get the note heads.
457             For now, simply set the hidden figure to a default value to
458             ensure the extenders of different figures always end at the same
459             position, e.g. in <12 5> <12 5>
460           */
461           group.figure_item_->set_property ("transparent", SCM_BOOL_T);
462           group.figure_item_->set_property ("text", ly_string2scm ("0"));
463           group.continuation_line_->set_bound (RIGHT, group.figure_item_);
464         }
465
466       if (groups_[i].group_)
467         groups_[i].group_->set_bound (RIGHT, muscol);
468
469     }
470
471 }
472
473 void
474 Figured_bass_engraver::add_brackets ()
475 {
476   vector<Grob *> encompass;
477   bool inside = false;
478   for (vsize i = 0; i < groups_.size (); i++)
479     {
480       if (!groups_[i].current_event_)
481         continue;
482
483       if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))
484         inside = true;
485
486       if (inside && groups_[i].figure_item_)
487         encompass.push_back (groups_[i].figure_item_);
488
489       if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
490         {
491           inside = false;
492
493           Item *brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
494           for (vsize j = 0; j < encompass.size (); j++)
495             {
496               Pointer_group_interface::add_grob (brack,
497                                                  ly_symbol2scm ("elements"),
498                                                  encompass[j]);
499             }
500           encompass.clear ();
501         }
502     }
503 }
504
505 void
506 Figured_bass_engraver::boot ()
507 {
508   ADD_LISTENER (Figured_bass_engraver, rest);
509   ADD_LISTENER (Figured_bass_engraver, bass_figure);
510 }
511
512 ADD_TRANSLATOR (Figured_bass_engraver,
513                 /* doc */
514                 "Make figured bass numbers.",
515
516                 /* create */
517                 "BassFigure "
518                 "BassFigureAlignment "
519                 "BassFigureBracket "
520                 "BassFigureContinuation "
521                 "BassFigureLine ",
522
523                 /* read */
524                 "figuredBassAlterationDirection "
525                 "figuredBassCenterContinuations "
526                 "figuredBassFormatter "
527                 "implicitBassFigures "
528                 "useBassFigureExtenders "
529                 "ignoreFiguredBassRest ",
530
531                 /* write */
532                 ""
533                );