]> git.donarmstrong.com Git - lilypond.git/blob - lily/dynamic-engraver.cc
release: 1.1.35
[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_dynamic_req * cresc_req_l_;
34   Array<Dynamic_req*> 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 * r)
68 {
69   if(Dynamic_req * d = dynamic_cast <Dynamic_req *> (r))
70     {
71       for (int i=0; i < dynamic_req_l_arr_.size (); i++)
72         if (d->equal_b (dynamic_req_l_arr_[i]))
73           return true;
74       
75       dynamic_req_l_arr_.push (d);
76       return true;
77     }
78   return false;
79 }
80 void
81 Dynamic_engraver::do_process_requests()
82 {
83   Crescendo*  new_cresc_p=0;
84   for (int i=0; i < dynamic_req_l_arr_.size(); i++)
85     {
86       Dynamic_req *dreq_l = dynamic_req_l_arr_[i];
87       if (Absolute_dynamic_req *absd = dynamic_cast<Absolute_dynamic_req *> (dreq_l))
88         {
89           if (text_p_)
90             {
91               dynamic_req_l_arr_[i]->warning (_("Got a dynamic already.  Continuing dazed and confused"));
92               continue;
93             }
94           
95           String loud = absd->loudness_str_;
96
97           text_p_ = new G_text_item;
98           text_p_->text_str_ =  loud; // ugh
99           Scalar prop = get_property ("dynamicStyle", 0);
100
101           text_p_->style_str_ = prop.length_i () ? prop :  "dynamic";
102
103           staff_side_p_ = new G_staff_side_item;
104           staff_side_p_->set_victim (text_p_);
105           staff_side_p_->axis_ = Y_AXIS;
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
119                = dynamic_cast <Span_dynamic_req *> (dreq_l))
120         {
121           if (span_l->spantype_ == STOP)
122             {
123               if (!cresc_p_)
124                 {
125                   span_l->warning (_ ("can't find (de)crescendo to end"));
126                 }
127               else
128                 {
129                   assert (!to_end_cresc_p_);
130                   to_end_cresc_p_ =cresc_p_;
131                   cresc_p_ = 0;
132                   Scalar prop = get_property ("dynamicDir", 0);
133                   if (prop.isnum_b ())
134                     {
135                       to_end_cresc_p_->dir_ = (Direction) (int) prop;
136                     }
137                   
138                 }
139             }
140           else if (span_l->spantype_ == START)
141             {
142               cresc_req_l_ = span_l;
143               assert (!new_cresc_p);
144               new_cresc_p  = new Crescendo;
145               new_cresc_p->grow_dir_ = span_l->dynamic_dir_;
146               announce_element (Score_element_info (new_cresc_p, span_l));
147             }
148         }
149     }
150
151   if (new_cresc_p)
152     {
153       if (cresc_p_)
154         {
155           ::warning (_ ("Too many crescendi here"));
156           typeset_element (cresc_p_);
157           cresc_p_ = 0;
158         }
159       
160       cresc_p_ = new_cresc_p;
161       cresc_p_->set_bounds(LEFT,get_staff_info().musical_pcol_l ());
162       if (text_p_)
163         {
164           cresc_p_->dyn_b_drul_[LEFT] = true;
165           if (to_end_cresc_p_)
166             to_end_cresc_p_->dyn_b_drul_[RIGHT] = true;
167         }
168     }
169 }
170
171 void
172 Dynamic_engraver::do_pre_move_processing()
173 {
174   typeset_all ();
175 }
176
177
178
179 ADD_THIS_TRANSLATOR(Dynamic_engraver);
180
181 void
182 Dynamic_engraver::do_removal_processing ()
183 {
184   if (cresc_p_)
185     {
186       typeset_element (cresc_p_ );
187       cresc_req_l_->warning (_ ("unended crescendo"));
188       cresc_p_ =0;
189     }
190   typeset_all ();
191 }
192
193
194 void
195 Dynamic_engraver::typeset_all ()
196 {  
197   if (to_end_cresc_p_)
198     {
199       to_end_cresc_p_->set_bounds(RIGHT,get_staff_info().musical_pcol_l ());
200       typeset_element (to_end_cresc_p_);
201       to_end_cresc_p_ =0;
202     }
203   if (text_p_)
204     {
205       typeset_element (text_p_);
206       typeset_element (staff_side_p_);
207       text_p_ =0;
208       staff_side_p_ =0;
209     }
210 }
211
212
213 void
214 Dynamic_engraver::acknowledge_element (Score_element_info i)
215 {
216   if (dynamic_cast<Stem *> (i.elem_l_)
217       || dynamic_cast<Note_head *> (i.elem_l_)
218       )
219     {
220       if (staff_side_p_)
221         staff_side_p_->add_support (i.elem_l_);
222
223       if (to_end_cresc_p_)
224         to_end_cresc_p_->add_support (i.elem_l_);
225
226       if (cresc_p_)
227         cresc_p_->add_support (i.elem_l_);
228     }
229 }