]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
patch::: 1.3.31.jcn1
[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 "lookup.hh"
13 #include "paper-def.hh"
14 #include "paper-column.hh"
15 #include "staff-symbol.hh"
16 #include "note-column.hh"
17 #include "text-item.hh"
18 #include "side-position-interface.hh"
19 #include "engraver.hh"
20 #include "stem.hh"
21 #include "note-head.hh"
22 #include "group-interface.hh"
23 #include "directional-element-interface.hh"
24 #include "staff-symbol-referencer.hh"
25
26 #define DYN_LINE
27
28 #ifdef DYN_LINE
29
30 class Dynamic_line_spanner : public Spanner
31 {
32 public:
33   Dynamic_line_spanner ();
34   
35   void add_element (Score_element*);
36   void add_column (Note_column*);
37   Direction get_default_dir () const;
38
39 protected:
40   virtual void do_add_processing ();
41   // URG: see Dynamic_engraver::do_removal_processing
42   friend class Dynamic_engraver;
43   virtual void do_post_processing ();
44
45 private:
46   void translate_elements (Real);
47   Real get_extreme_y () const;
48 };
49
50 Dynamic_line_spanner::Dynamic_line_spanner ()
51 {
52   set_elt_property ("transparent", SCM_BOOL_T);
53 }
54
55 void
56 Dynamic_line_spanner::add_element (Score_element* e)
57 {
58   Group_interface gi (this, "elements");
59   gi.add_element (e);
60   //?
61   Side_position_interface (e).set_axis (Y_AXIS);
62   add_dependency (e);
63 }
64
65 void
66 Dynamic_line_spanner::add_column (Note_column* n)
67 {
68   Group_interface gi (this, "note-columns");
69   gi.add_element (n);
70   add_dependency (n);
71 }
72
73 /*
74   Copied (urg: literally!) from slur.
75   Why not do this once, at post-processing stage?
76  */
77 void
78 Dynamic_line_spanner::do_add_processing ()
79 {
80   Link_array<Note_column> encompass_arr =
81     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
82
83   if (encompass_arr.size ())
84     {
85       set_bounds (LEFT, encompass_arr[0]);    
86       if (encompass_arr.size () > 1)
87         set_bounds (RIGHT, encompass_arr.top ());
88     }
89 }
90
91 #if 0
92 Molecule
93 Dynamic_line_spanner::do_brew_molecule () const
94 {
95   return Molecule ();
96 }
97 #endif
98
99 void
100 Dynamic_line_spanner::do_post_processing ()
101 {
102   translate_elements (get_extreme_y ());
103 }
104
105 void
106 Dynamic_line_spanner::translate_elements (Real dy)
107 {
108   SCM s = get_elt_property ("elements");
109   for (; gh_pair_p (s); s = gh_cdr (s))
110     {
111       Score_element* se = unsmob_element (gh_car (s));
112       se->translate_axis (dy, Y_AXIS);
113     }
114 }
115
116 Direction
117 Dynamic_line_spanner::get_default_dir () const
118 {
119   return DOWN;
120   //Direction dir = directional_element (this).get ();
121 }
122
123 Real
124 Dynamic_line_spanner::get_extreme_y () const
125 {
126   Link_array<Note_column> encompass_arr =
127     Group_interface__extract_elements (this, (Note_column*)0, "note-columns");
128   
129   Staff_symbol_referencer_interface si (this);
130   int stafflines = si.line_count ();
131   //hurg?
132   stafflines = stafflines != 0 ? stafflines : 5;
133   Direction dir = get_default_dir ();
134
135   Real staff_space = si.staff_space ();
136   // burp?  when are these available?
137   staff_space = staff_space != 0 ? staff_space : 5 PT;
138
139   // urg: TODO: padding
140   Real y = (stafflines / 2 + 1) * staff_space;
141
142   for (int i = 0; i < encompass_arr.size (); i++)
143     {
144       Note_column* column = encompass_arr[i];
145       Stem* stem = column->stem_l ();
146       if (stem)
147         {
148           Direction stem_dir = directional_element (stem).get ();
149           if ((stem_dir == dir)
150               && !stem->extent (Y_AXIS).empty_b ())
151             {
152               y = y >? (stem->extent (Y_AXIS)[dir]) * dir;
153             }
154           else
155             {
156               y = y >? (column->extent (Y_AXIS)[dir]) * dir;
157             }
158         }
159     }
160
161   return y * dir;
162 }
163
164 #endif
165
166 /*
167   TODO:
168     * why handle absolute and span requests in one cryptic engraver,
169     what about Span_dynamic_engraver?
170
171     * hairpin
172     * text:
173       - `cresc. --  --  --'
174       - `cresc. poco a poco -- -- --'
175  */
176
177 /**
178    print text & hairpin dynamics.
179  */
180 class Dynamic_engraver : public Engraver
181 {
182   Text_item * abs_text_p_;
183   Text_item * cr_text_p_;
184   Crescendo * to_end_cresc_p_;
185   Crescendo * cresc_p_;
186
187   Span_req * cresc_req_l_;
188   Array<Request*> dynamic_req_l_arr_;
189
190 #ifdef DYN_LINE
191   /*
192     We probably need two of these: line-up above and below staff
193    */
194   Dynamic_line_spanner* spanner_;
195   Moment last_request_mom_;
196 #endif
197   
198   void  typeset_all ();
199 public:
200   VIRTUAL_COPY_CONS(Translator);
201   Dynamic_engraver();
202   
203 protected:
204
205   void announce_element (Score_element_info);
206   
207   virtual void do_removal_processing ();
208   virtual void acknowledge_element (Score_element_info);
209   virtual bool do_try_music (Music *req_l);
210   virtual void do_process_requests();
211   virtual void do_pre_move_processing();
212   virtual void do_post_move_processing();
213   virtual void typeset_element (Score_element*);
214 };
215
216 ADD_THIS_TRANSLATOR (Dynamic_engraver);
217
218 void
219 Dynamic_engraver::announce_element (Score_element_info i)
220 {
221   group (i.elem_l_, "interfaces").add_thing (ly_symbol2scm ("dynamic"));
222   
223   Engraver::announce_element (i);
224 }
225
226
227 Dynamic_engraver::Dynamic_engraver ()
228 {
229   do_post_move_processing();
230   abs_text_p_ = 0;
231   cr_text_p_ = 0;
232   to_end_cresc_p_ = cresc_p_ = 0;
233   cresc_req_l_ = 0;
234 #ifdef DYN_LINE
235   spanner_ = 0;
236 #endif
237 }
238
239 void
240 Dynamic_engraver::do_post_move_processing()
241 {
242   dynamic_req_l_arr_.clear();
243 }
244
245 /*
246   ugr
247  */
248 bool
249 Dynamic_engraver::do_try_music (Music * m)
250 {
251   Request * r = dynamic_cast<Request*> (m);
252
253   if(Text_script_req * d = dynamic_cast <Text_script_req *> (r))
254     {
255       if (d->style_str_ != "dynamic")
256         return false;
257     }
258   else if (Span_req * s =  dynamic_cast <Span_req*> (r))
259     {
260       if (s-> span_type_str_ != "crescendo"
261           && s->span_type_str_ != "decrescendo")
262         return false;
263     }
264   else
265     return false;
266   
267 #ifdef DYN_LINE
268   if (!spanner_)
269     spanner_ = new Dynamic_line_spanner;
270   last_request_mom_ = now_mom ();
271 #endif
272   
273   for (int i=0; i < dynamic_req_l_arr_.size (); i++)
274     if (r->equal_b (dynamic_req_l_arr_[i]))
275       return true;
276   
277   dynamic_req_l_arr_.push (r);
278   return true;
279 }
280
281
282 void
283 Dynamic_engraver::do_process_requests()
284 {
285   Crescendo*  new_cresc_p=0;
286
287   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
288     {
289       if (Text_script_req *absd =
290           dynamic_cast<Text_script_req *> ( dynamic_req_l_arr_[i]))
291         {
292           if (abs_text_p_)
293             {
294               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused."));
295               continue;
296             }
297           
298           String loud = absd->text_str_;
299
300           abs_text_p_ = new Text_item;
301           abs_text_p_->set_elt_property ("text",
302                                      ly_str02scm (loud.ch_C()));
303           abs_text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
304           abs_text_p_->set_elt_property ("script-priority",
305                                      gh_int2scm (100));
306           
307 #ifdef DYN_LINE
308           assert (spanner_);
309           spanner_->add_element (abs_text_p_);
310 #else
311           Side_position_interface (abs_text_p_).set_axis (Y_AXIS);
312           
313           if (absd->get_direction ())
314             {
315               abs_text_p_->set_elt_property ("direction", gh_int2scm (absd->get_direction ()));
316             }
317
318
319           /*
320             UGH UGH 
321            */
322           SCM prop = get_property ("dynamicDirection");
323           if (!isdir_b (prop))
324             {
325               prop = get_property ("verticalDirection");
326             }
327
328           if (isdir_b (prop) && to_dir (prop))
329             abs_text_p_->set_elt_property ("direction", prop);
330
331           prop = get_property ("dynamicPadding");
332           if (gh_number_p(prop))
333             {
334               abs_text_p_->set_elt_property ("padding", prop);
335             }
336 #endif
337           announce_element (Score_element_info (abs_text_p_, absd));
338
339         }
340       else if (Span_req *span_l
341                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
342         {
343           if (span_l->span_dir_ == STOP)
344             {
345               if (!cresc_p_)
346                 {
347                   span_l->warning (_ ("Can't find (de)crescendo to end"));
348                 }
349               else
350                 {
351                   assert (!to_end_cresc_p_);
352                   to_end_cresc_p_ =cresc_p_;
353                   
354                   cresc_p_ = 0;
355                 }
356             }
357           else if (span_l->span_dir_ == START)
358             {
359               cresc_req_l_ = span_l;
360               assert (!new_cresc_p);
361               new_cresc_p  = new Crescendo;
362               new_cresc_p->set_elt_property
363                 ("grow-direction",
364                  gh_int2scm ((span_l->span_type_str_ == "crescendo")
365                              ? BIGGER : SMALLER));
366               
367               SCM s = get_property (span_l->span_type_str_ + "Text");
368               if (gh_string_p (s))
369                 {
370                   cr_text_p_ = new Text_item;
371                   cr_text_p_->set_elt_property ("text", s);
372                   // urg
373                   cr_text_p_->set_elt_property ("style", gh_str02scm ("italic"));
374                   // ?
375                   cr_text_p_->set_elt_property ("script-priority",
376                                                 gh_int2scm (100));
377                   
378                   /*
379                     This doesn't work.
380                     I'd like to have support like this:
381                            |
382                           x|
383                           cresc. - - -
384
385                     or
386                            |
387                           x|
388                           ff cresc. - - -
389
390                    */
391                   if (0) //abs_text_p_)
392                     {
393                       Side_position_interface (cr_text_p_).set_axis (X_AXIS);
394                       Side_position_interface (cr_text_p_).add_support (abs_text_p_);
395                     }
396
397 #ifdef DYN_LINE
398                   assert (spanner_);
399                   spanner_->add_element (cr_text_p_);
400 #endif
401
402                   //Side_position_interface (cr_text_p_).set_axis (Y_AXIS);
403                   announce_element (Score_element_info (cr_text_p_, span_l));
404                 }
405
406               s = get_property (span_l->span_type_str_ + "Spanner");
407               if (gh_string_p (s)) //&& ly_scm2string (s) != "hairpin")
408                 {
409                   new_cresc_p->set_elt_property ("spanner", s);
410                 }
411           
412 #ifdef DYN_LINE
413               assert (spanner_);
414               spanner_->add_element (new_cresc_p);
415 #else
416               // what's the diff between side_position and Side_pos_iface?
417               side_position (new_cresc_p).set_axis (Y_AXIS);
418 #endif
419               announce_element (Score_element_info (new_cresc_p, span_l));
420             }
421         }
422     }
423
424   if (new_cresc_p)
425     {
426       if (cresc_p_)
427         {
428           ::warning (_ ("Too many crescendi here"));
429
430           typeset_element (cresc_p_);
431
432           cresc_p_ = 0;
433         }
434       
435       cresc_p_ = new_cresc_p;
436       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
437
438       // arrragh, brr, urg: we know how wide text is, no?
439       if (abs_text_p_)
440         {
441           index_set_cell (cresc_p_->get_elt_property ("dynamic-drul"),
442                           LEFT, SCM_BOOL_T);
443           if (to_end_cresc_p_)
444             index_set_cell (to_end_cresc_p_->get_elt_property ("dynamic-drul"),
445                             RIGHT, SCM_BOOL_T);
446         }
447     }
448
449 }
450
451 void
452 Dynamic_engraver::do_pre_move_processing()
453 {
454   typeset_all ();
455 }
456
457 void
458 Dynamic_engraver::do_removal_processing ()
459 {
460   if (cresc_p_)
461     {
462       typeset_element (cresc_p_ );
463       cresc_req_l_->warning (_ ("unended crescendo"));
464       cresc_p_ =0;
465     }
466 #ifdef DYN_LINE
467   if (spanner_)
468     {
469       // URG urg.  We did't get a post_processing call !?
470       spanner_->do_post_processing ();
471       typeset_element (spanner_);
472       spanner_ = 0;
473     }
474 #endif
475   typeset_all ();
476 }
477
478
479 void
480 Dynamic_engraver::typeset_all ()
481 {  
482   if (to_end_cresc_p_)
483     {
484       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
485       typeset_element (to_end_cresc_p_);
486
487       to_end_cresc_p_ =0;
488
489     }
490   
491   if (abs_text_p_)
492     {
493       typeset_element (abs_text_p_);
494       abs_text_p_ = 0;
495     }
496
497   if (cr_text_p_)
498     {
499       typeset_element (cr_text_p_);
500       cr_text_p_ = 0;
501     }
502 #ifdef DYN_LINE
503   /*
504     TODO: This should be optionised:
505       * break when group of dynamic requests ends
506       * break now 
507       * continue through piece
508    */
509   if (spanner_ && last_request_mom_ < now_mom ())
510     {
511       typeset_element (spanner_);
512       spanner_ = 0;
513     }
514 #endif
515 }
516
517 void
518 Dynamic_engraver::typeset_element (Score_element * e)
519 {
520 #ifndef DYN_LINE
521   side_position(e).add_staff_support ();
522 #endif
523   Engraver::typeset_element (e);
524 }
525
526 #ifdef DYN_LINE
527
528 void
529 Dynamic_engraver::acknowledge_element (Score_element_info i)
530 {
531   if (spanner_)
532     {
533       if (Note_column* n = dynamic_cast<Note_column*> (i.elem_l_))
534         spanner_->add_column (n);
535     }
536 }
537
538 #else
539
540 void
541 Dynamic_engraver::acknowledge_element (Score_element_info i)
542 {
543   if (dynamic_cast<Stem *> (i.elem_l_)
544       || dynamic_cast<Note_head *> (i.elem_l_)
545       )
546     {
547       if (abs_text_p_)
548         Side_position_interface (abs_text_p_).add_support (i.elem_l_);
549
550       if (cr_text_p_)  ///&& !abs_text_p_)
551         {
552           Side_position_interface (cr_text_p_).set_axis (Y_AXIS);
553           Side_position_interface (cr_text_p_).add_support (i.elem_l_);
554         }
555
556       if (to_end_cresc_p_)
557         Side_position_interface (to_end_cresc_p_).add_support (i.elem_l_);
558
559       if (cresc_p_)
560         Side_position_interface(cresc_p_).add_support (i.elem_l_);
561     }
562 }
563
564 #endif