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");
76 if ((to_boolean (get_property ("printKeyCancellation"))
78 && !scm_is_eq (last, key))
80 SCM restore = SCM_EOL;
82 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
84 if (scm_assoc (scm_caar (s), key) == SCM_BOOL_F)
86 *tail = scm_acons (scm_caar (s),
87 scm_from_int (0), *tail);
88 tail = SCM_CDRLOC (*tail);
92 if (scm_is_pair (restore))
94 cancellation_ = make_item ("KeyCancellation",
96 ? key_event_->self_scm () : SCM_EOL);
98 cancellation_->set_property ("alteration-alist", restore);
99 cancellation_->set_property ("c0-position",
100 get_property ("middleCPosition"));
103 item_->set_property ("alteration-alist", key);
108 SCM visibility = get_property ("explicitKeySignatureVisibility");
109 item_->set_property ("break-visibility", visibility);
111 cancellation_->set_property ("break-visibility", visibility);
115 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
117 Key_engraver::listen_key_change (Stream_event *ev)
119 /* do this only once, just to be on the safe side. */
120 if (ASSIGN_EVENT_ONCE (key_event_, ev))
121 read_event (key_event_);
125 Key_engraver::acknowledge_clef (Grob_info info)
128 SCM c = get_property ("createKeyOnClefChange");
134 Key_engraver::acknowledge_bar_line (Grob_info info)
137 if (scm_is_pair (get_property ("keySignature")))
142 Key_engraver::process_music ()
145 || get_property ("lastKeySignature") != get_property ("keySignature"))
150 Key_engraver::stop_translation_timestep ()
153 context ()->set_property ("lastKeySignature", get_property ("keySignature"));
159 Key_engraver::read_event (Stream_event const *r)
161 SCM p = r->get_property ("pitch-alist");
162 if (!scm_is_pair (p))
165 SCM n = scm_list_copy (p);
167 for (SCM s = get_property ("keyAlterationOrder");
168 scm_is_pair (s); s = scm_cdr (s))
170 if (scm_is_pair (scm_member (scm_car (s), n)))
172 accs = scm_cons (scm_car (s), accs);
173 n = scm_delete_x (scm_car (s), n);
177 for (SCM s = n; scm_is_pair (s); s = scm_cdr (s))
178 if (scm_to_int (scm_cdar (s)))
179 accs = scm_cons (scm_car (s), accs);
181 context ()->set_property ("keySignature", accs);
182 context ()->set_property ("tonic",
183 r->get_property ("tonic"));
187 Key_engraver::initialize ()
189 context ()->set_property ("keySignature", SCM_EOL);
190 context ()->set_property ("lastKeySignature", SCM_EOL);
193 context ()->set_property ("tonic", p.smobbed_copy ());
196 ADD_ACKNOWLEDGER (Key_engraver, clef);
197 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
199 ADD_TRANSLATOR (Key_engraver,
201 /* create */ "KeySignature",
204 "createKeyOnClefChange "
205 "explicitKeySignatureVisibility "
206 "keyAlterationOrder "
210 "printKeyCancellation "