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>
12 #include "engraver.hh"
15 #include "protected-scm.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "stream-event.hh"
19 #include "translator.icc"
22 TODO: The representation of key sigs is all fucked.
26 Make the key signature.
28 class Key_engraver : public Engraver
30 void create_key (bool);
31 void read_event (Stream_event const *r);
33 Stream_event *key_event_;
37 TRANSLATOR_DECLARATIONS (Key_engraver);
40 virtual void initialize ();
41 virtual void finalize ();
42 void stop_translation_timestep ();
43 void process_music ();
45 DECLARE_TRANSLATOR_LISTENER (key_change);
46 DECLARE_ACKNOWLEDGER (clef);
47 DECLARE_ACKNOWLEDGER (bar_line);
51 Key_engraver::finalize ()
55 Key_engraver::Key_engraver ()
63 Key_engraver::create_key (bool is_default)
67 item_ = make_item ("KeySignature",
68 key_event_ ? key_event_->self_scm () : SCM_EOL);
70 item_->set_property ("c0-position",
71 get_property ("middleCPosition"));
73 SCM last = get_property ("lastKeySignature");
74 SCM key = get_property ("keySignature");
75 bool extranatural = to_boolean(get_property("extraNatural"));
77 if ((to_boolean (get_property ("printKeyCancellation"))
79 && !scm_is_eq (last, key))
81 SCM restore = SCM_EOL;
83 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
85 SCM new_alter_pair = scm_assoc (scm_caar (s), key);
86 int old_alter = scm_to_int (scm_cdar (s));
87 if (new_alter_pair == SCM_BOOL_F
89 && (scm_to_int(scm_cdr(new_alter_pair))-old_alter)*old_alter < 0)
91 *tail = scm_cons (scm_car (s), *tail);
92 tail = SCM_CDRLOC (*tail);
96 if (scm_is_pair (restore))
98 cancellation_ = make_item ("KeyCancellation",
100 ? key_event_->self_scm () : SCM_EOL);
102 cancellation_->set_property ("alteration-alist", restore);
103 cancellation_->set_property ("c0-position",
104 get_property ("middleCPosition"));
107 item_->set_property ("alteration-alist", key);
112 SCM visibility = get_property ("explicitKeySignatureVisibility");
113 item_->set_property ("break-visibility", visibility);
115 cancellation_->set_property ("break-visibility", visibility);
119 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
121 Key_engraver::listen_key_change (Stream_event *ev)
123 /* do this only once, just to be on the safe side. */
124 if (ASSIGN_EVENT_ONCE (key_event_, ev))
125 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 (Stream_event 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",
208 "createKeyOnClefChange "
209 "explicitKeySignatureVisibility "
211 "keyAlterationOrder "
215 "printKeyCancellation "