2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 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 "interval.hh"
16 #include "note-column.hh"
17 #include "paper-column.hh"
18 #include "script-interface.hh"
19 #include "side-position-interface.hh"
20 #include "staff-symbol-referencer.hh"
22 #include "self-alignment-interface.hh"
23 #include "pointer-group-interface.hh"
25 #include "translator.icc"
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
41 print text & hairpin dynamics.
43 class Dynamic_engraver : public Engraver
46 Spanner *line_spanner_;
49 Spanner *finished_line_spanner_;
50 Spanner *finished_cresc_;
53 Music *current_cresc_ev_;
55 Drul_array<Music *> accepted_spanevents_drul_;
57 Link_array<Note_column> pending_columns_;
58 Link_array<Grob> pending_elements_;
62 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
63 DECLARE_ACKNOWLEDGER (script);
64 DECLARE_ACKNOWLEDGER (note_column);
65 DECLARE_ACKNOWLEDGER (slur);
68 virtual void finalize ();
69 virtual bool try_music (Music *event);
70 void stop_translation_timestep ();
71 void process_music ();
74 Dynamic_engraver::Dynamic_engraver ()
79 finished_line_spanner_ = 0;
80 current_cresc_ev_ = 0;
84 accepted_spanevents_drul_[START] = 0;
85 accepted_spanevents_drul_[STOP] = 0;
89 Dynamic_engraver::try_music (Music *m)
91 if (m->is_mus_type ("absolute-dynamic-event"))
94 TODO: probably broken.
99 else if (m->is_mus_type ("decrescendo-event")
100 || m->is_mus_type ("crescendo-event"))
102 Direction d = to_dir (m->get_property ("span-direction"));
104 accepted_spanevents_drul_[d] = m;
105 if (current_cresc_ev_ && d == START)
106 accepted_spanevents_drul_[STOP] = m;
113 Dynamic_engraver::process_music ()
115 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
119 Music *rq = accepted_spanevents_drul_[START];
120 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
127 During a (de)crescendo, pending event will not be cleared,
128 and a line-spanner will always be created, as \< \! are already
131 Note: line-spanner must always have at least same duration
132 as (de)crecsendo, b.o. line-breaking.
136 maybe we should leave dynamic texts to the text-engraver and
137 simply acknowledge them?
141 script_ = make_item ("DynamicText", script_ev_->self_scm ());
142 script_->set_property ("text",
143 script_ev_->get_property ("text"));
145 if (Direction d = to_dir (script_ev_->get_property ("direction")))
146 set_grob_direction (line_spanner_, d);
147 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
148 set_grob_direction (script_, d);
150 Axis_group_interface::add_element (line_spanner_, script_);
153 Music *stop_ev = accepted_spanevents_drul_ [STOP]
154 ? accepted_spanevents_drul_[STOP] : script_ev_;
156 if (accepted_spanevents_drul_[STOP] || script_ev_)
159 finish side position alignment if the (de)cresc ends here, and
160 there are no new dynamics.
165 assert (!finished_cresc_ && cresc_);
169 cresc_->set_bound (RIGHT, script_);
170 add_bound_item (line_spanner_, script_);
173 finished_cresc_ = cresc_;
175 current_cresc_ev_ = 0;
177 else if (accepted_spanevents_drul_[STOP])
179 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
184 if (accepted_spanevents_drul_[START])
186 if (current_cresc_ev_)
188 String msg = _ ("already have a decrescendo");
189 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
190 msg = _ ("already have a crescendo");
192 accepted_spanevents_drul_[START]->origin ()->warning (msg);
193 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
197 current_cresc_ev_ = accepted_spanevents_drul_[START];
199 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
200 set_grob_direction (line_spanner_, d);
207 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
212 if (start_type == "DecrescendoEvent")
213 start_type = "decrescendo";
214 else if (start_type == "CrescendoEvent")
215 start_type = "crescendo";
217 SCM s = get_property ((start_type + "Spanner").to_str0 ());
218 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
220 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
223 Pointer_group_interface::add_grob (finished_cresc_,
224 ly_symbol2scm ("adjacent-hairpins"),
227 Pointer_group_interface::add_grob (cresc_,
228 ly_symbol2scm ("adjacent-hairpins"),
231 cresc_->set_property ("grow-direction",
232 scm_from_int ((start_type == "crescendo")
233 ? BIGGER : SMALLER));
237 This is a convenient (and legacy) interface to TextSpanners
238 for use in (de)crescendi.
243 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
244 cresc_->set_property ("style", s);
245 context ()->set_property ((start_type
246 + "Spanner").to_str0 (), SCM_EOL);
247 s = get_property ((start_type + "Text").to_str0 ());
249 FIXME: use get_markup () to check type.
251 if (scm_is_string (s) || scm_is_pair (s))
253 cresc_->set_property ("edge-text",
254 scm_cons (s, scm_makfrom0str ("")));
255 context ()->set_property ((start_type + "Text").to_str0 (),
262 cresc_->set_bound (LEFT, script_);
263 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
266 Axis_group_interface::add_element (line_spanner_, cresc_);
272 Dynamic_engraver::stop_translation_timestep ()
274 if (!current_cresc_ev_ && line_spanner_)
276 assert (!finished_line_spanner_);
277 finished_line_spanner_ = line_spanner_;
283 if (cresc_ && !cresc_->get_bound (LEFT))
285 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
286 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
290 accepted_spanevents_drul_[START] = 0;
291 accepted_spanevents_drul_[STOP] = 0;
295 Dynamic_engraver::finalize ()
300 && !line_spanner_->is_live ())
304 finished_line_spanner_ = line_spanner_;
309 && !cresc_->is_live ())
313 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
320 Dynamic_engraver::typeset_all ()
324 if (!finished_cresc_->get_bound (RIGHT))
326 finished_cresc_->set_bound (RIGHT, script_
328 : unsmob_grob (get_property ("currentMusicalColumn")));
330 if (finished_line_spanner_)
331 add_bound_item (finished_line_spanner_,
332 finished_cresc_->get_bound (RIGHT));
338 if (finished_line_spanner_)
343 extend-spanner-over-elements (finished_line_spanner_);
345 but this is rather kludgy, since finished_line_spanner_
346 typically has a staff-symbol field set , extending it over the
351 Grob *l = finished_line_spanner_->get_bound (LEFT);
352 Grob *r = finished_line_spanner_->get_bound (RIGHT);
354 finished_line_spanner_->set_bound (RIGHT, l);
356 finished_line_spanner_->set_bound (LEFT, r);
360 This is a isolated dynamic apparently, and does not even have
361 any interesting support item.
363 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
364 Item *ci = dynamic_cast<Item *> (cc);
365 finished_line_spanner_->set_bound (RIGHT, ci);
366 finished_line_spanner_->set_bound (LEFT, ci);
369 finished_line_spanner_ = 0;
374 Dynamic_engraver::acknowledge_slur (Grob_info info)
377 Side_position_interface::add_support (line_spanner_, info.grob ());
381 Dynamic_engraver::acknowledge_note_column (Grob_info info)
387 /* Don't refill killed spanner */
388 && line_spanner_->is_live ())
390 Side_position_interface::add_support (line_spanner_, info.grob ());
391 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
394 if (script_ && !script_->get_parent (X_AXIS))
396 extract_grob_set (info.grob (), "note-heads", heads);
399 Grob *head = heads[0];
400 script_->set_parent (head, X_AXIS);
401 script_->add_offset_callback (Self_alignment_interface::centered_on_parent_proc,
408 if (!cresc_->get_bound (LEFT))
410 cresc_->set_bound (LEFT, info.grob ());
411 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
415 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
416 finished_cresc_->set_bound (RIGHT, info.grob ());
420 Dynamic_engraver::acknowledge_script (Grob_info info)
422 if (!line_spanner_ || !script_)
425 SCM p = info.grob ()->get_property ("script-priority");
430 DynamicText doesn't really have a script-priority field.
432 if (scm_is_number (p)
434 < scm_to_int (script_->get_property ("script-priority")))
435 Side_position_interface::add_support (line_spanner_, info.grob ());
438 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
439 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
440 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
442 ADD_TRANSLATOR (Dynamic_engraver,
444 "This engraver creates hairpins, dynamic texts, and their vertical\n"
445 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
446 "which takes care of vertical positioning. ",
448 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
449 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",