]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
* lily/dynamic-text-spanner.cc (print): new file.
[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 #include "warn.hh"
9 #include "dimensions.hh"
10 #include "hairpin.hh"
11 #include "event.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 "context.hh"
20 #include "axis-group-interface.hh"
21 #include "script-interface.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   /*
137     maybe we should leave dynamic texts to the text-engraver and
138     simply acknowledge them?
139   */
140   if (script_ev_)
141     {
142       script_ = make_item ("DynamicText", script_ev_->self_scm ());
143       script_->set_property ("text",
144                              script_ev_->get_property ("text"));
145
146       
147       if (Direction d = to_dir (script_ev_->get_property ("direction")))
148         set_grob_direction (line_spanner_, d);
149
150       Axis_group_interface::add_element (line_spanner_, script_);
151     }
152
153   Music *stop_ev = accepted_spanreqs_drul_ [STOP] ?
154     accepted_spanreqs_drul_[STOP] : script_ev_;
155
156   if (accepted_spanreqs_drul_[STOP] || script_ev_)
157     {
158       /*
159         finish side position alignment if the (de)cresc ends here, and
160         there are no new dynamics.
161        */
162
163
164       if (cresc_)
165         {
166           assert (!finished_cresc_ && cresc_);
167
168           cresc_->set_bound (RIGHT, script_
169                                ? script_
170                                : unsmob_grob (get_property ("currentMusicalColumn")));
171           add_bound_item (line_spanner_, cresc_->get_bound (RIGHT));
172           
173
174           finished_cresc_ = cresc_;
175           cresc_ = 0;
176           current_cresc_ev_ = 0;
177         }
178       else if (accepted_spanreqs_drul_[STOP])
179         {
180           accepted_spanreqs_drul_[STOP]->origin ()->warning (_ ("can't find start of (de)crescendo"));
181           stop_ev = 0;
182         }
183       
184     }
185   
186   if (accepted_spanreqs_drul_[START])
187     {
188       if (current_cresc_ev_)
189         {
190           String msg = _ ("already have a decrescendo");
191           if (current_cresc_ev_->is_mus_type ("decrescendo-event"))
192             msg = _ ("already have a crescendo");
193
194           accepted_spanreqs_drul_[START]->origin ()->warning (msg);
195           current_cresc_ev_->origin ()->warning (_("Cresc started here"));
196         }
197       else
198         {
199           current_cresc_ev_ = accepted_spanreqs_drul_[START];
200
201           if (Direction d = to_dir (current_cresc_ev_->get_property ("direction")))
202             set_grob_direction (line_spanner_, d);
203
204           /*
205             TODO: Use symbols.
206           */
207
208           String start_type = 
209             ly_symbol2string (current_cresc_ev_->get_property ("name"));
210
211           /*
212             ugh. Use push/pop?
213           */
214           if (start_type == "DecrescendoEvent")
215             start_type = "decrescendo";
216           else if (start_type == "CrescendoEvent")
217             start_type = "crescendo";
218           
219           SCM s = get_property ((start_type + "Spanner").to_str0 ());
220           if (!ly_c_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
221             {
222               cresc_  = make_spanner ("Hairpin", accepted_spanreqs_drul_[START]->self_scm ());
223               cresc_->set_property ("grow-direction",
224                                     scm_int2num ((start_type == "crescendo")
225                                                  ? BIGGER : SMALLER));
226               
227             }
228
229           
230           /*
231             This is a convenient (and legacy) interface to TextSpanners
232             for use in (de)crescendi.
233             Hmm.
234           */
235           else
236             {
237               cresc_  = make_spanner ("DynamicTextSpanner", accepted_spanreqs_drul_[START]->self_scm ());
238               cresc_->set_property ("style", s);
239               context ()->set_property ((start_type
240                                          + "Spanner").to_str0 (), SCM_EOL);
241               s = get_property ((start_type + "Text").to_str0 ());
242               /*
243                 FIXME: use get_markup () to check type.
244               */
245               if (ly_c_string_p (s) || ly_c_pair_p (s))
246                 {
247                   cresc_->set_property ("edge-text",
248                                         scm_cons (s, scm_makfrom0str ("")));
249                   context ()->set_property ((start_type + "Text").to_str0 (),
250                                             SCM_EOL);
251                 }
252             }
253
254           cresc_->set_bound (LEFT, script_
255                              ? script_
256                              : unsmob_grob (get_property ("currentMusicalColumn")));
257
258           Axis_group_interface::add_element (line_spanner_, cresc_);
259
260           add_bound_item (line_spanner_, cresc_->get_bound (LEFT));
261         }
262     }
263 }
264
265 void
266 Dynamic_engraver::stop_translation_timestep ()
267 {
268   typeset_all ();
269   if (!current_cresc_ev_)
270     {
271       finished_line_spanner_ = line_spanner_;
272       line_spanner_ =0;
273       typeset_all ();
274     }
275
276   script_ev_ = 0;
277   accepted_spanreqs_drul_[START] = 0;
278   accepted_spanreqs_drul_[STOP] = 0;
279 }
280
281 void
282 Dynamic_engraver::finalize ()
283 {
284   typeset_all ();
285   
286   if (line_spanner_
287       && !line_spanner_->is_live ())
288     line_spanner_ = 0;
289   if (line_spanner_)
290     {
291       finished_line_spanner_ = line_spanner_;
292       typeset_all ();
293     }
294
295   if (cresc_
296       && !cresc_->is_live ())
297     cresc_ = 0;
298   if (cresc_)
299     {
300       current_cresc_ev_->origin ()->warning (_ ("unterminated (de)crescendo"));
301       cresc_->suicide ();
302       cresc_ = 0;
303     }
304 }
305
306 void
307 Dynamic_engraver::typeset_all ()
308 {  
309   if (finished_cresc_)
310     {
311       if (!finished_cresc_->get_bound (RIGHT))
312         {
313           finished_cresc_->set_bound (RIGHT, script_
314                                       ? script_
315                                       : unsmob_grob (get_property ("currentMusicalColumn")));
316
317           if (finished_line_spanner_)
318             add_bound_item (finished_line_spanner_,
319                             finished_cresc_->get_bound (RIGHT));
320         }
321       finished_cresc_ =0;
322     }
323   
324   script_ = 0;
325   if (finished_line_spanner_)
326     {
327       /*
328         We used to have
329         
330              extend-spanner-over-elements (finished_line_spanner_);
331
332         but this is rather kludgy, since finished_line_spanner_
333         typically has a staff-symbol field set , extending it over the
334         entire staff.
335
336       */
337
338       Grob * l = finished_line_spanner_->get_bound (LEFT);
339       Grob * r = finished_line_spanner_->get_bound (RIGHT);      
340       if (!r && l)
341         finished_line_spanner_->set_bound (RIGHT, l);
342       else if (!l && r)
343         finished_line_spanner_->set_bound (LEFT, r);
344       else if (!r && !l)
345         {
346           /*
347             This is a isolated dynamic apparently, and does not even have
348             any interesting support item.
349            */
350           Grob * cc = unsmob_grob (get_property ("currentMusicalColumn"));
351           Item * ci = dynamic_cast<Item*>(cc);
352           finished_line_spanner_->set_bound (RIGHT, ci);
353           finished_line_spanner_->set_bound (LEFT, ci);   
354         }
355         
356       finished_line_spanner_ = 0;
357     }
358 }
359
360 void
361 Dynamic_engraver::acknowledge_grob (Grob_info i)
362 {
363   if (!line_spanner_)
364     return ;
365   
366   if (Note_column::has_interface (i.grob_))
367     {
368       if (line_spanner_
369           /* Don't refill killed spanner */
370           && line_spanner_->is_live ())
371         {
372           Side_position_interface::add_support (line_spanner_,i.grob_);
373           add_bound_item (line_spanner_,dynamic_cast<Item*> (i.grob_));
374         }
375
376       if (script_ && !script_->get_parent (X_AXIS))
377         {
378           SCM head = scm_last_pair (i.grob_->get_property ("note-heads"));
379           if (ly_c_pair_p (head))
380             script_->set_parent (unsmob_grob (ly_car (head)),  X_AXIS);
381         }
382       
383     }
384   else if (Script_interface::has_interface (i.grob_) && script_)
385     {
386       SCM p = i.grob_->get_property ("script-priority");
387
388       /*
389         UGH.
390
391         DynamicText doesn't really have a script-priority field.
392        */
393       if (ly_c_number_p (p)
394           && ly_scm2int (p) < ly_scm2int (script_->get_property ("script-priority")))
395         {
396           Side_position_interface::add_support (line_spanner_, i.grob_);
397
398         }
399     }
400 }
401
402 ENTER_DESCRIPTION (Dynamic_engraver,
403 /* descr */       
404 "This engraver creates hairpins, dynamic texts, and their vertical\n"
405 "alignments.  The symbols are collected onto a DynamicLineSpanner grob\n"
406 "which takes care of vertical positioning.  "
407 ,
408                   
409 /* creats*/       "DynamicLineSpanner DynamicText Hairpin TextSpanner",
410 /* accepts */     "absolute-dynamic-event crescendo-event decrescendo-event",
411 /* acks  */      "note-column-interface script-interface",
412 /* reads */       "",
413 /* write */       "");