- item_p_ = new Key_item;
- item_p_->set_elt_property ("break-aligned", SCM_BOOL_T); // ugh
-
- item_p_->multi_octave_b_ = key_.multi_octave_b_;
- announce_element (Score_element_info (item_p_,keyreq_l_));
-
-
- for (int i = 0; i < accidental_idx_arr_.size(); i++)
- {
- Musical_pitch m_l =accidental_idx_arr_[i];
- int a =m_l.accidental_i_;
- if (key_.multi_octave_b_)
- item_p_->add (m_l.steps (), a);
- else
- item_p_->add (m_l.notename_i_, a);
- }
-
- for (int i = 0 ; i < old_accidental_idx_arr_.size(); i++)
- {
- Musical_pitch m_l =old_accidental_idx_arr_[i];
- int a =m_l.accidental_i_;
- if (key_.multi_octave_b_)
- item_p_->add_old (m_l.steps (), a);
- else
- item_p_->add_old (m_l.notename_i_, a);
- }
+ item_ = make_item ("KeySignature",
+ key_event_ ? key_event_->self_scm () : SCM_EOL);
+
+ /* Use middleCClefPosition rather than middleCPosition, because cue
+ * notes with a different clef will modify middleCPosition. The
+ * Key signature, however, should still be printed at the original
+ * position. */
+ item_->set_property ("c0-position",
+ get_property ("middleCClefPosition"));
+
+ SCM last = get_property ("lastKeyAlterations");
+ SCM key = get_property ("keyAlterations");
+
+ if ((to_boolean (get_property ("printKeyCancellation"))
+ || key == SCM_EOL)
+ && !scm_is_eq (last, key))
+ {
+ SCM restore = SCM_EOL;
+ for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
+ {
+ SCM new_alter_pair = scm_assoc (scm_caar (s), key);
+ Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
+ if (new_alter_pair == SCM_BOOL_F
+ || ((ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter) * old_alter
+ < Rational (0)))
+ {
+ restore = scm_cons (scm_car (s), restore);
+ }
+ }
+
+ if (scm_is_pair (restore))
+ {
+ cancellation_ = make_item ("KeyCancellation",
+ key_event_
+ ? key_event_->self_scm () : SCM_EOL);
+
+ cancellation_->set_property ("alteration-alist", restore);
+ cancellation_->set_property ("c0-position",
+ get_property ("middleCClefPosition"));
+ }
+ }
+
+ item_->set_property ("alteration-alist", scm_reverse (key));