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