2 accidental-engraver.cc -- implement accidental_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
10 #include "accidental-placement.hh"
11 #include "arpeggio.hh"
13 #include "engraver.hh"
14 #include "protected-scm.hh"
15 #include "rhythmic-head.hh"
16 #include "side-position-interface.hh"
22 #include "translator.icc"
24 class Accidental_entry
31 Engraver *origin_trans_;
38 Accidental_entry::Accidental_entry ()
48 class Accidental_engraver : public Engraver
51 int get_bar_number ();
52 void update_local_key_signature ();
55 TRANSLATOR_DECLARATIONS (Accidental_engraver);
56 PRECOMPUTED_VIRTUAL void process_music ();
57 virtual void acknowledge_grob (Grob_info);
58 PRECOMPUTED_VIRTUAL void stop_translation_timestep ();
59 virtual void initialize ();
60 PRECOMPUTED_VIRTUAL void process_acknowledged ();
61 virtual void finalize ();
63 virtual void derived_mark () const;
65 SCM last_keysig_; // ugh.
67 /* Urgh. Since the accidentals depend on lots of variables, we have
68 to store all information before we can really create the
70 Link_array<Grob> left_objects_;
71 Link_array<Grob> right_objects_;
73 Grob *accidental_placement_;
75 Array<Accidental_entry> accidentals_;
76 Link_array<Spanner> ties_;
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
89 set_property_on_children (Context *trans, char const *sym, SCM val)
91 trans->set_property (sym, ly_deep_copy (val));
92 for (SCM p = trans->children_contexts (); scm_is_pair (p); p = scm_cdr (p))
94 Context *trg = unsmob_context (scm_car (p));
95 set_property_on_children (trg, sym, ly_deep_copy (val));
99 Accidental_engraver::Accidental_engraver ()
101 accidental_placement_ = 0;
102 last_keysig_ = SCM_EOL;
106 Accidental_engraver::derived_mark () const
108 scm_gc_mark (last_keysig_);
112 Accidental_engraver::update_local_key_signature ()
114 last_keysig_ = get_property ("keySignature");
115 set_property_on_children (context (), "localKeySignature", last_keysig_);
117 Context *trans = context ()->get_parent_context ();
119 /* Huh. Don't understand what this is good for. --hwn. */
120 while (trans && trans->where_defined (ly_symbol2scm ("localKeySignature")))
122 trans->set_property ("localKeySignature", ly_deep_copy (last_keysig_));
123 trans = trans->get_parent_context ();
128 Accidental_engraver::initialize ()
130 update_local_key_signature ();
133 /** Calculate the number of accidentals on basis of the current local key
134 sig (passed as argument)
136 * First check step+octave (taking into account barnumbers if necessary).
138 * Then check the global signature (only step).
140 Return number of accidentals (0, 1 or 2). */
143 recent_enough (int bar_number, SCM alteration_def, SCM laziness)
145 if (scm_is_number (alteration_def)
146 || laziness == SCM_BOOL_T)
149 return (bar_number <= scm_to_int (scm_cdr (alteration_def)) + scm_to_int (laziness));
153 extract_alteration (SCM alteration_def)
155 if (scm_is_number (alteration_def))
156 return scm_to_int (alteration_def);
157 else if (scm_is_pair (alteration_def))
158 return scm_to_int (scm_car (alteration_def));
159 else if (alteration_def == SCM_BOOL_F)
167 is_tied (SCM alteration_def)
169 return (alteration_def == SCM_BOOL_T)
170 || (scm_is_pair (alteration_def) && scm_car (alteration_def) == SCM_BOOL_T);
174 number_accidentals_from_sig (bool *different, SCM sig, Pitch *pitch,
175 int bar_number, SCM laziness, bool ignore_octave)
177 int n = pitch->get_notename ();
178 int o = pitch->get_octave ();
180 SCM previous_alteration = SCM_BOOL_F;
182 SCM from_same_octave = ly_assoc_get (scm_cons (scm_int2num (o),
183 scm_int2num (n)), sig, SCM_BOOL_F);
184 SCM from_key_signature = ly_assoc_get (scm_int2num (n), sig, SCM_BOOL_F);
185 SCM from_other_octaves = SCM_BOOL_F;
186 for (SCM s = sig; scm_is_pair (s); s = scm_cdr (s))
188 SCM entry = scm_car (s);
189 if (scm_is_pair (scm_car (entry))
190 && scm_cdar (entry) == scm_int2num (n))
191 from_other_octaves = scm_cdr (entry);
194 if (from_same_octave != SCM_BOOL_F
195 && recent_enough (bar_number, from_same_octave, laziness))
197 previous_alteration = from_same_octave;
199 else if (ignore_octave
200 && from_other_octaves != SCM_BOOL_F
201 && recent_enough (bar_number, from_other_octaves, laziness))
203 previous_alteration = from_other_octaves;
205 else if (from_key_signature != SCM_BOOL_F)
207 previous_alteration = from_key_signature;
211 if (is_tied (previous_alteration))
218 int prev = extract_alteration (previous_alteration);
219 int alter = pitch->get_alteration ();
223 else if ((abs (alter) < abs (prev) || 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)).to_str0 ()));
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).to_str0 ()));
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).to_str0 ()));
291 Accidental_engraver::get_bar_number ()
293 SCM barnum = get_property ("currentBarNumber");
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_.top ().done_)
310 SCM accidentals = get_property ("autoAccidentals");
311 SCM cautionaries = get_property ("autoCautionaries");
312 int barnum = get_bar_number ();
314 bool extra_natural_b = get_property ("extraNatural") == SCM_BOOL_T;
315 for (int i = 0; i < accidentals_.size (); i++)
317 if (accidentals_[i].done_)
319 accidentals_[i].done_ = true;
320 Grob *support = accidentals_[i].head_;
321 Music *note = accidentals_[i].melodic_;
322 Context *origin = accidentals_[i].origin_;
324 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
328 bool different = false;
329 bool different_caut = false;
331 int num = number_accidentals (&different,
333 accidentals, barnum);
334 int num_caut = number_accidentals (&different_caut,
336 cautionaries, barnum);
338 bool cautionary = to_boolean (note->get_property ("cautionary"));
343 different = different_caut;
347 if (num == 0 && to_boolean (note->get_property ("force-accidental")))
350 /* Cannot look for ties: it's not guaranteed that they reach
351 us before the notes. */
355 We construct the accidentals at the originating Voice
356 level, so that we get the property settings for
357 Accidental from the respective Voice.
360 = make_item_from_properties (accidentals_[i].origin_trans_,
361 ly_symbol2scm ("Accidental"),
364 a->set_parent (support, Y_AXIS);
366 if (!accidental_placement_)
367 accidental_placement_ = make_item ("AccidentalPlacement",
369 Accidental_placement::add_accidental (accidental_placement_, a);
370 SCM accs = scm_cons (scm_int2num (pitch->get_alteration ()),
372 if (num == 2 && extra_natural_b)
373 accs = scm_cons (scm_int2num (0), accs);
375 /* TODO: add cautionary option in accidental. */
378 a->set_property ("cautionary", SCM_BOOL_T);
380 support->set_object ("accidental-grob", a->self_scm ());
382 a->set_property ("accidentals", accs);
383 accidentals_[i].accidental_ = a;
386 We add the accidentals to the support of the arpeggio,
387 so it is put left of the accidentals.
389 for (int i = 0; i < left_objects_.size (); i++)
390 Side_position_interface::add_support (left_objects_[i], a);
391 for (int i = 0; i < right_objects_.size (); i++)
392 Side_position_interface::add_support (a, right_objects_[i]);
399 Accidental_engraver::finalize ()
401 last_keysig_ = SCM_EOL;
405 Accidental_engraver::stop_translation_timestep ()
407 for (int j = ties_.size (); j--;)
409 Grob *r = Tie::head (ties_[j], RIGHT);
410 for (int i = accidentals_.size (); i--;)
411 if (accidentals_[i].head_ == r)
413 if (Grob *g = accidentals_[i].accidental_)
415 g->set_object ("tie", ties_[j]->self_scm ());
416 accidentals_[i].tied_ = true;
423 for (int i = accidentals_.size (); i--;)
425 int barnum = get_bar_number ();
427 Music *note = accidentals_[i].melodic_;
428 Context *origin = accidentals_[i].origin_;
430 Pitch *pitch = unsmob_pitch (note->get_property ("pitch"));
434 int n = pitch->get_notename ();
435 int o = pitch->get_octave ();
436 int a = pitch->get_alteration ();
437 SCM key = scm_cons (scm_int2num (o), scm_int2num (n));
440 && origin->where_defined (ly_symbol2scm ("localKeySignature")))
443 huh? we set props all the way to the top?
445 SCM localsig = origin->get_property ("localKeySignature");
447 if (accidentals_[i].tied_)
450 Remember an alteration that is different both from
451 that of the tied note and of the key signature.
453 localsig = ly_assoc_front_x
454 (localsig, key, scm_cons (SCM_BOOL_T, scm_int2num (barnum)));
461 not really really correct if there are more than one
462 noteheads with the same notename.
464 localsig = ly_assoc_front_x (localsig, key,
465 scm_cons (scm_int2num (a),
466 scm_int2num (barnum)));
471 origin->set_property ("localKeySignature", localsig);
473 origin = origin->get_parent_context ();
477 accidental_placement_ = 0;
478 accidentals_.clear ();
479 left_objects_.clear ();
480 right_objects_.clear ();
484 Accidental_engraver::acknowledge_grob (Grob_info info)
486 Music *note = info.music_cause ();
489 && note->is_mus_type ("note-event")
490 && Rhythmic_head::has_interface (info.grob ()))
492 if (to_boolean (get_property ("harmonicAccidentals"))
493 || !ly_is_equal (info.grob ()->get_property ("style"),
494 ly_symbol2scm ("harmonic")))
496 Accidental_entry entry;
497 entry.head_ = info.grob ();
498 entry.origin_trans_ = dynamic_cast<Engraver *> (info.origin_translator ());
499 entry.origin_ = entry.origin_trans_->context ();
500 entry.melodic_ = note;
502 accidentals_.push (entry);
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 ());
515 Accidental_engraver::process_music ()
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 ();
524 ADD_TRANSLATOR (Accidental_engraver,
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}. ",
532 "arpeggio-interface "
534 "rhythmic-head-interface "
539 "harmonicAccidentals "
541 "localKeySignature");