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"
27 * direction of text-dynamic-event if not equal to direction of
30 - TODO: this engraver is too complicated. We should split it into
31 the handling of the basic grobs and the linespanner
33 - 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 *line_spanner_;
46 Spanner *finished_line_spanner_;
47 Spanner *finished_cresc_;
50 Music *current_cresc_ev_;
52 Drul_array<Music *> accepted_spanreqs_drul_;
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 ();
69 Dynamic_engraver::Dynamic_engraver ()
74 finished_line_spanner_ = 0;
75 current_cresc_ev_ = 0;
79 accepted_spanreqs_drul_[START] = 0;
80 accepted_spanreqs_drul_[STOP] = 0;
84 Dynamic_engraver::try_music (Music *m)
86 if (m->is_mus_type ("absolute-dynamic-event"))
89 TODO: probably broken.
94 else if (m->is_mus_type ("decrescendo-event")
95 || m->is_mus_type ("crescendo-event"))
97 Direction d = to_dir (m->get_property ("span-direction"));
99 accepted_spanreqs_drul_[d] = m;
100 if (current_cresc_ev_ && d == START)
101 accepted_spanreqs_drul_[STOP] = m;
108 Dynamic_engraver::process_music ()
110 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
114 Music *rq = accepted_spanreqs_drul_[START];
115 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
123 During a (de)crescendo, pending event will not be cleared,
124 and a line-spanner will always be created, as \< \! are already
127 Note: line-spanner must always have at least same duration
128 as (de)crecsendo, b.o. line-breaking.
132 maybe we should leave dynamic texts to the text-engraver and
133 simply acknowledge them?
137 script_ = make_item ("DynamicText", script_ev_->self_scm ());
138 script_->set_property ("text",
139 script_ev_->get_property ("text"));
141 if (Direction d = to_dir (script_ev_->get_property ("direction")))
142 set_grob_direction (line_spanner_, d);
144 Axis_group_interface::add_element (line_spanner_, script_);
147 Music *stop_ev = accepted_spanreqs_drul_ [STOP]
148 ? accepted_spanreqs_drul_[STOP] : script_ev_;
150 if (accepted_spanreqs_drul_[STOP] || script_ev_)
153 finish side position alignment if the (de)cresc ends here, and
154 there are no new dynamics.
159 assert (!finished_cresc_ && cresc_);
163 cresc_->set_bound (RIGHT, script_);
164 add_bound_item (line_spanner_, script_);
167 finished_cresc_ = cresc_;
169 current_cresc_ev_ = 0;
171 else if (accepted_spanreqs_drul_[STOP])
173 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
178 if (accepted_spanreqs_drul_[START])
180 if (current_cresc_ev_)
182 String msg = _ ("already have a decrescendo");
183 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
184 msg = _ ("already have a crescendo");
186 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
187 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
191 current_cresc_ev_ = accepted_spanreqs_drul_[START];
193 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
194 set_grob_direction (line_spanner_, d);
201 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
206 if (start_type == "DecrescendoEvent")
207 start_type = "decrescendo";
208 else if (start_type == "CrescendoEvent")
209 start_type = "crescendo";
211 SCM s = get_property ((start_type + "Spanner").to_str0 ());
212 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
214 cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
217 Pointer_group_interface::add_grob (finished_cresc_,
218 ly_symbol2scm ("adjacent-hairpins"),
221 Pointer_group_interface::add_grob (cresc_,
222 ly_symbol2scm ("adjacent-hairpins"),
225 cresc_->set_property ("grow-direction",
226 scm_int2num ((start_type == "crescendo")
227 ? BIGGER : SMALLER));
231 This is a convenient (and legacy) interface to TextSpanners
232 for use in (de)crescendi.
237 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
238 cresc_->set_property ("style", s);
239 context ()->set_property ((start_type
240 + "Spanner").to_str0 (), SCM_EOL);
241 s = get_property ((start_type + "Text").to_str0 ());
243 FIXME: use get_markup () to check type.
245 if (scm_is_string (s) || scm_is_pair (s))
247 cresc_->set_property ("edge-text",
248 scm_cons (s, scm_makfrom0str ("")));
249 context ()->set_property ((start_type + "Text").to_str0 (),
256 cresc_->set_bound (LEFT, script_);
257 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
260 Axis_group_interface::add_element (line_spanner_, cresc_);
266 Dynamic_engraver::stop_translation_timestep ()
268 if (!current_cresc_ev_ && line_spanner_)
270 assert (!finished_line_spanner_);
271 finished_line_spanner_ = line_spanner_;
277 if (cresc_ && !cresc_->get_bound (LEFT))
279 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
280 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
284 accepted_spanreqs_drul_[START] = 0;
285 accepted_spanreqs_drul_[STOP] = 0;
289 Dynamic_engraver::finalize ()
294 && !line_spanner_->is_live ())
298 finished_line_spanner_ = line_spanner_;
303 && !cresc_->is_live ())
307 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
314 Dynamic_engraver::typeset_all ()
318 if (!finished_cresc_->get_bound (RIGHT))
320 finished_cresc_->set_bound (RIGHT, script_
322 : unsmob_grob (get_property ("currentMusicalColumn")));
324 if (finished_line_spanner_)
325 add_bound_item (finished_line_spanner_,
326 finished_cresc_->get_bound (RIGHT));
332 if (finished_line_spanner_)
337 extend-spanner-over-elements (finished_line_spanner_);
339 but this is rather kludgy, since finished_line_spanner_
340 typically has a staff-symbol field set , extending it over the
345 Grob *l = finished_line_spanner_->get_bound (LEFT);
346 Grob *r = finished_line_spanner_->get_bound (RIGHT);
348 finished_line_spanner_->set_bound (RIGHT, l);
350 finished_line_spanner_->set_bound (LEFT, r);
354 This is a isolated dynamic apparently, and does not even have
355 any interesting support item.
357 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
358 Item *ci = dynamic_cast<Item *> (cc);
359 finished_line_spanner_->set_bound (RIGHT, ci);
360 finished_line_spanner_->set_bound (LEFT, ci);
363 finished_line_spanner_ = 0;
368 Dynamic_engraver::acknowledge_grob (Grob_info info)
373 if (Note_column::has_interface (info.grob ()))
376 /* Don't refill killed spanner */
377 && line_spanner_->is_live ())
379 Side_position_interface::add_support (line_spanner_, info.grob ());
380 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
383 if (script_ && !script_->get_parent (X_AXIS))
385 SCM head = scm_last_pair (info.grob ()->get_property ("note-heads"));
386 if (scm_is_pair (head))
388 script_->set_parent (unsmob_grob (scm_car (head)), X_AXIS);
389 script_->add_offset_callback (Self_alignment_interface::centered_on_parent_proc,
397 if (!cresc_->get_bound (LEFT))
399 cresc_->set_bound (LEFT, info.grob ());
400 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
404 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
406 finished_cresc_->set_bound (RIGHT, info.grob ());
410 else if (Script_interface::has_interface (info.grob ()) && script_)
412 SCM p = info.grob ()->get_property ("script-priority");
417 DynamicText doesn't really have a script-priority field.
419 if (scm_is_number (p)
421 < scm_to_int (script_->get_property ("script-priority")))
422 Side_position_interface::add_support (line_spanner_, info.grob ());
426 ADD_TRANSLATOR (Dynamic_engraver,
428 "This engraver creates hairpins, dynamic texts, and their vertical\n"
429 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
430 "which takes care of vertical positioning. ",
432 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
433 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
434 /* acks */ "note-column-interface script-interface",