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