2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "axis-group-interface.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.hh"
15 #include "group-interface.hh"
17 #include "interval.hh"
20 #include "note-column.hh"
21 #include "paper-column.hh"
22 #include "script-interface.hh"
23 #include "side-position-interface.hh"
24 #include "staff-symbol-referencer.hh"
30 * direction of text-dynamic-event if not equal to direction of
33 - TODO: this engraver is too complicated. We should split it into
34 the handling of the basic grobs and the linespanner
36 - 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_;
51 Spanner *finished_line_spanner_;
52 Spanner *finished_cresc_;
55 Music *current_cresc_ev_;
57 Drul_array<Music*> accepted_spanreqs_drul_;
59 Link_array<Note_column> pending_columns_;
60 Link_array<Grob> pending_elements_;
64 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
67 virtual void finalize ();
68 virtual void acknowledge_grob (Grob_info);
69 virtual bool try_music (Music *req);
70 virtual void stop_translation_timestep ();
71 virtual void process_music ();
77 Dynamic_engraver::Dynamic_engraver ()
83 finished_line_spanner_ = 0;
84 current_cresc_ev_ = 0;
88 accepted_spanreqs_drul_[START] = 0;
89 accepted_spanreqs_drul_[STOP] = 0;
93 Dynamic_engraver::try_music (Music *m)
95 if (m->is_mus_type ("absolute-dynamic-event"))
98 TODO: probably broken.
103 else if (m->is_mus_type ("decrescendo-event")
104 || m->is_mus_type ("crescendo-event"))
106 Direction d = to_dir (m->get_property ("span-direction"));
108 accepted_spanreqs_drul_[d] = m;
109 if (current_cresc_ev_ && d == START)
110 accepted_spanreqs_drul_[STOP] = m;
117 Dynamic_engraver::process_music ()
119 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
123 Music * rq = accepted_spanreqs_drul_[START];
124 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm (): SCM_EOL );
132 During a (de)crescendo, pending event will not be cleared,
133 and a line-spanner will always be created, as \< \! are already
136 Note: line-spanner must always have at least same duration
137 as (de)crecsendo, b.o. line-breaking.
143 maybe we should leave dynamic texts to the text-engraver and
144 simply acknowledge them?
148 script_ = make_item ("DynamicText", script_ev_->self_scm ());
149 script_->set_property ("text",
150 script_ev_->get_property ("text"));
153 if (Direction d = to_dir (script_ev_->get_property ("direction")))
154 set_grob_direction (line_spanner_, d);
156 Axis_group_interface::add_element (line_spanner_, script_);
159 Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
160 accepted_spanreqs_drul_[STOP] : script_ev_;
162 if (accepted_spanreqs_drul_[STOP] || script_ev_)
165 finish side position alignment if the (de)cresc ends here, and
166 there are no new dynamics.
172 assert (!finished_cresc_ && cresc_);
174 cresc_->set_bound (RIGHT, script_
176 : unsmob_grob (get_property ("currentMusicalColumn")));
177 add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
180 finished_cresc_ = cresc_;
182 current_cresc_ev_ = 0;
184 else if (accepted_spanreqs_drul_[STOP])
186 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
192 if (accepted_spanreqs_drul_[START])
194 if (current_cresc_ev_)
196 String msg = _ ("already have a decrescendo");
197 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
198 msg = _ ("already have a crescendo");
200 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
201 current_cresc_ev_->origin ()->warning (_("Cresc started here"));
205 current_cresc_ev_ = accepted_spanreqs_drul_[START];
207 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
208 set_grob_direction (line_spanner_, d);
215 ly_symbol2string (current_cresc_ev_->get_property ("name"));
220 if (start_type == "DecrescendoEvent")
221 start_type = "decrescendo";
222 else if (start_type == "CrescendoEvent")
223 start_type = "crescendo";
225 SCM s = get_property ((start_type + "Spanner").to_str0 ());
226 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
228 cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
231 Pointer_group_interface::add_grob (finished_cresc_,
232 ly_symbol2scm ("adjacent-hairpins"),
235 Pointer_group_interface::add_grob (cresc_,
236 ly_symbol2scm ("adjacent-hairpins"),
239 cresc_->set_property ("grow-direction",
240 scm_int2num ((start_type == "crescendo")
241 ? BIGGER : SMALLER));
247 This is a convenient (and legacy) interface to TextSpanners
248 for use in (de)crescendi.
253 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
254 cresc_->set_property ("style", s);
255 context ()->set_property ((start_type
256 + "Spanner").to_str0 (), SCM_EOL);
257 s = get_property ((start_type + "Text").to_str0 ());
259 FIXME: use get_markup () to check type.
261 if (scm_is_string (s) || ly_c_pair_p (s))
263 cresc_->set_property ("edge-text",
264 scm_cons (s, scm_makfrom0str ("")));
265 context ()->set_property ((start_type + "Text").to_str0 (),
270 cresc_->set_bound (LEFT, script_
272 : unsmob_grob (get_property ("currentMusicalColumn")));
274 Axis_group_interface::add_element (line_spanner_, cresc_);
276 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
282 Dynamic_engraver::stop_translation_timestep ()
285 if (!current_cresc_ev_)
288 finished_line_spanner_ = line_spanner_;
294 accepted_spanreqs_drul_[START] = 0;
295 accepted_spanreqs_drul_[STOP] = 0;
299 Dynamic_engraver::finalize ()
304 && !line_spanner_->is_live ())
308 finished_line_spanner_ = line_spanner_;
313 && !cresc_->is_live ())
317 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
324 Dynamic_engraver::typeset_all ()
328 if (!finished_cresc_->get_bound (RIGHT))
330 finished_cresc_->set_bound (RIGHT, script_
332 : unsmob_grob (get_property ("currentMusicalColumn")));
334 if (finished_line_spanner_)
335 add_bound_item (finished_line_spanner_,
336 finished_cresc_->get_bound (RIGHT));
342 if (finished_line_spanner_)
347 extend-spanner-over-elements (finished_line_spanner_);
349 but this is rather kludgy, since finished_line_spanner_
350 typically has a staff-symbol field set , extending it over the
355 Grob * l = finished_line_spanner_->get_bound (LEFT);
356 Grob * r = finished_line_spanner_->get_bound (RIGHT);
358 finished_line_spanner_->set_bound (RIGHT, l);
360 finished_line_spanner_->set_bound (LEFT, r);
364 This is a isolated dynamic apparently, and does not even have
365 any interesting support item.
367 Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
368 Item * ci = dynamic_cast<Item*>(cc);
369 finished_line_spanner_->set_bound (RIGHT, ci);
370 finished_line_spanner_->set_bound (LEFT, ci);
373 finished_line_spanner_ = 0;
378 Dynamic_engraver::acknowledge_grob (Grob_info info)
383 if (Note_column::has_interface (info.grob_))
386 /* Don't refill killed spanner */
387 && line_spanner_->is_live ())
389 Side_position_interface::add_support (line_spanner_,info.grob_);
390 add_bound_item (line_spanner_,dynamic_cast<Item*> (info.grob_));
393 if (script_ && !script_->get_parent (X_AXIS))
395 SCM head = scm_last_pair (info.grob_->get_property ("note-heads"));
396 if (ly_c_pair_p (head))
397 script_->set_parent (unsmob_grob (ly_car (head)), X_AXIS);
400 else if (Script_interface::has_interface (info.grob_) && script_)
402 SCM p = info.grob_->get_property ("script-priority");
407 DynamicText doesn't really have a script-priority field.
409 if (scm_is_number (p)
411 < scm_to_int (script_->get_property ("script-priority")))
412 Side_position_interface::add_support (line_spanner_, info.grob_);
414 else if (Slur::has_interface (info.grob_))
415 slur_ = dynamic_cast<Spanner*> (info.grob_);
418 ENTER_DESCRIPTION (Dynamic_engraver,
420 "This engraver creates hairpins, dynamic texts, and their vertical\n"
421 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
422 "which takes care of vertical positioning. "
425 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
426 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
427 /* acks */ "note-column-interface script-interface slur-interface",