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