]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
apply Julian's patch to fix install-info warnings
[lilypond.git] / lily / key-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
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"
29
30 #include "translator.icc"
31
32 class Key_engraver : public Engraver
33 {
34   void create_key (bool);
35   void read_event (Stream_event const *r);
36
37   Stream_event *key_event_;
38   Item *item_;
39   Item *cancellation_;
40 public:
41   TRANSLATOR_DECLARATIONS (Key_engraver);
42
43 protected:
44   virtual void initialize ();
45   virtual void finalize ();
46   void stop_translation_timestep ();
47   void process_music ();
48
49   DECLARE_TRANSLATOR_LISTENER (key_change);
50   DECLARE_ACKNOWLEDGER (clef);
51   DECLARE_ACKNOWLEDGER (bar_line);
52 };
53
54 void
55 Key_engraver::finalize ()
56 {
57 }
58
59 Key_engraver::Key_engraver ()
60 {
61   key_event_ = 0;
62   item_ = 0;
63   cancellation_ = 0;
64 }
65
66
67 void
68 Key_engraver::create_key (bool is_default)
69 {
70   if (!item_)
71     {
72       item_ = make_item ("KeySignature",
73                          key_event_ ? key_event_->self_scm () : SCM_EOL);
74
75       /* Use middleCClefPosition rather than middleCPosition, because cue
76        * notes with a different clef will modify middleCPosition. The 
77        * Key signature, however, should still be printed at the original
78        * position. */
79       item_->set_property ("c0-position",
80                            get_property ("middleCClefPosition"));
81
82       SCM last = get_property ("lastKeySignature");
83       SCM key = get_property ("keySignature");
84
85       if ((to_boolean (get_property ("printKeyCancellation"))
86            || key == SCM_EOL)
87           && !scm_is_eq (last, key))
88         {
89           SCM restore = SCM_EOL;
90           SCM *tail = &restore;
91           for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
92             {
93               SCM new_alter_pair = scm_assoc (scm_caar (s), key);
94               Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
95               if (new_alter_pair == SCM_BOOL_F
96                   || ((ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter) * old_alter
97                       < Rational (0)))
98                 {
99                   *tail = scm_cons (scm_car (s), *tail);
100                   tail = SCM_CDRLOC (*tail);
101                 }
102             }
103
104           if (scm_is_pair (restore))
105             {
106               cancellation_ = make_item ("KeyCancellation",
107                                          key_event_
108                                          ? key_event_->self_scm () : SCM_EOL);
109               
110               cancellation_->set_property ("alteration-alist", scm_reverse (restore));
111               cancellation_->set_property ("c0-position",
112                                            get_property ("middleCPosition"));
113             }
114         }
115
116       item_->set_property ("alteration-alist", scm_reverse (key));
117     }
118
119   if (!is_default)
120     {
121       SCM visibility = get_property ("explicitKeySignatureVisibility");
122       item_->set_property ("break-visibility", visibility);
123     }
124 }
125
126 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
127 void
128 Key_engraver::listen_key_change (Stream_event *ev)
129 {
130   /* do this only once, just to be on the safe side.  */
131   if (ASSIGN_EVENT_ONCE (key_event_, ev))
132     read_event (key_event_);
133 }
134
135 void
136 Key_engraver::acknowledge_clef (Grob_info /* info */)
137 {
138   SCM c = get_property ("createKeyOnClefChange");
139   if (to_boolean (c))
140     create_key (false);
141 }
142
143 void
144 Key_engraver::acknowledge_bar_line (Grob_info /* info */)
145 {
146   if (scm_is_pair (get_property ("keySignature")))
147     create_key (true);
148 }
149
150 void
151 Key_engraver::process_music ()
152 {
153   if (key_event_
154       || get_property ("lastKeySignature") != get_property ("keySignature"))
155     create_key (false);
156 }
157
158 void
159 Key_engraver::stop_translation_timestep ()
160 {
161   item_ = 0;
162   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
163   cancellation_ = 0;
164   key_event_ = 0;
165 }
166
167 void
168 Key_engraver::read_event (Stream_event const *r)
169 {
170   SCM p = r->get_property ("pitch-alist");
171   if (!scm_is_pair (p))
172     return;
173
174   SCM accs = SCM_EOL;
175
176   SCM alist = scm_list_copy (p);
177   SCM order = get_property ("keyAlterationOrder");
178   for (SCM s = order;
179        scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
180     {
181       SCM head = scm_member (scm_car (s), alist);
182       
183       if (scm_is_pair (head))
184         {
185           accs = scm_cons (scm_car (head), accs);
186           alist = scm_delete_x (scm_car (head), alist);
187         }
188     }
189
190   if (scm_is_pair (alist))
191     {
192       bool warn = false;
193       for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
194         if (ly_scm2rational (scm_cdar (s)))
195           {
196             warn = true;
197             accs = scm_cons (scm_car (s), accs);
198           }
199
200       if (warn)
201         r->origin ()->warning ("Incomplete keyAlterationOrder for key signature");
202     }
203   
204   context ()->set_property ("keySignature", scm_reverse (accs));
205   context ()->set_property ("tonic",
206                             r->get_property ("tonic"));
207 }
208
209 void
210 Key_engraver::initialize ()
211 {
212   context ()->set_property ("keySignature", SCM_EOL);
213   context ()->set_property ("lastKeySignature", SCM_EOL);
214
215   Pitch p (0, 0, 0);
216   context ()->set_property ("tonic", p.smobbed_copy ());
217 }
218
219 ADD_ACKNOWLEDGER (Key_engraver, clef);
220 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
221
222 ADD_TRANSLATOR (Key_engraver,
223                 /* doc */
224                 "Engrave a key signature.",
225
226                 /* create */
227                 "KeyCancellation "
228                 "KeySignature ",
229                 
230                 /* read */
231                 "createKeyOnClefChange "
232                 "explicitKeySignatureVisibility "
233                 "extraNatural "
234                 "keyAlterationOrder "
235                 "keySignature "
236                 "lastKeySignature "
237                 "printKeyCancellation "
238                 "middleCClefPosition ",
239                 
240                 /* write */
241                 "keySignature "
242                 "lastKeySignature "
243                 "tonic "
244                 );