* Documentation/user/tutorial.itely (A lead sheet): remove
[lilypond.git] / lily / dynamic-engraver.cc
blob12dc059501cc22e6ab619b5460b74e3314145cfa
1 /*
2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "warn.hh"
9 #include "dimensions.hh"
10 #include "hairpin.hh"
11 #include "event.hh"
12 #include "paper-column.hh"
13 #include "note-column.hh"
14 #include "item.hh"
15 #include "side-position-interface.hh"
16 #include "engraver.hh"
17 #include "group-interface.hh"
18 #include "directional-element-interface.hh"
19 #include "translator-group.hh"
20 #include "axis-group-interface.hh"
21 #include "script.hh"
24 TODO:
26 * direction of text-dynamic-event if not equal to direction of
27 line-spanner
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
33 finished.
37 /**
38 print text & hairpin dynamics.
40 class Dynamic_engraver : public Engraver
42 Item * script_;
43 Spanner * finished_cresc_;
44 Spanner * cresc_;
46 Music* script_ev_;
48 Music * current_cresc_ev_;
49 Drul_array<Music*> accepted_spanreqs_drul_;
51 Spanner* line_spanner_;
52 Spanner* finished_line_spanner_;
54 Link_array<Note_column> pending_columns_;
55 Link_array<Grob> pending_elements_;
57 void typeset_all ();
59 TRANSLATOR_DECLARATIONS(Dynamic_engraver );
61 protected:
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 ();
67 virtual void start_translation_timestep ();
73 Dynamic_engraver::Dynamic_engraver ()
75 script_ = 0;
76 finished_cresc_ = 0;
77 line_spanner_ = 0;
78 finished_line_spanner_ = 0;
79 current_cresc_ev_ = 0;
80 cresc_ =0;
82 script_ev_ = 0;
83 accepted_spanreqs_drul_[START] = 0;
84 accepted_spanreqs_drul_[STOP] = 0;
87 void
88 Dynamic_engraver::start_translation_timestep ()
90 script_ev_ = 0;
91 accepted_spanreqs_drul_[START] = 0;
92 accepted_spanreqs_drul_[STOP] = 0;
95 bool
96 Dynamic_engraver::try_music (Music * m)
98 if (m->is_mus_type ("absolute-dynamic-event"))
101 TODO: probably broken.
103 script_ev_ = m;
104 return true;
106 else if (m->is_mus_type ("abort-event"))
108 accepted_spanreqs_drul_[LEFT] = 0;
109 accepted_spanreqs_drul_[RIGHT] = 0;
111 Let's not kill the line spanner, since that would fuck up
112 earlier, not-to-be-terminated stuff.
114 It will disappear by itself when stop_translation_timestep
115 () finds that there is nothing to support anymore. */
117 if (cresc_)
118 cresc_->suicide ();
119 cresc_ = 0;
121 else if (m->is_mus_type ("decrescendo-event")
122 || m->is_mus_type ("crescendo-event"))
124 Direction d = to_dir (m->get_mus_property ("span-direction"));
125 accepted_spanreqs_drul_[d] = m;
126 return true;
128 return false;
131 void
132 Dynamic_engraver::process_music ()
134 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
136 if (!line_spanner_)
138 line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
140 Music * rq = accepted_spanreqs_drul_[START];
141 if (script_ev_)
142 rq = script_ev_ ;
143 announce_grob(line_spanner_, rq ? rq->self_scm(): SCM_EOL);
148 During a (de)crescendo, pending event will not be cleared,
149 and a line-spanner will always be created, as \< \! are already
150 two events.
152 Note: line-spanner must always have at least same duration
153 as (de)crecsendo, b.o. line-breaking.
159 maybe we should leave dynamic texts to the text-engraver and
160 simply acknowledge them?
162 if (script_ev_)
164 script_ = new Item (get_property ("DynamicText"));
165 script_->set_grob_property ("text",
166 script_ev_->get_mus_property ("text"));
169 if (Direction d = to_dir (script_ev_->get_mus_property ("direction")))
170 Directional_element_interface::set (line_spanner_, d);
172 Axis_group_interface::add_element (line_spanner_, script_);
174 announce_grob(script_, script_ev_->self_scm());
177 Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
178 accepted_spanreqs_drul_[STOP] : script_ev_;
180 if (accepted_spanreqs_drul_[STOP] || script_ev_)
183 finish side position alignment if the (de)cresc ends here, and
184 there are no new dynamics.
188 if (cresc_)
190 assert (!finished_cresc_ && cresc_);
192 cresc_->set_bound (RIGHT, script_
193 ? script_
194 : unsmob_grob (get_property ("currentMusicalColumn")));
195 add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
198 finished_cresc_ = cresc_;
199 cresc_ = 0;
200 current_cresc_ev_ = 0;
202 else if (accepted_spanreqs_drul_[STOP] )
204 accepted_spanreqs_drul_[STOP]->origin ()->warning(_ ("can't find start of (de)crescendo"));
205 stop_ev = 0;
210 if (accepted_spanreqs_drul_[START])
212 if (current_cresc_ev_)
214 Direction sd = to_dir (current_cresc_ev_->get_mus_property ("span-direction"));
215 String msg = sd == 1
216 ? _ ("already have a crescendo")
217 : _ ("already have a decrescendo");
219 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
220 current_cresc_ev_->origin ()->warning (_("Cresc started here"));
222 else
224 current_cresc_ev_ = accepted_spanreqs_drul_[START];
226 if (Direction d = to_dir (current_cresc_ev_->get_mus_property ("direction")))
227 Directional_element_interface::set (line_spanner_, d);
230 TODO: Use symbols.
233 String start_type =
234 ly_symbol2string (current_cresc_ev_->get_mus_property ("name"));
237 ugh. Use push/pop?
239 if (start_type == "DecrescendoEvent")
240 start_type = "decrescendo";
241 else if (start_type == "CrescendoEvent")
242 start_type = "crescendo";
244 SCM s = get_property ((start_type + "Spanner").to_str0 ());
245 if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
247 cresc_ = new Spanner (get_property ("Hairpin"));
248 cresc_->set_grob_property ("grow-direction",
249 gh_int2scm ((start_type == "crescendo")
250 ? BIGGER : SMALLER));
256 This is a convenient (and legacy) interface to TextSpanners
257 for use in (de)crescendi.
258 Hmm.
260 else
262 cresc_ = new Spanner (get_property ("TextSpanner"));
263 cresc_->set_grob_property ("style", s);
264 daddy_trans_->set_property ((start_type
265 + "Spanner").to_str0 (), SCM_EOL);
266 s = get_property ((start_type + "Text").to_str0 ());
268 FIXME: use get_markup () to check type.
270 if (gh_string_p (s) || gh_pair_p (s))
272 cresc_->set_grob_property ("edge-text",
273 gh_cons (s, scm_makfrom0str ("")));
274 daddy_trans_->set_property ((start_type + "Text").to_str0 (),
275 SCM_EOL);
279 cresc_->set_bound (LEFT, script_
280 ? script_
281 : unsmob_grob (get_property ("currentMusicalColumn")));
283 Axis_group_interface::add_element (line_spanner_, cresc_);
285 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
287 announce_grob(cresc_, accepted_spanreqs_drul_[START]->self_scm());
292 void
293 Dynamic_engraver::stop_translation_timestep ()
295 typeset_all ();
296 if (!current_cresc_ev_)
298 finished_line_spanner_ = line_spanner_;
299 line_spanner_ =0;
300 typeset_all ();
304 void
305 Dynamic_engraver::finalize ()
307 typeset_all ();
309 if (line_spanner_
310 && !line_spanner_->live())
311 line_spanner_ = 0;
312 if (line_spanner_)
314 finished_line_spanner_ = line_spanner_;
315 typeset_all ();
318 if (cresc_
319 && !cresc_->live())
320 cresc_ = 0;
321 if (cresc_)
323 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
324 cresc_->suicide ();
325 cresc_ = 0;
329 void
330 Dynamic_engraver::typeset_all ()
333 remove suicided spanners,
334 ugh: we'll need this for every spanner, beam, slur
335 Hmm, how to do this, cleanly?
336 Maybe just check at typeset_grob ()?
338 if (finished_cresc_
339 && !finished_cresc_->live())
340 finished_cresc_ = 0;
341 if (finished_line_spanner_
342 && !finished_line_spanner_->live())
343 finished_line_spanner_ = 0;
345 if (finished_cresc_)
347 if (!finished_cresc_->get_bound (RIGHT))
349 finished_cresc_->set_bound (RIGHT, script_
350 ? script_
351 : unsmob_grob (get_property ("currentMusicalColumn")));
353 if (finished_line_spanner_)
354 add_bound_item (finished_line_spanner_,
355 finished_cresc_->get_bound (RIGHT));
357 typeset_grob (finished_cresc_);
358 finished_cresc_ =0;
361 if (script_)
363 typeset_grob (script_);
364 script_ = 0;
366 if (finished_line_spanner_)
368 /* To make sure that this works */
369 Side_position_interface::add_staff_support (finished_line_spanner_);
372 We used to have
374 extend-spanner-over-elements (finished_line_spanner_);
376 but this is rather kludgy, since finished_line_spanner_
377 typically has a staff-symbol field set , extending it over the
378 entire staff.
382 Grob * l = finished_line_spanner_->get_bound (LEFT );
383 Grob * r = finished_line_spanner_->get_bound (RIGHT);
384 if (!r && l)
385 finished_line_spanner_->set_bound (RIGHT, l);
386 else if (!l && r)
387 finished_line_spanner_->set_bound (LEFT, r);
388 else if (!r && !l)
391 This is a isolated dynamic apparently, and does not even have
392 any interesting support item.
394 Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
395 Item * ci = dynamic_cast<Item*>(cc);
396 finished_line_spanner_->set_bound (RIGHT, ci);
397 finished_line_spanner_->set_bound (LEFT, ci);
400 typeset_grob (finished_line_spanner_);
401 finished_line_spanner_ = 0;
405 void
406 Dynamic_engraver::acknowledge_grob (Grob_info i)
408 if (!line_spanner_)
409 return ;
411 if (Note_column::has_interface (i.grob_))
413 if (line_spanner_
414 /* Don't refill killed spanner */
415 && line_spanner_->live())
417 Side_position_interface::add_support (line_spanner_,i.grob_);
418 add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_));
421 if (script_ && !script_->get_parent (X_AXIS))
423 script_->set_parent (i.grob_, X_AXIS);
427 else if (Script_interface::has_interface (i.grob_) && script_)
429 SCM p = i.grob_->get_grob_property ("script-priority");
432 UGH.
434 DynamicText doesn't really have a script-priority field.
436 if (gh_number_p (p)
437 && gh_scm2int (p) < gh_scm2int (script_->get_grob_property ("script-priority")))
439 Side_position_interface::add_support (line_spanner_, i.grob_);
444 ENTER_DESCRIPTION(Dynamic_engraver,
445 /* descr */
446 "This engraver creates hairpins, dynamic texts, and their vertical\n"
447 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
448 "which takes care of vertical positioning. "
451 /* creats*/ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
452 /* accepts */ "absolute-dynamic-event crescendo-event decrescendo-event",
453 /* acks */ "note-column-interface script-interface",
454 /* reads */ "",
455 /* write */ "");