2 key-engraver.cc -- implement Key_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.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);
113 Key_engraver::try_music (Music *event)
115 if (event->is_mus_type ("key-change-event"))
117 /* do this only once, just to be on the safe side. */
121 read_event (key_event_);
129 Key_engraver::acknowledge_clef (Grob_info info)
132 SCM c = get_property ("createKeyOnClefChange");
138 Key_engraver::acknowledge_bar_line (Grob_info info)
141 if (scm_is_pair (get_property ("keySignature")))
146 Key_engraver::process_music ()
149 || get_property ("lastKeySignature") != get_property ("keySignature"))
154 Key_engraver::stop_translation_timestep ()
157 context ()->set_property ("lastKeySignature", get_property ("keySignature"));
163 Key_engraver::read_event (Music const *r)
165 SCM p = r->get_property ("pitch-alist");
166 if (!scm_is_pair (p))
169 SCM n = scm_list_copy (p);
171 for (SCM s = get_property ("keyAlterationOrder");
172 scm_is_pair (s); s = scm_cdr (s))
174 if (scm_is_pair (scm_member (scm_car (s), n)))
176 accs = scm_cons (scm_car (s), accs);
177 n = scm_delete_x (scm_car (s), n);
181 for (SCM s = n; scm_is_pair (s); s = scm_cdr (s))
182 if (scm_to_int (scm_cdar (s)))
183 accs = scm_cons (scm_car (s), accs);
185 context ()->set_property ("keySignature", accs);
186 context ()->set_property ("tonic",
187 r->get_property ("tonic"));
191 Key_engraver::initialize ()
193 context ()->set_property ("keySignature", SCM_EOL);
194 context ()->set_property ("lastKeySignature", SCM_EOL);
197 context ()->set_property ("tonic", p.smobbed_copy ());
200 ADD_ACKNOWLEDGER (Key_engraver, clef);
201 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
203 ADD_TRANSLATOR (Key_engraver,
205 /* create */ "KeySignature",
206 /* accept */ "key-change-event",
207 /* read */ "keySignature printKeyCancellation lastKeySignature "
208 "explicitKeySignatureVisibility createKeyOnClefChange "
209 "keyAlterationOrder keySignature",
210 /* write */ "lastKeySignature tonic keySignature");