]> git.donarmstrong.com Git - lilypond.git/blob - lily/figured-bass-engraver.cc
FiguredBass: Extract resetting a figure object to its own method
[lilypond.git] / lily / figured-bass-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2005--2010 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 };
95
96 struct Figured_bass_engraver : public Engraver
97 {
98   TRANSLATOR_DECLARATIONS (Figured_bass_engraver);
99   void clear_spanners ();
100   void add_brackets ();
101   void create_grobs ();
102
103   void center_continuations (vector<Spanner*> const &consecutive_lines);
104   void center_repeated_continuations ();
105 protected:
106   vector<Figure_group> groups_;
107   Spanner *alignment_;
108   vector<Stream_event *> new_events_;
109   bool continuation_;
110   bool new_event_found_;
111   
112   Moment stop_moment_;
113   Stream_event *rest_event_; 
114
115   DECLARE_TRANSLATOR_LISTENER (rest);
116   DECLARE_TRANSLATOR_LISTENER (bass_figure);
117
118   virtual void derived_mark () const; 
119
120   void start_translation_timestep ();
121   void stop_translation_timestep ();
122   void process_music ();
123 };
124
125 void
126 Figured_bass_engraver::derived_mark () const
127 {
128   for (vsize i = 0; i < groups_.size (); i++)
129     {
130       groups_[i].gc_mark ();
131     }
132 }
133
134 void
135 Figured_bass_engraver::stop_translation_timestep ()
136 {
137   if (groups_.empty ()
138       || now_mom ().main_part_ < stop_moment_.main_part_
139       || now_mom ().grace_part_ < Rational (0))
140     return ;
141   
142   bool found = false;
143   for (vsize i = 0; !found && i < groups_.size (); i++)
144     found  = found  || groups_[i].current_event_;
145
146   if (!found)
147     clear_spanners ();
148 }
149
150 Figured_bass_engraver::Figured_bass_engraver ()
151 {
152   alignment_ = 0;
153   continuation_ = false;
154   rest_event_ = 0;
155   new_event_found_ = false;
156 }
157
158 void
159 Figured_bass_engraver::start_translation_timestep ()
160 {
161   if (now_mom ().main_part_ < stop_moment_.main_part_
162       || now_mom ().grace_part_ < Rational (0))
163     return ;
164   
165   rest_event_ = 0;
166   new_events_.clear ();
167   for (vsize i = 0; i < groups_.size (); i++)
168     groups_[i].current_event_ = 0;
169
170   continuation_ = false;
171
172   
173 }
174
175 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
176 void
177 Figured_bass_engraver::listen_rest (Stream_event *ev)
178 {
179   if (to_boolean (get_property ("ignoreFiguredBassRest")))
180     {
181       new_event_found_ = true;
182
183       /*
184         No ASSIGN_EVENT_ONCE () ; otherwise we get warnings about
185         polyphonic rests.
186        */
187       rest_event_ = ev;
188     }
189 }
190
191 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
192 void
193 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
194 {
195   new_event_found_ = true;
196   Moment stop  = now_mom () + get_event_length (ev, now_mom ());
197   stop_moment_ = max (stop_moment_, stop);
198
199   // Handle no-continuation here, don't even add it to the already existing
200   // spanner... This fixes some layout issues (figure will be placed separately)
201   bool no_continuation = to_boolean (ev->get_property ("no-continuation"));
202   if (to_boolean (get_property ("useBassFigureExtenders")) && !no_continuation)
203     {
204       for (vsize i = 0; i < groups_.size (); i++)
205         {
206           if (!groups_[i].current_event_
207               && groups_[i].group_is_equal_to (ev))
208             {
209               groups_[i].current_event_ = ev;
210               continuation_ = true;
211               return;
212             }
213         }
214     }
215   new_events_.push_back (ev);
216 }
217
218 void
219 Figured_bass_engraver::center_continuations (vector<Spanner*> const &consecutive_lines)
220 {
221   if (consecutive_lines.size () == 2)
222     {
223       vector<Grob*> left_figs;
224       for (vsize j = consecutive_lines.size (); j--;)
225         left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
226
227       SCM  ga = Grob_array::make_array ();
228       unsmob_grob_array (ga)->set_array (left_figs);
229
230       for (vsize j = consecutive_lines.size (); j--;)
231         consecutive_lines[j]->set_object ("figures",
232                                           unsmob_grob_array (ga)->smobbed_copy ());
233     }
234 }
235
236 void
237 Figured_bass_engraver::center_repeated_continuations ()
238 {  
239   vector<Spanner*> consecutive_lines;
240   for (vsize i = 0; i <= groups_.size (); i++)
241     {
242       if (i < groups_.size ()
243           && groups_[i].continuation_line_
244           && (consecutive_lines.empty ()
245               || (consecutive_lines[0]->get_bound (LEFT)->get_column ()
246                   == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
247                   && consecutive_lines[0]->get_bound (RIGHT)->get_column ()
248                   == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
249         consecutive_lines.push_back (groups_[i].continuation_line_);      
250       else 
251         {
252           center_continuations (consecutive_lines);
253           consecutive_lines.clear ();
254         }
255     }
256 }
257
258 void
259 Figured_bass_engraver::clear_spanners ()
260 {
261   if (!alignment_)
262     return;
263
264   if (alignment_)
265     {
266       announce_end_grob (alignment_, SCM_EOL);
267       alignment_ = 0;
268     }
269
270   if (to_boolean (get_property ("figuredBassCenterContinuations")))
271     center_repeated_continuations ();
272   
273   for (vsize i = 0; i < groups_.size (); i++)
274     {
275       if (groups_[i].group_)
276         {
277           announce_end_grob (groups_[i].group_ , SCM_EOL);
278           groups_[i].group_ = 0;
279         }
280       
281       if (groups_[i].continuation_line_)
282         {
283           announce_end_grob (groups_[i].continuation_line_ , SCM_EOL);
284           groups_[i].continuation_line_ = 0;
285         }
286     }
287
288   /* Check me, groups_.clear () ? */
289 }
290
291 void
292 Figured_bass_engraver::add_brackets ()
293 {
294   vector<Grob*> encompass;
295   bool inside = false;
296   for (vsize i = 0; i < groups_.size (); i ++)
297     {
298       if (!groups_[i].current_event_)
299         continue;
300       
301       if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))       
302         inside = true;
303
304       if (inside && groups_[i].figure_item_)
305         encompass.push_back (groups_[i].figure_item_);
306
307        if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
308         {
309           inside = false;
310
311           Item * brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
312           for (vsize j = 0; j < encompass.size (); j++)
313             {
314               Pointer_group_interface::add_grob (brack,
315                                                  ly_symbol2scm ("elements"),
316                                                  encompass[j]);
317             }
318           encompass.clear ();
319         }
320     }
321 }
322
323 void
324 Figured_bass_engraver::process_music ()
325 {
326   bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
327   if (alignment_ && !use_extenders)
328     clear_spanners ();
329         
330   if (rest_event_)
331     {
332       clear_spanners ();
333       groups_.clear ();
334       return;
335     }
336   
337   if (!continuation_
338       && new_events_.empty ())
339     {
340       clear_spanners ();
341       groups_.clear ();
342       return;
343     }
344
345   if (!new_event_found_)
346     return;
347   
348   new_event_found_ = false;
349
350   /*
351     Don't need to sync alignments, if we're not using extenders. 
352    */
353   if (!use_extenders)
354     {
355       clear_spanners ();
356     }
357   
358   if (!continuation_)
359     {
360       clear_spanners ();
361       groups_.clear ();
362     }
363
364   vsize k = 0;
365   for (vsize i = 0; i < new_events_.size (); i++)
366     {
367       while (k < groups_.size ()
368              && groups_[k].current_event_)
369         k++;
370       
371       if (k >= groups_.size ())
372         {
373           Figure_group group;
374           groups_.push_back (group);
375         }
376       
377       groups_[k].current_event_ = new_events_[i];
378       groups_[k].figure_item_ = 0;
379       k++;
380     }
381
382   for (vsize i = 0; i < groups_.size (); i++)
383     {
384       if (!groups_[i].is_continuation ())
385         {
386           groups_[i].reset_figure ();
387         }
388     }
389
390   if (use_extenders)
391     {
392       vector<int> junk_continuations;
393       for (vsize i = 0; i < groups_.size (); i++)
394         {
395           Figure_group &group = groups_[i];
396
397           if (group.is_continuation ())
398             {
399               if (!group.continuation_line_)
400                 {
401                   Spanner * line
402                     = make_spanner ("BassFigureContinuation", SCM_EOL);
403                   Item * item = group.figure_item_;
404                   group.continuation_line_ = line;
405                   line->set_bound (LEFT, item);
406
407                   /*
408                     Don't add as child. This will cache the wrong
409                     (pre-break) stencil when callbacks are triggered.
410                   */
411                   line->set_parent (group.group_, Y_AXIS);
412                   Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
413
414                   group.figure_item_ = 0;
415                 }
416             }
417           else if (group.continuation_line_) 
418             junk_continuations.push_back (i); 
419         }
420
421       /*
422         Ugh, repeated code.
423        */
424       vector<Spanner*> consecutive;
425       if (to_boolean (get_property ("figuredBassCenterContinuations")))
426         {
427           for (vsize i = 0; i <= junk_continuations.size (); i++)
428             {
429               if (i < junk_continuations.size ()
430                   && (i == 0 || junk_continuations[i-1] == junk_continuations[i] - 1))
431                 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
432               else 
433                 {
434                   center_continuations (consecutive);
435                   consecutive.clear ();
436                   if (i < junk_continuations.size ())
437                     consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
438                 }
439             }
440         }
441       for (vsize i = 0; i < junk_continuations.size (); i++)
442         groups_[junk_continuations[i]].continuation_line_ = 0;
443     }
444   
445   create_grobs ();
446   add_brackets ();
447 }
448
449 void
450 Figured_bass_engraver::create_grobs () 
451 {
452   Grob *muscol
453     = dynamic_cast<Item*> (unsmob_grob (get_property ("currentMusicalColumn")));
454   if (!alignment_)
455     {
456       alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
457       alignment_->set_bound (LEFT, muscol);
458     }
459   alignment_->set_bound (RIGHT, muscol);
460
461   SCM proc = get_property ("figuredBassFormatter");
462   for (vsize i = 0; i < groups_.size (); i++)
463     {
464       Figure_group &group = groups_[i];
465       
466       if (group.current_event_)
467         {
468           Item *item
469             = make_item ("BassFigure",
470                          group.current_event_->self_scm ());
471
472           
473           SCM fig = group.current_event_->get_property ("figure");
474           if (!group.group_)
475             {
476               group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
477               group.group_->set_bound (LEFT, muscol);
478               Align_interface::add_element (alignment_,
479                                             group.group_);
480             }
481
482           if (scm_memq (fig, get_property ("implicitBassFigures")) != SCM_BOOL_F)
483             {
484               item->set_property ("transparent", SCM_BOOL_T); 
485               item->set_property ("implicit", SCM_BOOL_T);
486             }
487           
488           group.number_ = fig;
489           group.alteration_ = group.current_event_->get_property ("alteration");
490           group.augmented_ = group.current_event_->get_property ("augmented");
491           group.diminished_ = group.current_event_->get_property ("diminished");
492           group.augmented_slash_ = group.current_event_->get_property ("augmented-slash");
493           group.text_ = group.current_event_->get_property ("text");
494
495           SCM text = group.text_;
496           if (!Text_interface::is_markup (text)
497               && ly_is_procedure (proc))
498             {
499               text = scm_call_3 (proc, fig, group.current_event_->self_scm (),
500                                  context ()->self_scm ());
501             }
502
503           item->set_property ("text", text);
504           
505           Axis_group_interface::add_element (group.group_, item);
506           group.figure_item_ = item;
507         }
508
509       if (group.continuation_line_)
510         {
511           /*
512             UGH should connect to the bass staff, and get the note heads. 
513           */
514           group.figure_item_->set_property ("transparent", SCM_BOOL_T);
515           group.continuation_line_->set_bound (RIGHT, group.figure_item_);
516         }
517       
518       if (groups_[i].group_)
519         groups_[i].group_->set_bound (RIGHT, muscol);
520
521     }
522
523 }
524
525 ADD_TRANSLATOR (Figured_bass_engraver,
526                 /* doc */
527                 "Make figured bass numbers.",
528
529                 /* create */
530                 "BassFigure "
531                 "BassFigureAlignment "
532                 "BassFigureBracket "
533                 "BassFigureContinuation "
534                 "BassFigureLine ",
535
536                 /* read */
537                 "figuredBassAlterationDirection "
538                 "figuredBassCenterContinuations "
539                 "figuredBassFormatter "
540                 "implicitBassFigures "
541                 "useBassFigureExtenders "
542                 "ignoreFiguredBassRest ",
543
544                 /* write */
545                 ""
546                 );