]> git.donarmstrong.com Git - lilypond.git/blob - lily/figured-bass-engraver.cc
*** empty log message ***
[lilypond.git] / lily / figured-bass-engraver.cc
1 /*
2   figured-bass-engraver.cc -- implement Figured_bass_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7
8 */
9
10 #include "engraver.hh"
11
12 #include "align-interface.hh"
13 #include "axis-group-interface.hh"
14 #include "context.hh"
15 #include "grob-array.hh"
16 #include "item.hh"
17 #include "pointer-group-interface.hh"
18 #include "spanner.hh"
19 #include "stream-event.hh"
20 #include "text-interface.hh"
21
22 #include "translator.icc"
23
24 struct Figure_group
25 {
26   Spanner *group_;
27   Spanner *continuation_line_;
28   
29   SCM number_;
30   SCM alteration_;
31   
32   Item *figure_item_; 
33   Stream_event *current_event_;
34   bool force_no_continuation_;
35   
36   Figure_group ()
37   {
38     figure_item_ = 0;
39     force_no_continuation_ = false;
40     continuation_line_ = 0;
41     number_ = SCM_EOL;
42     alteration_ = SCM_EOL;
43     group_ = 0;
44     current_event_ = 0;
45   }
46   bool is_continuation () const
47   {
48     return
49       current_event_
50       && !force_no_continuation_
51       && ly_is_equal (number_,
52                       current_event_->get_property ("figure"))
53       && ly_is_equal (alteration_,
54                       current_event_->get_property ("alteration"));
55   }
56 };
57
58 struct Figured_bass_engraver : public Engraver
59 {
60   TRANSLATOR_DECLARATIONS(Figured_bass_engraver);
61   void clear_spanners();
62   void add_brackets ();
63   void create_grobs ();
64
65   void center_continuations (vector<Spanner*> const &consecutive_lines);
66   void center_repeated_continuations ();
67 protected:
68   vector<Figure_group> groups_;
69   Spanner *alignment_;
70   vector<Stream_event *> new_events_;
71   bool continuation_;
72   bool new_event_found_;
73   
74   Moment stop_moment_;
75   Stream_event *rest_event_; 
76
77   DECLARE_TRANSLATOR_LISTENER (rest);
78   DECLARE_TRANSLATOR_LISTENER (bass_figure);
79
80   virtual void derived_mark () const; 
81
82   void start_translation_timestep ();
83   void stop_translation_timestep ();
84   void process_music ();
85 };
86
87 void
88 Figured_bass_engraver::derived_mark () const
89 {
90   for (vsize i = 0; i < groups_.size (); i++)
91     {
92       scm_gc_mark (groups_[i].number_);
93       scm_gc_mark (groups_[i].alteration_);
94     }
95 }
96
97 void
98 Figured_bass_engraver::stop_translation_timestep ()
99 {
100   if (groups_.empty ()
101       || now_mom ().main_part_ < stop_moment_.main_part_
102       || now_mom ().grace_part_ < Rational (0))
103     return ;
104   
105   bool found = false;
106   for (vsize i = 0; !found && i < groups_.size (); i++)
107     found  = found  || groups_[i].current_event_;
108
109   if (!found)
110     clear_spanners ();
111 }
112
113 Figured_bass_engraver::Figured_bass_engraver ()
114 {
115   alignment_ = 0;
116   continuation_ = false;
117   rest_event_ = 0;
118   new_event_found_ = false;
119 }
120
121 void
122 Figured_bass_engraver::start_translation_timestep ()
123 {
124   if (now_mom ().main_part_ < stop_moment_.main_part_
125       || now_mom ().grace_part_ < Rational (0))
126     return ;
127   
128   rest_event_ = 0;
129   new_events_.clear ();
130   for (vsize i = 0; i < groups_.size (); i++)
131     groups_[i].current_event_ = 0;
132   continuation_ = false;
133 }
134
135 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
136 void
137 Figured_bass_engraver::listen_rest (Stream_event *ev)
138 {
139   new_event_found_ = true;
140   rest_event_ = ev;
141 }
142
143 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
144 void
145 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
146 {
147   new_event_found_ = true;
148   stop_moment_ = now_mom () + get_event_length (ev);
149      
150   SCM fig = ev->get_property ("figure");
151   for (vsize i = 0; i < groups_.size (); i++)
152     {
153       if (!groups_[i].current_event_
154           && ly_is_equal (groups_[i].number_, fig))
155         {
156           groups_[i].current_event_ = ev;
157           groups_[i].force_no_continuation_
158             = to_boolean (ev->get_property ("no-continuation"));
159           continuation_ = true;
160           return; 
161         }
162     }
163   
164   new_events_.push_back (ev);
165 }
166
167 void
168 Figured_bass_engraver::center_continuations (vector<Spanner*> const &consecutive_lines)
169 {
170   if (consecutive_lines.size () == 2)
171     {
172       vector<Grob*> left_figs;
173       for (vsize j = consecutive_lines.size(); j--;)
174         left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
175
176       SCM  ga = Grob_array::make_array ();
177       unsmob_grob_array (ga)->set_array (left_figs);
178
179       for (vsize j = consecutive_lines.size(); j--;)
180         consecutive_lines[j]->set_object ("figures",
181                                           unsmob_grob_array (ga)->smobbed_copy ());
182     }
183 }
184
185 void
186 Figured_bass_engraver::center_repeated_continuations ()
187 {  
188   vector<Spanner*> consecutive_lines;
189   for (vsize i = 0; i <= groups_.size(); i++)
190     {
191       if (i < groups_.size ()
192           && groups_[i].continuation_line_
193           && (consecutive_lines.empty ()
194               || (consecutive_lines[0]->get_bound(LEFT)->get_column ()
195                   == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
196                   && consecutive_lines[0]->get_bound(RIGHT)->get_column ()
197                   == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
198         consecutive_lines.push_back (groups_[i].continuation_line_);      
199       else 
200         {
201           center_continuations (consecutive_lines);
202           consecutive_lines.clear ();
203         }
204     }
205 }
206
207 void
208 Figured_bass_engraver::clear_spanners ()
209 {
210   if (!alignment_)
211     return;
212
213   if (alignment_)
214     {
215       announce_end_grob (alignment_, SCM_EOL);
216       alignment_ = 0;
217     }
218
219   if (to_boolean (get_property ("figuredBassCenterContinuations")))
220     center_repeated_continuations();
221   
222   for (vsize i = 0; i < groups_.size (); i++)
223     {
224       if (groups_[i].group_)
225         {
226           announce_end_grob (groups_[i].group_ , SCM_EOL);
227           groups_[i].group_ = 0;
228         }
229       
230       if (groups_[i].continuation_line_)
231         {
232           announce_end_grob (groups_[i].continuation_line_ , SCM_EOL);
233           groups_[i].continuation_line_ = 0;
234         }
235     }
236
237   /* Check me, groups_.clear () ? */
238 }
239
240 void
241 Figured_bass_engraver::add_brackets ()
242 {
243   vector<Grob*> encompass;
244   bool inside = false;
245   for (vsize i = 0; i < groups_.size (); i ++)
246     {
247       if (!groups_[i].current_event_)
248         continue;
249       
250       if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))       
251         inside = true;
252
253       if (inside && groups_[i].figure_item_)
254         encompass.push_back (groups_[i].figure_item_);
255
256        if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
257         {
258           inside = false;
259
260           Item * brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
261           for (vsize j = 0; j < encompass.size (); j++)
262             {
263               Pointer_group_interface::add_grob (brack,
264                                                  ly_symbol2scm ("elements"),
265                                                  encompass[j]);
266             }
267           encompass.clear ();
268         }
269     }
270 }
271
272 void
273 Figured_bass_engraver::process_music ()
274 {
275   if (rest_event_)
276     {
277       clear_spanners ();
278       groups_.clear ();
279       return;
280     }
281   
282   if (!continuation_
283       && new_events_.empty ())
284     {
285       clear_spanners ();
286       groups_.clear ();
287       return;
288     }
289
290   if (!new_event_found_)
291     return;
292   
293   new_event_found_ = false;
294
295   /*
296     Don't need to sync alignments, if we're not using extenders. 
297    */
298   bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
299   if (!use_extenders)
300     {
301       clear_spanners ();
302     }
303   
304   if (!continuation_)
305     {
306       clear_spanners ();
307       groups_.clear ();
308     }
309
310   vsize k = 0;
311   for (vsize i = 0; i < new_events_.size (); i++)
312     {
313       while (k < groups_.size ()
314              && groups_[k].current_event_)
315         k++;
316       
317       if (k >= groups_.size ())
318         {
319           Figure_group group;
320           groups_.push_back (group);
321         }
322       
323       groups_[k].current_event_ = new_events_[i];
324       groups_[k].figure_item_ = 0;
325       k++;
326     }
327
328   for (vsize i = 0; i < groups_.size (); i++)
329     {
330       if (!groups_[i].is_continuation ())
331         {
332           groups_[i].number_ = SCM_BOOL_F;
333           groups_[i].alteration_ = SCM_BOOL_F;
334         }
335     }
336
337   if (use_extenders)
338     {
339       vector<int> junk_continuations;
340       for (vsize i = 0; i < groups_.size(); i++)
341         {
342                 Figure_group &group = groups_[i];
343
344           if (group.is_continuation ())
345             {
346               if (!group.continuation_line_)
347                 {
348                   Spanner * line = make_spanner ("BassFigureContinuation", SCM_EOL);
349                   Item * item = group.figure_item_;
350                   group.continuation_line_ = line;
351                   line->set_bound (LEFT, item);
352
353                   /*
354                     Don't add as child. This will cache the wrong
355                     (pre-break) stencil when callbacks are triggered.
356                   */
357                   line->set_parent (group.group_, Y_AXIS);
358                   Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
359
360                   group.figure_item_ = 0;
361                 }
362             }
363           else if (group.continuation_line_) 
364             junk_continuations.push_back (i); 
365         }
366
367       /*
368         Ugh, repeated code.
369        */
370       vector<Spanner*> consecutive;
371       if (to_boolean (get_property ("figuredBassCenterContinuations")))
372         {
373           for (vsize i = 0; i <= junk_continuations.size (); i++)
374             {
375               if (i < junk_continuations.size()
376                   && (i == 0 || junk_continuations[i-1] == junk_continuations[i] - 1))
377                 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
378               else 
379                 {
380                   center_continuations (consecutive);
381                   consecutive.clear ();
382                   if (i < junk_continuations.size ())
383                     consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
384                 }
385             }
386         }
387       for (vsize i = 0; i < junk_continuations.size (); i++)
388         groups_[junk_continuations[i]].continuation_line_ = 0;
389     }
390   
391   create_grobs ();
392   add_brackets ();
393 }
394
395 void
396 Figured_bass_engraver::create_grobs () 
397 {
398   Grob *muscol = dynamic_cast<Item*> (unsmob_grob (get_property ("currentMusicalColumn")));
399   if (!alignment_)
400     {
401       alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
402       alignment_->set_bound (LEFT, muscol);
403     }
404   alignment_->set_bound (RIGHT, muscol);
405
406   SCM proc = get_property ("figuredBassFormatter");
407   for (vsize i = 0; i < groups_.size(); i++)
408     {
409       Figure_group &group = groups_[i];
410       
411       if (group.current_event_)
412         {
413           Item *item
414             = make_item ("BassFigure",
415                          group.current_event_->self_scm ());
416
417           
418           SCM fig = group.current_event_->get_property ("figure");
419           if (!group.group_)
420             {
421               group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
422               group.group_->set_bound (LEFT, muscol);
423               Align_interface::add_element (alignment_,
424                                             group.group_);
425             }
426
427           if (scm_memq (fig, get_property ("implicitBassFigures")) != SCM_BOOL_F)
428             {
429               item->set_property ("transparent", SCM_BOOL_T); 
430               item->set_property ("implicit", SCM_BOOL_T);
431             }
432           
433           group.number_ = fig;
434           group.alteration_ = group.current_event_->get_property ("alteration");
435
436           SCM text = group.current_event_->get_property ("text");
437           if (!Text_interface::is_markup (text)
438               && ly_is_procedure (proc))
439             {
440               text = scm_call_3 (proc, fig, group.current_event_->self_scm (),
441                                  context ()->self_scm ());
442             }
443
444           item->set_property ("text", text);
445           
446           Axis_group_interface::add_element (group.group_, item);
447           group.figure_item_ = item;
448         }
449
450       if (group.continuation_line_)
451         {
452           /*
453             UGH should connect to the bass staff, and get the note heads. 
454           */
455           group.figure_item_->set_property ("transparent", SCM_BOOL_T);
456           group.continuation_line_->set_bound (RIGHT, group.figure_item_);
457         }
458       
459       if (groups_[i].group_)
460         groups_[i].group_->set_bound (RIGHT, muscol);
461
462     }
463
464 }
465
466 ADD_TRANSLATOR (Figured_bass_engraver,
467                 /* doc */
468
469                 "Make figured bass numbers.",
470                 /* create */
471                 "BassFigure "
472                 "BassFigureAlignment "
473                 "BassFigureBracket "
474                 "BassFigureContinuation "
475                 "BassFigureLine "
476                 ,
477                 /* accept */
478                 "bass-figure-event rest-event",
479
480                 /* read */
481                 "figuredBassAlterationDirection "
482                 "figuredBassCenterContinuations "
483                 "figuredBassFormatter "
484                 "implicitBassFigures "
485                 "useBassFigureExtenders "
486                 ,
487
488                 /* write */
489                 "");