2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
22 #include "engraver.hh"
23 #include "international.hh"
26 #include "protected-scm.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "stream-event.hh"
30 #include "translator.icc"
32 class Key_engraver : public Engraver
34 void create_key (bool);
35 void read_event (Stream_event const *r);
37 Stream_event *key_event_;
41 TRANSLATOR_DECLARATIONS (Key_engraver);
44 virtual void initialize ();
45 virtual void finalize ();
46 void stop_translation_timestep ();
47 void process_music ();
49 DECLARE_TRANSLATOR_LISTENER (key_change);
50 DECLARE_ACKNOWLEDGER (clef);
51 DECLARE_ACKNOWLEDGER (bar_line);
55 Key_engraver::finalize ()
59 Key_engraver::Key_engraver ()
67 Key_engraver::create_key (bool is_default)
71 item_ = make_item ("KeySignature",
72 key_event_ ? key_event_->self_scm () : SCM_EOL);
74 /* Use middleCClefPosition rather than middleCPosition, because cue
75 * notes with a different clef will modify middleCPosition. The
76 * Key signature, however, should still be printed at the original
78 item_->set_property ("c0-position",
79 get_property ("middleCClefPosition"));
81 SCM last = get_property ("lastKeyAlterations");
82 SCM key = get_property ("keyAlterations");
84 if ((to_boolean (get_property ("printKeyCancellation"))
86 && !scm_is_eq (last, key))
88 SCM restore = SCM_EOL;
89 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
91 SCM new_alter_pair = scm_assoc (scm_caar (s), key);
92 Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
93 if (scm_is_false (new_alter_pair)
94 || ((ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter) * old_alter
97 restore = scm_cons (scm_car (s), restore);
101 if (scm_is_pair (restore))
103 cancellation_ = make_item ("KeyCancellation",
105 ? key_event_->self_scm () : SCM_EOL);
107 cancellation_->set_property ("alteration-alist", restore);
108 cancellation_->set_property ("c0-position",
109 get_property ("middleCClefPosition"));
113 item_->set_property ("alteration-alist", scm_reverse (key));
118 SCM visibility = get_property ("explicitKeySignatureVisibility");
119 item_->set_property ("break-visibility", visibility);
123 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
125 Key_engraver::listen_key_change (Stream_event *ev)
127 /* do this only once, just to be on the safe side. */
128 if (ASSIGN_EVENT_ONCE (key_event_, ev))
129 read_event (key_event_);
133 Key_engraver::acknowledge_clef (Grob_info /* info */)
135 SCM c = get_property ("createKeyOnClefChange");
141 Key_engraver::acknowledge_bar_line (Grob_info /* info */)
147 Key_engraver::process_music ()
150 || get_property ("lastKeyAlterations") != get_property ("keyAlterations"))
155 Key_engraver::stop_translation_timestep ()
158 context ()->set_property ("lastKeyAlterations", get_property ("keyAlterations"));
164 Key_engraver::read_event (Stream_event const *r)
166 SCM p = r->get_property ("pitch-alist");
167 if (!scm_is_pair (p))
172 SCM alist = scm_list_copy (p);
173 SCM order = get_property ("keyAlterationOrder");
175 scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
177 SCM head = scm_member (scm_car (s), alist);
179 if (scm_is_pair (head))
181 accs = scm_cons (scm_car (head), accs);
182 alist = scm_delete_x (scm_car (head), alist);
186 if (scm_is_pair (alist))
189 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
190 if (ly_scm2rational (scm_cdar (s)))
193 accs = scm_cons (scm_car (s), accs);
197 r->origin ()->warning (_ ("Incomplete keyAlterationOrder for key signature"));
200 context ()->set_property ("keyAlterations", scm_reverse_x (accs, SCM_EOL));
201 context ()->set_property ("tonic",
202 r->get_property ("tonic"));
206 Key_engraver::initialize ()
208 context ()->set_property ("keyAlterations", SCM_EOL);
209 context ()->set_property ("lastKeyAlterations", SCM_EOL);
212 context ()->set_property ("tonic", p.smobbed_copy ());
215 ADD_ACKNOWLEDGER (Key_engraver, clef);
216 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
218 ADD_TRANSLATOR (Key_engraver,
220 "Engrave a key signature.",
227 "createKeyOnClefChange "
228 "explicitKeySignatureVisibility "
230 "keyAlterationOrder "
232 "lastKeyAlterations "
233 "printKeyCancellation "
234 "middleCClefPosition ",
238 "lastKeyAlterations "