2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5 Modified 2001--2002 by Rune Zedeler <rz@daimi.au.dk>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "accidental-placement.hh"
22 #include "arpeggio.hh"
24 #include "duration.hh"
25 #include "engraver.hh"
26 #include "international.hh"
29 #include "protected-scm.hh"
30 #include "rhythmic-head.hh"
31 #include "separation-item.hh"
32 #include "side-position-interface.hh"
34 #include "stream-event.hh"
38 #include "translator.icc"
40 class Accidental_entry
44 Stream_event *melodic_;
47 Engraver *origin_engraver_;
54 Accidental_entry::Accidental_entry ()
65 class Accidental_engraver : public Engraver
67 void update_local_key_signature (SCM new_signature);
68 void create_accidental (Accidental_entry *entry, bool, bool);
69 Grob *make_standard_accidental (Stream_event *note, Grob *note_head, Engraver *trans, bool);
70 Grob *make_suggested_accidental (Stream_event *note, Grob *note_head, Engraver *trans);
73 TRANSLATOR_DECLARATIONS (Accidental_engraver);
74 void process_music ();
76 void acknowledge_end_tie (Grob_info);
77 void acknowledge_arpeggio (Grob_info);
78 void acknowledge_rhythmic_head (Grob_info);
79 void acknowledge_finger (Grob_info);
80 void acknowledge_note_column (Grob_info);
82 void stop_translation_timestep ();
83 void process_acknowledged ();
85 virtual void finalize ();
86 virtual void derived_mark () const;
91 vector<Grob *> left_objects_;
92 vector<Grob *> right_objects_;
94 Grob *accidental_placement_;
96 vector<Accidental_entry> accidentals_;
97 vector<Spanner *> ties_;
98 vector<Grob *> note_columns_;
102 localAlterations is changed at runtime, which means that references
103 in grobs should always store ly_deep_copy ()s of those.
106 Accidental_engraver::Accidental_engraver ()
108 accidental_placement_ = 0;
109 last_keysig_ = SCM_EOL;
113 Accidental_engraver::derived_mark () const
115 scm_gc_mark (last_keysig_);
119 Accidental_engraver::update_local_key_signature (SCM new_sig)
121 last_keysig_ = new_sig;
122 set_context_property_on_children (context (),
123 ly_symbol2scm ("localAlterations"),
126 Context *trans = context ()->get_parent_context ();
129 Reset parent contexts so that e.g. piano-accidentals won't remember old
130 cross-staff accidentals after key-sig-changes.
134 while (trans && trans->here_defined (ly_symbol2scm ("localAlterations"), &val))
136 trans->set_property ("localAlterations", ly_deep_copy (last_keysig_));
137 trans = trans->get_parent_context ();
141 struct Accidental_result
148 need_restore = need_acc = false;
151 Accidental_result (bool restore, bool acc)
153 need_restore = restore;
157 Accidental_result (SCM scm)
159 need_restore = to_boolean (scm_car (scm));
160 need_acc = to_boolean (scm_cdr (scm));
165 return need_acc ? 1 : 0
166 + need_restore ? 1 : 0;
172 check_pitch_against_rules (Pitch const &pitch, Context *origin,
173 SCM rules, int bar_number, SCM measurepos)
175 Accidental_result result;
176 SCM pitch_scm = pitch.smobbed_copy ();
177 SCM barnum_scm = scm_from_int (bar_number);
179 if (scm_is_pair (rules) && !scm_is_symbol (scm_car (rules)))
180 warning (_f ("accidental typesetting list must begin with context-name: %s",
181 ly_scm2string (scm_car (rules)).c_str ()));
183 for (; scm_is_pair (rules) && origin; rules = scm_cdr (rules))
185 SCM rule = scm_car (rules);
186 if (ly_is_procedure (rule))
188 SCM rule_result_scm = scm_call_4 (rule, origin->self_scm (),
189 pitch_scm, barnum_scm, measurepos);
190 Accidental_result rule_result (rule_result_scm);
192 result.need_acc |= rule_result.need_acc;
193 result.need_restore |= rule_result.need_restore;
197 If symbol then it is a context name. Scan parent contexts to
200 else if (scm_is_symbol (rule))
202 Context *dad = find_context_above (origin, rule);
207 warning (_f ("procedure or context-name expected for accidental rule, found %s",
208 print_scm_val (rule).c_str ()));
215 Accidental_engraver::process_acknowledged ()
217 if (accidentals_.size () && !accidentals_.back ().done_)
219 SCM accidental_rules = get_property ("autoAccidentals");
220 SCM cautionary_rules = get_property ("autoCautionaries");
221 SCM measure_position = get_property ("measurePosition");
222 int barnum = measure_number (context ());
224 for (vsize i = 0; i < accidentals_.size (); i++)
226 if (accidentals_[i].done_)
228 accidentals_[i].done_ = true;
230 Stream_event *note = accidentals_[i].melodic_;
231 Context *origin = accidentals_[i].origin_;
233 Pitch *pitch = unsmob<Pitch> (note->get_property ("pitch"));
237 Accidental_result acc = check_pitch_against_rules (*pitch, origin, accidental_rules,
238 barnum, measure_position);
239 Accidental_result caut = check_pitch_against_rules (*pitch, origin, cautionary_rules,
240 barnum, measure_position);
242 bool cautionary = to_boolean (note->get_property ("cautionary"));
243 if (caut.score () > acc.score ())
245 acc.need_acc |= caut.need_acc;
246 acc.need_restore |= caut.need_restore;
251 bool forced = to_boolean (note->get_property ("force-accidental"));
252 if (!acc.need_acc && forced)
256 Cannot look for ties: it's not guaranteed that they reach
259 if (!note->in_event_class ("trill-span-event"))
262 create_accidental (&accidentals_[i], acc.need_restore, cautionary);
264 if (forced || cautionary)
265 accidentals_[i].accidental_->set_property ("forced", SCM_BOOL_T);
272 Accidental_engraver::create_accidental (Accidental_entry *entry,
273 bool restore_natural,
276 Stream_event *note = entry->melodic_;
277 Grob *support = entry->head_;
278 bool as_suggestion = to_boolean (entry->origin_->get_property ("suggestAccidentals"));
281 a = make_suggested_accidental (note, support, entry->origin_engraver_);
283 a = make_standard_accidental (note, support, entry->origin_engraver_, cautionary);
287 if (to_boolean (get_property ("extraNatural")))
288 a->set_property ("restore-first", SCM_BOOL_T);
291 entry->accidental_ = a;
295 Accidental_engraver::make_standard_accidental (Stream_event * /* note */,
301 We construct the accidentals at the originating Voice
302 level, so that we get the property settings for
303 Accidental from the respective Voice.
307 a = trans->make_item ("AccidentalCautionary", note_head->self_scm ());
309 a = trans->make_item ("Accidental", note_head->self_scm ());
312 We add the accidentals to the support of the arpeggio,
313 so it is put left of the accidentals.
315 for (vsize i = 0; i < left_objects_.size (); i++)
317 if (scm_is_eq (left_objects_[i]->get_property ("side-axis"), scm_from_int (X_AXIS)))
318 Side_position_interface::add_support (left_objects_[i], a);
321 for (vsize i = 0; i < right_objects_.size (); i++)
322 Side_position_interface::add_support (a, right_objects_[i]);
324 a->set_parent (note_head, Y_AXIS);
326 if (!accidental_placement_)
327 accidental_placement_ = make_item ("AccidentalPlacement",
330 Accidental_placement::add_accidental
331 (accidental_placement_, a,
332 scm_is_eq (get_property ("accidentalGrouping"), ly_symbol2scm ("voice")),
335 note_head->set_object ("accidental-grob", a->self_scm ());
341 Accidental_engraver::make_suggested_accidental (Stream_event * /* note */,
345 Grob *a = trans->make_item ("AccidentalSuggestion", note_head->self_scm ());
347 Side_position_interface::add_support (a, note_head);
348 if (Grob *stem = unsmob<Grob> (a->get_object ("stem")))
349 Side_position_interface::add_support (a, stem);
351 a->set_parent (note_head, X_AXIS);
356 Accidental_engraver::finalize ()
358 last_keysig_ = SCM_EOL;
362 Accidental_engraver::stop_translation_timestep ()
364 for (vsize j = ties_.size (); j--;)
366 Grob *r = Tie::head (ties_[j], RIGHT);
367 Grob *l = Tie::head (ties_[j], LEFT);
370 // Don't mark accidentals as "tied" when the pitch is not
371 // actually the same. This is relevant for enharmonic ties.
372 Stream_event *le = unsmob<Stream_event> (l->get_property ("cause"));
373 Stream_event *re = unsmob<Stream_event> (r->get_property ("cause"));
375 && !ly_is_equal (le->get_property ("pitch"), re->get_property ("pitch")))
379 for (vsize i = accidentals_.size (); i--;)
380 if (accidentals_[i].head_ == r)
382 if (Grob *g = accidentals_[i].accidental_)
384 g->set_object ("tie", ties_[j]->self_scm ());
385 accidentals_[i].tied_ = true;
387 ties_.erase (ties_.begin () + j);
392 for (vsize i = accidentals_.size (); i--;)
394 Stream_event *note = accidentals_[i].melodic_;
395 Context *origin = accidentals_[i].origin_;
397 int barnum = measure_number (origin);
399 Pitch *pitch = unsmob<Pitch> (note->get_property ("pitch"));
403 int n = pitch->get_notename ();
404 int o = pitch->get_octave ();
405 Rational a = pitch->get_alteration ();
406 SCM key = scm_cons (scm_from_int (o), scm_from_int (n));
408 Moment end_mp = measure_position (context (),
409 unsmob<Duration> (note->get_property ("duration")));
410 SCM position = scm_cons (scm_from_int (barnum), end_mp.smobbed_copy ());
412 SCM localsig = SCM_EOL;
414 && origin->where_defined (ly_symbol2scm ("localAlterations"), &localsig))
417 if (accidentals_[i].tied_
418 && !(to_boolean (accidentals_[i].accidental_->get_property ("forced"))))
421 Remember an alteration that is different both from
422 that of the tied note and of the key signature.
424 localsig = ly_assoc_prepend_x (localsig, key, scm_cons (ly_symbol2scm ("tied"),
431 not really correct if there is more than one
432 note head with the same notename.
434 localsig = ly_assoc_prepend_x (localsig, key,
435 scm_cons (ly_rational2scm (a),
441 origin->set_property ("localAlterations", localsig);
443 origin = origin->get_parent_context ();
447 if (accidental_placement_)
448 for (vsize i = 0; i < note_columns_.size (); i++)
449 Separation_item::add_conditional_item (note_columns_[i], accidental_placement_);
451 accidental_placement_ = 0;
452 accidentals_.clear ();
453 note_columns_.clear ();
454 left_objects_.clear ();
455 right_objects_.clear ();
459 Accidental_engraver::acknowledge_rhythmic_head (Grob_info info)
461 Stream_event *note = info.event_cause ();
463 && (note->in_event_class ("note-event")
464 || note->in_event_class ("trill-span-event"))
465 // option to skip accidentals on string harmonics
466 && (to_boolean (get_property ("harmonicAccidentals"))
467 || !scm_is_eq (info.grob ()->get_property ("style"),
468 ly_symbol2scm ("harmonic")))
469 // ignore accidentals in non-printing voices like NullVoice
470 && !to_boolean (info.context ()->get_property ("nullAccidentals")))
472 Accidental_entry entry;
473 entry.head_ = info.grob ();
474 entry.origin_engraver_ = dynamic_cast<Engraver *> (info.origin_translator ());
475 entry.origin_ = entry.origin_engraver_->context ();
476 entry.melodic_ = note;
478 accidentals_.push_back (entry);
483 Accidental_engraver::acknowledge_end_tie (Grob_info info)
485 ties_.push_back (dynamic_cast<Spanner *> (info.grob ()));
489 Accidental_engraver::acknowledge_note_column (Grob_info info)
491 note_columns_.push_back (info.grob ());
495 Accidental_engraver::acknowledge_arpeggio (Grob_info info)
497 left_objects_.push_back (info.grob ());
501 Accidental_engraver::acknowledge_finger (Grob_info info)
503 left_objects_.push_back (info.grob ());
507 Accidental_engraver::process_music ()
509 SCM sig = get_property ("keyAlterations");
510 if (last_keysig_ != sig)
511 update_local_key_signature (sig);
514 ADD_ACKNOWLEDGER (Accidental_engraver, arpeggio);
515 ADD_ACKNOWLEDGER (Accidental_engraver, finger);
516 ADD_ACKNOWLEDGER (Accidental_engraver, rhythmic_head);
517 ADD_END_ACKNOWLEDGER (Accidental_engraver, tie);
518 ADD_ACKNOWLEDGER (Accidental_engraver, note_column);
520 ADD_TRANSLATOR (Accidental_engraver,
523 " Catch note heads, ties and notices key-change events."
524 " This engraver usually lives at Staff level, but"
525 " reads the settings for Accidental at @code{Voice} level,"
526 " so you can @code{\\override} them at @code{Voice}.",
530 "AccidentalCautionary "
531 "AccidentalPlacement "
532 "AccidentalSuggestion ",
539 "harmonicAccidentals "
540 "accidentalGrouping "