]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
Fix white space
[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       item_->set_property ("c0-position",
76                            get_property ("middleCPosition"));
77
78       SCM last = get_property ("lastKeySignature");
79       SCM key = get_property ("keySignature");
80
81       if ((to_boolean (get_property ("printKeyCancellation"))
82            || key == SCM_EOL)
83           && !scm_is_eq (last, key))
84         {
85           SCM restore = SCM_EOL;
86           SCM *tail = &restore;
87           for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
88             {
89               SCM new_alter_pair = scm_assoc (scm_caar (s), key);
90               Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
91               if (new_alter_pair == SCM_BOOL_F
92                   || ((ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter) * old_alter
93                       < Rational (0)))
94                 {
95                   *tail = scm_cons (scm_car (s), *tail);
96                   tail = SCM_CDRLOC (*tail);
97                 }
98             }
99
100           if (scm_is_pair (restore))
101             {
102               cancellation_ = make_item ("KeyCancellation",
103                                          key_event_
104                                          ? key_event_->self_scm () : SCM_EOL);
105               
106               cancellation_->set_property ("alteration-alist", scm_reverse (restore));
107               cancellation_->set_property ("c0-position",
108                                            get_property ("middleCPosition"));
109             }
110         }
111
112       item_->set_property ("alteration-alist", scm_reverse (key));
113     }
114
115   if (!is_default)
116     {
117       SCM visibility = get_property ("explicitKeySignatureVisibility");
118       item_->set_property ("break-visibility", visibility);
119     }
120 }
121
122 IMPLEMENT_TRANSLATOR_LISTENER (Key_engraver, key_change);
123 void
124 Key_engraver::listen_key_change (Stream_event *ev)
125 {
126   /* do this only once, just to be on the safe side.  */
127   if (ASSIGN_EVENT_ONCE (key_event_, ev))
128     read_event (key_event_);
129 }
130
131 void
132 Key_engraver::acknowledge_clef (Grob_info /* info */)
133 {
134   SCM c = get_property ("createKeyOnClefChange");
135   if (to_boolean (c))
136     create_key (false);
137 }
138
139 void
140 Key_engraver::acknowledge_bar_line (Grob_info /* info */)
141 {
142   if (scm_is_pair (get_property ("keySignature")))
143     create_key (true);
144 }
145
146 void
147 Key_engraver::process_music ()
148 {
149   if (key_event_
150       || get_property ("lastKeySignature") != get_property ("keySignature"))
151     create_key (false);
152 }
153
154 void
155 Key_engraver::stop_translation_timestep ()
156 {
157   item_ = 0;
158   context ()->set_property ("lastKeySignature", get_property ("keySignature"));
159   cancellation_ = 0;
160   key_event_ = 0;
161 }
162
163 void
164 Key_engraver::read_event (Stream_event const *r)
165 {
166   SCM p = r->get_property ("pitch-alist");
167   if (!scm_is_pair (p))
168     return;
169
170   SCM accs = SCM_EOL;
171
172   SCM alist = scm_list_copy (p);
173   SCM order = get_property ("keyAlterationOrder");
174   for (SCM s = order;
175        scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
176     {
177       SCM head = scm_member (scm_car (s), alist);
178       
179       if (scm_is_pair (head))
180         {
181           accs = scm_cons (scm_car (head), accs);
182           alist = scm_delete_x (scm_car (head), alist);
183         }
184     }
185
186   if (scm_is_pair (alist))
187     {
188       bool warn = false;
189       for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
190         if (ly_scm2rational (scm_cdar (s)))
191           {
192             warn = true;
193             accs = scm_cons (scm_car (s), accs);
194           }
195
196       if (warn)
197         r->origin ()->warning ("Incomplete keyAlterationOrder for key signature");
198     }
199   
200   context ()->set_property ("keySignature", scm_reverse (accs));
201   context ()->set_property ("tonic",
202                             r->get_property ("tonic"));
203 }
204
205 void
206 Key_engraver::initialize ()
207 {
208   context ()->set_property ("keySignature", SCM_EOL);
209   context ()->set_property ("lastKeySignature", SCM_EOL);
210
211   Pitch p (0, 0, 0);
212   context ()->set_property ("tonic", p.smobbed_copy ());
213 }
214
215 ADD_ACKNOWLEDGER (Key_engraver, clef);
216 ADD_ACKNOWLEDGER (Key_engraver, bar_line);
217
218 ADD_TRANSLATOR (Key_engraver,
219                 /* doc */
220                 "Engrave a key signature.",
221
222                 /* create */
223                 "KeyCancellation "
224                 "KeySignature ",
225                 
226                 /* read */
227                 "createKeyOnClefChange "
228                 "explicitKeySignatureVisibility "
229                 "extraNatural "
230                 "keyAlterationOrder "
231                 "keySignature "
232                 "lastKeySignature "
233                 "printKeyCancellation ",
234                 
235                 /* write */
236                 "keySignature "
237                 "lastKeySignature "
238                 "tonic "
239                 );