2 accidental-engraver.cc -- implement accidental_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
15 #include "engraver.hh"
16 #include "international.hh"
18 #include "protected-scm.hh"
19 #include "rhythmic-head.hh"
20 #include "side-position-interface.hh"
21 #include "stream-event.hh"
25 #include "translator.icc"
27 class Accidental_entry
31 Stream_event *melodic_;
34 Engraver *origin_engraver_;
41 Accidental_entry::Accidental_entry ()
51 class Accidental_engraver : public Engraver
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);
60 TRANSLATOR_DECLARATIONS (Accidental_engraver);
61 void process_music ();
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);
68 void stop_translation_timestep ();
69 void process_acknowledged ();
70 virtual void finalize ();
71 virtual void derived_mark () const;
74 SCM last_keysig_; // ugh.
77 Urgh. Since the accidentals depend on lots of variables, we have
78 to store all information before we can really create the
81 vector<Grob*> left_objects_;
82 vector<Grob*> right_objects_;
84 Grob *accidental_placement_;
86 vector<Accidental_entry> accidentals_;
87 vector<Spanner*> ties_;
93 ugh, it is not clear what properties are mutable and which
94 aren't. eg. localKeySignature is changed at runtime, which means
95 that references in grobs should always store ly_deep_copy ()s of
100 Accidental_engraver::Accidental_engraver ()
102 accidental_placement_ = 0;
103 last_keysig_ = SCM_EOL;
107 Accidental_engraver::derived_mark () const
109 scm_gc_mark (last_keysig_);
113 Accidental_engraver::update_local_key_signature ()
115 last_keysig_ = get_property ("keySignature");
116 set_context_property_on_children (context (),
117 ly_symbol2scm ("localKeySignature"),
120 Context *trans = context ()->get_parent_context ();
122 /* Reset parent contexts so that e.g. piano-accidentals won't remember old
123 cross-staff accidentals after key-sig-changes */
126 while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature"), &val)==trans)
128 trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
129 trans = trans->get_parent_context ();
134 /** Calculate the number of accidentals on basis of the current local key
135 sig (passed as argument)
137 * First check step+octave (taking into account barnumbers if necessary).
139 * Then check the global signature (only step).
141 Return number of accidentals (0, 1 or 2). */
144 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
146 if (scm_is_number (alteration_def)
147 || laziness == SCM_BOOL_T)
150 return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
154 extract_alteration (SCM alteration_def)
156 if (scm_is_number (alteration_def))
157 return scm_to_int (alteration_def);
158 else if (scm_is_pair (alteration_def))
159 return scm_to_int (scm_car (alteration_def));
160 else if (alteration_def == SCM_BOOL_F)
168 is_tied (SCM alteration_def)
170 return (alteration_def == SCM_BOOL_T)
171 || (scm_is_pair (alteration_def) && scm_car (alteration_def) == SCM_BOOL_T);
175 number_accidentals_from_sig (bool *different, SCM sig, Pitch *pitch,
176 int bar_number, SCM laziness, bool ignore_octave)
178 int n = pitch->get_notename ();
179 int o = pitch->get_octave ();
181 SCM previous_alteration = SCM_BOOL_F;
183 SCM from_same_octave = ly_assoc_get (scm_cons (scm_from_int (o),
184 scm_from_int (n)), sig, SCM_BOOL_F);
185 SCM from_key_signature = ly_assoc_get (scm_from_int (n), sig, SCM_BOOL_F);
186 SCM from_other_octaves = SCM_BOOL_F;
187 for (SCM s = sig; scm_is_pair (s); s = scm_cdr (s))
189 SCM entry = scm_car (s);
190 if (scm_is_pair (scm_car (entry))
191 && scm_cdar (entry) == scm_from_int (n))
193 from_other_octaves = scm_cdr (entry);
199 && from_same_octave != SCM_BOOL_F
200 && recent_enough (bar_number, from_same_octave, laziness))
201 previous_alteration = from_same_octave;
202 else if (ignore_octave
203 && from_other_octaves != SCM_BOOL_F
204 && recent_enough (bar_number, from_other_octaves, laziness))
205 previous_alteration = from_other_octaves;
206 else if (from_key_signature != SCM_BOOL_F)
207 previous_alteration = from_key_signature;
210 if (is_tied (previous_alteration))
217 int prev = extract_alteration (previous_alteration);
218 int alter = pitch->get_alteration ();
222 else if ((abs (alter) < abs (prev)
223 || prev * alter < 0) && alter != 0)
225 *different = (alter != prev);
231 number_accidentals (bool *different,
232 Pitch *pitch, Context *origin,
233 SCM accidentals, int bar_number)
238 if (scm_is_pair (accidentals) && !scm_is_symbol (scm_car (accidentals)))
239 warning (_f ("accidental typesetting list must begin with context-name: %s",
240 ly_scm2string (scm_car (accidentals)).c_str ()));
242 for (; scm_is_pair (accidentals) && origin;
243 accidentals = scm_cdr (accidentals))
245 // If pair then it is a new accidentals typesetting rule to be checked
246 SCM rule = scm_car (accidentals);
247 if (scm_is_pair (rule))
249 SCM type = scm_car (rule);
250 SCM laziness = scm_cdr (rule);
251 SCM localsig = origin->get_property ("localKeySignature");
254 = scm_is_eq (ly_symbol2scm ("same-octave"), type);
256 = scm_is_eq (ly_symbol2scm ("any-octave"), type);
258 if (same_octave_b || any_octave_b)
261 int n = number_accidentals_from_sig
262 (&d, localsig, pitch, bar_number, laziness, any_octave_b);
263 *different = *different || d;
264 number = max (number, n);
267 warning (_f ("ignoring unknown accidental: %s",
268 ly_symbol2string (type).c_str ()));
271 /* if symbol then it is a context name. Scan parent contexts to
273 else if (scm_is_symbol (rule))
275 Context *dad = origin;
276 while (dad && !dad->is_alias (rule))
277 dad = dad->get_parent_context ();
283 warning (_f ("pair or context-name expected for accidental rule, found %s",
284 ly_scm2string (rule).c_str ()));
291 Accidental_engraver::get_bar_number ()
293 SCM barnum = get_property ("internalBarNumber");
294 SCM smp = get_property ("measurePosition");
296 int bn = robust_scm2int (barnum, 0);
298 Moment mp = robust_scm2moment (smp, Moment (0));
299 if (mp.main_part_ < Rational (0))
306 Accidental_engraver::process_acknowledged ()
308 if (accidentals_.size () && !accidentals_.back ().done_)
310 SCM accidentals = get_property ("autoAccidentals");
311 SCM cautionaries = get_property ("autoCautionaries");
312 int barnum = get_bar_number ();
314 for (vsize i = 0; i < accidentals_.size (); i++)
316 if (accidentals_[i].done_)
318 accidentals_[i].done_ = true;
320 Stream_event *note = accidentals_[i].melodic_;
321 Context *origin = accidentals_[i].origin_;
323 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
327 bool different = false;
328 bool different_caut = false;
330 int num = number_accidentals (&different,
332 accidentals, barnum);
333 int num_caut = number_accidentals (&different_caut,
335 cautionaries, barnum);
338 bool cautionary = to_boolean (note->get_property ("cautionary"));
342 different = different_caut;
346 bool forced = to_boolean (note->get_property ("force-accidental"));
347 if (num == 0 && forced)
350 /* Cannot look for ties: it's not guaranteed that they reach
351 us before the notes. */
353 && !note->in_event_class ("trill-span-event"))
354 create_accidental (&accidentals_[i], num > 1, cautionary);
357 if (forced || cautionary)
358 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
364 Accidental_engraver::create_accidental (Accidental_entry *entry,
365 bool restore_natural,
368 Stream_event *note = entry->melodic_;
369 Grob *support = entry->head_;
370 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
372 bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
375 a = make_suggested_accidental (note, support, entry->origin_engraver_);
377 a = make_standard_accidental (note, support, entry->origin_engraver_);
379 SCM accs = scm_cons (scm_from_int (pitch->get_alteration ()),
383 if (to_boolean (get_property ("extraNatural")))
384 accs = scm_cons (scm_from_int (0), accs);
387 /* TODO: add cautionary option in accidental. */
389 a->set_property ("cautionary", SCM_BOOL_T);
391 a->set_property ("accidentals", accs);
392 entry->accidental_ = a;
396 Accidental_engraver::make_standard_accidental (Stream_event *note,
403 We construct the accidentals at the originating Voice
404 level, so that we get the property settings for
405 Accidental from the respective Voice.
407 Grob *a = trans->make_item ("Accidental", note_head->self_scm ());
410 We add the accidentals to the support of the arpeggio,
411 so it is put left of the accidentals.
413 for (vsize i = 0; i < left_objects_.size (); i++)
415 if (left_objects_[i]->get_property ("side-axis") == scm_from_int (X_AXIS))
416 Side_position_interface::add_support (left_objects_[i], a);
422 for (vsize i = 0; i < right_objects_.size (); i++)
423 Side_position_interface::add_support (a, right_objects_[i]);
425 a->set_parent (note_head, Y_AXIS);
427 if (!accidental_placement_)
428 accidental_placement_ = make_item ("AccidentalPlacement",
430 Accidental_placement::add_accidental (accidental_placement_, a);
432 note_head->set_object ("accidental-grob", a->self_scm ());
438 Accidental_engraver::make_suggested_accidental (Stream_event *note,
443 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
445 Side_position_interface::add_support (a, note_head);
446 if (Grob *stem = unsmob_grob (a->get_object ("stem")))
447 Side_position_interface::add_support (a, stem);
449 a->set_parent (note_head, X_AXIS);
454 Accidental_engraver::finalize ()
456 last_keysig_ = SCM_EOL;
460 Accidental_engraver::stop_translation_timestep ()
462 for (vsize j = ties_.size (); j--;)
464 Grob *r = Tie::head (ties_[j], RIGHT);
465 for (vsize i = accidentals_.size (); i--;)
466 if (accidentals_[i].head_ == r)
468 if (Grob *g = accidentals_[i].accidental_)
470 g->set_object ("tie", ties_[j]->self_scm ());
471 accidentals_[i].tied_ = true;
473 ties_.erase (ties_.begin () + j);
478 for (vsize i = accidentals_.size (); i--;)
480 int barnum = get_bar_number ();
482 Stream_event *note = accidentals_[i].melodic_;
483 Context *origin = accidentals_[i].origin_;
485 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
489 int n = pitch->get_notename ();
490 int o = pitch->get_octave ();
491 int a = pitch->get_alteration ();
492 SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
494 SCM localsig = SCM_EOL;
496 && origin->where_defined (ly_symbol2scm ("localKeySignature"), &localsig))
499 if (accidentals_[i].tied_)
502 Remember an alteration that is different both from
503 that of the tied note and of the key signature.
505 localsig = ly_assoc_front_x
506 (localsig, key, scm_cons (SCM_BOOL_T, scm_from_int (barnum)));
513 not really really correct if there are more than one
514 noteheads with the same notename.
516 localsig = ly_assoc_front_x (localsig, key,
517 scm_cons (scm_from_int (a),
518 scm_from_int (barnum)));
523 origin->set_property ("localKeySignature", localsig);
525 origin = origin->get_parent_context ();
529 accidental_placement_ = 0;
530 accidentals_.clear ();
531 left_objects_.clear ();
532 right_objects_.clear ();
536 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
538 Stream_event *note = info.event_cause ();
540 && (note->in_event_class ("note-event")
541 || note->in_event_class ("trill-span-event")))
544 string harmonics usually don't have accidentals.
546 if (to_boolean (get_property ("harmonicAccidentals"))
547 || !ly_is_equal (info.grob ()->get_property ("style"),
548 ly_symbol2scm ("harmonic")))
550 Accidental_entry entry;
551 entry.head_ = info.grob ();
552 entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
553 entry.origin_ = entry.origin_engraver_->context ();
554 entry.melodic_ = note;
556 accidentals_.push_back (entry);
562 Accidental_engraver::acknowledge_tie (Grob_info info)
564 ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
568 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
570 left_objects_.push_back (info.grob ());
574 Accidental_engraver::acknowledge_finger (Grob_info info)
576 left_objects_.push_back (info.grob ());
580 Accidental_engraver::process_music ()
582 SCM sig = get_property ("keySignature");
583 /* Detect key sig changes.
584 Update all parents and children. */
585 if (last_keysig_ != sig)
586 update_local_key_signature ();
589 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
590 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
591 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
592 ADD_ACKNOWLEDGER (Accidental_engraver, tie);
594 ADD_TRANSLATOR (Accidental_engraver,
597 "Catch note heads, ties and notices key-change events. "
598 "This engraver usually lives at Staff level, but "
599 "reads the settings for Accidental at @code{Voice} level, "
600 "so you can @code{\\override} them at @code{Voice}. ",
601 "Accidental AccidentalSuggestion",
607 "harmonicAccidentals "
609 "localKeySignature");