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"
28 * direction of text-dynamic-event if not equal to direction of
31 - TODO: this engraver is too complicated. We should split it into
32 the handling of the basic grobs and the linespanner
34 - TODO: the line-spanner is not killed after the (de)crescs are
39 print text & hairpin dynamics.
41 class Dynamic_engraver : public Engraver
44 Spanner *line_spanner_;
47 Spanner *finished_line_spanner_;
48 Spanner *finished_cresc_;
51 Music *current_cresc_ev_;
53 Drul_array<Music *> accepted_spanevents_drul_;
55 Link_array<Note_column> pending_columns_;
56 Link_array<Grob> pending_elements_;
60 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
63 virtual void finalize ();
64 virtual void acknowledge_grob (Grob_info);
65 virtual bool try_music (Music *event);
66 PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
67 PRECOMPUTED_VIRTUAL void process_music ();
70 Dynamic_engraver::Dynamic_engraver ()
75 finished_line_spanner_ = 0;
76 current_cresc_ev_ = 0;
80 accepted_spanevents_drul_[START] = 0;
81 accepted_spanevents_drul_[STOP] = 0;
85 Dynamic_engraver::try_music (Music *m)
87 if (m->is_mus_type ("absolute-dynamic-event"))
90 TODO: probably broken.
95 else if (m->is_mus_type ("decrescendo-event")
96 || m->is_mus_type ("crescendo-event"))
98 Direction d = to_dir (m->get_property ("span-direction"));
100 accepted_spanevents_drul_[d] = m;
101 if (current_cresc_ev_ && d == START)
102 accepted_spanevents_drul_[STOP] = m;
109 Dynamic_engraver::process_music ()
111 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
115 Music *rq = accepted_spanevents_drul_[START];
116 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
124 During a (de)crescendo, pending event will not be cleared,
125 and a line-spanner will always be created, as \< \! are already
128 Note: line-spanner must always have at least same duration
129 as (de)crecsendo, b.o. line-breaking.
133 maybe we should leave dynamic texts to the text-engraver and
134 simply acknowledge them?
138 script_ = make_item ("DynamicText", script_ev_->self_scm ());
139 script_->set_property ("text",
140 script_ev_->get_property ("text"));
142 if (Direction d = to_dir (script_ev_->get_property ("direction")))
143 set_grob_direction (line_spanner_, d);
145 Axis_group_interface::add_element (line_spanner_, script_);
148 Music *stop_ev = accepted_spanevents_drul_ [STOP]
149 ? accepted_spanevents_drul_[STOP] : script_ev_;
151 if (accepted_spanevents_drul_[STOP] || script_ev_)
154 finish side position alignment if the (de)cresc ends here, and
155 there are no new dynamics.
160 assert (!finished_cresc_ && cresc_);
164 cresc_->set_bound (RIGHT, script_);
165 add_bound_item (line_spanner_, script_);
168 finished_cresc_ = cresc_;
170 current_cresc_ev_ = 0;
172 else if (accepted_spanevents_drul_[STOP])
174 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
179 if (accepted_spanevents_drul_[START])
181 if (current_cresc_ev_)
183 String msg = _ ("already have a decrescendo");
184 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
185 msg = _ ("already have a crescendo");
187 accepted_spanevents_drul_[START]->origin ()->warning (msg);
188 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
192 current_cresc_ev_ = accepted_spanevents_drul_[START];
194 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
195 set_grob_direction (line_spanner_, d);
202 = ly_symbol2string (current_cresc_ev_->get_property ("name"));
207 if (start_type == "DecrescendoEvent")
208 start_type = "decrescendo";
209 else if (start_type == "CrescendoEvent")
210 start_type = "crescendo";
212 SCM s = get_property ((start_type + "Spanner").to_str0 ());
213 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
215 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
218 Pointer_group_interface::add_grob (finished_cresc_,
219 ly_symbol2scm ("adjacent-hairpins"),
222 Pointer_group_interface::add_grob (cresc_,
223 ly_symbol2scm ("adjacent-hairpins"),
226 cresc_->set_property ("grow-direction",
227 scm_int2num ((start_type == "crescendo")
228 ? BIGGER : SMALLER));
232 This is a convenient (and legacy) interface to TextSpanners
233 for use in (de)crescendi.
238 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_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 (scm_is_string (s) || scm_is_pair (s))
248 cresc_->set_property ("edge-text",
249 scm_cons (s, scm_makfrom0str ("")));
250 context ()->set_property ((start_type + "Text").to_str0 (),
257 cresc_->set_bound (LEFT, script_);
258 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
261 Axis_group_interface::add_element (line_spanner_, cresc_);
267 Dynamic_engraver::stop_translation_timestep ()
269 if (!current_cresc_ev_ && line_spanner_)
271 assert (!finished_line_spanner_);
272 finished_line_spanner_ = line_spanner_;
278 if (cresc_ && !cresc_->get_bound (LEFT))
280 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
281 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
285 accepted_spanevents_drul_[START] = 0;
286 accepted_spanevents_drul_[STOP] = 0;
290 Dynamic_engraver::finalize ()
295 && !line_spanner_->is_live ())
299 finished_line_spanner_ = line_spanner_;
304 && !cresc_->is_live ())
308 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
315 Dynamic_engraver::typeset_all ()
319 if (!finished_cresc_->get_bound (RIGHT))
321 finished_cresc_->set_bound (RIGHT, script_
323 : unsmob_grob (get_property ("currentMusicalColumn")));
325 if (finished_line_spanner_)
326 add_bound_item (finished_line_spanner_,
327 finished_cresc_->get_bound (RIGHT));
333 if (finished_line_spanner_)
338 extend-spanner-over-elements (finished_line_spanner_);
340 but this is rather kludgy, since finished_line_spanner_
341 typically has a staff-symbol field set , extending it over the
346 Grob *l = finished_line_spanner_->get_bound (LEFT);
347 Grob *r = finished_line_spanner_->get_bound (RIGHT);
349 finished_line_spanner_->set_bound (RIGHT, l);
351 finished_line_spanner_->set_bound (LEFT, r);
355 This is a isolated dynamic apparently, and does not even have
356 any interesting support item.
358 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
359 Item *ci = dynamic_cast<Item *> (cc);
360 finished_line_spanner_->set_bound (RIGHT, ci);
361 finished_line_spanner_->set_bound (LEFT, ci);
364 finished_line_spanner_ = 0;
369 Dynamic_engraver::acknowledge_grob (Grob_info info)
374 if (Note_column::has_interface (info.grob ()))
377 /* Don't refill killed spanner */
378 && line_spanner_->is_live ())
380 Side_position_interface::add_support (line_spanner_, info.grob ());
381 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
384 if (script_ && !script_->get_parent (X_AXIS))
386 extract_grob_set (info.grob (), "note-heads", heads);
389 Grob *head = heads[0];
390 script_->set_parent (head, X_AXIS);
391 script_->add_offset_callback (Self_alignment_interface::centered_on_parent_proc,
399 if (!cresc_->get_bound (LEFT))
401 cresc_->set_bound (LEFT, info.grob ());
402 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
406 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
408 finished_cresc_->set_bound (RIGHT, info.grob ());
412 else if (Script_interface::has_interface (info.grob ()) && script_)
414 SCM p = info.grob ()->get_property ("script-priority");
419 DynamicText doesn't really have a script-priority field.
421 if (scm_is_number (p)
423 < scm_to_int (script_->get_property ("script-priority")))
424 Side_position_interface::add_support (line_spanner_, info.grob ());
428 #include "translator.icc"
430 ADD_TRANSLATOR (Dynamic_engraver,
432 "This engraver creates hairpins, dynamic texts, and their vertical\n"
433 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
434 "which takes care of vertical positioning. ",
436 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
437 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
438 /* acks */ "note-column-interface script-interface",