]> git.donarmstrong.com Git - lilypond.git/blob - lily/figured-bass-engraver.cc
Merge branch 'master' into lilypond/translation
[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   bool force_no_continuation_;
50   
51   Figure_group ()
52   {
53     figure_item_ = 0;
54     force_no_continuation_ = false;
55     continuation_line_ = 0;
56     number_ = SCM_EOL;
57     alteration_ = SCM_EOL;
58     augmented_ = SCM_EOL;
59     diminished_ = SCM_EOL;
60     augmented_slash_ = SCM_EOL;
61     text_ = SCM_EOL;
62     group_ = 0;
63     current_event_ = 0;
64   }
65   /* Mark the members of the struct as used for the GUILE Garbage Collection */
66   void gc_mark () const
67   {
68     scm_gc_mark (number_);
69     scm_gc_mark (alteration_);
70     scm_gc_mark (augmented_);
71     scm_gc_mark (diminished_);
72     scm_gc_mark (augmented_slash_);
73     scm_gc_mark (text_);
74   }
75   bool group_is_equal_to (Stream_event *evt) const
76   {
77     return
78       ly_is_equal (number_, evt->get_property ("figure"))
79       && ly_is_equal (alteration_, evt->get_property ("alteration"))
80       && ly_is_equal (augmented_, evt->get_property ("augmented"))
81       && ly_is_equal (diminished_, evt->get_property ("diminished"))
82       && ly_is_equal (augmented_slash_, evt->get_property ("augmented-slash"))
83       && ly_is_equal (text_, evt->get_property ("text"));
84   }
85   bool is_continuation () const
86   {
87     return
88       current_event_
89       && !force_no_continuation_
90       && group_is_equal_to (current_event_);
91   }
92 };
93
94 struct Figured_bass_engraver : public Engraver
95 {
96   TRANSLATOR_DECLARATIONS (Figured_bass_engraver);
97   void clear_spanners ();
98   void add_brackets ();
99   void create_grobs ();
100
101   void center_continuations (vector<Spanner*> const &consecutive_lines);
102   void center_repeated_continuations ();
103 protected:
104   vector<Figure_group> groups_;
105   Spanner *alignment_;
106   vector<Stream_event *> new_events_;
107   bool continuation_;
108   bool new_event_found_;
109   
110   Moment stop_moment_;
111   Stream_event *rest_event_; 
112
113   DECLARE_TRANSLATOR_LISTENER (rest);
114   DECLARE_TRANSLATOR_LISTENER (bass_figure);
115
116   virtual void derived_mark () const; 
117
118   void start_translation_timestep ();
119   void stop_translation_timestep ();
120   void process_music ();
121 };
122
123 void
124 Figured_bass_engraver::derived_mark () const
125 {
126   for (vsize i = 0; i < groups_.size (); i++)
127     {
128       groups_[i].gc_mark ();
129     }
130 }
131
132 void
133 Figured_bass_engraver::stop_translation_timestep ()
134 {
135   if (groups_.empty ()
136       || now_mom ().main_part_ < stop_moment_.main_part_
137       || now_mom ().grace_part_ < Rational (0))
138     return ;
139   
140   bool found = false;
141   for (vsize i = 0; !found && i < groups_.size (); i++)
142     found  = found  || groups_[i].current_event_;
143
144   if (!found)
145     clear_spanners ();
146 }
147
148 Figured_bass_engraver::Figured_bass_engraver ()
149 {
150   alignment_ = 0;
151   continuation_ = false;
152   rest_event_ = 0;
153   new_event_found_ = false;
154 }
155
156 void
157 Figured_bass_engraver::start_translation_timestep ()
158 {
159   if (now_mom ().main_part_ < stop_moment_.main_part_
160       || now_mom ().grace_part_ < Rational (0))
161     return ;
162   
163   rest_event_ = 0;
164   new_events_.clear ();
165   for (vsize i = 0; i < groups_.size (); i++)
166     groups_[i].current_event_ = 0;
167
168   continuation_ = false;
169
170   
171 }
172
173 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
174 void
175 Figured_bass_engraver::listen_rest (Stream_event *ev)
176 {
177   if (to_boolean (get_property ("ignoreFiguredBassRest")))
178     {
179       new_event_found_ = true;
180
181       /*
182         No ASSIGN_EVENT_ONCE () ; otherwise we get warnings about
183         polyphonic rests.
184        */
185       rest_event_ = ev;
186     }
187 }
188
189 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
190 void
191 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
192 {
193   new_event_found_ = true;
194   Moment stop  = now_mom () + get_event_length (ev, now_mom ());
195   stop_moment_ = max (stop_moment_, stop);
196
197   if (to_boolean (get_property ("useBassFigureExtenders")))
198     {
199       SCM fig = ev->get_property ("figure");
200       SCM txt = ev->get_property ("text");
201       for (vsize i = 0; i < groups_.size (); i++)
202         {
203           if (!groups_[i].current_event_
204               && ly_is_equal (groups_[i].number_, fig)
205               && ly_is_equal (groups_[i].text_, txt))
206             {
207               groups_[i].current_event_ = ev;
208               groups_[i].force_no_continuation_
209                 = to_boolean (ev->get_property ("no-continuation"));
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   if (alignment_ && !to_boolean (get_property ("useBassFigureExtenders")))
327     clear_spanners ();
328         
329   if (rest_event_)
330     {
331       clear_spanners ();
332       groups_.clear ();
333       return;
334     }
335   
336   if (!continuation_
337       && new_events_.empty ())
338     {
339       clear_spanners ();
340       groups_.clear ();
341       return;
342     }
343
344   if (!new_event_found_)
345     return;
346   
347   new_event_found_ = false;
348
349   /*
350     Don't need to sync alignments, if we're not using extenders. 
351    */
352   bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
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].number_ = SCM_BOOL_F;
387           groups_[i].alteration_ = SCM_BOOL_F;
388           groups_[i].augmented_ = SCM_BOOL_F;
389           groups_[i].diminished_ = SCM_BOOL_F;
390           groups_[i].augmented_slash_ = SCM_BOOL_F;
391           groups_[i].text_ = SCM_BOOL_F;
392         }
393     }
394
395   if (use_extenders)
396     {
397       vector<int> junk_continuations;
398       for (vsize i = 0; i < groups_.size (); i++)
399         {
400           Figure_group &group = groups_[i];
401
402           if (group.is_continuation ())
403             {
404               if (!group.continuation_line_)
405                 {
406                   Spanner * line
407                     = make_spanner ("BassFigureContinuation", SCM_EOL);
408                   Item * item = group.figure_item_;
409                   group.continuation_line_ = line;
410                   line->set_bound (LEFT, item);
411
412                   /*
413                     Don't add as child. This will cache the wrong
414                     (pre-break) stencil when callbacks are triggered.
415                   */
416                   line->set_parent (group.group_, Y_AXIS);
417                   Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
418
419                   group.figure_item_ = 0;
420                 }
421             }
422           else if (group.continuation_line_) 
423             junk_continuations.push_back (i); 
424         }
425
426       /*
427         Ugh, repeated code.
428        */
429       vector<Spanner*> consecutive;
430       if (to_boolean (get_property ("figuredBassCenterContinuations")))
431         {
432           for (vsize i = 0; i <= junk_continuations.size (); i++)
433             {
434               if (i < junk_continuations.size ()
435                   && (i == 0 || junk_continuations[i-1] == junk_continuations[i] - 1))
436                 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
437               else 
438                 {
439                   center_continuations (consecutive);
440                   consecutive.clear ();
441                   if (i < junk_continuations.size ())
442                     consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
443                 }
444             }
445         }
446       for (vsize i = 0; i < junk_continuations.size (); i++)
447         groups_[junk_continuations[i]].continuation_line_ = 0;
448     }
449   
450   create_grobs ();
451   add_brackets ();
452 }
453
454 void
455 Figured_bass_engraver::create_grobs () 
456 {
457   Grob *muscol
458     = dynamic_cast<Item*> (unsmob_grob (get_property ("currentMusicalColumn")));
459   if (!alignment_)
460     {
461       alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
462       alignment_->set_bound (LEFT, muscol);
463     }
464   alignment_->set_bound (RIGHT, muscol);
465
466   SCM proc = get_property ("figuredBassFormatter");
467   for (vsize i = 0; i < groups_.size (); i++)
468     {
469       Figure_group &group = groups_[i];
470       
471       if (group.current_event_)
472         {
473           Item *item
474             = make_item ("BassFigure",
475                          group.current_event_->self_scm ());
476
477           
478           SCM fig = group.current_event_->get_property ("figure");
479           if (!group.group_)
480             {
481               group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
482               group.group_->set_bound (LEFT, muscol);
483               Align_interface::add_element (alignment_,
484                                             group.group_);
485             }
486
487           if (scm_memq (fig, get_property ("implicitBassFigures")) != SCM_BOOL_F)
488             {
489               item->set_property ("transparent", SCM_BOOL_T); 
490               item->set_property ("implicit", SCM_BOOL_T);
491             }
492           
493           group.number_ = fig;
494           group.alteration_ = group.current_event_->get_property ("alteration");
495           group.augmented_ = group.current_event_->get_property ("augmented");
496           group.diminished_ = group.current_event_->get_property ("diminished");
497           group.augmented_slash_ = group.current_event_->get_property ("augmented-slash");
498           group.text_ = group.current_event_->get_property ("text");
499
500           SCM text = group.text_;
501           if (!Text_interface::is_markup (text)
502               && ly_is_procedure (proc))
503             {
504               text = scm_call_3 (proc, fig, group.current_event_->self_scm (),
505                                  context ()->self_scm ());
506             }
507
508           item->set_property ("text", text);
509           
510           Axis_group_interface::add_element (group.group_, item);
511           group.figure_item_ = item;
512         }
513
514       if (group.continuation_line_)
515         {
516           /*
517             UGH should connect to the bass staff, and get the note heads. 
518           */
519           group.figure_item_->set_property ("transparent", SCM_BOOL_T);
520           group.continuation_line_->set_bound (RIGHT, group.figure_item_);
521         }
522       
523       if (groups_[i].group_)
524         groups_[i].group_->set_bound (RIGHT, muscol);
525
526     }
527
528 }
529
530 ADD_TRANSLATOR (Figured_bass_engraver,
531                 /* doc */
532                 "Make figured bass numbers.",
533
534                 /* create */
535                 "BassFigure "
536                 "BassFigureAlignment "
537                 "BassFigureBracket "
538                 "BassFigureContinuation "
539                 "BassFigureLine ",
540
541                 /* read */
542                 "figuredBassAlterationDirection "
543                 "figuredBassCenterContinuations "
544                 "figuredBassFormatter "
545                 "implicitBassFigures "
546                 "useBassFigureExtenders "
547                 "ignoreFiguredBassRest ",
548
549                 /* write */
550                 ""
551                 );