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