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"
21 class Key_engraver : public Engraver
23 void create_key (bool);
24 void read_event (Stream_event const *r);
26 Stream_event *key_event_;
30 TRANSLATOR_DECLARATIONS (Key_engraver);
33 virtual void initialize ();
34 virtual void finalize ();
35 void stop_translation_timestep ();
36 void process_music ();
38 DECLARE_TRANSLATOR_LISTENER (key_change);
39 DECLARE_ACKNOWLEDGER (clef);
40 DECLARE_ACKNOWLEDGER (bar_line);
44 Key_engraver::finalize ()
48 Key_engraver::Key_engraver ()
57 make_qt_key (SCM rat_key)
62 for (SCM s = rat_key; scm_is_pair (s); s = scm_cdr (s))
64 *tail = scm_cons (scm_cons (scm_caar (s),
65 scm_from_int (Rational (4)* ly_scm2rational (scm_cdar (s)))),
67 tail = SCM_CDRLOC (*tail);
74 Key_engraver::create_key (bool is_default)
78 item_ = make_item ("KeySignature",
79 key_event_ ? key_event_->self_scm () : SCM_EOL);
81 item_->set_property ("c0-position",
82 get_property ("middleCPosition"));
84 SCM last = get_property ("lastKeySignature");
85 SCM key = get_property ("keySignature");
86 bool extranatural = to_boolean(get_property("extraNatural"));
88 if ((to_boolean (get_property ("printKeyCancellation"))
90 && !scm_is_eq (last, key))
92 SCM restore = SCM_EOL;
94 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
96 SCM new_alter_pair = scm_assoc (scm_caar (s), key);
97 Rational old_alter = ly_scm2rational (scm_cdar (s));
98 if (new_alter_pair == SCM_BOOL_F
100 && (ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter)*old_alter < Rational (0))
102 *tail = scm_cons (scm_car (s), *tail);
103 tail = SCM_CDRLOC (*tail);
107 if (scm_is_pair (restore))
109 cancellation_ = make_item ("KeyCancellation",
111 ? key_event_->self_scm () : SCM_EOL);
113 cancellation_->set_property ("alteration-alist", make_qt_key (restore));
114 cancellation_->set_property ("c0-position",
115 get_property ("middleCPosition"));
120 item_->set_property ("alteration-alist", make_qt_key (key));
125 SCM visibility = get_property ("explicitKeySignatureVisibility");
126 item_->set_property ("break-visibility", visibility);
128 cancellation_->set_property ("break-visibility", visibility);
132 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
134 Key_engraver::listen_key_change (Stream_event *ev)
136 /* do this only once, just to be on the safe side. */
137 if (ASSIGN_EVENT_ONCE (key_event_, ev))
138 read_event (key_event_);
142 Key_engraver::acknowledge_clef (Grob_info info)
145 SCM c = get_property ("createKeyOnClefChange");
151 Key_engraver::acknowledge_bar_line (Grob_info info)
154 if (scm_is_pair (get_property ("keySignature")))
159 Key_engraver::process_music ()
162 || get_property ("lastKeySignature") != get_property ("keySignature"))
167 Key_engraver::stop_translation_timestep ()
170 context ()->set_property ("lastKeySignature", get_property ("keySignature"));
176 Key_engraver::read_event (Stream_event const *r)
178 SCM p = r->get_property ("pitch-alist");
179 if (!scm_is_pair (p))
184 SCM alist = scm_list_copy (p);
185 SCM order = get_property ("keyAlterationOrder");
187 scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
189 SCM head = scm_member (scm_car (s), alist);
191 if (scm_is_pair (head))
193 accs = scm_cons (scm_car (head), accs);
194 alist = scm_delete_x (scm_car (head), alist);
198 if (scm_is_pair (alist))
200 r->origin ()->warning ("No ordering for key signature alterations");
201 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
202 if (ly_scm2rational (scm_cdar (s)))
203 accs = scm_cons (scm_car (s), accs);
206 context ()->set_property ("keySignature", accs);
207 context ()->set_property ("tonic",
208 r->get_property ("tonic"));
212 Key_engraver::initialize ()
214 context ()->set_property ("keySignature", SCM_EOL);
215 context ()->set_property ("lastKeySignature", SCM_EOL);
218 context ()->set_property ("tonic", p.smobbed_copy ());
221 ADD_ACKNOWLEDGER (Key_engraver, clef);
222 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
224 ADD_TRANSLATOR (Key_engraver,
226 /* create */ "KeySignature",
229 "createKeyOnClefChange "
230 "explicitKeySignatureVisibility "
232 "keyAlterationOrder "
236 "printKeyCancellation "