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