]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
patch::: 1.3.98.jcn2
[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 "crescendo.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<Score_element> 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 do_removal_processing ();
58   virtual void acknowledge_element (Score_element_info);
59   virtual bool do_try_music (Music *req_l);
60   virtual void do_process_music ();
61   virtual void do_pre_move_processing ();
62   virtual void do_post_move_processing ();
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::do_post_move_processing ()
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::do_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       if (s->span_type_str_ == "abort")
102         {
103           accepted_spanreqs_drul_[LEFT] = 0;
104           accepted_spanreqs_drul_[RIGHT] = 0;
105           if (line_spanner_)
106             line_spanner_->suicide ();
107           line_spanner_ = 0;
108           if (cresc_p_)
109             cresc_p_->suicide ();
110           cresc_p_ = 0;
111         }
112       else if ((s->span_type_str_ == "crescendo"
113            || s->span_type_str_ == "decrescendo"))
114         {
115           accepted_spanreqs_drul_[s->span_dir_] = s;
116           return true;
117         }
118     }
119   return false;
120 }
121
122 void
123 Dynamic_engraver::do_process_music ()
124 {
125   if (accepted_spanreqs_drul_[START] || accepted_spanreqs_drul_[STOP] || script_req_l_)
126     
127     {
128       if (!line_spanner_)
129         {
130           line_spanner_ = new Spanner (get_property ("DynamicLineSpanner"));
131
132           Side_position::set_axis (line_spanner_, Y_AXIS);
133           Axis_group_interface::set_interface (line_spanner_);
134           Axis_group_interface::set_axes (line_spanner_, Y_AXIS, Y_AXIS);
135
136           Music * rq = accepted_spanreqs_drul_[START];
137           if (script_req_l_)
138             rq =  script_req_l_ ;
139           announce_element (line_spanner_, rq);
140                          
141
142         }
143     }
144
145   /*
146     finish side position alignment if the (de)cresc ends here, and
147     there are no new dynamics.
148     
149    */
150   else if (accepted_spanreqs_drul_[STOP]
151            && !accepted_spanreqs_drul_[START] && !script_req_l_)
152     {
153       finished_line_spanner_ = line_spanner_;
154       line_spanner_ = 0;
155     }
156
157         /*
158         todo: resurrect  dynamic{direction, padding,minimumspace}
159         */
160         /*
161         During a (de)crescendo, pending request will not be cleared,
162         and a line-spanner will always be created, as \< \! are already
163         two requests.
164
165         Maybe always creating a line-spanner for a (de)crescendo (see
166         below) is not a good idea:
167
168             a\< b\p \!c
169
170         the \p will be centred on the line-spanner, and thus clash
171         with the hairpin.  When axis-group code is in place, the \p
172         should move below the hairpin, which is probably better?
173
174         Urg, but line-spanner must always have at least same duration
175         as (de)crecsendo, b.o. line-breaking.
176         */
177
178   
179
180   /*
181     maybe we should leave dynamic texts to the text-engraver and
182     simply acknowledge them?
183   */
184   if (script_req_l_)
185     {
186       script_p_ = new Item (get_property ("DynamicText"));
187       script_p_->set_elt_property ("text",
188                                    script_req_l_->get_mus_property ("text"));
189       if (Direction d = script_req_l_->get_direction ())
190         Directional_element_interface::set (line_spanner_, d);
191
192       Axis_group_interface::add_element (line_spanner_, script_p_);
193
194       announce_element (script_p_, script_req_l_);
195     }
196
197   if (accepted_spanreqs_drul_[STOP])
198     {
199       if (!cresc_p_)
200         {
201           accepted_spanreqs_drul_[STOP]->origin ()->warning
202             (_ ("can't find start of (de)crescendo"));
203         }
204       else
205         {
206           assert (!finished_cresc_p_);
207           Score_element* cc = unsmob_element (get_property ("currentMusicalColumn"));
208           
209           cresc_p_->set_bound (RIGHT, cc);
210
211           finished_cresc_p_ = cresc_p_;
212           cresc_p_ = 0;
213           current_cresc_req_ = 0;
214         }
215     }
216
217   if (accepted_spanreqs_drul_[START])
218     {
219       if (current_cresc_req_)
220         {
221           accepted_spanreqs_drul_[START]->origin ()->warning
222             (current_cresc_req_->span_dir_ == 1
223              ? _ ("already have a crescendo")
224              : _ ("already have a decrescendo"));
225         }
226       else
227         {
228           current_cresc_req_ = accepted_spanreqs_drul_[START];
229
230           /*
231             TODO: Use symbols.
232            */
233           SCM s = get_property ((accepted_spanreqs_drul_[START]->span_type_str_ + "Spanner").ch_C());
234
235           if (!gh_string_p (s) || ly_scm2string (s) == "hairpin")
236             {
237               cresc_p_  = new Spanner (get_property ("Crescendo"));
238               cresc_p_->set_elt_property ("grow-direction",
239                                           gh_int2scm ((accepted_spanreqs_drul_[START]->span_type_str_ == "crescendo")
240                                                       ? BIGGER : SMALLER));
241               
242             }
243           /*
244             This is a convenient (and legacy) interface to TextSpanners
245             for use in (de)crescendi.
246             Hmm.
247            */
248           else
249             {
250               cresc_p_  = new Spanner (get_property ("TextSpanner"));
251               cresc_p_->set_elt_property ("type", s);
252               daddy_trans_l_->set_property (accepted_spanreqs_drul_[START]->span_type_str_
253                                             + "Spanner", SCM_UNDEFINED);
254               s = get_property ((accepted_spanreqs_drul_[START]->span_type_str_ + "Text").ch_C());
255               if (gh_string_p (s))
256                 {
257                   cresc_p_->set_elt_property ("edge-text",
258                                               gh_cons (s, ly_str02scm ("")));
259                   daddy_trans_l_->set_property (accepted_spanreqs_drul_[START]->span_type_str_
260                                                 + "Text", SCM_UNDEFINED);
261                 }
262             }
263         
264           Score_element *cc = unsmob_element (get_property ("currentMusicalColumn"));
265           cresc_p_->set_bound (LEFT, cc);
266
267           if (script_p_)
268             {
269               Side_position::set_direction (script_p_, LEFT);
270               Side_position::set_axis (script_p_, X_AXIS);
271               Side_position::add_support (script_p_, cresc_p_);
272             }
273
274           Axis_group_interface::add_element (line_spanner_, cresc_p_);
275           announce_element (cresc_p_, accepted_spanreqs_drul_[START]);
276         }
277     }
278 }
279
280 void
281 Dynamic_engraver::do_pre_move_processing ()
282 {
283   typeset_all ();
284 }
285
286 void
287 Dynamic_engraver::do_removal_processing ()
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       typeset_element (finished_cresc_p_);
310       finished_cresc_p_ =0;
311     }
312   
313   if (script_p_)
314     {
315       typeset_element (script_p_);
316       script_p_ = 0;
317     }
318   if (finished_line_spanner_)
319     {
320       Side_position::add_staff_support (finished_line_spanner_);
321       extend_spanner_over_elements (finished_line_spanner_);
322       typeset_element (finished_line_spanner_);
323       finished_line_spanner_ = 0;
324     }
325 }
326
327 void
328 Dynamic_engraver::acknowledge_element (Score_element_info i)
329 {
330   if (Note_column::has_interface (i.elem_l_))
331     {
332       if (line_spanner_)
333         {
334           Side_position::add_support (line_spanner_,i.elem_l_);
335           add_bound_item (line_spanner_,dynamic_cast<Item*>(i.elem_l_));
336         }
337     }
338 }