]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
patch::: 1.3.116.jcn3
[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--2000 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"
21
22
23 /*
24   TODO:
25
26   * direction of text-dynamic-request if not equal to direction of
27   line-spanner
28 */
29
30 /**
31    print text & hairpin dynamics.
32  */
33 class Dynamic_engraver : public Engraver
34 {
35   Item * script_p_;
36   Spanner * finished_cresc_p_;
37   Spanner * cresc_p_;
38
39   Text_script_req* script_req_l_;
40   
41   Span_req * current_cresc_req_;
42   Drul_array<Span_req*> accepted_spanreqs_drul_;
43
44   Spanner* line_spanner_;
45   Spanner* finished_line_spanner_;
46
47   Link_array<Note_column> pending_column_arr_;
48   Link_array<Grob> pending_element_arr_;
49   
50   void typeset_all ();
51
52 public:
53   VIRTUAL_COPY_CONS(Translator);
54   Dynamic_engraver ();
55   
56 protected:
57   virtual void finalize ();
58   virtual void acknowledge_grob (Grob_info);
59   virtual bool try_music (Music *req_l);
60   virtual void stop_translation_timestep ();
61   virtual void create_grobs ();
62   virtual void start_translation_timestep ();
63 };
64
65 ADD_THIS_TRANSLATOR (Dynamic_engraver);
66
67
68 Dynamic_engraver::Dynamic_engraver ()
69 {
70   script_p_ = 0;
71   finished_cresc_p_ = 0;
72   line_spanner_ = 0;
73   finished_line_spanner_ = 0;
74   current_cresc_req_ = 0;
75   cresc_p_ =0;
76
77   script_req_l_ = 0;
78   accepted_spanreqs_drul_[START] = 0;
79   accepted_spanreqs_drul_[STOP] = 0;
80 }
81
82 void
83 Dynamic_engraver::start_translation_timestep ()
84 {
85   script_req_l_ = 0;
86   accepted_spanreqs_drul_[START] = 0;
87   accepted_spanreqs_drul_[STOP] = 0;
88 }
89
90 bool
91 Dynamic_engraver::try_music (Music * m)
92 {
93   if (dynamic_cast <Text_script_req*> (m)
94       && m->get_mus_property ("text-type") == ly_symbol2scm ("dynamic"))
95     {
96       script_req_l_ = dynamic_cast<Text_script_req*> (m);
97       return true;
98     }
99   else if (Span_req* s =  dynamic_cast <Span_req*> (m))
100     {
101       String t = ly_scm2string (s->get_mus_property ("span-type"));
102       if (t== "abort")
103         {
104           accepted_spanreqs_drul_[LEFT] = 0;
105           accepted_spanreqs_drul_[RIGHT] = 0;
106           if (line_spanner_)
107             line_spanner_->suicide ();
108           line_spanner_ = 0;
109           if (cresc_p_)
110             cresc_p_->suicide ();
111           cresc_p_ = 0;
112         }
113       else if (t == "crescendo"
114            || t == "decrescendo")
115         {
116           accepted_spanreqs_drul_[s->get_span_dir()] = s;
117           return true;
118         }
119     }
120   return false;
121 }
122
123 void
124 Dynamic_engraver::create_grobs ()
125 {
126   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
127     
128     {
129       if (!line_spanner_)
130         {
131           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
132
133           Side_position::set_axis (line_spanner_, Y_AXIS);
134           Axis_group_interface::set_interface (line_spanner_);
135           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
136
137           Music * rq = accepted_spanreqs_drul_[START];
138           if (script_req_l_)
139             rq =  script_req_l_ ;
140           announce_grob (line_spanner_, rq);
141                          
142
143         }
144     }
145
146   /*
147     finish side position alignment if the (de)cresc ends here, and
148     there are no new dynamics.
149     
150    */
151   else if (accepted_spanreqs_drul_[STOP]
152            && !accepted_spanreqs_drul_[START] && !script_req_l_)
153     {
154       finished_line_spanner_ = line_spanner_;
155       line_spanner_ = 0;
156     }
157
158         /*
159         todo: resurrect  dynamic{direction, padding,minimumspace}
160         */
161         /*
162         During a (de)crescendo, pending request will not be cleared,
163         and a line-spanner will always be created, as \< \! are already
164         two requests.
165
166         Note: line-spanner must always have at least same duration
167         as (de)crecsendo, b.o. line-breaking.
168         */
169
170   
171
172   /*
173     maybe we should leave dynamic texts to the text-engraver and
174     simply acknowledge them?
175   */
176   if (script_req_l_)
177     {
178       script_p_ = new Item (get_property ("DynamicText"));
179       script_p_->set_grob_property ("text",
180                                    script_req_l_->get_mus_property ("text"));
181       
182       Side_position::set_direction (script_p_, LEFT);
183       Side_position::set_axis (script_p_, X_AXIS);
184       
185       if (Direction d = script_req_l_->get_direction ())
186         Directional_element_interface::set (line_spanner_, d);
187
188       Axis_group_interface::add_element (line_spanner_, script_p_);
189
190       announce_grob (script_p_, script_req_l_);
191     }
192
193   if (accepted_spanreqs_drul_[STOP])
194     {
195       if (!cresc_p_)
196         {
197           accepted_spanreqs_drul_[STOP]->origin ()->warning
198             (_ ("can't find start of (de)crescendo"));
199         }
200       else
201         {
202           assert (!finished_cresc_p_);
203
204           cresc_p_->set_bound (RIGHT, script_p_
205                            ? script_p_
206                            : unsmob_grob (get_property ("currentMusicalColumn")));
207
208           finished_cresc_p_ = cresc_p_;
209           cresc_p_ = 0;
210           current_cresc_req_ = 0;
211         }
212     }
213
214   if (accepted_spanreqs_drul_[START])
215     {
216       if (current_cresc_req_)
217         {
218           accepted_spanreqs_drul_[START]->origin ()->warning
219             (current_cresc_req_->get_span_dir() == 1
220              ? _ ("already have a crescendo")
221              : _ ("already have a decrescendo"));
222         }
223       else
224         {
225           current_cresc_req_ = accepted_spanreqs_drul_[START];
226
227           /*
228             TODO: Use symbols.
229            */
230
231           String start_type = ly_scm2string (accepted_spanreqs_drul_[START]->get_mus_property ("span-type"));
232
233           /*
234             ugh. Use push/pop?
235            */
236           SCM s = get_property ((start_type + "Spanner").ch_C());
237           if (!gh_symbol_p (s) || s == ly_symbol2scm ("hairpin"))
238             {
239               cresc_p_  = new Spanner (get_property ("Hairpin"));
240               cresc_p_->set_grob_property ("grow-direction",
241                                           gh_int2scm ((start_type == "crescendo")
242                                                       ? BIGGER : SMALLER));
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_p_  = new Spanner (get_property ("TextSpanner"));
253               cresc_p_->set_grob_property ("type", s);
254               
255               daddy_trans_l_->set_property (start_type
256                                             + "Spanner", SCM_UNDEFINED);
257               s = get_property ((start_type + "Text").ch_C());
258               if (gh_string_p (s))
259                 {
260                   cresc_p_->set_grob_property ("edge-text",
261                                               gh_cons (s, ly_str02scm ("")));
262                   daddy_trans_l_->set_property (start_type + "Text",
263                                                 SCM_UNDEFINED);
264                 }
265             }
266
267           cresc_p_->set_bound (LEFT, script_p_
268                                ? script_p_
269                                : unsmob_grob (get_property ("currentMusicalColumn")));
270
271           Axis_group_interface::add_element (line_spanner_, cresc_p_);
272           announce_grob (cresc_p_, accepted_spanreqs_drul_[START]);
273         }
274     }
275   script_req_l_ = 0;
276   accepted_spanreqs_drul_[START] = 0;
277   accepted_spanreqs_drul_[STOP] = 0;
278 }
279
280 void
281 Dynamic_engraver::stop_translation_timestep ()
282 {
283   typeset_all ();
284 }
285
286 void
287 Dynamic_engraver::finalize ()
288 {
289   typeset_all ();
290   if (line_spanner_)
291     {
292       finished_line_spanner_ = line_spanner_;
293       typeset_all ();
294     }
295
296   if (cresc_p_)
297     {
298       current_cresc_req_->origin ()->warning (_ ("unterminated (de)crescendo"));
299       cresc_p_->suicide ();
300       cresc_p_ = 0;
301     }
302 }
303
304 void
305 Dynamic_engraver::typeset_all ()
306 {  
307   if (finished_cresc_p_)
308     {
309 #if 1
310       finished_cresc_p_->set_bound (RIGHT, script_p_
311                            ? script_p_
312                            : unsmob_grob (get_property ("currentMusicalColumn")));
313 #endif          
314       typeset_grob (finished_cresc_p_);
315       finished_cresc_p_ =0;
316     }
317   
318   if (script_p_)
319     {
320       typeset_grob (script_p_);
321       script_p_ = 0;
322     }
323   if (finished_line_spanner_)
324     {
325       Side_position::add_staff_support (finished_line_spanner_);
326       extend_spanner_over_elements (finished_line_spanner_);
327       typeset_grob (finished_line_spanner_);
328       finished_line_spanner_ = 0;
329     }
330 }
331
332 void
333 Dynamic_engraver::acknowledge_grob (Grob_info i)
334 {
335   if (Note_column::has_interface (i.elem_l_))
336     {
337       if (line_spanner_)
338         {
339           Side_position::add_support (line_spanner_,i.elem_l_);
340           add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
341         }
342     }
343 }