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