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_);
162 cresc_->set_bound (RIGHT, script_);
163 add_bound_item (line_spanner_, script_);
166 finished_cresc_ = cresc_;
168 current_cresc_ev_ = 0;
170 else if (accepted_spanreqs_drul_[STOP])
172 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
177 if (accepted_spanreqs_drul_[START])
179 if (current_cresc_ev_)
181 String msg = _ ("already have a decrescendo");
182 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
183 msg = _ ("already have a crescendo");
185 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
186 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
190 current_cresc_ev_ = accepted_spanreqs_drul_[START];
192 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
193 set_grob_direction (line_spanner_, d);
200 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
205 if (start_type == "DecrescendoEvent")
206 start_type = "decrescendo";
207 else if (start_type == "CrescendoEvent")
208 start_type = "crescendo";
210 SCM s = get_property ((start_type + "Spanner").to_str0 ());
211 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
213 cresc_ = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
216 Pointer_group_interface::add_grob (finished_cresc_,
217 ly_symbol2scm ("adjacent-hairpins"),
220 Pointer_group_interface::add_grob (cresc_,
221 ly_symbol2scm ("adjacent-hairpins"),
224 cresc_->set_property ("grow-direction",
225 scm_int2num ((start_type == "crescendo")
226 ? BIGGER : SMALLER));
230 This is a convenient (and legacy) interface to TextSpanners
231 for use in (de)crescendi.
236 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
237 cresc_->set_property ("style", s);
238 context ()->set_property ((start_type
239 + "Spanner").to_str0 (), SCM_EOL);
240 s = get_property ((start_type + "Text").to_str0 ());
242 FIXME: use get_markup () to check type.
244 if (scm_is_string (s) || scm_is_pair (s))
246 cresc_->set_property ("edge-text",
247 scm_cons (s, scm_makfrom0str ("")));
248 context ()->set_property ((start_type + "Text").to_str0 (),
255 cresc_->set_bound (LEFT, script_);
256 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
259 Axis_group_interface::add_element (line_spanner_, cresc_);
265 Dynamic_engraver::stop_translation_timestep ()
268 if (!current_cresc_ev_)
270 finished_line_spanner_ = line_spanner_;
275 if (cresc_ && !cresc_->get_bound (LEFT))
277 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
278 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
282 accepted_spanreqs_drul_[START] = 0;
283 accepted_spanreqs_drul_[STOP] = 0;
287 Dynamic_engraver::finalize ()
292 && !line_spanner_->is_live ())
296 finished_line_spanner_ = line_spanner_;
301 && !cresc_->is_live ())
305 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
312 Dynamic_engraver::typeset_all ()
316 if (!finished_cresc_->get_bound (RIGHT))
318 finished_cresc_->set_bound (RIGHT, script_
320 : unsmob_grob (get_property ("currentMusicalColumn")));
322 if (finished_line_spanner_)
323 add_bound_item (finished_line_spanner_,
324 finished_cresc_->get_bound (RIGHT));
330 if (finished_line_spanner_)
335 extend-spanner-over-elements (finished_line_spanner_);
337 but this is rather kludgy, since finished_line_spanner_
338 typically has a staff-symbol field set , extending it over the
343 Grob *l = finished_line_spanner_->get_bound (LEFT);
344 Grob *r = finished_line_spanner_->get_bound (RIGHT);
346 finished_line_spanner_->set_bound (RIGHT, l);
348 finished_line_spanner_->set_bound (LEFT, r);
352 This is a isolated dynamic apparently, and does not even have
353 any interesting support item.
355 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
356 Item *ci = dynamic_cast<Item *> (cc);
357 finished_line_spanner_->set_bound (RIGHT, ci);
358 finished_line_spanner_->set_bound (LEFT, ci);
361 finished_line_spanner_ = 0;
366 Dynamic_engraver::acknowledge_grob (Grob_info info)
371 if (Note_column::has_interface (info.grob_))
374 /* Don't refill killed spanner */
375 && line_spanner_->is_live ())
377 Side_position_interface::add_support (line_spanner_, info.grob_);
378 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob_));
381 if (script_ && !script_->get_parent (X_AXIS))
383 SCM head = scm_last_pair (info.grob_->get_property ("note-heads"));
384 if (scm_is_pair (head))
385 script_->set_parent (unsmob_grob (scm_car (head)), X_AXIS);
390 if (!cresc_->get_bound (LEFT))
392 cresc_->set_bound (LEFT, info.grob_);
393 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
397 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
399 finished_cresc_->set_bound (RIGHT, info.grob_);
403 else if (Script_interface::has_interface (info.grob_) && script_)
405 SCM p = info.grob_->get_property ("script-priority");
410 DynamicText doesn't really have a script-priority field.
412 if (scm_is_number (p)
414 < scm_to_int (script_->get_property ("script-priority")))
415 Side_position_interface::add_support (line_spanner_, info.grob_);
419 ADD_TRANSLATOR (Dynamic_engraver,
421 "This engraver creates hairpins, dynamic texts, and their vertical\n"
422 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
423 "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",