Correct left text alignment of DynamicTextSpanner.
[lilypond.git] / lily / dynamic-engraver.cc
blob94057f0bde25ebe7ca50288758c73233d4c4f003
1 /*
2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
9 #include "axis-group-interface.hh"
10 #include "context.hh"
11 #include "engraver.hh"
12 #include "dimensions.hh"
13 #include "directional-element-interface.hh"
14 #include "engraver.hh"
15 #include "hairpin.hh"
16 #include "international.hh"
17 #include "interval.hh"
18 #include "note-column.hh"
19 #include "paper-column.hh"
20 #include "pointer-group-interface.hh"
21 #include "script-interface.hh"
22 #include "self-alignment-interface.hh"
23 #include "side-position-interface.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "stream-event.hh"
26 #include "warn.hh"
27 #include "spanner.hh"
28 #include "text-interface.hh"
30 #include "translator.icc"
33 TODO:
35 * direction of text-dynamic-event if not equal to direction of
36 line-spanner
38 - TODO: this engraver is too complicated. We should split it into
39 the handling of the basic grobs and the linespanner
41 - TODO: the line-spanner is not killed after the (de)crescs are
42 finished.
45 /**
46 print text & hairpin dynamics.
48 class Dynamic_engraver : public Engraver
50 Item *script_;
51 Spanner *line_spanner_;
52 Spanner *cresc_;
54 Spanner *finished_line_spanner_;
55 Spanner *finished_cresc_;
57 Stream_event *script_ev_;
58 Stream_event *current_cresc_ev_;
60 Drul_array<Stream_event *> accepted_spanevents_drul_;
62 vector<Note_column*> pending_columns_;
63 vector<Grob*> pending_elements_;
65 void typeset_all ();
67 TRANSLATOR_DECLARATIONS (Dynamic_engraver);
68 DECLARE_ACKNOWLEDGER (note_column);
69 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic);
70 DECLARE_TRANSLATOR_LISTENER (span_dynamic);
72 protected:
73 virtual void finalize ();
74 void stop_translation_timestep ();
75 void process_music ();
78 Dynamic_engraver::Dynamic_engraver ()
80 script_ = 0;
81 finished_cresc_ = 0;
82 line_spanner_ = 0;
83 finished_line_spanner_ = 0;
84 current_cresc_ev_ = 0;
85 cresc_ = 0;
87 script_ev_ = 0;
88 accepted_spanevents_drul_[START] = 0;
89 accepted_spanevents_drul_[STOP] = 0;
92 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, absolute_dynamic);
93 void
94 Dynamic_engraver::listen_absolute_dynamic (Stream_event *ev)
97 TODO: probably broken.
99 ASSIGN_EVENT_ONCE (script_ev_, ev);
102 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver, span_dynamic);
103 void
104 Dynamic_engraver::listen_span_dynamic (Stream_event *ev)
106 Direction d = to_dir (ev->get_property ("span-direction"));
108 if (d == START)
109 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[START], ev);
111 /* Cancel any ongoing crescendo, either explicitly by \! or
112 implicitly by a new crescendo. Also avoid warning if cresc is
113 cancelled both implicitly and explicitly. */
114 if ((d == STOP || current_cresc_ev_) && !accepted_spanevents_drul_[STOP])
115 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_[STOP], ev);
118 void
119 Dynamic_engraver::process_music ()
121 if (accepted_spanevents_drul_[START] || accepted_spanevents_drul_[STOP] || script_ev_)
123 if (!line_spanner_)
125 Stream_event *rq = accepted_spanevents_drul_[START];
126 line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm () : SCM_EOL);
127 if (script_ev_)
128 rq = script_ev_;
133 During a (de)crescendo, pending event will not be cleared,
134 and a line-spanner will always be created, as \< \! are already
135 two events.
137 Note: line-spanner must always have at least same duration
138 as (de)crecsendo, b.o. line-breaking.
142 maybe we should leave dynamic texts to the text-engraver and
143 simply acknowledge them?
145 if (script_ev_)
147 script_ = make_item ("DynamicText", script_ev_->self_scm ());
148 script_->set_property ("text",
149 script_ev_->get_property ("text"));
151 if (Direction d = to_dir (script_ev_->get_property ("direction")))
152 set_grob_direction (line_spanner_, d);
153 else if (Direction d = to_dir (line_spanner_->get_property ("direction")))
154 set_grob_direction (script_, d);
156 Axis_group_interface::add_element (line_spanner_, script_);
159 Stream_event *stop_ev = accepted_spanevents_drul_ [STOP]
160 ? accepted_spanevents_drul_[STOP] : script_ev_;
162 if (accepted_spanevents_drul_[STOP] || script_ev_)
165 finish side position alignment if the (de)cresc ends here, and
166 there are no new dynamics.
169 if (cresc_)
171 assert (!finished_cresc_ && cresc_);
173 if (script_)
175 cresc_->set_bound (RIGHT, script_);
176 add_bound_item (line_spanner_, script_);
179 finished_cresc_ = cresc_;
180 announce_end_grob (finished_cresc_, SCM_EOL);
181 cresc_ = 0;
182 current_cresc_ev_ = 0;
184 else if (accepted_spanevents_drul_[STOP])
186 accepted_spanevents_drul_[STOP]->origin ()->warning (_ ("cannot 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 ());
258 if (Text_interface::is_markup (s))
260 cresc_->set_property ("text", s);
261 context ()->set_property ((start_type + "Text").c_str (),
262 SCM_EOL);
265 if (script_)
267 set_nested_property (cresc_,
268 scm_list_3 (ly_symbol2scm ("bound-details"),
269 ly_symbol2scm ("left"),
270 ly_symbol2scm ("attach-dir")
272 scm_from_int (RIGHT));
276 if (script_)
278 cresc_->set_bound (LEFT, script_);
279 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
281 Axis_group_interface::add_element (line_spanner_, cresc_);
286 void
287 Dynamic_engraver::stop_translation_timestep ()
289 if (!current_cresc_ev_ && line_spanner_)
291 assert (!finished_line_spanner_);
292 finished_line_spanner_ = line_spanner_;
293 line_spanner_ = 0;
296 typeset_all ();
298 if (cresc_ && !cresc_->get_bound (LEFT))
300 cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
301 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
304 script_ev_ = 0;
305 accepted_spanevents_drul_[START] = 0;
306 accepted_spanevents_drul_[STOP] = 0;
309 void
310 Dynamic_engraver::finalize ()
312 typeset_all ();
314 if (line_spanner_
315 && !line_spanner_->is_live ())
316 line_spanner_ = 0;
317 if (line_spanner_)
319 finished_line_spanner_ = line_spanner_;
320 typeset_all ();
323 if (cresc_
324 && !cresc_->is_live ())
325 cresc_ = 0;
326 if (cresc_)
328 current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
329 cresc_->suicide ();
330 cresc_ = 0;
334 void
335 Dynamic_engraver::typeset_all ()
337 if (finished_cresc_)
339 if (!finished_cresc_->get_bound (RIGHT))
342 Grob *column_bound = unsmob_grob (get_property ("currentMusicalColumn"));
344 finished_cresc_->set_bound (RIGHT, script_
345 ? script_
346 : column_bound);
348 if (finished_line_spanner_)
349 add_bound_item (finished_line_spanner_,
350 finished_cresc_->get_bound (RIGHT));
352 finished_cresc_ = 0;
355 script_ = 0;
356 if (finished_line_spanner_)
359 We used to have
361 extend-spanner-over-elements (finished_line_spanner_);
363 but this is rather kludgy, since finished_line_spanner_
364 typically has a staff-symbol field set , extending it over the
365 entire staff.
369 Grob *l = finished_line_spanner_->get_bound (LEFT);
370 Grob *r = finished_line_spanner_->get_bound (RIGHT);
371 if (!r && l)
372 finished_line_spanner_->set_bound (RIGHT, l);
373 else if (!l && r)
374 finished_line_spanner_->set_bound (LEFT, r);
375 else if (!r && !l)
378 This is a isolated dynamic apparently, and does not even have
379 any interesting support item.
381 Grob *cc = unsmob_grob (get_property ("currentMusicalColumn"));
382 Item *ci = dynamic_cast<Item *> (cc);
383 finished_line_spanner_->set_bound (RIGHT, ci);
384 finished_line_spanner_->set_bound (LEFT, ci);
386 finished_line_spanner_ = 0;
390 void
391 Dynamic_engraver::acknowledge_note_column (Grob_info info)
393 if (!line_spanner_)
394 return;
396 if (line_spanner_
397 /* Don't refill killed spanner */
398 && line_spanner_->is_live ())
400 Side_position_interface::add_support (line_spanner_, info.grob ());
401 add_bound_item (line_spanner_, dynamic_cast<Item *> (info.grob ()));
404 if (script_ && !script_->get_parent (X_AXIS))
406 extract_grob_set (info.grob (), "note-heads", heads);
407 if (heads.size ())
409 Grob *head = heads[0];
410 script_->set_parent (head, X_AXIS);
411 Self_alignment_interface::set_center_parent (script_, X_AXIS);
415 if (cresc_)
417 if (!cresc_->get_bound (LEFT))
419 cresc_->set_bound (LEFT, info.grob ());
420 add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
424 if (finished_cresc_ && !finished_cresc_->get_bound (RIGHT))
425 finished_cresc_->set_bound (RIGHT, info.grob ());
428 ADD_ACKNOWLEDGER (Dynamic_engraver, note_column);
430 ADD_TRANSLATOR (Dynamic_engraver,
431 /* doc */
432 "Create hairpins, dynamic texts, and their vertical"
433 " alignments. The symbols are collected onto a"
434 " @code{DynamicLineSpanner} grob which takes care of vertical"
435 " positioning.",
437 /* create */
438 "DynamicLineSpanner "
439 "DynamicTextSpanner "
440 "DynamicText "
441 "Hairpin "
442 "TextSpanner ",
444 /* read */
447 /* write */