]> git.donarmstrong.com Git - lilypond.git/blob - lily/local-key-engraver.cc
release: 1.3.64
[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   
42   Link_array<Note_req> mel_l_arr_;
43   Link_array<Item> support_l_arr_;
44   Link_array<Item> forced_l_arr_;
45   Link_array<Item> tied_l_arr_;
46   Local_key_engraver();
47
48   Grace_align_item * grace_align_l_;
49 };
50
51 Local_key_engraver::Local_key_engraver()
52 {
53   key_item_p_ =0;
54   grace_align_l_ =0;
55   last_keysig_ = SCM_EOL;
56 }
57
58 void
59 Local_key_engraver::do_creation_processing ()
60 {
61   last_keysig_ = get_property ("keySignature");
62   daddy_trans_l_->set_property ("localKeySignature",  last_keysig_);  
63 }
64
65 void
66 Local_key_engraver::process_acknowledged ()
67 {
68   SCM localsig = get_property ("localKeySignature");
69   
70   if (!key_item_p_ && mel_l_arr_.size()) 
71     {
72       SCM f = get_property ("forgetAccidentals");
73       bool forget = to_boolean (f);
74       for (int i=0; i  < mel_l_arr_.size(); i++) 
75         {
76           Item * support_l = support_l_arr_[i];
77           Note_req * note_l = mel_l_arr_[i];
78
79           int n = note_l->pitch_.notename_i_;
80           int o = note_l->pitch_.octave_i_;
81           int a = note_l->pitch_.accidental_i_;
82           
83           /* see if there's a tie that "changes" the accidental */
84           /* works because if there's a tie, the note to the left
85              is of the same pitch as the actual note */
86
87           SCM prev = scm_assoc (gh_cons (gh_int2scm (o), gh_int2scm (n)), localsig);
88           if (prev == SCM_BOOL_F)
89             prev = scm_assoc (gh_int2scm (n), localsig);
90           int prev_acc = (prev == SCM_BOOL_F) ? 0 : gh_scm2int (gh_cdr (prev));
91           bool different = prev_acc != a;
92           
93           bool tie_changes = tied_l_arr_.find_l (support_l) && different;
94           if (!forget
95               && (note_l->forceacc_b_ || different)
96               && !tie_changes)
97             {
98               if (!key_item_p_) 
99                 {
100                   key_item_p_ = new Local_key_item (get_property ("basicLocalKeyProperties"));
101                   Side_position_interface (key_item_p_).set_axis (X_AXIS);
102                   Side_position_interface (key_item_p_).set_direction (LEFT);
103                   staff_symbol_referencer(key_item_p_).set_interface ();
104                          
105                   announce_element (Score_element_info (key_item_p_, 0));
106                 }
107
108               
109               bool extra_natural =
110                 sign (prev_acc) * (prev_acc - a) == 1
111                 && abs(prev_acc) == 2;
112
113               key_item_p_->add_pitch (note_l->pitch_,
114                                       note_l->cautionary_b_,
115                                       extra_natural);
116               Side_position_interface (key_item_p_).add_support (support_l);
117             }
118           
119           if (!forget)
120             {
121               localsig = scm_assoc_set_x (localsig, gh_cons (gh_int2scm (o),
122                                                              gh_int2scm (n)),
123                                           gh_int2scm (a)); 
124
125 #if 0
126               /*
127                 TESTME!
128                */
129               if (!tied_l_arr_.find_l (support_l))
130                 {
131                   local_key_.clear_internal_forceacc (note_l->pitch_);
132                 }
133               else if (tie_changes)
134                 {
135                   local_key_.set_internal_forceacc (note_l->pitch_);
136                 }
137 #endif
138             }
139         }
140     }
141
142   /*
143     UGH ! 
144    */
145   
146   if (key_item_p_ && grace_align_l_)
147     {
148       Side_position_interface (grace_align_l_).add_support (key_item_p_);
149       grace_align_l_ =0;
150     }
151   
152 }
153
154 void
155 Local_key_engraver::do_removal_processing ()
156 {
157   // TODO: if grace ? signal accidentals to Local_key_engraver the 
158 }
159
160 void
161 Local_key_engraver::do_pre_move_processing()
162 {
163   if (key_item_p_)
164     {
165       for (int i=0; i < support_l_arr_.size(); i++)
166         Side_position_interface (key_item_p_).add_support (support_l_arr_[i]);
167
168       typeset_element (key_item_p_);
169       key_item_p_ =0;
170     }
171
172   grace_align_l_ = 0;
173   mel_l_arr_.clear();
174   tied_l_arr_.clear();
175   support_l_arr_.clear();
176   forced_l_arr_.clear();        
177 }
178
179 void
180 Local_key_engraver::acknowledge_element (Score_element_info info)
181 {
182   SCM wg= get_property ("weAreGraceContext");
183   
184   bool selfgr = gh_boolean_p (wg) &&gh_scm2bool (wg);
185   bool he_gr = to_boolean (info.elem_l_->get_elt_property ("grace"));
186
187   Grace_align_item * gai = dynamic_cast<Grace_align_item*> (info.elem_l_);  
188   if (he_gr && !selfgr && gai)
189     {
190       grace_align_l_ = gai;
191     }
192   Note_req * note_l =  dynamic_cast <Note_req *> (info.req_l_);
193   Note_head * note_head = dynamic_cast<Note_head *> (info.elem_l_);
194
195   if (he_gr != selfgr)
196     return;
197   
198   if (note_l && note_head)
199     {
200       mel_l_arr_.push (note_l);
201       support_l_arr_.push (note_head);
202     }
203   else if (Tie * tie_l = dynamic_cast<Tie *> (info.elem_l_))
204     {
205       tied_l_arr_.push (tie_l->head (RIGHT));
206     }
207 }
208
209 /*
210   ugh. deep_copy uses lots of space.
211  */
212 void
213 Local_key_engraver::do_process_music()
214 {
215   SCM smp = get_property ("measurePosition");
216   Moment mp =  (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
217
218   SCM sig = get_property ("keySignature");
219   if (!mp)
220     {
221       if (!to_boolean (get_property ("noResetKey")))
222         daddy_trans_l_->set_property ("localKeySignature",  ly_deep_copy (sig));
223     }
224   else if (last_keysig_ != sig) 
225     {
226       daddy_trans_l_->set_property ("localKeySignature",  ly_deep_copy (sig));
227     }
228 }
229
230
231
232 ADD_THIS_TRANSLATOR(Local_key_engraver);
233