]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.1.43
[lilypond.git] / lily / dynamic-engraver.cc
1 /*
2   dynamic-reg.cc -- implement Dynamic_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c)  1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
9 #include "crescendo.hh"
10 #include "dynamic-engraver.hh"
11 #include "musical-request.hh"
12 #include "lookup.hh"
13 #include "paper-def.hh"
14 #include "score-column.hh"
15 #include "staff-symbol.hh"
16 #include "note-column.hh"
17 #include "g-text-item.hh"
18 #include "g-staff-side.hh"
19 #include "engraver.hh"
20 #include "stem.hh"
21 #include "note-head.hh"
22
23 /**
24    print text & hairpin dynamics.
25  */
26 class Dynamic_engraver : public Engraver
27 {
28   G_text_item * text_p_;
29   G_staff_side_item * staff_side_p_;
30   
31   Crescendo * to_end_cresc_p_;
32   Crescendo * cresc_p_;
33   Span_req * cresc_req_l_;
34   Array<Request*> dynamic_req_l_arr_;
35   void  typeset_all ();
36 public:
37   VIRTUAL_COPY_CONS(Translator);
38   Dynamic_engraver();
39   
40 protected:
41   virtual void do_removal_processing ();
42   virtual void acknowledge_element (Score_element_info);
43   virtual bool do_try_music (Music *req_l);
44   virtual void do_process_requests();
45   virtual void do_pre_move_processing();
46   virtual void do_post_move_processing();
47 };
48
49
50
51 Dynamic_engraver::Dynamic_engraver()
52 {
53   do_post_move_processing();
54   text_p_ =0;
55   staff_side_p_ =0;
56   to_end_cresc_p_ = cresc_p_ = 0;
57   cresc_req_l_ = 0;
58 }
59
60 void
61 Dynamic_engraver::do_post_move_processing()
62 {
63   dynamic_req_l_arr_.clear();
64 }
65
66 bool
67 Dynamic_engraver::do_try_music (Music * m)
68 {
69   Request * r = dynamic_cast<Request*> (m);
70
71   if(Text_script_req * d = dynamic_cast <Text_script_req *> (r))
72     {
73       if (d->style_str_ != "dynamic")
74         return false;
75     }
76   else if (Span_req * s =  dynamic_cast <Span_req*> (r))
77     {
78       if (s-> span_type_str_ != "crescendo"
79           && s->span_type_str_ != "decrescendo")
80         return false;
81     }
82   else
83     return false;
84   
85   for (int i=0; i < dynamic_req_l_arr_.size (); i++)
86     if (r->equal_b (dynamic_req_l_arr_[i]))
87       return true;
88   
89   dynamic_req_l_arr_.push (r);
90   return true;
91 }
92
93
94 void
95 Dynamic_engraver::do_process_requests()
96 {
97   Crescendo*  new_cresc_p=0;
98   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
99     {
100       if (Text_script_req *absd =
101           dynamic_cast<Text_script_req *> ( dynamic_req_l_arr_[i]))
102         {
103           if (text_p_)
104             {
105               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused"));
106               continue;
107             }
108           
109           String loud = absd->text_str_;
110
111           text_p_ = new G_text_item;
112           text_p_->text_str_ =  loud; // ugh
113           Scalar prop = get_property ("dynamicStyle", 0);
114
115           text_p_->style_str_ = prop.length_i () ? prop :  "dynamic";
116
117           staff_side_p_ = new G_staff_side_item;
118           staff_side_p_->set_elt_property (script_priority_scm_sym,
119                                            gh_int2scm (100));
120                                            
121           staff_side_p_->set_victim (text_p_);
122           staff_side_p_->axis_ = Y_AXIS;
123           
124
125           prop = get_property ("dynamicDir", 0);
126           if (prop.isnum_b ())
127             {
128               staff_side_p_->dir_ = (Direction) (int) prop;
129             }
130
131
132           announce_element (Score_element_info (text_p_, absd));
133           announce_element (Score_element_info (staff_side_p_, absd));
134         }
135       else if (Span_req *span_l
136                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
137         {
138           if (span_l->span_dir_ == STOP)
139             {
140               if (!cresc_p_)
141                 {
142                   span_l->warning (_ ("can't find (de)crescendo to end"));
143                 }
144               else
145                 {
146                   assert (!to_end_cresc_p_);
147                   to_end_cresc_p_ =cresc_p_;
148                   cresc_p_ = 0;
149                   Scalar prop = get_property ("dynamicDir", 0);
150                   if (prop.isnum_b ())
151                     {
152                       to_end_cresc_p_->dir_ = (Direction) (int) prop;
153                     }
154                   
155                 }
156             }
157           else if (span_l->span_dir_ == START)
158             {
159               cresc_req_l_ = span_l;
160               assert (!new_cresc_p);
161               new_cresc_p  = new Crescendo;
162               new_cresc_p->grow_dir_ = (span_l->span_type_str_ == "crescendo")  ? BIGGER : SMALLER;
163               announce_element (Score_element_info (new_cresc_p, span_l));
164             }
165         }
166     }
167
168   if (new_cresc_p)
169     {
170       if (cresc_p_)
171         {
172           ::warning (_ ("Too many crescendi here"));
173           typeset_element (cresc_p_);
174           cresc_p_ = 0;
175         }
176       
177       cresc_p_ = new_cresc_p;
178       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
179       if (text_p_)
180         {
181           cresc_p_->dyn_b_drul_[LEFT] = true;
182           if (to_end_cresc_p_)
183             to_end_cresc_p_->dyn_b_drul_[RIGHT] = true;
184         }
185     }
186 }
187
188 void
189 Dynamic_engraver::do_pre_move_processing()
190 {
191   typeset_all ();
192 }
193
194
195
196 ADD_THIS_TRANSLATOR(Dynamic_engraver);
197
198 void
199 Dynamic_engraver::do_removal_processing ()
200 {
201   if (cresc_p_)
202     {
203       typeset_element (cresc_p_ );
204       cresc_req_l_->warning (_ ("unended crescendo"));
205       cresc_p_ =0;
206     }
207   typeset_all ();
208 }
209
210
211 void
212 Dynamic_engraver::typeset_all ()
213 {  
214   if (to_end_cresc_p_)
215     {
216       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
217       typeset_element (to_end_cresc_p_);
218       to_end_cresc_p_ =0;
219     }
220   if (text_p_)
221     {
222       typeset_element (text_p_);
223       typeset_element (staff_side_p_);
224       text_p_ =0;
225       staff_side_p_ =0;
226     }
227 }
228
229
230 void
231 Dynamic_engraver::acknowledge_element (Score_element_info i)
232 {
233   if (dynamic_cast<Stem *> (i.elem_l_)
234       || dynamic_cast<Note_head *> (i.elem_l_)
235       )
236     {
237       if (staff_side_p_)
238         staff_side_p_->add_support (i.elem_l_);
239
240       if (to_end_cresc_p_)
241         to_end_cresc_p_->add_support (i.elem_l_);
242
243       if (cresc_p_)
244         cresc_p_->add_support (i.elem_l_);
245     }
246 }