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