]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
* scm/music-functions.scm (has-request-chord): don't use
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   dynamic-engraver.cc -- implement Dynamic_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
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 "interval.hh"
16 #include "note-column.hh"
17 #include "paper-column.hh"
18 #include "script-interface.hh"
19 #include "side-position-interface.hh"
20 #include "staff-symbol-referencer.hh"
21 #include "warn.hh"
22
23 /*
24   TODO:
25
26   * direction of text-dynamic-event if not equal to direction of
27   line-spanner
28
29   - TODO: this engraver is too complicated. We should split it into
30   the handling of the basic grobs and the  linespanner
31
32   - TODO: the line-spanner is not killed after the (de)crescs are
33   finished.
34
35 */
36
37 /**
38    print text & hairpin dynamics.
39  */
40 class Dynamic_engraver : public Engraver
41 {
42   Item *script_;
43   Spanner *line_spanner_;
44   Spanner *cresc_;
45
46   Spanner *finished_line_spanner_;
47   Spanner *finished_cresc_;
48
49   Music *script_ev_;
50   Music *current_cresc_ev_;
51   
52   Drul_array<Music*> accepted_spanreqs_drul_;
53
54   Link_array<Note_column> pending_columns_;
55   Link_array<Grob> pending_elements_;
56   
57   void typeset_all ();
58
59   TRANSLATOR_DECLARATIONS (Dynamic_engraver);
60   
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 ();  
67 };
68
69
70
71
72 Dynamic_engraver::Dynamic_engraver ()
73 {
74   script_ = 0;
75   finished_cresc_ = 0;
76   line_spanner_ = 0;
77   finished_line_spanner_ = 0;
78   current_cresc_ev_ = 0;
79   cresc_ = 0;
80
81   script_ev_ = 0;
82   accepted_spanreqs_drul_[START] = 0;
83   accepted_spanreqs_drul_[STOP] = 0;
84 }
85
86 bool
87 Dynamic_engraver::try_music (Music *m)
88 {
89   if (m->is_mus_type ("absolute-dynamic-event"))
90     {
91       /*
92         TODO: probably broken.
93       */
94       script_ev_ = m;
95       return true;
96     }
97   else if (m->is_mus_type ("decrescendo-event")
98            || m->is_mus_type ("crescendo-event"))
99     {
100       Direction d = to_dir (m->get_property ("span-direction"));
101
102       accepted_spanreqs_drul_[d] = m;
103       if (current_cresc_ev_ && d == START)
104         accepted_spanreqs_drul_[STOP] = m;
105       return true;
106     }
107   return false;
108 }
109
110 void
111 Dynamic_engraver::process_music ()
112 {
113   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_ev_)
114     {
115       if (!line_spanner_)
116         {
117           Music * rq = accepted_spanreqs_drul_[START];
118           line_spanner_ = make_spanner ("DynamicLineSpanner", rq ? rq->self_scm (): SCM_EOL );
119
120           if (script_ev_)
121             rq =  script_ev_;
122         }
123     }
124   
125   /*
126     During a (de)crescendo, pending event will not be cleared,
127     and a line-spanner will always be created, as \< \! are already
128     two events.
129
130     Note: line-spanner must always have at least same duration
131     as (de)crecsendo, b.o. line-breaking.
132   */
133   
134
135   /*
136     maybe we should leave dynamic texts to the text-engraver and
137     simply acknowledge them?
138   */
139   if (script_ev_)
140     {
141       script_ = make_item ("DynamicText", script_ev_->self_scm ());
142       script_->set_property ("text",
143                              script_ev_->get_property ("text"));
144
145       
146       if (Direction d = to_dir (script_ev_->get_property ("direction")))
147         set_grob_direction (line_spanner_, d);
148
149       Axis_group_interface::add_element (line_spanner_, script_);
150     }
151
152   Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
153     accepted_spanreqs_drul_[STOP] : script_ev_;
154
155   if (accepted_spanreqs_drul_[STOP] || script_ev_)
156     {
157       /*
158         finish side position alignment if the (de)cresc ends here, and
159         there are no new dynamics.
160        */
161
162
163       if (cresc_)
164         {
165           assert (!finished_cresc_ && cresc_);
166
167           cresc_->set_bound (RIGHT, script_
168                                ? script_
169                                : unsmob_grob (get_property ("currentMusicalColumn")));
170           add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
171           
172
173           finished_cresc_ = cresc_;
174           cresc_ = 0;
175           current_cresc_ev_ = 0;
176         }
177       else if (accepted_spanreqs_drul_[STOP])
178         {
179           accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
180           stop_ev = 0;
181         }
182       
183     }
184   
185   if (accepted_spanreqs_drul_[START])
186     {
187       if (current_cresc_ev_)
188         {
189           String msg = _ ("already have a decrescendo");
190           if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
191             msg = _ ("already have a crescendo");
192
193           accepted_spanreqs_drul_[START]->origin ()->warning (msg);
194           current_cresc_ev_->origin ()->warning (_("Cresc started here"));
195         }
196       else
197         {
198           current_cresc_ev_ = accepted_spanreqs_drul_[START];
199
200           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
201             set_grob_direction (line_spanner_, d);
202
203           /*
204             TODO: Use symbols.
205           */
206
207           String start_type = 
208             ly_symbol2string (current_cresc_ev_->get_property ("name"));
209
210           /*
211             ugh. Use push/pop?
212           */
213           if (start_type == "DecrescendoEvent")
214             start_type = "decrescendo";
215           else if (start_type == "CrescendoEvent")
216             start_type = "crescendo";
217           
218           SCM s = get_property ((start_type + "Spanner").to_str0 ());
219           if (!scm_is_symbol (s) || s == ly_symbol2scm ("hairpin"))
220             {
221               cresc_  = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
222               if (finished_cresc_)
223                 {
224                   Pointer_group_interface::add_grob (finished_cresc_,
225                                                      ly_symbol2scm ("adjacent-hairpins"),
226                                                      cresc_);
227                   
228                   Pointer_group_interface::add_grob (cresc_,
229                                                      ly_symbol2scm ("adjacent-hairpins"),
230                                                      finished_cresc_);
231                 }
232               cresc_->set_property ("grow-direction",
233                                     scm_int2num ((start_type == "crescendo")
234                                                  ? BIGGER : SMALLER));
235               
236             }
237
238           
239           /*
240             This is a convenient (and legacy) interface to TextSpanners
241             for use in (de)crescendi.
242             Hmm.
243           */
244           else
245             {
246               cresc_  = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
247               cresc_->set_property ("style", s);
248               context ()->set_property ((start_type
249                                          + "Spanner").to_str0 (), SCM_EOL);
250               s = get_property ((start_type + "Text").to_str0 ());
251               /*
252                 FIXME: use get_markup () to check type.
253               */
254               if (scm_is_string (s) || scm_is_pair (s))
255                 {
256                   cresc_->set_property ("edge-text",
257                                         scm_cons (s, scm_makfrom0str ("")));
258                   context ()->set_property ((start_type + "Text").to_str0 (),
259                                             SCM_EOL);
260                 }
261             }
262
263           if (script_)
264             {
265               cresc_->set_bound (LEFT, script_);
266               add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
267             }
268           
269           Axis_group_interface::add_element (line_spanner_, cresc_);
270         }
271     }
272 }
273
274 void
275 Dynamic_engraver::stop_translation_timestep ()
276 {
277   typeset_all ();
278   if (!current_cresc_ev_)
279     {
280       finished_line_spanner_ = line_spanner_;
281       line_spanner_ = 0;
282       typeset_all ();
283     }
284
285   if (cresc_ && !cresc_->get_bound (LEFT))
286     {
287       cresc_->set_bound (LEFT, unsmob_grob (get_property ("currentMusicalColumn")));
288       add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
289     }
290   
291   script_ev_ = 0;
292   accepted_spanreqs_drul_[START] = 0;
293   accepted_spanreqs_drul_[STOP] = 0;
294 }
295
296 void
297 Dynamic_engraver::finalize ()
298 {
299   typeset_all ();
300   
301   if (line_spanner_
302       && !line_spanner_->is_live ())
303     line_spanner_ = 0;
304   if (line_spanner_)
305     {
306       finished_line_spanner_ = line_spanner_;
307       typeset_all ();
308     }
309
310   if (cresc_
311       && !cresc_->is_live ())
312     cresc_ = 0;
313   if (cresc_)
314     {
315       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
316       cresc_->suicide ();
317       cresc_ = 0;
318     }
319 }
320
321 void
322 Dynamic_engraver::typeset_all ()
323 {
324   if (finished_cresc_)
325     {
326       if (!finished_cresc_->get_bound (RIGHT))
327         {
328           finished_cresc_->set_bound (RIGHT, script_
329                                       ? script_
330                                       : unsmob_grob (get_property ("currentMusicalColumn")));
331           
332           if (finished_line_spanner_)
333             add_bound_item (finished_line_spanner_,
334                             finished_cresc_->get_bound (RIGHT));
335         }
336       finished_cresc_ = 0;
337     }
338   
339   script_ = 0;
340   if (finished_line_spanner_)
341     {
342       /*
343         We used to have
344         
345              extend-spanner-over-elements (finished_line_spanner_);
346
347         but this is rather kludgy, since finished_line_spanner_
348         typically has a staff-symbol field set , extending it over the
349         entire staff.
350
351       */
352
353       Grob * l = finished_line_spanner_->get_bound (LEFT);
354       Grob * r = finished_line_spanner_->get_bound (RIGHT);      
355       if (!r && l)
356         finished_line_spanner_->set_bound (RIGHT, l);
357       else if (!l && r)
358         finished_line_spanner_->set_bound (LEFT, r);
359       else if (!r && !l)
360         {
361           /*
362             This is a isolated dynamic apparently, and does not even have
363             any interesting support item.
364            */
365           Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
366           Item * ci = dynamic_cast<Item*>(cc);
367           finished_line_spanner_->set_bound (RIGHT, ci);
368           finished_line_spanner_->set_bound (LEFT, ci);   
369         }
370         
371       finished_line_spanner_ = 0;
372     }
373 }
374
375 void
376 Dynamic_engraver::acknowledge_grob (Grob_info info)
377 {
378   if (!line_spanner_)
379     return;
380
381   if (Note_column::has_interface (info.grob_))
382     {
383       if (line_spanner_
384           /* Don't refill killed spanner */
385           && line_spanner_->is_live ())
386         {
387           Side_position_interface::add_support (line_spanner_, info.grob_);
388           add_bound_item (line_spanner_, dynamic_cast<Item*> (info.grob_));
389         }
390
391       if (script_ && !script_->get_parent (X_AXIS))
392         {
393           SCM head = scm_last_pair (info.grob_->get_property ("note-heads"));
394           if (scm_is_pair (head))
395             script_->set_parent (unsmob_grob (scm_car (head)),  X_AXIS);
396         }
397
398
399
400       if (cresc_ && !cresc_->get_bound (LEFT))
401         {
402           cresc_->set_bound (LEFT, info.grob_);
403           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
404         }
405         
406     }
407   else if (Script_interface::has_interface (info.grob_) && script_)
408     {
409       SCM p = info.grob_->get_property ("script-priority");
410
411       /*
412         UGH.
413
414         DynamicText doesn't really have a script-priority field.
415        */
416       if (scm_is_number (p)
417           && scm_to_int (p)
418           < scm_to_int (script_->get_property ("script-priority")))
419         Side_position_interface::add_support (line_spanner_, info.grob_);
420     }
421 }
422
423 ADD_TRANSLATOR (Dynamic_engraver,
424 /* descr */       
425 "This engraver creates hairpins, dynamic texts, and their vertical\n"
426 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
427 "which takes care of vertical positioning.  "
428 ,
429                   
430 /* creats*/       "DynamicLineSpanner DynamicText Hairpin TextSpanner",
431 /* accepts */     "absolute-dynamic-event crescendo-event decrescendo-event",
432 /* acks  */      "note-column-interface script-interface",
433 /* reads */       "",
434 /* write */       "");