2 key-engraver.cc -- implement Key_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "bar-line.hh"
11 #include "staff-symbol-referencer.hh"
13 #include "engraver.hh"
14 #include "protected-scm.hh"
18 #include "translator.icc"
21 TODO: The representation of key sigs is all fucked.
25 Make the key signature.
27 class Key_engraver : public Engraver
29 void create_key (bool);
30 void read_event (Music const *r);
36 TRANSLATOR_DECLARATIONS (Key_engraver);
39 virtual void initialize ();
40 virtual void finalize ();
41 virtual bool try_music (Music *event);
42 void stop_translation_timestep ();
43 void process_music ();
45 DECLARE_ACKNOWLEDGER (clef);
46 DECLARE_ACKNOWLEDGER (bar_line);
50 Key_engraver::finalize ()
54 Key_engraver::Key_engraver ()
62 Key_engraver::create_key (bool is_default)
66 item_ = make_item ("KeySignature",
67 key_event_ ? key_event_->self_scm () : SCM_EOL);
69 item_->set_property ("c0-position",
70 get_property ("middleCPosition"));
72 SCM last = get_property ("lastKeySignature");
73 SCM key = get_property ("keySignature");
75 if ((to_boolean (get_property ("printKeyCancellation"))
77 && !scm_is_eq (last, key))
79 SCM restore = SCM_EOL;
81 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
83 if (scm_assoc (scm_caar (s), key) == SCM_BOOL_F)
85 *tail = scm_acons (scm_caar (s),
86 scm_from_int (0), *tail);
87 tail = SCM_CDRLOC (*tail);
91 if (scm_is_pair (restore))
93 cancellation_ = make_item ("KeyCancellation",
95 ? key_event_->self_scm () : SCM_EOL);
97 cancellation_->set_property ("alteration-alist", restore);
98 cancellation_->set_property ("c0-position",
99 get_property ("middleCPosition"));
102 item_->set_property ("alteration-alist", key);
107 SCM visibility = get_property ("explicitKeySignatureVisibility");
108 item_->set_property ("break-visibility", visibility);
110 cancellation_->set_property ("break-visibility", visibility);
115 Key_engraver::try_music (Music *event)
117 if (event->is_mus_type ("key-change-event"))
119 /* do this only once, just to be on the safe side. */
123 read_event (key_event_);
131 Key_engraver::acknowledge_clef (Grob_info info)
134 SCM c = get_property ("createKeyOnClefChange");
140 Key_engraver::acknowledge_bar_line (Grob_info info)
143 if (scm_is_pair (get_property ("keySignature")))
148 Key_engraver::process_music ()
151 || get_property ("lastKeySignature") != get_property ("keySignature"))
156 Key_engraver::stop_translation_timestep ()
159 context ()->set_property ("lastKeySignature", get_property ("keySignature"));
165 Key_engraver::read_event (Music const *r)
167 SCM p = r->get_property ("pitch-alist");
168 if (!scm_is_pair (p))
171 SCM n = scm_list_copy (p);
173 for (SCM s = get_property ("keyAlterationOrder");
174 scm_is_pair (s); s = scm_cdr (s))
176 if (scm_is_pair (scm_member (scm_car (s), n)))
178 accs = scm_cons (scm_car (s), accs);
179 n = scm_delete_x (scm_car (s), n);
183 for (SCM s = n; scm_is_pair (s); s = scm_cdr (s))
184 if (scm_to_int (scm_cdar (s)))
185 accs = scm_cons (scm_car (s), accs);
187 context ()->set_property ("keySignature", accs);
188 context ()->set_property ("tonic",
189 r->get_property ("tonic"));
193 Key_engraver::initialize ()
195 context ()->set_property ("keySignature", SCM_EOL);
196 context ()->set_property ("lastKeySignature", SCM_EOL);
199 context ()->set_property ("tonic", p.smobbed_copy ());
202 ADD_ACKNOWLEDGER (Key_engraver, clef);
203 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
205 ADD_TRANSLATOR (Key_engraver,
207 /* create */ "KeySignature",
208 /* accept */ "key-change-event",
211 "createKeyOnClefChange "
212 "explicitKeySignatureVisibility "
213 "keyAlterationOrder "
217 "printKeyCancellation "