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 "dimensions.hh"
12 #include "paper-column.hh"
13 #include "note-column.hh"
15 #include "side-position-interface.hh"
16 #include "engraver.hh"
17 #include "group-interface.hh"
18 #include "directional-element-interface.hh"
20 #include "axis-group-interface.hh"
26 * direction of text-dynamic-event if not equal to direction of
29 - TODO: this engraver is too complicated. We should split it into
30 the handling of the basic grobs and the linespanner
32 - TODO: the line-spanner is not killed after the (de)crescs are
38 print text & hairpin dynamics.
40 class Dynamic_engraver : public Engraver
43 Spanner * finished_cresc_;
48 Music * current_cresc_ev_;
49 Drul_array<Music*> accepted_spanreqs_drul_;
51 Spanner* line_spanner_;
52 Spanner* finished_line_spanner_;
54 Link_array<Note_column> pending_columns_;
55 Link_array<Grob> pending_elements_;
59 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
62 virtual void finalize ();
63 virtual void acknowledge_grob (Grob_info);
64 virtual bool try_music (Music *req);
65 virtual void stop_translation_timestep ();
66 virtual void process_music ();
72 Dynamic_engraver::Dynamic_engraver ()
77 finished_line_spanner_ = 0;
78 current_cresc_ev_ = 0;
82 accepted_spanreqs_drul_[START] = 0;
83 accepted_spanreqs_drul_[STOP] = 0;
87 Dynamic_engraver::try_music (Music * m)
89 if (m->is_mus_type ("absolute-dynamic-event"))
92 TODO: probably broken.
97 else if (m->is_mus_type ("decrescendo-event")
98 || m->is_mus_type ("crescendo-event"))
100 Direction d = to_dir (m->get_property ("span-direction"));
102 accepted_spanreqs_drul_[d] = m;
103 if (current_cresc_ev_ && d == START)
104 accepted_spanreqs_drul_[STOP] = m;
111 Dynamic_engraver::process_music ()
113 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
117 Music * rq = accepted_spanreqs_drul_[START];
118 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm (): SCM_EOL );
126 During a (de)crescendo, pending event will not be cleared,
127 and a line-spanner will always be created, as \< \! are already
130 Note: line-spanner must always have at least same duration
131 as (de)crecsendo, b.o. line-breaking.
137 maybe we should leave dynamic texts to the text-engraver and
138 simply acknowledge them?
142 script_ = make_item ("DynamicText", script_ev_->self_scm ());
143 script_->set_property ("text",
144 script_ev_->get_property ("text"));
147 if (Direction d = to_dir (script_ev_->get_property ("direction")))
148 set_grob_direction (line_spanner_, d);
150 Axis_group_interface::add_element (line_spanner_, script_);
154 Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
155 accepted_spanreqs_drul_[STOP] : script_ev_;
157 if (accepted_spanreqs_drul_[STOP] || script_ev_)
160 finish side position alignment if the (de)cresc ends here, and
161 there are no new dynamics.
167 assert (!finished_cresc_ && cresc_);
169 cresc_->set_bound (RIGHT, script_
171 : unsmob_grob (get_property ("currentMusicalColumn")));
172 add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
175 finished_cresc_ = cresc_;
177 current_cresc_ev_ = 0;
179 else if (accepted_spanreqs_drul_[STOP])
181 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
187 if (accepted_spanreqs_drul_[START])
189 if (current_cresc_ev_)
191 String msg = _ ("already have a decrescendo");
192 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
193 msg = _ ("already have a crescendo");
195 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
196 current_cresc_ev_->origin ()->warning (_("Cresc started here"));
200 current_cresc_ev_ = accepted_spanreqs_drul_[START];
202 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
203 set_grob_direction (line_spanner_, d);
210 ly_symbol2string (current_cresc_ev_->get_property ("name"));
215 if (start_type == "DecrescendoEvent")
216 start_type = "decrescendo";
217 else if (start_type == "CrescendoEvent")
218 start_type = "crescendo";
220 SCM s = get_property ((start_type + "Spanner").to_str0 ());
221 if (!ly_c_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
223 cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
224 cresc_->set_property ("grow-direction",
225 scm_int2num ((start_type == "crescendo")
226 ? BIGGER : SMALLER));
232 This is a convenient (and legacy) interface to TextSpanners
233 for use in (de)crescendi.
238 cresc_ = make_spanner ("TextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
239 cresc_->set_property ("style", s);
240 context ()->set_property ((start_type
241 + "Spanner").to_str0 (), SCM_EOL);
242 s = get_property ((start_type + "Text").to_str0 ());
244 FIXME: use get_markup () to check type.
246 if (ly_c_string_p (s) || ly_c_pair_p (s))
248 cresc_->set_property ("edge-text",
249 scm_cons (s, scm_makfrom0str ("")));
250 context ()->set_property ((start_type + "Text").to_str0 (),
255 cresc_->set_bound (LEFT, script_
257 : unsmob_grob (get_property ("currentMusicalColumn")));
259 Axis_group_interface::add_element (line_spanner_, cresc_);
261 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
268 Dynamic_engraver::stop_translation_timestep ()
271 if (!current_cresc_ev_)
273 finished_line_spanner_ = line_spanner_;
279 accepted_spanreqs_drul_[START] = 0;
280 accepted_spanreqs_drul_[STOP] = 0;
284 Dynamic_engraver::finalize ()
289 && !line_spanner_->live ())
293 finished_line_spanner_ = line_spanner_;
302 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
309 Dynamic_engraver::typeset_all ()
312 remove suicided spanners,
313 ugh: we'll need this for every spanner, beam, slur
314 Hmm, how to do this, cleanly?
315 Maybe just check at typeset_grob ()?
318 && !finished_cresc_->live ())
320 if (finished_line_spanner_
321 && !finished_line_spanner_->live ())
322 finished_line_spanner_ = 0;
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;
376 Dynamic_engraver::acknowledge_grob (Grob_info i)
381 if (Note_column::has_interface (i.grob_))
384 /* Don't refill killed spanner */
385 && line_spanner_->live ())
387 Side_position_interface::add_support (line_spanner_,i.grob_);
388 add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_));
391 if (script_ && !script_->get_parent (X_AXIS))
393 SCM head = scm_last_pair (i.grob_->get_property ("note-heads"));
394 if (ly_c_pair_p (head))
395 script_->set_parent (unsmob_grob (ly_car (head)), X_AXIS);
399 else if (Script_interface::has_interface (i.grob_) && script_)
401 SCM p = i.grob_->get_property ("script-priority");
406 DynamicText doesn't really have a script-priority field.
408 if (ly_c_number_p (p)
409 && ly_scm2int (p) < ly_scm2int (script_->get_property ("script-priority")))
411 Side_position_interface::add_support (line_spanner_, i.grob_);
416 ENTER_DESCRIPTION (Dynamic_engraver,
418 "This engraver creates hairpins, dynamic texts, and their vertical\n"
419 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
420 "which takes care of vertical positioning. "
423 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
424 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
425 /* acks */ "note-column-interface script-interface",