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"
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
37 print text & hairpin dynamics.
39 class Dynamic_engraver : public Engraver
42 Spanner *line_spanner_;
45 Spanner *finished_line_spanner_;
46 Spanner *finished_cresc_;
49 Music *current_cresc_ev_;
51 Drul_array<Music *> accepted_spanreqs_drul_;
53 Link_array<Note_column> pending_columns_;
54 Link_array<Grob> pending_elements_;
58 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
61 virtual void finalize ();
62 virtual void acknowledge_grob (Grob_info);
63 virtual bool try_music (Music *req);
64 virtual void stop_translation_timestep ();
65 virtual void process_music ();
68 Dynamic_engraver::Dynamic_engraver ()
73 finished_line_spanner_ = 0;
74 current_cresc_ev_ = 0;
78 accepted_spanreqs_drul_[START] = 0;
79 accepted_spanreqs_drul_[STOP] = 0;
83 Dynamic_engraver::try_music (Music *m)
85 if (m->is_mus_type ("absolute-dynamic-event"))
88 TODO: probably broken.
93 else if (m->is_mus_type ("decrescendo-event")
94 || m->is_mus_type ("crescendo-event"))
96 Direction d = to_dir (m->get_property ("span-direction"));
98 accepted_spanreqs_drul_[d] = m;
99 if (current_cresc_ev_ && d == START)
100 accepted_spanreqs_drul_[STOP] = m;
107 Dynamic_engraver::process_music ()
109 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
113 Music *rq = accepted_spanreqs_drul_[START];
114 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
122 During a (de)crescendo, pending event will not be cleared,
123 and a line-spanner will always be created, as \< \! are already
126 Note: line-spanner must always have at least same duration
127 as (de)crecsendo, b.o. line-breaking.
131 maybe we should leave dynamic texts to the text-engraver and
132 simply acknowledge them?
136 script_ = make_item ("DynamicText", script_ev_->self_scm ());
137 script_->set_property ("text",
138 script_ev_->get_property ("text"));
140 if (Direction d = to_dir (script_ev_->get_property ("direction")))
141 set_grob_direction (line_spanner_, d);
143 Axis_group_interface::add_element (line_spanner_, script_);
146 Music *stop_ev = accepted_spanreqs_drul_ [STOP]
147 ? accepted_spanreqs_drul_[STOP] : script_ev_;
149 if (accepted_spanreqs_drul_[STOP] || script_ev_)
152 finish side position alignment if the (de)cresc ends here, and
153 there are no new dynamics.
158 assert (!finished_cresc_ && cresc_);
160 cresc_->set_bound (RIGHT, script_
162 : unsmob_grob (get_property ("currentMusicalColumn")));
163 add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
165 finished_cresc_ = cresc_;
167 current_cresc_ev_ = 0;
169 else if (accepted_spanreqs_drul_[STOP])
171 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
176 if (accepted_spanreqs_drul_[START])
178 if (current_cresc_ev_)
180 String msg = _ ("already have a decrescendo");
181 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
182 msg = _ ("already have a crescendo");
184 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
185 current_cresc_ev_->origin ()->warning (_ ("Cresc started here"));
189 current_cresc_ev_ = accepted_spanreqs_drul_[START];
191 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
192 set_grob_direction (line_spanner_, d);
199 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
204 if (start_type == "DecrescendoEvent")
205 start_type = "decrescendo";
206 else if (start_type == "CrescendoEvent")
207 start_type = "crescendo";
209 SCM s = get_property ((start_type + "Spanner").to_str0 ());
210 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
212 cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
215 Pointer_group_interface::add_grob (finished_cresc_,
216 ly_symbol2scm ("adjacent-hairpins"),
219 Pointer_group_interface::add_grob (cresc_,
220 ly_symbol2scm ("adjacent-hairpins"),
223 cresc_->set_property ("grow-direction",
224 scm_int2num ((start_type == "crescendo")
225 ? BIGGER : SMALLER));
229 This is a convenient (and legacy) interface to TextSpanners
230 for use in (de)crescendi.
235 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
236 cresc_->set_property ("style", s);
237 context ()->set_property ((start_type
238 + "Spanner").to_str0 (), SCM_EOL);
239 s = get_property ((start_type + "Text").to_str0 ());
241 FIXME: use get_markup () to check type.
243 if (scm_is_string (s) || scm_is_pair (s))
245 cresc_->set_property ("edge-text",
246 scm_cons (s, scm_makfrom0str ("")));
247 context ()->set_property ((start_type + "Text").to_str0 (),
254 cresc_->set_bound (LEFT, script_);
255 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
258 Axis_group_interface::add_element (line_spanner_, cresc_);
264 Dynamic_engraver::stop_translation_timestep ()
267 if (!current_cresc_ev_)
269 finished_line_spanner_ = line_spanner_;
274 if (cresc_ && !cresc_->get_bound (LEFT))
276 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
277 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
281 accepted_spanreqs_drul_[START] = 0;
282 accepted_spanreqs_drul_[STOP] = 0;
286 Dynamic_engraver::finalize ()
291 && !line_spanner_->is_live ())
295 finished_line_spanner_ = line_spanner_;
300 && !cresc_->is_live ())
304 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
311 Dynamic_engraver::typeset_all ()
315 if (!finished_cresc_->get_bound (RIGHT))
317 finished_cresc_->set_bound (RIGHT, script_
319 : unsmob_grob (get_property ("currentMusicalColumn")));
321 if (finished_line_spanner_)
322 add_bound_item (finished_line_spanner_,
323 finished_cresc_->get_bound (RIGHT));
329 if (finished_line_spanner_)
334 extend-spanner-over-elements (finished_line_spanner_);
336 but this is rather kludgy, since finished_line_spanner_
337 typically has a staff-symbol field set , extending it over the
342 Grob *l = finished_line_spanner_->get_bound (LEFT);
343 Grob *r = finished_line_spanner_->get_bound (RIGHT);
345 finished_line_spanner_->set_bound (RIGHT, l);
347 finished_line_spanner_->set_bound (LEFT, r);
351 This is a isolated dynamic apparently, and does not even have
352 any interesting support item.
354 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
355 Item *ci = dynamic_cast<Item *> (cc);
356 finished_line_spanner_->set_bound (RIGHT, ci);
357 finished_line_spanner_->set_bound (LEFT, ci);
360 finished_line_spanner_ = 0;
365 Dynamic_engraver::acknowledge_grob (Grob_info info)
370 if (Note_column::has_interface (info.grob_))
373 /* Don't refill killed spanner */
374 && line_spanner_->is_live ())
376 Side_position_interface::add_support (line_spanner_, info.grob_);
377 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob_));
380 if (script_ && !script_->get_parent (X_AXIS))
382 SCM head = scm_last_pair (info.grob_->get_property ("note-heads"));
383 if (scm_is_pair (head))
384 script_->set_parent (unsmob_grob (scm_car (head)), X_AXIS);
387 if (cresc_ && !cresc_->get_bound (LEFT))
389 cresc_->set_bound (LEFT, info.grob_);
390 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
393 else if (Script_interface::has_interface (info.grob_) && script_)
395 SCM p = info.grob_->get_property ("script-priority");
400 DynamicText doesn't really have a script-priority field.
402 if (scm_is_number (p)
404 < scm_to_int (script_->get_property ("script-priority")))
405 Side_position_interface::add_support (line_spanner_, info.grob_);
409 ADD_TRANSLATOR (Dynamic_engraver,
411 "This engraver creates hairpins, dynamic texts, and their vertical\n"
412 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
413 "which takes care of vertical positioning. ",
415 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
416 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
417 /* acks */ "note-column-interface script-interface",