Fix type predicates/docstrings for two music properties.
[lilypond/mpolesky.git] / lily / key-engraver.cc
blob2365a37756c793511f3f9ebd0e08b7247ec0a19a
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "bar-line.hh"
21 #include "clef.hh"
22 #include "context.hh"
23 #include "engraver.hh"
24 #include "item.hh"
25 #include "pitch.hh"
26 #include "protected-scm.hh"
27 #include "staff-symbol-referencer.hh"
28 #include "stream-event.hh"
30 #include "translator.icc"
32 class Key_engraver : public Engraver
34 void create_key (bool);
35 void read_event (Stream_event const *r);
37 Stream_event *key_event_;
38 Item *item_;
39 Item *cancellation_;
40 public:
41 TRANSLATOR_DECLARATIONS (Key_engraver);
43 protected:
44 virtual void initialize ();
45 virtual void finalize ();
46 void stop_translation_timestep ();
47 void process_music ();
49 DECLARE_TRANSLATOR_LISTENER (key_change);
50 DECLARE_ACKNOWLEDGER (clef);
51 DECLARE_ACKNOWLEDGER (bar_line);
54 void
55 Key_engraver::finalize ()
59 Key_engraver::Key_engraver ()
61 key_event_ = 0;
62 item_ = 0;
63 cancellation_ = 0;
67 void
68 Key_engraver::create_key (bool is_default)
70 if (!item_)
72 item_ = make_item ("KeySignature",
73 key_event_ ? key_event_->self_scm () : SCM_EOL);
75 item_->set_property ("c0-position",
76 get_property ("middleCPosition"));
78 SCM last = get_property ("lastKeySignature");
79 SCM key = get_property ("keySignature");
80 bool extranatural = to_boolean (get_property ("extraNatural"));
82 if ((to_boolean (get_property ("printKeyCancellation"))
83 || key == SCM_EOL)
84 && !scm_is_eq (last, key))
86 SCM restore = SCM_EOL;
87 SCM *tail = &restore;
88 for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
90 SCM new_alter_pair = scm_assoc (scm_caar (s), key);
91 Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
92 if (new_alter_pair == SCM_BOOL_F
93 || (extranatural
94 && (ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter)*old_alter
95 < Rational (0)))
97 *tail = scm_cons (scm_car (s), *tail);
98 tail = SCM_CDRLOC (*tail);
102 if (scm_is_pair (restore))
104 cancellation_ = make_item ("KeyCancellation",
105 key_event_
106 ? key_event_->self_scm () : SCM_EOL);
108 cancellation_->set_property ("alteration-alist", scm_reverse (restore));
109 cancellation_->set_property ("c0-position",
110 get_property ("middleCPosition"));
114 item_->set_property ("alteration-alist", scm_reverse (key));
117 if (!is_default)
119 SCM visibility = get_property ("explicitKeySignatureVisibility");
120 item_->set_property ("break-visibility", visibility);
124 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
125 void
126 Key_engraver::listen_key_change (Stream_event *ev)
128 /* do this only once, just to be on the safe side. */
129 if (ASSIGN_EVENT_ONCE (key_event_, ev))
130 read_event (key_event_);
133 void
134 Key_engraver::acknowledge_clef (Grob_info /* info */)
136 SCM c = get_property ("createKeyOnClefChange");
137 if (to_boolean (c))
138 create_key (false);
141 void
142 Key_engraver::acknowledge_bar_line (Grob_info /* info */)
144 if (scm_is_pair (get_property ("keySignature")))
145 create_key (true);
148 void
149 Key_engraver::process_music ()
151 if (key_event_
152 || get_property ("lastKeySignature") != get_property ("keySignature"))
153 create_key (false);
156 void
157 Key_engraver::stop_translation_timestep ()
159 item_ = 0;
160 context ()->set_property ("lastKeySignature", get_property ("keySignature"));
161 cancellation_ = 0;
162 key_event_ = 0;
165 void
166 Key_engraver::read_event (Stream_event const *r)
168 SCM p = r->get_property ("pitch-alist");
169 if (!scm_is_pair (p))
170 return;
172 SCM accs = SCM_EOL;
174 SCM alist = scm_list_copy (p);
175 SCM order = get_property ("keyAlterationOrder");
176 for (SCM s = order;
177 scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
179 SCM head = scm_member (scm_car (s), alist);
181 if (scm_is_pair (head))
183 accs = scm_cons (scm_car (head), accs);
184 alist = scm_delete_x (scm_car (head), alist);
188 if (scm_is_pair (alist))
190 bool warn = false;
191 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
192 if (ly_scm2rational (scm_cdar (s)))
194 warn = true;
195 accs = scm_cons (scm_car (s), accs);
198 if (warn)
199 r->origin ()->warning ("No ordering for key signature alterations");
202 context ()->set_property ("keySignature", scm_reverse (accs));
203 context ()->set_property ("tonic",
204 r->get_property ("tonic"));
207 void
208 Key_engraver::initialize ()
210 context ()->set_property ("keySignature", SCM_EOL);
211 context ()->set_property ("lastKeySignature", SCM_EOL);
213 Pitch p (0, 0, 0);
214 context ()->set_property ("tonic", p.smobbed_copy ());
217 ADD_ACKNOWLEDGER (Key_engraver, clef);
218 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
220 ADD_TRANSLATOR (Key_engraver,
221 /* doc */
222 "Engrave a key signature.",
224 /* create */
225 "KeyCancellation "
226 "KeySignature ",
228 /* read */
229 "createKeyOnClefChange "
230 "explicitKeySignatureVisibility "
231 "extraNatural "
232 "keyAlterationOrder "
233 "keySignature "
234 "lastKeySignature "
235 "printKeyCancellation ",
237 /* write */
238 "keySignature "
239 "lastKeySignature "
240 "tonic "