2 figured-bass-engraver.cc -- implement Figured_bass_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 2005--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "engraver.hh"
12 #include "align-interface.hh"
13 #include "axis-group-interface.hh"
15 #include "grob-array.hh"
17 #include "pointer-group-interface.hh"
19 #include "stream-event.hh"
20 #include "text-interface.hh"
22 #include "translator.icc"
27 Spanner *continuation_line_;
33 Stream_event *current_event_;
34 bool force_no_continuation_;
39 force_no_continuation_ = false;
40 continuation_line_ = 0;
42 alteration_ = SCM_EOL;
46 bool is_continuation () const
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"));
58 struct Figured_bass_engraver : public Engraver
60 TRANSLATOR_DECLARATIONS(Figured_bass_engraver);
61 void clear_spanners();
65 void center_continuations (vector<Spanner*> const &consecutive_lines);
66 void center_repeated_continuations ();
68 vector<Figure_group> groups_;
70 vector<Stream_event *> new_events_;
72 bool new_event_found_;
75 Stream_event *rest_event_;
77 DECLARE_TRANSLATOR_LISTENER (rest);
78 DECLARE_TRANSLATOR_LISTENER (bass_figure);
80 virtual void derived_mark () const;
82 void start_translation_timestep ();
83 void stop_translation_timestep ();
84 void process_music ();
88 Figured_bass_engraver::derived_mark () const
90 for (vsize i = 0; i < groups_.size (); i++)
92 scm_gc_mark (groups_[i].number_);
93 scm_gc_mark (groups_[i].alteration_);
98 Figured_bass_engraver::stop_translation_timestep ()
101 || now_mom ().main_part_ < stop_moment_.main_part_
102 || now_mom ().grace_part_ < Rational (0))
106 for (vsize i = 0; !found && i < groups_.size (); i++)
107 found = found || groups_[i].current_event_;
113 Figured_bass_engraver::Figured_bass_engraver ()
116 continuation_ = false;
118 new_event_found_ = false;
122 Figured_bass_engraver::start_translation_timestep ()
124 if (now_mom ().main_part_ < stop_moment_.main_part_
125 || now_mom ().grace_part_ < Rational (0))
129 new_events_.clear ();
130 for (vsize i = 0; i < groups_.size (); i++)
131 groups_[i].current_event_ = 0;
133 continuation_ = false;
136 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
138 Figured_bass_engraver::listen_rest (Stream_event *ev)
140 if (to_boolean (get_property ("ignoreFiguredBassRest")))
142 new_event_found_ = true;
143 ASSIGN_EVENT_ONCE (rest_event_, ev);
147 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
149 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
151 new_event_found_ = true;
152 Moment stop = now_mom () + get_event_length (ev);
153 stop_moment_ = max (stop_moment_, stop);
155 SCM fig = ev->get_property ("figure");
156 for (vsize i = 0; i < groups_.size (); i++)
158 if (!groups_[i].current_event_
159 && ly_is_equal (groups_[i].number_, fig))
161 groups_[i].current_event_ = ev;
162 groups_[i].force_no_continuation_
163 = to_boolean (ev->get_property ("no-continuation"));
164 continuation_ = true;
169 new_events_.push_back (ev);
173 Figured_bass_engraver::center_continuations (vector<Spanner*> const &consecutive_lines)
175 if (consecutive_lines.size () == 2)
177 vector<Grob*> left_figs;
178 for (vsize j = consecutive_lines.size(); j--;)
179 left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
181 SCM ga = Grob_array::make_array ();
182 unsmob_grob_array (ga)->set_array (left_figs);
184 for (vsize j = consecutive_lines.size(); j--;)
185 consecutive_lines[j]->set_object ("figures",
186 unsmob_grob_array (ga)->smobbed_copy ());
191 Figured_bass_engraver::center_repeated_continuations ()
193 vector<Spanner*> consecutive_lines;
194 for (vsize i = 0; i <= groups_.size(); i++)
196 if (i < groups_.size ()
197 && groups_[i].continuation_line_
198 && (consecutive_lines.empty ()
199 || (consecutive_lines[0]->get_bound(LEFT)->get_column ()
200 == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
201 && consecutive_lines[0]->get_bound(RIGHT)->get_column ()
202 == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
203 consecutive_lines.push_back (groups_[i].continuation_line_);
206 center_continuations (consecutive_lines);
207 consecutive_lines.clear ();
213 Figured_bass_engraver::clear_spanners ()
220 announce_end_grob (alignment_, SCM_EOL);
224 if (to_boolean (get_property ("figuredBassCenterContinuations")))
225 center_repeated_continuations();
227 for (vsize i = 0; i < groups_.size (); i++)
229 if (groups_[i].group_)
231 announce_end_grob (groups_[i].group_ , SCM_EOL);
232 groups_[i].group_ = 0;
235 if (groups_[i].continuation_line_)
237 announce_end_grob (groups_[i].continuation_line_ , SCM_EOL);
238 groups_[i].continuation_line_ = 0;
242 /* Check me, groups_.clear () ? */
246 Figured_bass_engraver::add_brackets ()
248 vector<Grob*> encompass;
250 for (vsize i = 0; i < groups_.size (); i ++)
252 if (!groups_[i].current_event_)
255 if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))
258 if (inside && groups_[i].figure_item_)
259 encompass.push_back (groups_[i].figure_item_);
261 if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
265 Item * brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
266 for (vsize j = 0; j < encompass.size (); j++)
268 Pointer_group_interface::add_grob (brack,
269 ly_symbol2scm ("elements"),
278 Figured_bass_engraver::process_music ()
288 && new_events_.empty ())
295 if (!new_event_found_)
298 new_event_found_ = false;
301 Don't need to sync alignments, if we're not using extenders.
303 bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
316 for (vsize i = 0; i < new_events_.size (); i++)
318 while (k < groups_.size ()
319 && groups_[k].current_event_)
322 if (k >= groups_.size ())
325 groups_.push_back (group);
328 groups_[k].current_event_ = new_events_[i];
329 groups_[k].figure_item_ = 0;
333 for (vsize i = 0; i < groups_.size (); i++)
335 if (!groups_[i].is_continuation ())
337 groups_[i].number_ = SCM_BOOL_F;
338 groups_[i].alteration_ = SCM_BOOL_F;
344 vector<int> junk_continuations;
345 for (vsize i = 0; i < groups_.size(); i++)
347 Figure_group &group = groups_[i];
349 if (group.is_continuation ())
351 if (!group.continuation_line_)
353 Spanner * line = make_spanner ("BassFigureContinuation", SCM_EOL);
354 Item * item = group.figure_item_;
355 group.continuation_line_ = line;
356 line->set_bound (LEFT, item);
359 Don't add as child. This will cache the wrong
360 (pre-break) stencil when callbacks are triggered.
362 line->set_parent (group.group_, Y_AXIS);
363 Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
365 group.figure_item_ = 0;
368 else if (group.continuation_line_)
369 junk_continuations.push_back (i);
375 vector<Spanner*> consecutive;
376 if (to_boolean (get_property ("figuredBassCenterContinuations")))
378 for (vsize i = 0; i <= junk_continuations.size (); i++)
380 if (i < junk_continuations.size()
381 && (i == 0 || junk_continuations[i-1] == junk_continuations[i] - 1))
382 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
385 center_continuations (consecutive);
386 consecutive.clear ();
387 if (i < junk_continuations.size ())
388 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
392 for (vsize i = 0; i < junk_continuations.size (); i++)
393 groups_[junk_continuations[i]].continuation_line_ = 0;
401 Figured_bass_engraver::create_grobs ()
403 Grob *muscol = dynamic_cast<Item*> (unsmob_grob (get_property ("currentMusicalColumn")));
406 alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
407 alignment_->set_bound (LEFT, muscol);
409 alignment_->set_bound (RIGHT, muscol);
411 SCM proc = get_property ("figuredBassFormatter");
412 for (vsize i = 0; i < groups_.size(); i++)
414 Figure_group &group = groups_[i];
416 if (group.current_event_)
419 = make_item ("BassFigure",
420 group.current_event_->self_scm ());
423 SCM fig = group.current_event_->get_property ("figure");
426 group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
427 group.group_->set_bound (LEFT, muscol);
428 Align_interface::add_element (alignment_,
432 if (scm_memq (fig, get_property ("implicitBassFigures")) != SCM_BOOL_F)
434 item->set_property ("transparent", SCM_BOOL_T);
435 item->set_property ("implicit", SCM_BOOL_T);
439 group.alteration_ = group.current_event_->get_property ("alteration");
441 SCM text = group.current_event_->get_property ("text");
442 if (!Text_interface::is_markup (text)
443 && ly_is_procedure (proc))
445 text = scm_call_3 (proc, fig, group.current_event_->self_scm (),
446 context ()->self_scm ());
449 item->set_property ("text", text);
451 Axis_group_interface::add_element (group.group_, item);
452 group.figure_item_ = item;
455 if (group.continuation_line_)
458 UGH should connect to the bass staff, and get the note heads.
460 group.figure_item_->set_property ("transparent", SCM_BOOL_T);
461 group.continuation_line_->set_bound (RIGHT, group.figure_item_);
464 if (groups_[i].group_)
465 groups_[i].group_->set_bound (RIGHT, muscol);
471 ADD_TRANSLATOR (Figured_bass_engraver,
474 "Make figured bass numbers.",
477 "BassFigureAlignment "
479 "BassFigureContinuation "
487 "figuredBassAlterationDirection "
488 "figuredBassCenterContinuations "
489 "figuredBassFormatter "
490 "implicitBassFigures "
491 "useBassFigureExtenders "
492 "ignoreFiguredBassRest "