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;
132 continuation_ = false;
135 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, rest);
137 Figured_bass_engraver::listen_rest (Stream_event *ev)
139 new_event_found_ = true;
140 ASSIGN_EVENT_ONCE (rest_event_, ev);
143 IMPLEMENT_TRANSLATOR_LISTENER (Figured_bass_engraver, bass_figure);
145 Figured_bass_engraver::listen_bass_figure (Stream_event *ev)
147 new_event_found_ = true;
148 Moment stop = now_mom () + get_event_length (ev);
149 stop_moment_ = max (stop_moment_, stop);
151 SCM fig = ev->get_property ("figure");
152 for (vsize i = 0; i < groups_.size (); i++)
154 if (!groups_[i].current_event_
155 && ly_is_equal (groups_[i].number_, fig))
157 groups_[i].current_event_ = ev;
158 groups_[i].force_no_continuation_
159 = to_boolean (ev->get_property ("no-continuation"));
160 continuation_ = true;
165 new_events_.push_back (ev);
169 Figured_bass_engraver::center_continuations (vector<Spanner*> const &consecutive_lines)
171 if (consecutive_lines.size () == 2)
173 vector<Grob*> left_figs;
174 for (vsize j = consecutive_lines.size(); j--;)
175 left_figs.push_back (consecutive_lines[j]->get_bound (LEFT));
177 SCM ga = Grob_array::make_array ();
178 unsmob_grob_array (ga)->set_array (left_figs);
180 for (vsize j = consecutive_lines.size(); j--;)
181 consecutive_lines[j]->set_object ("figures",
182 unsmob_grob_array (ga)->smobbed_copy ());
187 Figured_bass_engraver::center_repeated_continuations ()
189 vector<Spanner*> consecutive_lines;
190 for (vsize i = 0; i <= groups_.size(); i++)
192 if (i < groups_.size ()
193 && groups_[i].continuation_line_
194 && (consecutive_lines.empty ()
195 || (consecutive_lines[0]->get_bound(LEFT)->get_column ()
196 == groups_[i].continuation_line_->get_bound (LEFT)->get_column ()
197 && consecutive_lines[0]->get_bound(RIGHT)->get_column ()
198 == groups_[i].continuation_line_->get_bound (RIGHT)->get_column ())))
199 consecutive_lines.push_back (groups_[i].continuation_line_);
202 center_continuations (consecutive_lines);
203 consecutive_lines.clear ();
209 Figured_bass_engraver::clear_spanners ()
216 announce_end_grob (alignment_, SCM_EOL);
220 if (to_boolean (get_property ("figuredBassCenterContinuations")))
221 center_repeated_continuations();
223 for (vsize i = 0; i < groups_.size (); i++)
225 if (groups_[i].group_)
227 announce_end_grob (groups_[i].group_ , SCM_EOL);
228 groups_[i].group_ = 0;
231 if (groups_[i].continuation_line_)
233 announce_end_grob (groups_[i].continuation_line_ , SCM_EOL);
234 groups_[i].continuation_line_ = 0;
238 /* Check me, groups_.clear () ? */
242 Figured_bass_engraver::add_brackets ()
244 vector<Grob*> encompass;
246 for (vsize i = 0; i < groups_.size (); i ++)
248 if (!groups_[i].current_event_)
251 if (to_boolean (groups_[i].current_event_->get_property ("bracket-start")))
254 if (inside && groups_[i].figure_item_)
255 encompass.push_back (groups_[i].figure_item_);
257 if (to_boolean (groups_[i].current_event_->get_property ("bracket-stop")))
261 Item * brack = make_item ("BassFigureBracket", groups_[i].current_event_->self_scm ());
262 for (vsize j = 0; j < encompass.size (); j++)
264 Pointer_group_interface::add_grob (brack,
265 ly_symbol2scm ("elements"),
274 Figured_bass_engraver::process_music ()
284 && new_events_.empty ())
291 if (!new_event_found_)
294 new_event_found_ = false;
297 Don't need to sync alignments, if we're not using extenders.
299 bool use_extenders = to_boolean (get_property ("useBassFigureExtenders"));
312 for (vsize i = 0; i < new_events_.size (); i++)
314 while (k < groups_.size ()
315 && groups_[k].current_event_)
318 if (k >= groups_.size ())
321 groups_.push_back (group);
324 groups_[k].current_event_ = new_events_[i];
325 groups_[k].figure_item_ = 0;
329 for (vsize i = 0; i < groups_.size (); i++)
331 if (!groups_[i].is_continuation ())
333 groups_[i].number_ = SCM_BOOL_F;
334 groups_[i].alteration_ = SCM_BOOL_F;
340 vector<int> junk_continuations;
341 for (vsize i = 0; i < groups_.size(); i++)
343 Figure_group &group = groups_[i];
345 if (group.is_continuation ())
347 if (!group.continuation_line_)
349 Spanner * line = make_spanner ("BassFigureContinuation", SCM_EOL);
350 Item * item = group.figure_item_;
351 group.continuation_line_ = line;
352 line->set_bound (LEFT, item);
355 Don't add as child. This will cache the wrong
356 (pre-break) stencil when callbacks are triggered.
358 line->set_parent (group.group_, Y_AXIS);
359 Pointer_group_interface::add_grob (line, ly_symbol2scm ("figures"), item);
361 group.figure_item_ = 0;
364 else if (group.continuation_line_)
365 junk_continuations.push_back (i);
371 vector<Spanner*> consecutive;
372 if (to_boolean (get_property ("figuredBassCenterContinuations")))
374 for (vsize i = 0; i <= junk_continuations.size (); i++)
376 if (i < junk_continuations.size()
377 && (i == 0 || junk_continuations[i-1] == junk_continuations[i] - 1))
378 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
381 center_continuations (consecutive);
382 consecutive.clear ();
383 if (i < junk_continuations.size ())
384 consecutive.push_back (groups_[junk_continuations[i]].continuation_line_);
388 for (vsize i = 0; i < junk_continuations.size (); i++)
389 groups_[junk_continuations[i]].continuation_line_ = 0;
397 Figured_bass_engraver::create_grobs ()
399 Grob *muscol = dynamic_cast<Item*> (unsmob_grob (get_property ("currentMusicalColumn")));
402 alignment_ = make_spanner ("BassFigureAlignment", SCM_EOL);
403 alignment_->set_bound (LEFT, muscol);
405 alignment_->set_bound (RIGHT, muscol);
407 SCM proc = get_property ("figuredBassFormatter");
408 for (vsize i = 0; i < groups_.size(); i++)
410 Figure_group &group = groups_[i];
412 if (group.current_event_)
415 = make_item ("BassFigure",
416 group.current_event_->self_scm ());
419 SCM fig = group.current_event_->get_property ("figure");
422 group.group_ = make_spanner ("BassFigureLine", SCM_EOL);
423 group.group_->set_bound (LEFT, muscol);
424 Align_interface::add_element (alignment_,
428 if (scm_memq (fig, get_property ("implicitBassFigures")) != SCM_BOOL_F)
430 item->set_property ("transparent", SCM_BOOL_T);
431 item->set_property ("implicit", SCM_BOOL_T);
435 group.alteration_ = group.current_event_->get_property ("alteration");
437 SCM text = group.current_event_->get_property ("text");
438 if (!Text_interface::is_markup (text)
439 && ly_is_procedure (proc))
441 text = scm_call_3 (proc, fig, group.current_event_->self_scm (),
442 context ()->self_scm ());
445 item->set_property ("text", text);
447 Axis_group_interface::add_element (group.group_, item);
448 group.figure_item_ = item;
451 if (group.continuation_line_)
454 UGH should connect to the bass staff, and get the note heads.
456 group.figure_item_->set_property ("transparent", SCM_BOOL_T);
457 group.continuation_line_->set_bound (RIGHT, group.figure_item_);
460 if (groups_[i].group_)
461 groups_[i].group_->set_bound (RIGHT, muscol);
467 ADD_TRANSLATOR (Figured_bass_engraver,
470 "Make figured bass numbers.",
473 "BassFigureAlignment "
475 "BassFigureContinuation "
479 "bass-figure-event rest-event",
482 "figuredBassAlterationDirection "
483 "figuredBassCenterContinuations "
484 "figuredBassFormatter "
485 "implicitBassFigures "
486 "useBassFigureExtenders "