2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2005--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
21 #include "engraver.hh"
23 #include "align-interface.hh"
24 #include "axis-group-interface.hh"
26 #include "grob-array.hh"
28 #include "pointer-group-interface.hh"
30 #include "stream-event.hh"
31 #include "text-interface.hh"
33 #include "translator.icc"
38 Spanner *continuation_line_;
48 Stream_event *current_event_;
53 continuation_line_ = 0;
58 /* Reset (or init) all figure information to FALSE */
62 alteration_ = SCM_BOOL_F;
63 augmented_ = SCM_BOOL_F;
64 diminished_ = SCM_BOOL_F;
65 augmented_slash_ = SCM_BOOL_F;
68 /* Mark the members of the struct as used for the GUILE Garbage Collection */
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_);
78 bool group_is_equal_to (Stream_event *evt) const
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"));
88 bool is_continuation () const
92 && group_is_equal_to (current_event_);
94 void assign_from_event (Stream_event *currevt, Item *item)
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");
106 struct Figured_bass_engraver : public Engraver
108 TRANSLATOR_DECLARATIONS (Figured_bass_engraver);
109 void clear_spanners ();
110 void add_brackets ();
111 void create_grobs ();
113 void center_continuations (vector<Spanner *> const &consecutive_lines);
114 void center_repeated_continuations ();
116 vector<Figure_group> groups_;
118 vector<Stream_event *> new_events_;
120 bool new_event_found_;
125 void listen_rest (Stream_event *);
126 void listen_bass_figure (Stream_event *);
128 virtual void derived_mark () const;
130 void start_translation_timestep ();
131 void stop_translation_timestep ();
132 void process_music ();
135 Figured_bass_engraver::Figured_bass_engraver (Context *c)
139 continuation_ = false;
141 new_event_found_ = false;
145 Figured_bass_engraver::derived_mark () const
147 for (vsize i = 0; i < groups_.size (); i++)
149 groups_[i].gc_mark ();
154 Figured_bass_engraver::start_translation_timestep ()
156 if (now_mom ().main_part_ < stop_moment_.main_part_
157 || now_mom ().grace_part_ < Rational (0))
161 new_events_.clear ();
162 for (vsize i = 0; i < groups_.size (); i++)
163 groups_[i].current_event_ = 0;
165 continuation_ = false;
169 Figured_bass_engraver::stop_translation_timestep ()
172 || now_mom ().main_part_ < stop_moment_.main_part_
173 || now_mom ().grace_part_ < Rational (0))
177 for (vsize i = 0; !found && i < groups_.size (); i++)
178 found = found || groups_[i].current_event_;
185 Figured_bass_engraver::listen_rest (Stream_event *)
191 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
193 new_event_found_ = true;
194 Moment stop = now_mom () + get_event_length (ev, now_mom ());
195 stop_moment_ = max (stop_moment_, stop);
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)
202 for (vsize i = 0; i < groups_.size (); i++)
204 if (!groups_[i].current_event_
205 && groups_[i].group_is_equal_to (ev))
207 groups_[i].current_event_ = ev;
208 continuation_ = true;
213 new_events_.push_back (ev);
217 Figured_bass_engraver::center_continuations (vector<Spanner *> const &consecutive_lines)
219 vector<Grob *> left_figs;
220 for (vsize j = consecutive_lines.size (); j--;)
221 left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
223 SCM ga = Grob_array::make_array ();
224 unsmob<Grob_array> (ga)->set_array (left_figs);
226 for (vsize j = consecutive_lines.size (); j--;)
227 consecutive_lines[j]->set_object ("figures",
228 unsmob<Grob_array> (ga)->smobbed_copy ());
232 Figured_bass_engraver::center_repeated_continuations ()
234 vector<Spanner *> consecutive_lines;
235 for (vsize i = 0; i <= groups_.size (); i++)
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_);
247 center_continuations (consecutive_lines);
248 consecutive_lines.clear ();
254 Figured_bass_engraver::clear_spanners ()
259 announce_end_grob (alignment_, SCM_EOL);
262 if (to_boolean (get_property ("figuredBassCenterContinuations")))
263 center_repeated_continuations ();
265 for (vsize i = 0; i < groups_.size (); i++)
267 if (groups_[i].group_)
269 announce_end_grob (groups_[i].group_, SCM_EOL);
270 groups_[i].group_ = 0;
273 if (groups_[i].continuation_line_)
275 announce_end_grob (groups_[i].continuation_line_, SCM_EOL);
276 groups_[i].continuation_line_ = 0;
280 /* Check me, groups_.clear () ? */
284 Figured_bass_engraver::process_music ()
286 bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
287 if (alignment_ && !use_extenders)
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 ()))
300 if (!new_event_found_)
303 new_event_found_ = false;
306 Don't need to sync alignments, if we're not using extenders.
320 for (vsize i = 0; i < new_events_.size (); i++)
322 while (k < groups_.size ()
323 && groups_[k].current_event_)
326 if (k >= groups_.size ())
329 groups_.push_back (group);
332 groups_[k].reset_figure ();
333 groups_[k].current_event_ = new_events_[i];
334 groups_[k].figure_item_ = 0;
338 for (vsize i = 0; i < groups_.size (); i++)
340 if (!groups_[i].is_continuation ())
342 groups_[i].reset_figure ();
348 vector<int> junk_continuations;
349 for (vsize i = 0; i < groups_.size (); i++)
351 Figure_group &group = groups_[i];
353 if (group.is_continuation ())
355 if (!group.continuation_line_)
358 = make_spanner ("BassFigureContinuation", SCM_EOL);
359 Item *item = group.figure_item_;
360 group.continuation_line_ = line;
361 line->set_bound (LEFT, item);
364 Don't add as child. This will cache the wrong
365 (pre-break) stencil when callbacks are triggered.
367 line->set_parent (group.group_, Y_AXIS);
368 Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
370 group.figure_item_ = 0;
373 else if (group.continuation_line_)
374 junk_continuations.push_back (i);
380 vector<Spanner *> consecutive;
381 if (to_boolean (get_property ("figuredBassCenterContinuations")))
383 for (vsize i = 0; i <= junk_continuations.size (); i++)
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_);
390 center_continuations (consecutive);
391 consecutive.clear ();
392 if (i < junk_continuations.size ())
393 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
397 for (vsize i = 0; i < junk_continuations.size (); i++)
398 groups_[junk_continuations[i]].continuation_line_ = 0;
406 Figured_bass_engraver::create_grobs ()
409 = unsmob<Item> (get_property ("currentMusicalColumn"));
412 alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
413 alignment_->set_bound (LEFT, muscol);
415 alignment_->set_bound (RIGHT, muscol);
417 SCM proc = get_property ("figuredBassFormatter");
418 for (vsize i = 0; i < groups_.size (); i++)
420 Figure_group &group = groups_[i];
422 if (group.current_event_)
425 = make_item ("BassFigure", group.current_event_->self_scm ());
426 group.assign_from_event (group.current_event_, item);
430 group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
431 group.group_->set_bound (LEFT, muscol);
432 Align_interface::add_element (alignment_, group.group_);
435 if (scm_is_true (scm_memq (group.number_, get_property ("implicitBassFigures"))))
437 item->set_property ("transparent", SCM_BOOL_T);
438 item->set_property ("implicit", SCM_BOOL_T);
441 SCM text = group.text_;
442 if (!Text_interface::is_markup (text)
443 && ly_is_procedure (proc))
445 text = scm_call_3 (proc, group.number_, group.current_event_->self_scm (),
446 context ()->self_scm ());
449 item->set_property ("text", text);
451 Axis_group_interface::add_element (group.group_, item);
454 if (group.continuation_line_)
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>
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_);
467 if (groups_[i].group_)
468 groups_[i].group_->set_bound (RIGHT, muscol);
475 Figured_bass_engraver::add_brackets ()
477 vector<Grob *> encompass;
479 for (vsize i = 0; i < groups_.size (); i++)
481 if (!groups_[i].current_event_)
484 if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))
487 if (inside && groups_[i].figure_item_)
488 encompass.push_back (groups_[i].figure_item_);
490 if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
494 Item *brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
495 for (vsize j = 0; j < encompass.size (); j++)
497 Pointer_group_interface::add_grob (brack,
498 ly_symbol2scm ("elements"),
507 Figured_bass_engraver::boot ()
509 ADD_LISTENER (Figured_bass_engraver, rest);
510 ADD_LISTENER (Figured_bass_engraver, bass_figure);
513 ADD_TRANSLATOR (Figured_bass_engraver,
515 "Make figured bass numbers.",
519 "BassFigureAlignment "
521 "BassFigureContinuation "
525 "figuredBassAlterationDirection "
526 "figuredBassCenterContinuations "
527 "figuredBassFormatter "
528 "implicitBassFigures "
529 "useBassFigureExtenders "
530 "ignoreFiguredBassRest ",