]> git.donarmstrong.com Git - lilypond.git/blob - lily/accidental-engraver.cc
*** empty log message ***
[lilypond.git] / lily / accidental-engraver.cc
1 /*
2   accidental-engraver.cc -- implement accidental_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
8 */
9
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
12 #include "context.hh"
13 #include "engraver.hh"
14 #include "event.hh"
15 #include "item.hh"
16 #include "protected-scm.hh"
17 #include "rhythmic-head.hh"
18 #include "side-position-interface.hh"
19 #include "spanner.hh"
20 #include "tie.hh"
21 #include "warn.hh"
22
23
24 class Accidental_entry
25 {
26 public:
27   bool done_;
28   Music *melodic_;
29   Grob *accidental_;
30   Context *origin_;
31   Engraver *origin_trans_;
32   Grob *head_;
33   bool tied_;
34
35   Accidental_entry ();
36 };
37
38 Accidental_entry::Accidental_entry ()
39 {
40   tied_ = false;
41   done_ = false;
42   melodic_ = 0;
43   accidental_ = 0;
44   origin_ = 0;
45   head_ = 0;
46 }
47
48 class Accidental_engraver : public Engraver
49 {
50 public:
51   int get_bar_number ();
52   void update_local_key_signature ();
53
54 protected:
55   TRANSLATOR_DECLARATIONS (Accidental_engraver);
56   virtual void process_music ();
57   virtual void acknowledge_grob (Grob_info);
58   virtual void stop_translation_timestep ();
59   virtual void initialize ();
60   virtual void process_acknowledged_grobs ();
61   virtual void finalize ();
62
63 public:
64   Protected_scm last_keysig_;   // ugh.
65   
66   /* Urgh. Since the accidentals depend on lots of variables, we have
67     to store all information before we can really create the
68     accidentals.  */
69   Link_array<Grob> left_objects_;
70   Link_array<Grob> right_objects_;
71
72   Grob *accidental_placement_;
73
74   Array<Accidental_entry> accidentals_;
75   Link_array<Spanner> ties_;
76 };
77
78
79 /*
80   TODO:
81
82   ugh, it is not clear what properties are mutable and which
83   aren't. eg. localKeySignature is changed at runtime, which means
84   that references in grobs should always store ly_deep_copy ()s of
85   those.
86  */
87
88 static void
89 set_property_on_children (Context *trans, char const *sym, SCM val)
90 {
91   trans->set_property (sym, ly_deep_copy (val));
92   for (SCM p = trans->children_contexts (); ly_c_pair_p (p); p = ly_cdr (p))
93     {
94       Context *trg = unsmob_context (ly_car (p));
95       set_property_on_children (trg, sym, ly_deep_copy (val));
96     }
97 }
98
99 Accidental_engraver::Accidental_engraver ()
100 {
101   accidental_placement_ = 0;
102   last_keysig_ = SCM_EOL;
103 }
104
105 void
106 Accidental_engraver::update_local_key_signature ()
107 {
108   last_keysig_ = get_property ("keySignature");
109   set_property_on_children (context (), "localKeySignature", last_keysig_);
110
111   Context *trans = context ()->get_parent_context ();
112
113   /* Huh. Don't understand what this is good for. --hwn.  */
114   while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature")))
115     {
116       trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
117       trans = trans->get_parent_context ();
118     }
119 }
120
121 void
122 Accidental_engraver::initialize ()
123 {
124   update_local_key_signature (); 
125 }
126
127
128 /** Calculate the number of accidentals on basis of the current local key
129     sig (passed as argument)
130     
131     * First check step+octave (taking into account barnumbers if necessary).
132    
133     * Then check the global signature (only step).
134   
135     Return number of accidentals (0, 1 or 2).  */
136 static int
137 number_accidentals_from_sig (bool *different, SCM sig, Pitch *pitch,
138                              int curbarnum, SCM laziness, bool ignore_octave)
139 {
140   int n = pitch->get_notename ();
141   int o = pitch->get_octave ();
142   int a = pitch->get_alteration ();
143
144   SCM prev_alt = SCM_BOOL_F;
145
146   if (!ignore_octave)
147     {
148       SCM prev_local
149         = scm_assoc (scm_cons (scm_int2num (o), scm_int2num (n)), sig);
150
151       if (ly_c_pair_p (prev_local))
152         {
153           if (ly_c_pair_p (ly_cdr (prev_local))
154               && ly_c_number_p (laziness))
155             {
156               int barnum = scm_to_int (ly_cddr (prev_local));
157
158               prev_local = scm_cons (ly_car (prev_local), ly_cadr (prev_local));
159               if (curbarnum <= barnum + scm_to_int (laziness))
160                 prev_alt = prev_local;
161             }
162         }
163     }
164
165   if (prev_alt == SCM_BOOL_F)
166     prev_alt = scm_assoc (scm_int2num (n), sig);
167
168   prev_alt =  (prev_alt == SCM_BOOL_F) ? scm_int2num (0) : ly_cdr (prev_alt); 
169     
170   /* UGH. prev_acc can be #t in case of ties. What is this for?  */
171   int p = ly_c_number_p (prev_alt) ? scm_to_int (prev_alt) : 0;
172
173   int num;
174   if (a == p && ly_c_number_p (prev_alt))
175     num = 0;
176   else if ( (abs (a)<abs (p) || p*a<0) && a != 0 )
177     num = 2;
178   else
179     num = 1;
180
181   *different = (a != p);
182   return num;
183 }
184
185 static int
186 number_accidentals (bool *different,
187                     Pitch *pitch, Context *origin,
188                     SCM accidentals, int curbarnum)
189 {
190   int number = 0;
191
192   *different = false;
193   if (ly_c_pair_p (accidentals) && !ly_c_symbol_p (ly_car (accidentals)))
194     warning (_f ("Accidental typesetting list must begin with context-name: %s", 
195                  ly_scm2string (ly_car (accidentals)).to_str0 ()));
196   
197   for (; ly_c_pair_p (accidentals) && origin;
198        accidentals = ly_cdr (accidentals))
199     {
200       // If pair then it is a new accidentals typesetting rule to be checked
201       SCM rule = ly_car (accidentals);
202       if (ly_c_pair_p (rule))
203         {
204           SCM type = ly_car (rule);
205           SCM laziness = ly_cdr (rule);
206           SCM localsig = origin->get_property ("localKeySignature");
207           
208           bool same_octave_b = 
209             ly_c_eq_p (ly_symbol2scm ("same-octave"), type);
210           bool any_octave_b = 
211             ly_c_eq_p (ly_symbol2scm ("any-octave"), type);
212
213           if (same_octave_b || any_octave_b)
214             {
215               bool d = false;
216               int n = number_accidentals_from_sig
217                 (&d, localsig, pitch, curbarnum, laziness, any_octave_b);
218               *different = *different || d;
219               number = max (number, n);     
220             }
221           else
222             warning (_f ("ignoring unknown accidental: %s", 
223                          ly_symbol2string (type).to_str0 ()));
224         }
225       
226
227       /*
228         if symbol then it is a context name. Scan parent contexts to find it.
229       */
230       else if (ly_c_symbol_p (rule))
231         {
232           Context *dad = origin;
233           while (dad && !dad->is_alias (rule))
234             dad = dad->get_parent_context ();
235       
236           if (dad)
237             origin = dad;
238         }
239       else warning (_f ("Accidental rule must be pair or context-name; Found %s", 
240                         ly_scm2string (rule).to_str0 ()));
241     }
242
243   return number;
244 }
245
246 int
247 Accidental_engraver::get_bar_number ()
248 {
249   SCM barnum = get_property ("currentBarNumber");
250   SCM smp = get_property ("measurePosition");
251
252   int bn = robust_scm2int (barnum, 0);
253   
254   Moment mp = (unsmob_moment (smp)) ? *unsmob_moment (smp) : Moment (0);
255   if (mp.main_part_ < Rational (0))
256     bn--;
257   
258   return bn;
259 }
260
261 void
262 Accidental_engraver::process_acknowledged_grobs ()
263 {
264   if (accidentals_.size () && !accidentals_.top ().done_)
265     {
266       SCM accidentals =  get_property ("autoAccidentals");
267       SCM cautionaries =  get_property ("autoCautionaries");
268       int barnum = get_bar_number ();
269       
270       bool extra_natural_b = get_property ("extraNatural") == SCM_BOOL_T;
271       for (int i = 0; i < accidentals_.size (); i++) 
272         {
273           if (accidentals_[i].done_ )
274             continue;
275           accidentals_[i].done_  = true;
276           Grob *support = accidentals_[i].head_;
277           Music *note = accidentals_[i].melodic_;
278           Context *origin = accidentals_[i].origin_;
279
280           Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
281           if (!pitch)
282             continue;
283
284           bool different = false;
285           bool different_caut = false;
286           
287           int num = number_accidentals (&different,
288                                         pitch, origin,
289                                         accidentals, barnum);
290           int num_caut = number_accidentals (&different_caut,
291                                              pitch, origin,
292                                              cautionaries, barnum);
293
294           bool cautionary = to_boolean (note->get_property ("cautionary"));
295           
296           if (num_caut > num)
297             {
298               num = num_caut;
299               different = different_caut;
300               cautionary = true;
301             }
302
303           if (num == 0 && to_boolean (note->get_property ("force-accidental")))
304             num = 1;
305           
306
307           /*
308             Can not look for ties: it's not guaranteed that they reach
309             us before the notes
310            */
311         
312           if (num)
313             {
314               /*
315                 We construct the accidentals at the originating Voice
316                 level, so that we get the property settings for
317                 Accidental from the respective Voice.
318                */
319               Grob *a
320                 = make_item_from_properties (accidentals_[i].origin_trans_,
321                                              ly_symbol2scm ("Accidental"),
322                                              note->self_scm ());
323               a->set_parent (support, Y_AXIS);
324
325               if (!accidental_placement_)
326                 accidental_placement_ = make_item ("AccidentalPlacement",
327                                                    a->self_scm ());
328               Accidental_placement::add_accidental (accidental_placement_, a);
329               SCM accs = scm_cons (scm_int2num (pitch->get_alteration ()),
330                                    SCM_EOL);
331               if (num == 2 && extra_natural_b)
332                 accs = scm_cons (scm_int2num (0), accs);
333
334               /* TODO: add cautionary option in accidental. */
335
336               if (cautionary)
337                 a->set_property ("cautionary", SCM_BOOL_T);
338               
339               support->set_property ("accidental-grob", a->self_scm ());
340
341               a->set_property ("accidentals", accs);
342               accidentals_[i].accidental_ = a;
343
344               /*
345                 We add the accidentals to the support of the arpeggio,
346                 so it is put left of the accidentals.
347               */
348               for (int i = 0;  i < left_objects_.size ();  i++)
349                 Side_position_interface::add_support (left_objects_[i], a);
350               for (int i = 0;  i < right_objects_.size ();  i++)
351                 Side_position_interface::add_support (a, right_objects_[i]);
352             }
353         }
354     }
355 }
356
357 void
358 Accidental_engraver::finalize ()
359 {
360   last_keysig_ = SCM_EOL;
361 }
362
363 void
364 Accidental_engraver::stop_translation_timestep ()
365 {
366   for (int j = ties_.size (); j--;)
367     {
368       Grob *r = Tie::head (ties_[j], RIGHT);
369       for (int i = accidentals_.size ();  i--;)
370         if (accidentals_[i].head_ == r)
371           {
372             if (Grob *g = accidentals_[i].accidental_)
373               {
374                 g->set_property ("tie", ties_[j]->self_scm ());
375                 accidentals_[i].tied_ = true;
376               }
377             ties_.del (j);
378             break;
379           }
380     }
381
382   for (int i = accidentals_.size (); i--;) 
383     {
384       int barnum = get_bar_number ();
385
386       Music *note = accidentals_[i].melodic_;
387       Context * origin = accidentals_[i].origin_;
388
389       Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
390       if (!pitch)
391         continue;
392
393       int n = pitch->get_notename ();
394       int o = pitch->get_octave ();
395       int a = pitch->get_alteration ();
396       SCM key = scm_cons (scm_int2num (o), scm_int2num (n));
397
398       while (origin
399              && origin->where_defined (ly_symbol2scm ("localKeySignature")))
400         {
401           /*
402             huh? we set props all the way to the top? 
403           */
404           SCM localsig = origin->get_property ("localKeySignature");
405           bool change = false;
406           if (accidentals_[i].tied_)
407             {
408               /*
409                 Remember an alteration that is different both from
410                 that of the tied note and of the key signature.
411               */
412               localsig = ly_assoc_front_x
413                 (localsig, key, scm_cons (SCM_BOOL_T, scm_int2num (barnum)));
414
415               change = true;
416             }
417           else
418             {
419               /*
420                 not really really correct if there are more than one
421                 noteheads with the same notename.
422               */
423               localsig = ly_assoc_front_x (localsig, key,
424                                            scm_cons (scm_int2num (a),
425                                                      scm_int2num (barnum)));
426               change = true;
427             }
428
429           if (change)
430             origin->set_property ("localKeySignature", localsig);
431
432           origin = origin->get_parent_context ();
433         }
434     }
435
436   accidental_placement_ = 0;
437   accidentals_.clear ();
438   left_objects_.clear ();
439   right_objects_.clear ();
440 }
441
442 void
443 Accidental_engraver::acknowledge_grob (Grob_info info)
444 {
445   Music *note = info.music_cause ();
446
447   if (note
448       && note->is_mus_type ("note-event")
449       && Rhythmic_head::has_interface (info.grob_))
450     {
451       if (to_boolean ( get_property ("harmonicAccidentals"))
452           || !ly_c_equal_p (info.grob_->get_property ("style"),
453                           ly_symbol2scm ("harmonic")))
454         {
455           
456           Accidental_entry entry ;
457           entry.head_ = info.grob_;
458           entry.origin_trans_ = dynamic_cast<Engraver*> (info.origin_trans_);
459           entry.origin_ = info.origin_trans_->context ();
460           entry.melodic_ = note;
461
462           accidentals_.push (entry);
463         }
464     }
465   else if (Tie::has_interface (info.grob_))
466     ties_.push (dynamic_cast<Spanner*> (info.grob_));
467   else if (Arpeggio::has_interface (info.grob_))
468     left_objects_.push (info.grob_); 
469   else if (info.grob_
470            ->internal_has_interface (ly_symbol2scm ("finger-interface")))
471     left_objects_.push (info.grob_); 
472 }
473
474 void
475 Accidental_engraver::process_music ()
476 {
477   SCM sig = get_property ("keySignature");
478   /* Detect key sig changes.
479      Update all parents and children.  */
480   if (last_keysig_ != sig)
481     update_local_key_signature ();
482 }
483
484 ENTER_DESCRIPTION (Accidental_engraver,
485                    "Make accidentals.  "
486                    "Catch note heads, ties and notices key-change events.  "
487                    "This engraver usually lives at Staff level, but "
488                    "reads the settings for Accidental at @code{Voice} level, " 
489                    "so you can @code{\\override} them at @code{Voice}. "
490                    ,
491                    "Accidental"
492                    ,
493                    ""
494                    ,
495                    "arpeggio-interface "
496                    "finger-interface "
497                    "rhythmic-head-interface "
498                    "tie-interface "
499                    ,
500                    "autoAccidentals "
501                    "autoCautionaries "
502                    "extraNatural "
503                    "harmonicAccidentals "
504                    "localKeySignature"
505                    ,
506                    "localKeySignature"
507                    );