]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-engraver.cc
release: 1.3.60
[lilypond.git] / lily / local-key-engraver.cc
1 /*
2   local-key-engraver.cc -- implement Local_key_engraver
3
4   (c)  1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
5 */
6
7 #include "musical-request.hh"
8 #include "command-request.hh"
9 #include "local-key-item.hh"
10 #include "key-item.hh"
11 #include "tie.hh"
12 #include "note-head.hh"
13 #include "timing-translator.hh"
14 #include "engraver-group-engraver.hh"
15 #include "grace-align-item.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "side-position-interface.hh"
18 #include "engraver.hh"
19
20
21 /**
22    Make accidentals.  Catches note heads, ties and notices key-change
23    events.  Due to interaction with ties (which don't come together
24    with note heads), this needs to be in a context higher than Tie_engraver.
25    (FIXME).
26  */
27 struct Local_key_engraver : Engraver {
28   Local_key_item *key_item_p_;
29 protected:
30   VIRTUAL_COPY_CONS(Translator);
31   virtual void do_process_music();
32   virtual void acknowledge_element (Score_element_info);
33   virtual void do_pre_move_processing();
34   virtual void do_creation_processing ();
35   virtual void process_acknowledged ();
36   virtual void do_removal_processing ();
37 public:
38
39   // todo -> property
40   SCM last_keysig_;
41   Key_engraver *key_grav_l_;
42   
43   Link_array<Note_req> mel_l_arr_;
44   Link_array<Item> support_l_arr_;
45   Link_array<Item> forced_l_arr_;
46   Link_array<Item> tied_l_arr_;
47   Local_key_engraver();
48
49   Grace_align_item * grace_align_l_;
50 };
51
52 Local_key_engraver::Local_key_engraver()
53 {
54   key_grav_l_ = 0;
55   key_item_p_ =0;
56   grace_align_l_ =0;
57   last_keysig_ = SCM_EOL;
58 }
59
60 void
61 Local_key_engraver::do_creation_processing ()
62 {
63   last_keysig_ = get_property ("keySignature");
64   daddy_trans_l_->set_property ("localKeySignature",  last_keysig_);  
65 }
66
67 void
68 Local_key_engraver::process_acknowledged ()
69 {
70   SCM localsig = get_property ("localKeySignature");
71   
72   if (!key_item_p_ && mel_l_arr_.size()) 
73     {
74       SCM f = get_property ("forgetAccidentals");
75       bool forget = to_boolean (f);
76       for (int i=0; i  < mel_l_arr_.size(); i++) 
77         {
78           Item * support_l = support_l_arr_[i];
79           Note_req * note_l = mel_l_arr_[i];
80
81           int n = note_l->pitch_.notename_i_;
82           int o = note_l->pitch_.octave_i_;
83           int a = note_l->pitch_.accidental_i_;
84           
85           /* see if there's a tie that "changes" the accidental */
86           /* works because if there's a tie, the note to the left
87              is of the same pitch as the actual note */
88
89           SCM prev = scm_assoc (gh_cons (gh_int2scm (o), gh_int2scm (n)), localsig);
90           if (prev == SCM_BOOL_F)
91             prev = scm_assoc (gh_int2scm (n), localsig);
92           int prev_acc = (prev == SCM_BOOL_F) ? 0 : gh_scm2int (gh_cdr (prev));
93           bool different = prev_acc != a;
94           
95           bool tie_changes = tied_l_arr_.find_l (support_l) && different;
96           if (!forget
97               && (note_l->forceacc_b_ || different)
98               && !tie_changes)
99             {
100               if (!key_item_p_) 
101                 {
102                   key_item_p_ = new Local_key_item (get_property ("basicLocalKeyProperties"));
103                   Side_position_interface (key_item_p_).set_axis (X_AXIS);
104                   Side_position_interface (key_item_p_).set_direction (LEFT);
105                   staff_symbol_referencer(key_item_p_).set_interface ();
106                          
107                   announce_element (Score_element_info (key_item_p_, 0));
108                 }
109
110               
111               bool extra_natural =
112                 sign (prev_acc) * (prev_acc - a) == 1
113                 && abs(prev_acc) == 2;
114
115               key_item_p_->add_pitch (note_l->pitch_,
116                                       note_l->cautionary_b_,
117                                       extra_natural);
118               Side_position_interface (key_item_p_).add_support (support_l);
119             }
120           
121           if (!forget)
122             {
123               localsig = scm_assoc_set_x (localsig, gh_cons (gh_int2scm (o),
124                                                              gh_int2scm (n)),
125                                           gh_int2scm (a)); 
126
127 #if 0
128               /*
129                 TESTME!
130                */
131               if (!tied_l_arr_.find_l (support_l))
132                 {
133                   local_key_.clear_internal_forceacc (note_l->pitch_);
134                 }
135               else if (tie_changes)
136                 {
137                   local_key_.set_internal_forceacc (note_l->pitch_);
138                 }
139 #endif
140             }
141         }
142     }
143
144   /*
145     UGH ! 
146    */
147   
148   if (key_item_p_ && grace_align_l_)
149     {
150       Side_position_interface (grace_align_l_).add_support (key_item_p_);
151       grace_align_l_ =0;
152     }
153   
154 }
155
156 void
157 Local_key_engraver::do_removal_processing ()
158 {
159   // TODO: if grace ? signal accidentals to Local_key_engraver the 
160 }
161
162 void
163 Local_key_engraver::do_pre_move_processing()
164 {
165   if (key_item_p_)
166     {
167       for (int i=0; i < support_l_arr_.size(); i++)
168         Side_position_interface (key_item_p_).add_support (support_l_arr_[i]);
169
170       typeset_element (key_item_p_);
171       key_item_p_ =0;
172     }
173
174   grace_align_l_ = 0;
175   mel_l_arr_.clear();
176   tied_l_arr_.clear();
177   support_l_arr_.clear();
178   forced_l_arr_.clear();        
179 }
180
181 void
182 Local_key_engraver::acknowledge_element (Score_element_info info)
183 {
184   SCM wg= get_property ("weAreGraceContext");
185   
186   bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
187   bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
188
189   Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);  
190   if (he_gr && !selfgr && gai)
191     {
192       grace_align_l_ = gai;
193     }
194   Note_req * note_l =  dynamic_cast <Note_req *> (info.req_l_);
195   Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
196
197   if (he_gr != selfgr)
198     return;
199   
200   if (note_l && note_head)
201     {
202       mel_l_arr_.push (note_l);
203       support_l_arr_.push (note_head);
204     }
205   else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
206     {
207       tied_l_arr_.push (tie_l->head (RIGHT));
208     }
209 }
210
211 void
212 Local_key_engraver::do_process_music()
213 {
214   SCM smp = get_property ("measurePosition");
215   Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
216
217   SCM sig = get_property ("keySignature");
218   if (!mp)
219     {
220       if (!to_boolean (get_property ("noResetKey")))
221         daddy_trans_l_->set_property ("localKeySignature",  sig);
222     }
223   else if (last_keysig_ != sig) 
224     {
225       daddy_trans_l_->set_property ("localKeySignature",  sig);
226     }
227 }
228
229
230
231 ADD_THIS_TRANSLATOR(Local_key_engraver);
232