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