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