(all-grob-descriptions): remove gap from
[lilypond.git] / lily / dynamic-engraver.cc
blob9ea571da32e643b017e95d89693267469ff2f2fd
1 /*
2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2004 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 "context.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 ();
72 Dynamic_engraver::Dynamic_engraver ()
74 script_ = 0;
75 finished_cresc_ = 0;
76 line_spanner_ = 0;
77 finished_line_spanner_ = 0;
78 current_cresc_ev_ = 0;
79 cresc_ =0;
81 script_ev_ = 0;
82 accepted_spanreqs_drul_[START] = 0;
83 accepted_spanreqs_drul_[STOP] = 0;
86 bool
87 Dynamic_engraver::try_music (Music * m)
89 if (m->is_mus_type ("absolute-dynamic-event"))
92 TODO: probably broken.
94 script_ev_ = m;
95 return true;
97 else if (m->is_mus_type ("decrescendo-event")
98 || m->is_mus_type ("crescendo-event"))
100 Direction d = to_dir (m->get_property ("span-direction"));
102 accepted_spanreqs_drul_[d] = m;
103 if (current_cresc_ev_ && d == START)
104 accepted_spanreqs_drul_[STOP] = m;
105 return true;
107 return false;
110 void
111 Dynamic_engraver::process_music ()
113 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
115 if (!line_spanner_)
117 line_spanner_ = make_spanner ("DynamicLineSpanner");
119 Music * rq = accepted_spanreqs_drul_[START];
120 if (script_ev_)
121 rq = script_ev_ ;
122 announce_grob (line_spanner_, rq ? rq->self_scm (): SCM_EOL);
127 During a (de)crescendo, pending event will not be cleared,
128 and a line-spanner will always be created, as \< \! are already
129 two events.
131 Note: line-spanner must always have at least same duration
132 as (de)crecsendo, b.o. line-breaking.
138 maybe we should leave dynamic texts to the text-engraver and
139 simply acknowledge them?
141 if (script_ev_)
143 script_ = make_item ("DynamicText");
144 script_->set_property ("text",
145 script_ev_->get_property ("text"));
148 if (Direction d = to_dir (script_ev_->get_property ("direction")))
149 set_grob_direction (line_spanner_, d);
151 Axis_group_interface::add_element (line_spanner_, script_);
153 announce_grob (script_, script_ev_->self_scm ());
156 Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
157 accepted_spanreqs_drul_[STOP] : script_ev_;
159 if (accepted_spanreqs_drul_[STOP] || script_ev_)
162 finish side position alignment if the (de)cresc ends here, and
163 there are no new dynamics.
167 if (cresc_)
169 assert (!finished_cresc_ && cresc_);
171 cresc_->set_bound (RIGHT, script_
172 ? script_
173 : unsmob_grob (get_property ("currentMusicalColumn")));
174 add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
177 finished_cresc_ = cresc_;
178 cresc_ = 0;
179 current_cresc_ev_ = 0;
181 else if (accepted_spanreqs_drul_[STOP])
183 accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
184 stop_ev = 0;
189 if (accepted_spanreqs_drul_[START])
191 if (current_cresc_ev_)
193 String msg = _ ("already have a decrescendo");
194 if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
195 msg = _ ("already have a crescendo");
197 accepted_spanreqs_drul_[START]->origin ()->warning (msg);
198 current_cresc_ev_->origin ()->warning (_("Cresc started here"));
200 else
202 current_cresc_ev_ = accepted_spanreqs_drul_[START];
204 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
205 set_grob_direction (line_spanner_, d);
208 TODO: Use symbols.
211 String start_type =
212 ly_symbol2string (current_cresc_ev_->get_property ("name"));
215 ugh. Use push/pop?
217 if (start_type == "DecrescendoEvent")
218 start_type = "decrescendo";
219 else if (start_type == "CrescendoEvent")
220 start_type = "crescendo";
222 SCM s = get_property ((start_type + "Spanner").to_str0 ());
223 if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
225 cresc_ = make_spanner ("Hairpin");
226 cresc_->set_property ("grow-direction",
227 gh_int2scm ((start_type == "crescendo")
228 ? BIGGER : SMALLER));
234 This is a convenient (and legacy) interface to TextSpanners
235 for use in (de)crescendi.
236 Hmm.
238 else
240 cresc_ = make_spanner ("TextSpanner");
241 cresc_->set_property ("style", s);
242 daddy_context_->set_property ((start_type
243 + "Spanner").to_str0 (), SCM_EOL);
244 s = get_property ((start_type + "Text").to_str0 ());
246 FIXME: use get_markup () to check type.
248 if (gh_string_p (s) || gh_pair_p (s))
250 cresc_->set_property ("edge-text",
251 gh_cons (s, scm_makfrom0str ("")));
252 daddy_context_->set_property ((start_type + "Text").to_str0 (),
253 SCM_EOL);
257 cresc_->set_bound (LEFT, script_
258 ? script_
259 : unsmob_grob (get_property ("currentMusicalColumn")));
261 Axis_group_interface::add_element (line_spanner_, cresc_);
263 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
265 announce_grob (cresc_, accepted_spanreqs_drul_[START]->self_scm ());
270 void
271 Dynamic_engraver::stop_translation_timestep ()
273 typeset_all ();
274 if (!current_cresc_ev_)
276 finished_line_spanner_ = line_spanner_;
277 line_spanner_ =0;
278 typeset_all ();
281 script_ev_ = 0;
282 accepted_spanreqs_drul_[START] = 0;
283 accepted_spanreqs_drul_[STOP] = 0;
286 void
287 Dynamic_engraver::finalize ()
289 typeset_all ();
291 if (line_spanner_
292 && !line_spanner_->live ())
293 line_spanner_ = 0;
294 if (line_spanner_)
296 finished_line_spanner_ = line_spanner_;
297 typeset_all ();
300 if (cresc_
301 && !cresc_->live ())
302 cresc_ = 0;
303 if (cresc_)
305 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
306 cresc_->suicide ();
307 cresc_ = 0;
311 void
312 Dynamic_engraver::typeset_all ()
315 remove suicided spanners,
316 ugh: we'll need this for every spanner, beam, slur
317 Hmm, how to do this, cleanly?
318 Maybe just check at typeset_grob ()?
320 if (finished_cresc_
321 && !finished_cresc_->live ())
322 finished_cresc_ = 0;
323 if (finished_line_spanner_
324 && !finished_line_spanner_->live ())
325 finished_line_spanner_ = 0;
327 if (finished_cresc_)
329 if (!finished_cresc_->get_bound (RIGHT))
331 finished_cresc_->set_bound (RIGHT, script_
332 ? script_
333 : unsmob_grob (get_property ("currentMusicalColumn")));
335 if (finished_line_spanner_)
336 add_bound_item (finished_line_spanner_,
337 finished_cresc_->get_bound (RIGHT));
339 typeset_grob (finished_cresc_);
340 finished_cresc_ =0;
343 if (script_)
345 typeset_grob (script_);
346 script_ = 0;
348 if (finished_line_spanner_)
351 We used to have
353 extend-spanner-over-elements (finished_line_spanner_);
355 but this is rather kludgy, since finished_line_spanner_
356 typically has a staff-symbol field set , extending it over the
357 entire staff.
361 Grob * l = finished_line_spanner_->get_bound (LEFT );
362 Grob * r = finished_line_spanner_->get_bound (RIGHT);
363 if (!r && l)
364 finished_line_spanner_->set_bound (RIGHT, l);
365 else if (!l && r)
366 finished_line_spanner_->set_bound (LEFT, r);
367 else if (!r && !l)
370 This is a isolated dynamic apparently, and does not even have
371 any interesting support item.
373 Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
374 Item * ci = dynamic_cast<Item*>(cc);
375 finished_line_spanner_->set_bound (RIGHT, ci);
376 finished_line_spanner_->set_bound (LEFT, ci);
379 typeset_grob (finished_line_spanner_);
380 finished_line_spanner_ = 0;
384 void
385 Dynamic_engraver::acknowledge_grob (Grob_info i)
387 if (!line_spanner_)
388 return ;
390 if (Note_column::has_interface (i.grob_))
392 if (line_spanner_
393 /* Don't refill killed spanner */
394 && line_spanner_->live ())
396 Side_position_interface::add_support (line_spanner_,i.grob_);
397 add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_));
400 if (script_ && !script_->get_parent (X_AXIS))
402 SCM head = scm_last_pair (i.grob_->get_property ("note-heads"));
403 if (gh_pair_p (head))
404 script_->set_parent (unsmob_grob (gh_car (head)), X_AXIS);
408 else if (Script_interface::has_interface (i.grob_) && script_)
410 SCM p = i.grob_->get_property ("script-priority");
413 UGH.
415 DynamicText doesn't really have a script-priority field.
417 if (gh_number_p (p)
418 && gh_scm2int (p) < gh_scm2int (script_->get_property ("script-priority")))
420 Side_position_interface::add_support (line_spanner_, i.grob_);
425 ENTER_DESCRIPTION (Dynamic_engraver,
426 /* descr */
427 "This engraver creates hairpins, dynamic texts, and their vertical\n"
428 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
429 "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",
435 /* reads */ "",
436 /* write */ "");