lilypond-1.5.1
[lilypond.git] / lily / dynamic-engraver.cc
blob23ae1bd02099a793af3910efdba5053dad7adcb6
1 /*
2 dynamic-engraver.cc -- implement Dynamic_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
9 #include "dimensions.hh"
10 #include "hairpin.hh"
11 #include "musical-request.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"
24 TODO:
26 * direction of text-dynamic-request 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_p_;
43 Spanner * finished_cresc_p_;
44 Spanner * cresc_p_;
46 Text_script_req* script_req_l_;
48 Span_req * current_cresc_req_;
49 Drul_array<Span_req*> accepted_spanreqs_drul_;
51 Spanner* line_spanner_;
52 Spanner* finished_line_spanner_;
54 Link_array<Note_column> pending_column_arr_;
55 Link_array<Grob> pending_element_arr_;
57 void typeset_all ();
59 public:
60 VIRTUAL_COPY_CONS (Translator);
61 Dynamic_engraver ();
63 protected:
64 virtual void finalize ();
65 virtual void acknowledge_grob (Grob_info);
66 virtual bool try_music (Music *req_l);
67 virtual void stop_translation_timestep ();
68 virtual void process_music ();
69 virtual void start_translation_timestep ();
72 ADD_THIS_TRANSLATOR (Dynamic_engraver);
75 Dynamic_engraver::Dynamic_engraver ()
77 script_p_ = 0;
78 finished_cresc_p_ = 0;
79 line_spanner_ = 0;
80 finished_line_spanner_ = 0;
81 current_cresc_req_ = 0;
82 cresc_p_ =0;
84 script_req_l_ = 0;
85 accepted_spanreqs_drul_[START] = 0;
86 accepted_spanreqs_drul_[STOP] = 0;
89 void
90 Dynamic_engraver::start_translation_timestep ()
92 script_req_l_ = 0;
93 accepted_spanreqs_drul_[START] = 0;
94 accepted_spanreqs_drul_[STOP] = 0;
97 bool
98 Dynamic_engraver::try_music (Music * m)
100 if (dynamic_cast <Text_script_req*> (m)
101 && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
103 script_req_l_ = dynamic_cast<Text_script_req*> (m);
104 return true;
106 else if (Span_req* s = dynamic_cast <Span_req*> (m))
108 String t = ly_scm2string (s->get_mus_property ("span-type"));
109 if (t== "abort")
111 accepted_spanreqs_drul_[LEFT] = 0;
112 accepted_spanreqs_drul_[RIGHT] = 0;
114 Let's not kill the line spanner, since that would fuck up
115 earlier, not-to-be-terminated stuff.
117 It will disappear by itself when stop_translation_timestep
118 () finds that there is nothing to support anymore. */
120 if (cresc_p_)
121 cresc_p_->suicide ();
122 cresc_p_ = 0;
124 else if (t == "crescendo"
125 || t == "decrescendo")
127 accepted_spanreqs_drul_[s->get_span_dir ()] = s;
128 return true;
131 return false;
134 void
135 Dynamic_engraver::process_music ()
137 if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
139 if (!line_spanner_)
141 line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
143 Side_position_interface::set_axis (line_spanner_, Y_AXIS);
144 Axis_group_interface::set_interface (line_spanner_);
145 Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
147 Music * rq = accepted_spanreqs_drul_[START];
148 if (script_req_l_)
149 rq = script_req_l_ ;
150 announce_grob (line_spanner_, rq);
157 During a (de)crescendo, pending request will not be cleared,
158 and a line-spanner will always be created, as \< \! are already
159 two requests.
161 Note: line-spanner must always have at least same duration
162 as (de)crecsendo, b.o. line-breaking.
168 maybe we should leave dynamic texts to the text-engraver and
169 simply acknowledge them?
171 if (script_req_l_)
173 script_p_ = new Item (get_property ("DynamicText"));
174 script_p_->set_grob_property ("text",
175 script_req_l_->get_mus_property ("text"));
177 Side_position_interface::set_direction (script_p_, DOWN);
179 if (Direction d = script_req_l_->get_direction ())
180 Directional_element_interface::set (line_spanner_, d);
182 Axis_group_interface::add_element (line_spanner_, script_p_);
184 announce_grob (script_p_, script_req_l_);
187 if (accepted_spanreqs_drul_[STOP])
190 finish side position alignment if the (de)cresc ends here, and
191 there are no new dynamics.
194 if (!cresc_p_)
196 accepted_spanreqs_drul_[STOP]->origin ()->warning
197 (_ ("can't find start of (de)crescendo"));
198 accepted_spanreqs_drul_[STOP] = 0;
200 else
202 assert (!finished_cresc_p_ && cresc_p_);
204 cresc_p_->set_bound (RIGHT, script_p_
205 ? script_p_
206 : unsmob_grob (get_property ("currentMusicalColumn")));
207 add_bound_item (line_spanner_, cresc_p_->get_bound (RIGHT));
210 finished_cresc_p_ = cresc_p_;
211 cresc_p_ = 0;
212 current_cresc_req_ = 0;
216 if (accepted_spanreqs_drul_[START])
218 if (current_cresc_req_)
220 accepted_spanreqs_drul_[START]->origin ()->warning
221 (current_cresc_req_->get_span_dir () == 1
222 ? _ ("already have a crescendo")
223 : _ ("already have a decrescendo"));
225 else
227 current_cresc_req_ = accepted_spanreqs_drul_[START];
230 TODO: Use symbols.
233 String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
236 ugh. Use push/pop?
238 SCM s = get_property ((start_type + "Spanner").ch_C ());
239 if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
241 cresc_p_ = new Spanner (get_property ("Hairpin"));
242 cresc_p_->set_grob_property ("grow-direction",
243 gh_int2scm ((start_type == "crescendo")
244 ? BIGGER : SMALLER));
248 This is a convenient (and legacy) interface to TextSpanners
249 for use in (de)crescendi.
250 Hmm.
252 else
254 cresc_p_ = new Spanner (get_property ("TextSpanner"));
255 cresc_p_->set_interface (ly_symbol2scm ("dynamic-interface"));
256 cresc_p_->set_grob_property ("type", s);
258 daddy_trans_l_->set_property (start_type
259 + "Spanner", SCM_UNDEFINED);
260 s = get_property ((start_type + "Text").ch_C ());
262 FIXME: use markup_p () to check type.
264 if (gh_string_p (s) || gh_pair_p (s))
266 cresc_p_->set_grob_property ("edge-text",
267 gh_cons (s, ly_str02scm ("")));
268 daddy_trans_l_->set_property (start_type + "Text",
269 SCM_UNDEFINED);
273 cresc_p_->set_bound (LEFT, script_p_
274 ? script_p_
275 : unsmob_grob (get_property ("currentMusicalColumn")));
277 Axis_group_interface::add_element (line_spanner_, cresc_p_);
279 add_bound_item (line_spanner_, cresc_p_->get_bound (LEFT));
281 announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
286 void
287 Dynamic_engraver::stop_translation_timestep ()
289 typeset_all ();
290 if (script_req_l_ && !current_cresc_req_)
292 finished_line_spanner_ = line_spanner_;
293 line_spanner_ =0;
294 typeset_all ();
298 void
299 Dynamic_engraver::finalize ()
301 typeset_all ();
303 if (line_spanner_
304 && line_spanner_->immutable_property_alist_ == SCM_EOL)
305 line_spanner_ = 0;
306 if (line_spanner_)
308 finished_line_spanner_ = line_spanner_;
309 typeset_all ();
312 if (cresc_p_
313 && cresc_p_->immutable_property_alist_ == SCM_EOL)
314 cresc_p_ = 0;
315 if (cresc_p_)
317 current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
318 cresc_p_->suicide ();
319 cresc_p_ = 0;
323 void
324 Dynamic_engraver::typeset_all ()
327 remove suicided spanners,
328 ugh: we'll need this for every spanner, beam, slur
329 Hmm, how to do this, cleanly?
330 Maybe just check at typeset_grob ()?
332 if (finished_cresc_p_
333 && finished_cresc_p_->immutable_property_alist_ == SCM_EOL)
334 finished_cresc_p_ = 0;
335 if (finished_line_spanner_
336 && finished_line_spanner_->immutable_property_alist_ == SCM_EOL)
337 finished_line_spanner_ = 0;
339 if (finished_cresc_p_)
341 if (!finished_cresc_p_->get_bound (RIGHT))
343 finished_cresc_p_->set_bound (RIGHT, script_p_
344 ? script_p_
345 : unsmob_grob (get_property ("currentMusicalColumn")));
347 if (finished_line_spanner_)
348 add_bound_item (finished_line_spanner_,
349 finished_cresc_p_->get_bound (RIGHT));
351 typeset_grob (finished_cresc_p_);
352 finished_cresc_p_ =0;
355 if (script_p_)
357 typeset_grob (script_p_);
358 script_p_ = 0;
360 if (finished_line_spanner_)
362 /* To make sure that this works */
363 Side_position_interface::add_staff_support (finished_line_spanner_);
366 We used to have
368 extend_spanner_over_elements (finished_line_spanner_);
370 but this is rather kludgy, since finished_line_spanner_
371 typically has a staff-symbol field set , extending it over the
372 entire staff.
376 Grob * l = finished_line_spanner_->get_bound (LEFT );
377 Grob * r = finished_line_spanner_->get_bound (RIGHT);
378 if (!r && l)
379 finished_line_spanner_->set_bound (RIGHT, l);
380 else if (!l && r)
381 finished_line_spanner_->set_bound (LEFT, r);
382 else if (!r && !l)
385 This is a isolated dynamic apparently, and does not even have
386 any interesting support item.
388 Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
389 Item * ci = dynamic_cast<Item*>(cc);
390 finished_line_spanner_->set_bound (RIGHT, ci);
391 finished_line_spanner_->set_bound (LEFT, ci);
394 typeset_grob (finished_line_spanner_);
395 finished_line_spanner_ = 0;
399 void
400 Dynamic_engraver::acknowledge_grob (Grob_info i)
402 if (Note_column::has_interface (i.elem_l_))
404 if (line_spanner_
405 /* Don't refill killed spanner */
406 && line_spanner_->immutable_property_alist_ != SCM_EOL)
408 Side_position_interface::add_support (line_spanner_,i.elem_l_);
409 add_bound_item (line_spanner_,dynamic_cast<Item*> (i.elem_l_));