2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "axis-group-interface.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.hh"
15 #include "international.hh"
16 #include "interval.hh"
17 #include "note-column.hh"
18 #include "paper-column.hh"
19 #include "pointer-group-interface.hh"
20 #include "script-interface.hh"
21 #include "self-alignment-interface.hh"
22 #include "side-position-interface.hh"
23 #include "staff-symbol-referencer.hh"
26 #include "translator.icc"
31 * direction of text-dynamic-event if not equal to direction of
34 - TODO: this engraver is too complicated. We should split it into
35 the handling of the basic grobs and the linespanner
37 - TODO: the line-spanner is not killed after the (de)crescs are
42 print text & hairpin dynamics.
44 class Dynamic_engraver : public Engraver
47 Spanner *line_spanner_;
50 Spanner *finished_line_spanner_;
51 Spanner *finished_cresc_;
54 Music *current_cresc_ev_;
56 Drul_array<Music *> accepted_spanevents_drul_;
58 vector<Note_column*> pending_columns_;
59 vector<Grob*> pending_elements_;
63 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
64 DECLARE_ACKNOWLEDGER (script);
65 DECLARE_ACKNOWLEDGER (stem_tremolo);
66 DECLARE_ACKNOWLEDGER (note_column);
67 DECLARE_ACKNOWLEDGER (slur);
70 virtual void finalize ();
71 virtual bool try_music (Music *event);
72 void stop_translation_timestep ();
73 void process_music ();
76 Dynamic_engraver::Dynamic_engraver ()
81 finished_line_spanner_ = 0;
82 current_cresc_ev_ = 0;
86 accepted_spanevents_drul_[START] = 0;
87 accepted_spanevents_drul_[STOP] = 0;
91 Dynamic_engraver::try_music (Music *m)
93 if (m->is_mus_type ("absolute-dynamic-event"))
96 TODO: probably broken.
101 else if (m->is_mus_type ("decrescendo-event")
102 || m->is_mus_type ("crescendo-event"))
104 Direction d = to_dir (m->get_property ("span-direction"));
106 accepted_spanevents_drul_[d] = m;
107 if (current_cresc_ev_ && d == START)
108 accepted_spanevents_drul_[STOP] = m;
115 Dynamic_engraver::process_music ()
117 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
121 Music *rq = accepted_spanevents_drul_[START];
122 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
129 During a (de)crescendo, pending event will not be cleared,
130 and a line-spanner will always be created, as \< \! are already
133 Note: line-spanner must always have at least same duration
134 as (de)crecsendo, b.o. line-breaking.
138 maybe we should leave dynamic texts to the text-engraver and
139 simply acknowledge them?
143 script_ = make_item ("DynamicText", script_ev_->self_scm ());
144 script_->set_property ("text",
145 script_ev_->get_property ("text"));
147 if (Direction d = to_dir (script_ev_->get_property ("direction")))
148 set_grob_direction (line_spanner_, d);
149 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
150 set_grob_direction (script_, d);
152 Axis_group_interface::add_element (line_spanner_, script_);
155 Music *stop_ev = accepted_spanevents_drul_ [STOP]
156 ? accepted_spanevents_drul_[STOP] : script_ev_;
158 if (accepted_spanevents_drul_[STOP] || script_ev_)
161 finish side position alignment if the (de)cresc ends here, and
162 there are no new dynamics.
167 assert (!finished_cresc_ && cresc_);
171 cresc_->set_bound (RIGHT, script_);
172 add_bound_item (line_spanner_, script_);
175 finished_cresc_ = cresc_;
177 current_cresc_ev_ = 0;
179 else if (accepted_spanevents_drul_[STOP])
181 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
186 if (accepted_spanevents_drul_[START])
188 if (current_cresc_ev_)
190 string msg = _ ("already have a decrescendo");
191 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
192 msg = _ ("already have a crescendo");
194 accepted_spanevents_drul_[START]->origin ()->warning (msg);
195 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
199 current_cresc_ev_ = accepted_spanevents_drul_[START];
201 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
202 set_grob_direction (line_spanner_, d);
209 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
211 if (start_type == "DecrescendoEvent")
212 start_type = "decrescendo";
213 else if (start_type == "CrescendoEvent")
214 start_type = "crescendo";
219 UGH. TODO: should read from original event, so appearance
220 may be altered with \tweak.
222 SCM s = get_property ((start_type + "Spanner").c_str ());
223 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
225 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
228 Pointer_group_interface::add_grob (finished_cresc_,
229 ly_symbol2scm ("adjacent-hairpins"),
232 Pointer_group_interface::add_grob (cresc_,
233 ly_symbol2scm ("adjacent-hairpins"),
236 cresc_->set_property ("grow-direction",
237 scm_from_int ((start_type == "crescendo")
238 ? BIGGER : SMALLER));
242 This is a convenient (and legacy) interface to TextSpanners
243 for use in (de)crescendi.
248 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
249 cresc_->set_property ("style", s);
250 context ()->set_property ((start_type
251 + "Spanner").c_str (), SCM_EOL);
252 s = get_property ((start_type + "Text").c_str ());
254 FIXME: use get_markup () to check type.
256 if (scm_is_string (s) || scm_is_pair (s))
258 cresc_->set_property ("edge-text",
259 scm_cons (s, scm_makfrom0str ("")));
260 context ()->set_property ((start_type + "Text").c_str (),
267 cresc_->set_bound (LEFT, script_);
268 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
271 Axis_group_interface::add_element (line_spanner_, cresc_);
277 Dynamic_engraver::stop_translation_timestep ()
279 if (!current_cresc_ev_ && line_spanner_)
281 assert (!finished_line_spanner_);
282 finished_line_spanner_ = line_spanner_;
288 if (cresc_ && !cresc_->get_bound (LEFT))
290 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
291 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
295 accepted_spanevents_drul_[START] = 0;
296 accepted_spanevents_drul_[STOP] = 0;
300 Dynamic_engraver::finalize ()
305 && !line_spanner_->is_live ())
309 finished_line_spanner_ = line_spanner_;
314 && !cresc_->is_live ())
318 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
325 Dynamic_engraver::typeset_all ()
329 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
330 && scm_is_string (get_property ("whichBar"))
334 if (!finished_cresc_->get_bound (RIGHT)
337 Grob *column_bound = unsmob_grob (use_bar
338 ? get_property ("currentCommandColumn")
339 : get_property ("currentMusicalColumn"));
341 finished_cresc_->set_bound (RIGHT, script_
345 if (finished_line_spanner_)
346 add_bound_item (finished_line_spanner_,
347 finished_cresc_->get_bound (RIGHT));
353 if (finished_line_spanner_)
358 extend-spanner-over-elements (finished_line_spanner_);
360 but this is rather kludgy, since finished_line_spanner_
361 typically has a staff-symbol field set , extending it over the
366 Grob *l = finished_line_spanner_->get_bound (LEFT);
367 Grob *r = finished_line_spanner_->get_bound (RIGHT);
369 finished_line_spanner_->set_bound (RIGHT, l);
371 finished_line_spanner_->set_bound (LEFT, r);
375 This is a isolated dynamic apparently, and does not even have
376 any interesting support item.
378 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
379 Item *ci = dynamic_cast<Item *> (cc);
380 finished_line_spanner_->set_bound (RIGHT, ci);
381 finished_line_spanner_->set_bound (LEFT, ci);
384 finished_line_spanner_ = 0;
390 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
393 Side_position_interface::add_support (line_spanner_, info.grob ());
398 Dynamic_engraver::acknowledge_slur (Grob_info info)
401 Side_position_interface::add_support (line_spanner_, info.grob ());
406 Dynamic_engraver::acknowledge_note_column (Grob_info info)
412 /* Don't refill killed spanner */
413 && line_spanner_->is_live ())
415 Side_position_interface::add_support (line_spanner_, info.grob ());
416 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
419 if (script_ && !script_->get_parent (X_AXIS))
421 extract_grob_set (info.grob (), "note-heads", heads);
424 Grob *head = heads[0];
425 script_->set_parent (head, X_AXIS);
426 Self_alignment_interface::set_center_parent (script_, X_AXIS);
432 if (!cresc_->get_bound (LEFT))
434 cresc_->set_bound (LEFT, info.grob ());
435 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
439 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
440 finished_cresc_->set_bound (RIGHT, info.grob ());
444 Dynamic_engraver::acknowledge_script (Grob_info info)
446 if (!line_spanner_ || !script_)
449 SCM p = info.grob ()->get_property ("script-priority");
454 DynamicText doesn't really have a script-priority field.
456 if (scm_is_number (p)
458 < scm_to_int (script_->get_property ("script-priority")))
459 Side_position_interface::add_support (line_spanner_, info.grob ());
462 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
463 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
464 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
465 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
467 ADD_TRANSLATOR (Dynamic_engraver,
469 "This engraver creates hairpins, dynamic texts, and their vertical\n"
470 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
471 "which takes care of vertical positioning. ",
473 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
474 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",