* lexer-gcc-3.1.sh: Remove.
[lilypond/patrick.git] / lily / dynamic-engraver.cc
blobdaba7713865d3c82d43b6a8f8603992fb23a852d
1 /*
2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "axis-group-interface.hh"
10 #include "context.hh"
11 #include "dimensions.hh"
12 #include "directional-element-interface.hh"
13 #include "engraver.hh"
14 #include "hairpin.hh"
15 #include "international.hh"
16 #include "interval.hh"
17 #include "note-column.hh"
18 #include "paper-column.hh"
19 #include "pointer-group-interface.hh"
20 #include "script-interface.hh"
21 #include "self-alignment-interface.hh"
22 #include "side-position-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "stream-event.hh"
25 #include "warn.hh"
27 #include "translator.icc"
30 TODO:
32 * direction of text-dynamic-event if not equal to direction of
33 line-spanner
35 - TODO: this engraver is too complicated. We should split it into
36 the handling of the basic grobs and the linespanner
38 - TODO: the line-spanner is not killed after the (de)crescs are
39 finished.
42 /**
43 print text & hairpin dynamics.
45 class Dynamic_engraver : public Engraver
47 Item *script_;
48 Spanner *line_spanner_;
49 Spanner *cresc_;
51 Spanner *finished_line_spanner_;
52 Spanner *finished_cresc_;
54 Stream_event *script_ev_;
55 Stream_event *current_cresc_ev_;
57 Drul_array<Stream_event *> accepted_spanevents_drul_;
59 vector<Note_column*> pending_columns_;
60 vector<Grob*> pending_elements_;
62 void typeset_all ();
64 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
65 DECLARE_ACKNOWLEDGER (accidental);
66 DECLARE_ACKNOWLEDGER (script);
67 DECLARE_ACKNOWLEDGER (stem_tremolo);
68 DECLARE_ACKNOWLEDGER (note_column);
69 DECLARE_ACKNOWLEDGER (slur);
70 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
71 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
73 protected:
74 virtual void finalize ();
75 void stop_translation_timestep ();
76 void process_music ();
79 Dynamic_engraver::Dynamic_engraver ()
81 script_ = 0;
82 finished_cresc_ = 0;
83 line_spanner_ = 0;
84 finished_line_spanner_ = 0;
85 current_cresc_ev_ = 0;
86 cresc_ = 0;
88 script_ev_ = 0;
89 accepted_spanevents_drul_[START] = 0;
90 accepted_spanevents_drul_[STOP] = 0;
93 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
94 void
95 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
98 TODO: probably broken.
100 ASSIGN_EVENT_ONCE (script_ev_, ev);
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
104 void
105 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
107 Direction d = to_dir (ev->get_property ("span-direction"));
109 if (d == START)
110 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
112 /* Cancel any ongoing crescendo, either explicitly by \! or
113 implicitly by a new crescendo. Also avoid warning if cresc is
114 cancelled both implicitly and explicitly. */
115 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
116 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
119 void
120 Dynamic_engraver::process_music ()
122 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
124 if (!line_spanner_)
126 Stream_event *rq = accepted_spanevents_drul_[START];
127 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
128 if (script_ev_)
129 rq = script_ev_;
134 During a (de)crescendo, pending event will not be cleared,
135 and a line-spanner will always be created, as \< \! are already
136 two events.
138 Note: line-spanner must always have at least same duration
139 as (de)crecsendo, b.o. line-breaking.
143 maybe we should leave dynamic texts to the text-engraver and
144 simply acknowledge them?
146 if (script_ev_)
148 script_ = make_item ("DynamicText", script_ev_->self_scm ());
149 script_->set_property ("text",
150 script_ev_->get_property ("text"));
152 if (Direction d = to_dir (script_ev_->get_property ("direction")))
153 set_grob_direction (line_spanner_, d);
154 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
155 set_grob_direction (script_, d);
157 Axis_group_interface::add_element (line_spanner_, script_);
160 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
161 ? accepted_spanevents_drul_[STOP] : script_ev_;
163 if (accepted_spanevents_drul_[STOP] || script_ev_)
166 finish side position alignment if the (de)cresc ends here, and
167 there are no new dynamics.
170 if (cresc_)
172 assert (!finished_cresc_ && cresc_);
174 if (script_)
176 cresc_->set_bound (RIGHT, script_);
177 add_bound_item (line_spanner_, script_);
180 finished_cresc_ = cresc_;
181 cresc_ = 0;
182 current_cresc_ev_ = 0;
184 else if (accepted_spanevents_drul_[STOP])
186 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
187 stop_ev = 0;
191 if (accepted_spanevents_drul_[START])
193 if (current_cresc_ev_)
195 string msg = _ ("already have a decrescendo");
196 if (current_cresc_ev_->in_event_class ("crescendo-event"))
197 msg = _ ("already have a crescendo");
199 accepted_spanevents_drul_[START]->origin ()->warning (msg);
200 current_cresc_ev_->origin ()->warning (_ ("cresc starts here"));
202 else
204 current_cresc_ev_ = accepted_spanevents_drul_[START];
206 if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
207 set_grob_direction (line_spanner_, d);
210 TODO: Use symbols.
213 SCM start_sym = current_cresc_ev_->get_property ("class");
214 string start_type;
216 if (start_sym == ly_symbol2scm ("decrescendo-event"))
217 start_type = "decrescendo";
218 else if (start_sym == ly_symbol2scm ("crescendo-event"))
219 start_type = "crescendo";
220 else
222 programming_error ("unknown dynamic spanner type");
223 return;
227 UGH. TODO: should read from original event, so appearance
228 may be altered with \tweak.
230 SCM s = get_property ((start_type + "Spanner").c_str ());
231 if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
233 cresc_ = make_spanner ("Hairpin", accepted_spanevents_drul_[START]->self_scm ());
234 if (finished_cresc_)
236 Pointer_group_interface::add_grob (finished_cresc_,
237 ly_symbol2scm ("adjacent-hairpins"),
238 cresc_);
240 Pointer_group_interface::add_grob (cresc_,
241 ly_symbol2scm ("adjacent-hairpins"),
242 finished_cresc_);
247 This is a convenient (and legacy) interface to TextSpanners
248 for use in (de)crescendi.
249 Hmm.
251 else
253 cresc_ = make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_[START]->self_scm ());
254 cresc_->set_property ("style", s);
255 context ()->set_property ((start_type
256 + "Spanner").c_str (), SCM_EOL);
257 s = get_property ((start_type + "Text").c_str ());
259 FIXME: use get_markup () to check type.
261 if (scm_is_string (s) || scm_is_pair (s))
263 cresc_->set_property ("edge-text",
264 scm_cons (s, scm_makfrom0str ("")));
265 context ()->set_property ((start_type + "Text").c_str (),
266 SCM_EOL);
270 if (script_)
272 cresc_->set_bound (LEFT, script_);
273 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
276 Axis_group_interface::add_element (line_spanner_, cresc_);
281 void
282 Dynamic_engraver::stop_translation_timestep ()
284 if (!current_cresc_ev_ && line_spanner_)
286 assert (!finished_line_spanner_);
287 finished_line_spanner_ = line_spanner_;
288 line_spanner_ = 0;
291 typeset_all ();
293 if (cresc_ && !cresc_->get_bound (LEFT))
295 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
296 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
299 script_ev_ = 0;
300 accepted_spanevents_drul_[START] = 0;
301 accepted_spanevents_drul_[STOP] = 0;
304 void
305 Dynamic_engraver::finalize ()
307 typeset_all ();
309 if (line_spanner_
310 && !line_spanner_->is_live ())
311 line_spanner_ = 0;
312 if (line_spanner_)
314 finished_line_spanner_ = line_spanner_;
315 typeset_all ();
318 if (cresc_
319 && !cresc_->is_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 ()
332 if (finished_cresc_)
334 bool use_bar = to_boolean (get_property ("hairpinToBarline"))
335 && scm_is_string (get_property ("whichBar"))
336 && !script_ev_;
339 if (!finished_cresc_->get_bound (RIGHT)
340 || use_bar)
342 Grob *column_bound = unsmob_grob (use_bar
343 ? get_property ("currentCommandColumn")
344 : get_property ("currentMusicalColumn"));
346 finished_cresc_->set_bound (RIGHT, script_
347 ? script_
348 : column_bound);
350 if (finished_line_spanner_)
351 add_bound_item (finished_line_spanner_,
352 finished_cresc_->get_bound (RIGHT));
354 finished_cresc_ = 0;
357 script_ = 0;
358 if (finished_line_spanner_)
361 We used to have
363 extend-spanner-over-elements (finished_line_spanner_);
365 but this is rather kludgy, since finished_line_spanner_
366 typically has a staff-symbol field set , extending it over the
367 entire staff.
371 Grob *l = finished_line_spanner_->get_bound (LEFT);
372 Grob *r = finished_line_spanner_->get_bound (RIGHT);
373 if (!r && l)
374 finished_line_spanner_->set_bound (RIGHT, l);
375 else if (!l && r)
376 finished_line_spanner_->set_bound (LEFT, r);
377 else if (!r && !l)
380 This is a isolated dynamic apparently, and does not even have
381 any interesting support item.
383 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
384 Item *ci = dynamic_cast<Item *> (cc);
385 finished_line_spanner_->set_bound (RIGHT, ci);
386 finished_line_spanner_->set_bound (LEFT, ci);
389 finished_line_spanner_ = 0;
394 void
395 Dynamic_engraver::acknowledge_accidental (Grob_info info)
397 if (line_spanner_)
398 Side_position_interface::add_support (line_spanner_, info.grob ());
402 void
403 Dynamic_engraver::acknowledge_stem_tremolo (Grob_info info)
405 if (line_spanner_)
406 Side_position_interface::add_support (line_spanner_, info.grob ());
410 void
411 Dynamic_engraver::acknowledge_slur (Grob_info info)
413 if (line_spanner_)
414 Side_position_interface::add_support (line_spanner_, info.grob ());
418 void
419 Dynamic_engraver::acknowledge_note_column (Grob_info info)
421 if (!line_spanner_)
422 return;
424 if (line_spanner_
425 /* Don't refill killed spanner */
426 && line_spanner_->is_live ())
428 Side_position_interface::add_support (line_spanner_, info.grob ());
429 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
432 if (script_ && !script_->get_parent (X_AXIS))
434 extract_grob_set (info.grob (), "note-heads", heads);
435 if (heads.size ())
437 Grob *head = heads[0];
438 script_->set_parent (head, X_AXIS);
439 Self_alignment_interface::set_center_parent (script_, X_AXIS);
443 if (cresc_)
445 if (!cresc_->get_bound (LEFT))
447 cresc_->set_bound (LEFT, info.grob ());
448 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
452 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
453 finished_cresc_->set_bound (RIGHT, info.grob ());
456 void
457 Dynamic_engraver::acknowledge_script (Grob_info info)
459 if (!line_spanner_ || !script_)
460 return;
462 SCM p = info.grob ()->get_property ("script-priority");
465 UGH.
467 DynamicText doesn't really have a script-priority field.
469 if (scm_is_number (p)
470 && scm_to_int (p)
471 < scm_to_int (script_->get_property ("script-priority")))
472 Side_position_interface::add_support (line_spanner_, info.grob ());
475 ADD_ACKNOWLEDGER (Dynamic_engraver, accidental);
476 ADD_ACKNOWLEDGER (Dynamic_engraver, script);
477 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
478 ADD_ACKNOWLEDGER (Dynamic_engraver, slur);
479 ADD_ACKNOWLEDGER (Dynamic_engraver, stem_tremolo);
481 ADD_TRANSLATOR (Dynamic_engraver,
482 /* doc */
483 "This engraver creates hairpins, dynamic texts, and their vertical\n"
484 "alignments. The symbols are collected onto a DynamicLineSpanner grob\n"
485 "which takes care of vertical positioning. ",
487 /* create */ "DynamicLineSpanner DynamicText Hairpin TextSpanner",
488 /* accept */ "absolute-dynamic-event crescendo-event decrescendo-event",
489 /* read */ "",
490 /* write */ "");