]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
patch::: 1.3.30.jcn2
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "debug.hh"
9 #include "crescendo.hh"
10 #include "musical-request.hh"
11 #include "lookup.hh"
12 #include "paper-def.hh"
13 #include "paper-column.hh"
14 #include "staff-symbol.hh"
15 #include "note-column.hh"
16 #include "text-item.hh"
17 #include "side-position-interface.hh"
18 #include "engraver.hh"
19 #include "stem.hh"
20 #include "note-head.hh"
21 #include "group-interface.hh"
22
23 /*
24   TODO:
25     multiple type of span dynamic:
26     * hairpin
27     * text:
28       - `cresc. --  --  --'
29       - `cresc. poco a poco -- -- --'
30  */
31
32 /**
33    print text & hairpin dynamics.
34  */
35 class Dynamic_engraver : public Engraver
36 {
37   Text_item * text_p_;
38   Crescendo * to_end_cresc_p_;
39   Crescendo * cresc_p_;
40
41   Span_req * cresc_req_l_;
42   Array<Request*> dynamic_req_l_arr_;
43   void  typeset_all ();
44 public:
45   VIRTUAL_COPY_CONS(Translator);
46   Dynamic_engraver();
47   
48 protected:
49
50   void announce_element (Score_element_info);
51   
52   virtual void do_removal_processing ();
53   virtual void acknowledge_element (Score_element_info);
54   virtual bool do_try_music (Music *req_l);
55   virtual void do_process_requests();
56   virtual void do_pre_move_processing();
57   virtual void do_post_move_processing();
58   virtual void typeset_element (Score_element*);
59 };
60
61 void
62 Dynamic_engraver::announce_element (Score_element_info i)
63 {
64   group (i.elem_l_, "interfaces").add_thing (ly_symbol2scm ("dynamic"));
65   
66   Engraver::announce_element (i);
67 }
68
69
70 Dynamic_engraver::Dynamic_engraver()
71 {
72   do_post_move_processing();
73   text_p_ =0;
74
75   to_end_cresc_p_ = cresc_p_ = 0;
76
77   cresc_req_l_ = 0;
78 }
79
80 void
81 Dynamic_engraver::do_post_move_processing()
82 {
83   dynamic_req_l_arr_.clear();
84 }
85
86 bool
87 Dynamic_engraver::do_try_music (Music * m)
88 {
89   Request * r = dynamic_cast<Request*> (m);
90
91   if(Text_script_req * d = dynamic_cast <Text_script_req *> (r))
92     {
93       if (d->style_str_ != "dynamic")
94         return false;
95     }
96   else if (Span_req * s =  dynamic_cast <Span_req*> (r))
97     {
98       if (s-> span_type_str_ != "crescendo"
99           && s->span_type_str_ != "decrescendo")
100         return false;
101     }
102   else
103     return false;
104   
105   for (int i=0; i < dynamic_req_l_arr_.size (); i++)
106     if (r->equal_b (dynamic_req_l_arr_[i]))
107       return true;
108   
109   dynamic_req_l_arr_.push (r);
110   return true;
111 }
112
113
114 void
115 Dynamic_engraver::do_process_requests()
116 {
117   Crescendo*  new_cresc_p=0;
118
119   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
120     {
121       if (Text_script_req *absd =
122           dynamic_cast<Text_script_req *> ( dynamic_req_l_arr_[i]))
123         {
124           if (text_p_)
125             {
126               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused."));
127               continue;
128             }
129           
130           String loud = absd->text_str_;
131
132           text_p_ = new Text_item;
133           text_p_->set_elt_property ("text",
134                                      ly_str02scm (loud.ch_C()));
135           text_p_->set_elt_property ("style", gh_str02scm ("dynamic"));
136           text_p_->set_elt_property ("script-priority",
137                                      gh_int2scm (100));
138           
139           Side_position_interface (text_p_).set_axis (Y_AXIS);
140
141           
142           if (absd->get_direction ())
143             {
144               text_p_->set_elt_property ("direction", gh_int2scm (absd->get_direction ()));
145             }
146
147
148           /*
149             UGH UGH 
150            */
151           SCM prop = get_property ("dynamicDirection");
152           if (!isdir_b (prop))
153             {
154               prop = get_property ("verticalDirection");
155             }
156
157           if (isdir_b (prop) && to_dir (prop))
158             text_p_->set_elt_property ("direction", prop);
159
160           prop = get_property ("dynamicPadding");
161           if (gh_number_p(prop))
162             {
163               text_p_->set_elt_property ("padding", prop);
164             }
165           announce_element (Score_element_info (text_p_, absd));
166         }
167       else if (Span_req *span_l
168                = dynamic_cast <Span_req *> (dynamic_req_l_arr_[i]))
169         {
170           if (span_l->span_dir_ == STOP)
171             {
172               if (!cresc_p_)
173                 {
174                   span_l->warning (_ ("Can't find (de)crescendo to end"));
175                 }
176               else
177                 {
178                   assert (!to_end_cresc_p_);
179                   to_end_cresc_p_ =cresc_p_;
180                   
181                   cresc_p_ = 0;
182                 }
183             }
184           else if (span_l->span_dir_ == START)
185             {
186               cresc_req_l_ = span_l;
187               assert (!new_cresc_p);
188               new_cresc_p  = new Crescendo;
189               new_cresc_p
190                 ->set_elt_property ("grow-direction",
191                                     gh_int2scm ((span_l->span_type_str_ == "crescendo") ? BIGGER : SMALLER));
192               
193               side_position (new_cresc_p).set_axis (Y_AXIS);
194               announce_element (Score_element_info (new_cresc_p, span_l));
195             }
196         }
197     }
198
199   if (new_cresc_p)
200     {
201       if (cresc_p_)
202         {
203           ::warning (_ ("Too many crescendi here"));
204
205           typeset_element (cresc_p_);
206
207           cresc_p_ = 0;
208         }
209       
210       cresc_p_ = new_cresc_p;
211       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
212
213       if (text_p_)
214         {
215           index_set_cell (cresc_p_->get_elt_property ("dynamic-drul"),
216                           LEFT, SCM_BOOL_T);
217           if (to_end_cresc_p_)
218             index_set_cell (to_end_cresc_p_->get_elt_property ("dynamic-drul"),
219                             RIGHT, SCM_BOOL_T);
220         }
221     }
222 }
223
224 void
225 Dynamic_engraver::do_pre_move_processing()
226 {
227   typeset_all ();
228 }
229
230
231
232 ADD_THIS_TRANSLATOR(Dynamic_engraver);
233
234 void
235 Dynamic_engraver::do_removal_processing ()
236 {
237   if (cresc_p_)
238     {
239       typeset_element (cresc_p_ );
240       cresc_req_l_->warning (_ ("unended crescendo"));
241       cresc_p_ =0;
242     }
243   typeset_all ();
244 }
245
246
247 void
248 Dynamic_engraver::typeset_all ()
249 {  
250   if (to_end_cresc_p_)
251     {
252       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
253       typeset_element (to_end_cresc_p_);
254
255       to_end_cresc_p_ =0;
256
257     }
258   
259   if (text_p_)
260     {
261       typeset_element (text_p_);
262       text_p_ =0;
263     }
264 }
265
266 void
267 Dynamic_engraver::typeset_element (Score_element * e)
268 {
269   side_position(e).add_staff_support ();
270   Engraver::typeset_element (e);
271 }
272
273 void
274 Dynamic_engraver::acknowledge_element (Score_element_info i)
275 {
276   if (dynamic_cast<Stem *> (i.elem_l_)
277       || dynamic_cast<Note_head *> (i.elem_l_)
278       )
279     {
280       if (text_p_)
281         Side_position_interface (text_p_).add_support (i.elem_l_);
282
283       if (to_end_cresc_p_)
284         Side_position_interface (to_end_cresc_p_).add_support (i.elem_l_);
285
286       if (cresc_p_)
287         Side_position_interface(cresc_p_).add_support (i.elem_l_);
288     }
289 }
290