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"));
214 if (start_type == "DecrescendoEvent")
215 start_type = "decrescendo";
216 else if (start_type == "CrescendoEvent")
217 start_type = "crescendo";
219 SCM s = get_property ((start_type + "Spanner").c_str ());
220 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
222 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
225 Pointer_group_interface::add_grob (finished_cresc_,
226 ly_symbol2scm ("adjacent-hairpins"),
229 Pointer_group_interface::add_grob (cresc_,
230 ly_symbol2scm ("adjacent-hairpins"),
233 cresc_->set_property ("grow-direction",
234 scm_from_int ((start_type == "crescendo")
235 ? BIGGER : SMALLER));
239 This is a convenient (and legacy) interface to TextSpanners
240 for use in (de)crescendi.
245 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
246 cresc_->set_property ("style", s);
247 context ()->set_property ((start_type
248 + "Spanner").c_str (), SCM_EOL);
249 s = get_property ((start_type + "Text").c_str ());
251 FIXME: use get_markup () to check type.
253 if (scm_is_string (s) || scm_is_pair (s))
255 cresc_->set_property ("edge-text",
256 scm_cons (s, scm_makfrom0str ("")));
257 context ()->set_property ((start_type + "Text").c_str (),
264 cresc_->set_bound (LEFT, script_);
265 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
268 Axis_group_interface::add_element (line_spanner_, cresc_);
274 Dynamic_engraver::stop_translation_timestep ()
276 if (!current_cresc_ev_ && line_spanner_)
278 assert (!finished_line_spanner_);
279 finished_line_spanner_ = line_spanner_;
285 if (cresc_ && !cresc_->get_bound (LEFT))
287 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
288 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
292 accepted_spanevents_drul_[START] = 0;
293 accepted_spanevents_drul_[STOP] = 0;
297 Dynamic_engraver::finalize ()
302 && !line_spanner_->is_live ())
306 finished_line_spanner_ = line_spanner_;
311 && !cresc_->is_live ())
315 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
322 Dynamic_engraver::typeset_all ()
326 if (!finished_cresc_->get_bound (RIGHT))
328 finished_cresc_->set_bound (RIGHT, script_
330 : unsmob_grob (get_property ("currentMusicalColumn")));
332 if (finished_line_spanner_)
333 add_bound_item (finished_line_spanner_,
334 finished_cresc_->get_bound (RIGHT));
340 if (finished_line_spanner_)
345 extend-spanner-over-elements (finished_line_spanner_);
347 but this is rather kludgy, since finished_line_spanner_
348 typically has a staff-symbol field set , extending it over the
353 Grob *l = finished_line_spanner_->get_bound (LEFT);
354 Grob *r = finished_line_spanner_->get_bound (RIGHT);
356 finished_line_spanner_->set_bound (RIGHT, l);
358 finished_line_spanner_->set_bound (LEFT, r);
362 This is a isolated dynamic apparently, and does not even have
363 any interesting support item.
365 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
366 Item *ci = dynamic_cast<Item *> (cc);
367 finished_line_spanner_->set_bound (RIGHT, ci);
368 finished_line_spanner_->set_bound (LEFT, ci);
371 finished_line_spanner_ = 0;
377 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
380 Side_position_interface::add_support (line_spanner_, info.grob ());
385 Dynamic_engraver::acknowledge_slur (Grob_info info)
388 Side_position_interface::add_support (line_spanner_, info.grob ());
393 Dynamic_engraver::acknowledge_note_column (Grob_info info)
399 /* Don't refill killed spanner */
400 && line_spanner_->is_live ())
402 Side_position_interface::add_support (line_spanner_, info.grob ());
403 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
406 if (script_ && !script_->get_parent (X_AXIS))
408 extract_grob_set (info.grob (), "note-heads", heads);
411 Grob *head = heads[0];
412 script_->set_parent (head, X_AXIS);
413 Self_alignment_interface::set_center_parent (script_, X_AXIS);
419 if (!cresc_->get_bound (LEFT))
421 cresc_->set_bound (LEFT, info.grob ());
422 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
426 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
427 finished_cresc_->set_bound (RIGHT, info.grob ());
431 Dynamic_engraver::acknowledge_script (Grob_info info)
433 if (!line_spanner_ || !script_)
436 SCM p = info.grob ()->get_property ("script-priority");
441 DynamicText doesn't really have a script-priority field.
443 if (scm_is_number (p)
445 < scm_to_int (script_->get_property ("script-priority")))
446 Side_position_interface::add_support (line_spanner_, info.grob ());
449 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
450 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
451 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
452 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
454 ADD_TRANSLATOR (Dynamic_engraver,
456 "This engraver creates hairpins, dynamic texts, and their vertical\n"
457 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
458 "which takes care of vertical positioning. ",
460 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
461 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",