]> git.donarmstrong.com Git - lilypond.git/blob - lily/key-engraver.cc
Bring dev/johngourlay/issue-4751 up to date with respect to staging.
[lilypond.git] / lily / key-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "clef.hh"
21 #include "context.hh"
22 #include "engraver.hh"
23 #include "international.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   void listen_key_change (Stream_event *);
50   void acknowledge_clef (Grob_info);
51   void acknowledge_bar_line (Grob_info);
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 ("lastKeyAlterations");
82       SCM key = get_property ("keyAlterations");
83
84       if ((to_boolean (get_property ("printKeyCancellation"))
85            || scm_is_null (key))
86           && !scm_is_eq (last, key))
87         {
88           SCM restore = SCM_EOL;
89           for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
90             {
91               SCM new_alter_pair = scm_assoc (scm_caar (s), key);
92               Rational old_alter = robust_scm2rational (scm_cdar (s), 0);
93               if (scm_is_false (new_alter_pair)
94                   || ((ly_scm2rational (scm_cdr (new_alter_pair)) - old_alter) * old_alter
95                       < Rational (0)))
96                 {
97                   restore = scm_cons (scm_car (s), restore);
98                 }
99             }
100
101           if (scm_is_pair (restore))
102             {
103               cancellation_ = make_item ("KeyCancellation",
104                                          key_event_
105                                          ? key_event_->self_scm () : SCM_EOL);
106
107               cancellation_->set_property ("alteration-alist", restore);
108               cancellation_->set_property ("c0-position",
109                                            get_property ("middleCClefPosition"));
110             }
111         }
112
113       item_->set_property ("alteration-alist", scm_reverse (key));
114     }
115
116   if (!is_default)
117     {
118       SCM visibility = get_property ("explicitKeySignatureVisibility");
119       item_->set_property ("break-visibility", visibility);
120     }
121 }
122
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   create_key (true);
143 }
144
145 void
146 Key_engraver::process_music ()
147 {
148   if (key_event_
149       || get_property ("lastKeyAlterations") != get_property ("keyAlterations"))
150     create_key (false);
151 }
152
153 void
154 Key_engraver::stop_translation_timestep ()
155 {
156   item_ = 0;
157   context ()->set_property ("lastKeyAlterations", get_property ("keyAlterations"));
158   cancellation_ = 0;
159   key_event_ = 0;
160 }
161
162 void
163 Key_engraver::read_event (Stream_event const *r)
164 {
165   SCM p = r->get_property ("pitch-alist");
166   if (!scm_is_pair (p))
167     return;
168
169   SCM accs = SCM_EOL;
170
171   SCM alist = scm_list_copy (p);
172   SCM order = get_property ("keyAlterationOrder");
173   for (SCM s = order;
174        scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
175     {
176       SCM head = scm_member (scm_car (s), alist);
177
178       if (scm_is_pair (head))
179         {
180           accs = scm_cons (scm_car (head), accs);
181           alist = scm_delete_x (scm_car (head), alist);
182         }
183     }
184
185   if (scm_is_pair (alist))
186     {
187       bool warn = false;
188       for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
189         if (ly_scm2rational (scm_cdar (s)))
190           {
191             warn = true;
192             accs = scm_cons (scm_car (s), accs);
193           }
194
195       if (warn)
196         r->origin ()->warning (_ ("Incomplete keyAlterationOrder for key signature"));
197     }
198
199   context ()->set_property ("keyAlterations", scm_reverse_x (accs, SCM_EOL));
200   context ()->set_property ("tonic",
201                             r->get_property ("tonic"));
202 }
203
204 void
205 Key_engraver::initialize ()
206 {
207   context ()->set_property ("keyAlterations", SCM_EOL);
208   context ()->set_property ("lastKeyAlterations", SCM_EOL);
209
210   Pitch p;
211   context ()->set_property ("tonic", p.smobbed_copy ());
212 }
213
214
215 void
216 Key_engraver::boot ()
217 {
218   ADD_LISTENER (Key_engraver, key_change);
219   ADD_ACKNOWLEDGER (Key_engraver, clef);
220   ADD_ACKNOWLEDGER (Key_engraver, bar_line);
221 }
222
223 ADD_TRANSLATOR (Key_engraver,
224                 /* doc */
225                 "Engrave a key signature.",
226
227                 /* create */
228                 "KeyCancellation "
229                 "KeySignature ",
230
231                 /* read */
232                 "createKeyOnClefChange "
233                 "explicitKeySignatureVisibility "
234                 "extraNatural "
235                 "keyAlterationOrder "
236                 "keyAlterations "
237                 "lastKeyAlterations "
238                 "printKeyCancellation "
239                 "middleCClefPosition ",
240
241                 /* write */
242                 "keyAlterations "
243                 "lastKeyAlterations "
244                 "tonic "
245                );