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