]> git.donarmstrong.com Git - lilypond.git/blob - lily/figured-bass-engraver.cc
2f8004f84cfa65c713d6386ff504b8803dee3b04
[lilypond.git] / lily / figured-bass-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2012 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   DECLARE_TRANSLATOR_LISTENER (rest);
126   DECLARE_TRANSLATOR_LISTENER (bass_figure);
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 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
184 void
185 Figured_bass_engraver::listen_rest (Stream_event *)
186 {
187   have_rest_ = true;
188 }
189
190 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
191 void
192 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
193 {
194   new_event_found_ = true;
195   Moment stop = now_mom () + get_event_length (ev, now_mom ());
196   stop_moment_ = max (stop_moment_, stop);
197
198   // Handle no-continuation here, don't even add it to the already existing
199   // spanner... This fixes some layout issues (figure will be placed separately)
200   bool no_continuation = to_boolean (ev->get_property ("no-continuation"));
201   if (to_boolean (get_property ("useBassFigureExtenders")) && !no_continuation)
202     {
203       for (vsize i = 0; i < groups_.size (); i++)
204         {
205           if (!groups_[i].current_event_
206               && groups_[i].group_is_equal_to (ev))
207             {
208               groups_[i].current_event_ = ev;
209               continuation_ = true;
210               return;
211             }
212         }
213     }
214   new_events_.push_back (ev);
215 }
216
217 void
218 Figured_bass_engraver::center_continuations (vector<Spanner *> const &consecutive_lines)
219 {
220   vector<Grob *> left_figs;
221   for (vsize j = consecutive_lines.size (); j--;)
222     left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
223
224   SCM ga = Grob_array::make_array ();
225   unsmob_grob_array (ga)->set_array (left_figs);
226
227   for (vsize j = consecutive_lines.size (); j--;)
228     consecutive_lines[j]->set_object ("figures",
229                                       unsmob_grob_array (ga)->smobbed_copy ());
230 }
231
232 void
233 Figured_bass_engraver::center_repeated_continuations ()
234 {
235   vector<Spanner *> consecutive_lines;
236   for (vsize i = 0; i <= groups_.size (); i++)
237     {
238       if (i < groups_.size ()
239           && groups_[i].continuation_line_
240           && (consecutive_lines.empty ()
241               || (consecutive_lines[0]->get_bound (LEFT)->get_column ()
242                   == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
243                   && consecutive_lines[0]->get_bound (RIGHT)->get_column ()
244                   == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
245         consecutive_lines.push_back (groups_[i].continuation_line_);
246       else
247         {
248           center_continuations (consecutive_lines);
249           consecutive_lines.clear ();
250         }
251     }
252 }
253
254 void
255 Figured_bass_engraver::clear_spanners ()
256 {
257   if (!alignment_)
258     return;
259
260   announce_end_grob (alignment_, SCM_EOL);
261   alignment_ = 0;
262
263   if (to_boolean (get_property ("figuredBassCenterContinuations")))
264     center_repeated_continuations ();
265
266   for (vsize i = 0; i < groups_.size (); i++)
267     {
268       if (groups_[i].group_)
269         {
270           announce_end_grob (groups_[i].group_, SCM_EOL);
271           groups_[i].group_ = 0;
272         }
273
274       if (groups_[i].continuation_line_)
275         {
276           announce_end_grob (groups_[i].continuation_line_, SCM_EOL);
277           groups_[i].continuation_line_ = 0;
278         }
279     }
280
281   /* Check me, groups_.clear () ? */
282 }
283
284 void
285 Figured_bass_engraver::process_music ()
286 {
287   bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
288   if (alignment_ && !use_extenders)
289     clear_spanners ();
290
291   // If we have a rest, or we have no new or continued events, clear all spanners
292   bool ignore_rest = to_boolean (get_property ("ignoreFiguredBassRest"));
293   if ((ignore_rest && have_rest_)
294       || (!continuation_ && new_events_.empty ()))
295     {
296       clear_spanners ();
297       groups_.clear ();
298       return;
299     }
300
301   if (!new_event_found_)
302     return;
303
304   new_event_found_ = false;
305
306   /*
307     Don't need to sync alignments, if we're not using extenders.
308    */
309   if (!use_extenders)
310     {
311       clear_spanners ();
312     }
313
314   if (!continuation_)
315     {
316       clear_spanners ();
317       groups_.clear ();
318     }
319
320   vsize k = 0;
321   for (vsize i = 0; i < new_events_.size (); i++)
322     {
323       while (k < groups_.size ()
324              && groups_[k].current_event_)
325         k++;
326
327       if (k >= groups_.size ())
328         {
329           Figure_group group;
330           groups_.push_back (group);
331         }
332
333       groups_[k].reset_figure ();
334       groups_[k].current_event_ = new_events_[i];
335       groups_[k].figure_item_ = 0;
336       k++;
337     }
338
339   for (vsize i = 0; i < groups_.size (); i++)
340     {
341       if (!groups_[i].is_continuation ())
342         {
343           groups_[i].reset_figure ();
344         }
345     }
346
347   if (use_extenders)
348     {
349       vector<int> junk_continuations;
350       for (vsize i = 0; i < groups_.size (); i++)
351         {
352           Figure_group &group = groups_[i];
353
354           if (group.is_continuation ())
355             {
356               if (!group.continuation_line_)
357                 {
358                   Spanner *line
359                     = make_spanner ("BassFigureContinuation", SCM_EOL);
360                   Item *item = group.figure_item_;
361                   group.continuation_line_ = line;
362                   line->set_bound (LEFT, item);
363
364                   /*
365                     Don't add as child. This will cache the wrong
366                     (pre-break) stencil when callbacks are triggered.
367                   */
368                   line->set_parent (group.group_, Y_AXIS);
369                   Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
370
371                   group.figure_item_ = 0;
372                 }
373             }
374           else if (group.continuation_line_)
375             junk_continuations.push_back (i);
376         }
377
378       /*
379         Ugh, repeated code.
380        */
381       vector<Spanner *> consecutive;
382       if (to_boolean (get_property ("figuredBassCenterContinuations")))
383         {
384           for (vsize i = 0; i <= junk_continuations.size (); i++)
385             {
386               if (i < junk_continuations.size ()
387                   && (i == 0 || junk_continuations[i - 1] == junk_continuations[i] - 1))
388                 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
389               else
390                 {
391                   center_continuations (consecutive);
392                   consecutive.clear ();
393                   if (i < junk_continuations.size ())
394                     consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
395                 }
396             }
397         }
398       for (vsize i = 0; i < junk_continuations.size (); i++)
399         groups_[junk_continuations[i]].continuation_line_ = 0;
400     }
401
402   create_grobs ();
403   add_brackets ();
404 }
405
406 void
407 Figured_bass_engraver::create_grobs ()
408 {
409   Grob *muscol
410     = dynamic_cast<Item *> (unsmob_grob (get_property ("currentMusicalColumn")));
411   if (!alignment_)
412     {
413       alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
414       alignment_->set_bound (LEFT, muscol);
415     }
416   alignment_->set_bound (RIGHT, muscol);
417
418   SCM proc = get_property ("figuredBassFormatter");
419   for (vsize i = 0; i < groups_.size (); i++)
420     {
421       Figure_group &group = groups_[i];
422
423       if (group.current_event_)
424         {
425           Item *item
426             = make_item ("BassFigure", group.current_event_->self_scm ());
427           group.assign_from_event (group.current_event_, item);
428
429           if (!group.group_)
430             {
431               group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
432               group.group_->set_bound (LEFT, muscol);
433               Align_interface::add_element (alignment_, group.group_);
434             }
435
436           if (scm_memq (group.number_, get_property ("implicitBassFigures")) != SCM_BOOL_F)
437             {
438               item->set_property ("transparent", SCM_BOOL_T);
439               item->set_property ("implicit", SCM_BOOL_T);
440             }
441
442           SCM text = group.text_;
443           if (!Text_interface::is_markup (text)
444               && ly_is_procedure (proc))
445             {
446               text = scm_call_3 (proc, group.number_, group.current_event_->self_scm (),
447                                  context ()->self_scm ());
448             }
449
450           item->set_property ("text", text);
451
452           Axis_group_interface::add_element (group.group_, item);
453         }
454
455       if (group.continuation_line_)
456         {
457           /*
458             UGH should connect to the bass staff, and get the note heads.
459             For now, simply set the hidden figure to a default value to
460             ensure the extenders of different figures always end at the same
461             position, e.g. in <12 5> <12 5>
462           */
463           group.figure_item_->set_property ("transparent", SCM_BOOL_T);
464           group.figure_item_->set_property ("text", ly_string2scm ("0"));
465           group.continuation_line_->set_bound (RIGHT, group.figure_item_);
466         }
467
468       if (groups_[i].group_)
469         groups_[i].group_->set_bound (RIGHT, muscol);
470
471     }
472
473 }
474
475 void
476 Figured_bass_engraver::add_brackets ()
477 {
478   vector<Grob *> encompass;
479   bool inside = false;
480   for (vsize i = 0; i < groups_.size (); i++)
481     {
482       if (!groups_[i].current_event_)
483         continue;
484
485       if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))
486         inside = true;
487
488       if (inside && groups_[i].figure_item_)
489         encompass.push_back (groups_[i].figure_item_);
490
491       if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
492         {
493           inside = false;
494
495           Item *brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
496           for (vsize j = 0; j < encompass.size (); j++)
497             {
498               Pointer_group_interface::add_grob (brack,
499                                                  ly_symbol2scm ("elements"),
500                                                  encompass[j]);
501             }
502           encompass.clear ();
503         }
504     }
505 }
506
507 ADD_TRANSLATOR (Figured_bass_engraver,
508                 /* doc */
509                 "Make figured bass numbers.",
510
511                 /* create */
512                 "BassFigure "
513                 "BassFigureAlignment "
514                 "BassFigureBracket "
515                 "BassFigureContinuation "
516                 "BassFigureLine ",
517
518                 /* read */
519                 "figuredBassAlterationDirection "
520                 "figuredBassCenterContinuations "
521                 "figuredBassFormatter "
522                 "implicitBassFigures "
523                 "useBassFigureExtenders "
524                 "ignoreFiguredBassRest ",
525
526                 /* write */
527                 ""
528                );