2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "axis-group-interface.hh"
22 #include "engraver.hh"
23 #include "dimensions.hh"
24 #include "directional-element-interface.hh"
25 #include "engraver.hh"
27 #include "international.hh"
28 #include "interval.hh"
29 #include "note-column.hh"
30 #include "paper-column.hh"
31 #include "pointer-group-interface.hh"
32 #include "script-interface.hh"
33 #include "self-alignment-interface.hh"
34 #include "side-position-interface.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "stream-event.hh"
39 #include "text-interface.hh"
41 #include "translator.icc"
46 * direction of text-dynamic-event if not equal to direction of
49 - TODO: this engraver is too complicated. We should split it into
50 the handling of the basic grobs and the linespanner
52 - TODO: the line-spanner is not killed after the (de)crescs are
57 print text & hairpin dynamics.
59 class Dynamic_engraver
: public Engraver
62 Spanner
*line_spanner_
;
65 Spanner
*finished_line_spanner_
;
66 Spanner
*finished_cresc_
;
68 Stream_event
*script_ev_
;
69 Stream_event
*current_cresc_ev_
;
71 Drul_array
<Stream_event
*> accepted_spanevents_drul_
;
73 vector
<Note_column
*> pending_columns_
;
74 vector
<Grob
*> pending_elements_
;
78 TRANSLATOR_DECLARATIONS (Dynamic_engraver
);
79 DECLARE_ACKNOWLEDGER (note_column
);
80 DECLARE_TRANSLATOR_LISTENER (absolute_dynamic
);
81 DECLARE_TRANSLATOR_LISTENER (span_dynamic
);
84 virtual void finalize ();
85 void stop_translation_timestep ();
86 void process_music ();
89 Dynamic_engraver::Dynamic_engraver ()
94 finished_line_spanner_
= 0;
95 current_cresc_ev_
= 0;
99 accepted_spanevents_drul_
[START
] = 0;
100 accepted_spanevents_drul_
[STOP
] = 0;
103 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver
, absolute_dynamic
);
105 Dynamic_engraver::listen_absolute_dynamic (Stream_event
*ev
)
108 TODO: probably broken.
110 ASSIGN_EVENT_ONCE (script_ev_
, ev
);
113 IMPLEMENT_TRANSLATOR_LISTENER (Dynamic_engraver
, span_dynamic
);
115 Dynamic_engraver::listen_span_dynamic (Stream_event
*ev
)
117 Direction d
= to_dir (ev
->get_property ("span-direction"));
120 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_
[START
], ev
);
122 /* Cancel any ongoing crescendo, either explicitly by \! or
123 implicitly by a new crescendo. Also avoid warning if cresc is
124 cancelled both implicitly and explicitly. */
125 if ((d
== STOP
|| current_cresc_ev_
) && !accepted_spanevents_drul_
[STOP
])
126 ASSIGN_EVENT_ONCE (accepted_spanevents_drul_
[STOP
], ev
);
130 Dynamic_engraver::process_music ()
132 if (accepted_spanevents_drul_
[START
] || accepted_spanevents_drul_
[STOP
] || script_ev_
)
136 Stream_event
*rq
= accepted_spanevents_drul_
[START
];
137 line_spanner_
= make_spanner ("DynamicLineSpanner", rq
? rq
->self_scm () : SCM_EOL
);
144 During a (de)crescendo, pending event will not be cleared,
145 and a line-spanner will always be created, as \< \! are already
148 Note: line-spanner must always have at least same duration
149 as (de)crecsendo, b.o. line-breaking.
153 maybe we should leave dynamic texts to the text-engraver and
154 simply acknowledge them?
158 script_
= make_item ("DynamicText", script_ev_
->self_scm ());
159 script_
->set_property ("text",
160 script_ev_
->get_property ("text"));
162 if (Direction d
= to_dir (script_ev_
->get_property ("direction")))
163 set_grob_direction (line_spanner_
, d
);
164 else if (Direction d
= to_dir (line_spanner_
->get_property ("direction")))
165 set_grob_direction (script_
, d
);
167 Axis_group_interface::add_element (line_spanner_
, script_
);
170 Stream_event
*stop_ev
= accepted_spanevents_drul_
[STOP
]
171 ? accepted_spanevents_drul_
[STOP
] : script_ev_
;
173 if (accepted_spanevents_drul_
[STOP
] || script_ev_
)
176 finish side position alignment if the (de)cresc ends here, and
177 there are no new dynamics.
182 assert (!finished_cresc_
&& cresc_
);
186 cresc_
->set_bound (RIGHT
, script_
);
187 add_bound_item (line_spanner_
, script_
);
190 finished_cresc_
= cresc_
;
191 announce_end_grob (finished_cresc_
, SCM_EOL
);
193 current_cresc_ev_
= 0;
195 else if (accepted_spanevents_drul_
[STOP
])
197 accepted_spanevents_drul_
[STOP
]->origin ()->warning (_ ("cannot find start of (de)crescendo"));
202 if (accepted_spanevents_drul_
[START
])
204 if (current_cresc_ev_
)
206 string msg
= _ ("already have a decrescendo");
207 if (current_cresc_ev_
->in_event_class ("crescendo-event"))
208 msg
= _ ("already have a crescendo");
210 accepted_spanevents_drul_
[START
]->origin ()->warning (msg
);
211 current_cresc_ev_
->origin ()->warning (_ ("cresc starts here"));
215 current_cresc_ev_
= accepted_spanevents_drul_
[START
];
217 if (Direction d
= to_dir (current_cresc_ev_
->get_property ("direction")))
218 set_grob_direction (line_spanner_
, d
);
224 SCM start_sym
= current_cresc_ev_
->get_property ("class");
227 if (start_sym
== ly_symbol2scm ("decrescendo-event"))
228 start_type
= "decrescendo";
229 else if (start_sym
== ly_symbol2scm ("crescendo-event"))
230 start_type
= "crescendo";
233 programming_error ("unknown dynamic spanner type");
238 UGH. TODO: should read from original event, so appearance
239 may be altered with \tweak.
241 SCM s
= get_property ((start_type
+ "Spanner").c_str ());
242 if (!scm_is_symbol (s
) || s
== ly_symbol2scm ("hairpin"))
244 cresc_
= make_spanner ("Hairpin", accepted_spanevents_drul_
[START
]->self_scm ());
247 Pointer_group_interface::add_grob (finished_cresc_
,
248 ly_symbol2scm ("adjacent-hairpins"),
251 Pointer_group_interface::add_grob (cresc_
,
252 ly_symbol2scm ("adjacent-hairpins"),
258 This is a convenient (and legacy) interface to TextSpanners
259 for use in (de)crescendi.
264 cresc_
= make_spanner ("DynamicTextSpanner", accepted_spanevents_drul_
[START
]->self_scm ());
265 cresc_
->set_property ("style", s
);
266 context ()->set_property ((start_type
267 + "Spanner").c_str (), SCM_EOL
);
268 s
= get_property ((start_type
+ "Text").c_str ());
269 if (Text_interface::is_markup (s
))
271 cresc_
->set_property ("text", s
);
272 context ()->set_property ((start_type
+ "Text").c_str (),
278 set_nested_property (cresc_
,
279 scm_list_3 (ly_symbol2scm ("bound-details"),
280 ly_symbol2scm ("left"),
281 ly_symbol2scm ("attach-dir")
283 scm_from_int (RIGHT
));
289 cresc_
->set_bound (LEFT
, script_
);
290 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
292 Axis_group_interface::add_element (line_spanner_
, cresc_
);
298 Dynamic_engraver::stop_translation_timestep ()
300 if (!current_cresc_ev_
&& line_spanner_
)
302 assert (!finished_line_spanner_
);
303 finished_line_spanner_
= line_spanner_
;
309 if (cresc_
&& !cresc_
->get_bound (LEFT
))
311 cresc_
->set_bound (LEFT
, unsmob_grob (get_property ("currentMusicalColumn")));
312 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
316 accepted_spanevents_drul_
[START
] = 0;
317 accepted_spanevents_drul_
[STOP
] = 0;
321 Dynamic_engraver::finalize ()
326 && !line_spanner_
->is_live ())
330 finished_line_spanner_
= line_spanner_
;
335 && !cresc_
->is_live ())
339 current_cresc_ev_
->origin ()->warning (_ ("unterminated (de)crescendo"));
346 Dynamic_engraver::typeset_all ()
350 if (!finished_cresc_
->get_bound (RIGHT
))
353 Grob
*column_bound
= unsmob_grob (get_property ("currentMusicalColumn"));
355 finished_cresc_
->set_bound (RIGHT
, script_
359 if (finished_line_spanner_
)
360 add_bound_item (finished_line_spanner_
,
361 finished_cresc_
->get_bound (RIGHT
));
367 if (finished_line_spanner_
)
372 extend-spanner-over-elements (finished_line_spanner_);
374 but this is rather kludgy, since finished_line_spanner_
375 typically has a staff-symbol field set , extending it over the
380 Grob
*l
= finished_line_spanner_
->get_bound (LEFT
);
381 Grob
*r
= finished_line_spanner_
->get_bound (RIGHT
);
383 finished_line_spanner_
->set_bound (RIGHT
, l
);
385 finished_line_spanner_
->set_bound (LEFT
, r
);
389 This is a isolated dynamic apparently, and does not even have
390 any interesting support item.
392 Grob
*cc
= unsmob_grob (get_property ("currentMusicalColumn"));
393 Item
*ci
= dynamic_cast<Item
*> (cc
);
394 finished_line_spanner_
->set_bound (RIGHT
, ci
);
395 finished_line_spanner_
->set_bound (LEFT
, ci
);
397 finished_line_spanner_
= 0;
402 Dynamic_engraver::acknowledge_note_column (Grob_info info
)
408 /* Don't refill killed spanner */
409 && line_spanner_
->is_live ())
411 Side_position_interface::add_support (line_spanner_
, info
.grob ());
412 add_bound_item (line_spanner_
, dynamic_cast<Item
*> (info
.grob ()));
415 if (script_
&& !script_
->get_parent (X_AXIS
))
417 extract_grob_set (info
.grob (), "note-heads", heads
);
420 Grob
*head
= heads
[0];
421 script_
->set_parent (head
, X_AXIS
);
422 Self_alignment_interface::set_center_parent (script_
, X_AXIS
);
428 if (!cresc_
->get_bound (LEFT
))
430 cresc_
->set_bound (LEFT
, info
.grob ());
431 add_bound_item (line_spanner_
, cresc_
->get_bound (LEFT
));
435 if (finished_cresc_
&& !finished_cresc_
->get_bound (RIGHT
))
436 finished_cresc_
->set_bound (RIGHT
, info
.grob ());
439 ADD_ACKNOWLEDGER (Dynamic_engraver
, note_column
);
441 ADD_TRANSLATOR (Dynamic_engraver
,
443 "Create hairpins, dynamic texts, and their vertical"
444 " alignments. The symbols are collected onto a"
445 " @code{DynamicLineSpanner} grob which takes care of vertical"
449 "DynamicLineSpanner "
450 "DynamicTextSpanner "