1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3 
4   Copyright (C) 1997--2020 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   void initialize () override;
45   void finalize () override;
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
finalize()55 Key_engraver::finalize ()
56 {
57 }
58 
Key_engraver(Context * c)59 Key_engraver::Key_engraver (Context *c)
60   : Engraver (c)
61 {
62   key_event_ = 0;
63   item_ = 0;
64   cancellation_ = 0;
65 }
66 
67 void
create_key(bool is_default)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       set_property (item_, "c0-position",
80                     get_property (this, "middleCClefPosition"));
81 
82       SCM last = get_property (this, "lastKeyAlterations");
83       SCM key = get_property (this, "keyAlterations");
84 
85       if ((from_scm<bool> (get_property (this, "printKeyCancellation"))
86            || scm_is_null (key))
87           && !scm_is_eq (last, key))
88         {
89           SCM restore = SCM_EOL;
90           for (SCM s = last; scm_is_pair (s); s = scm_cdr (s))
91             {
92               SCM new_alter_pair = ly_assoc (scm_caar (s), key);
93               Rational old_alter = from_scm<Rational> (scm_cdar (s), 0);
94               if (scm_is_false (new_alter_pair)
95                   || ((from_scm<Rational> (scm_cdr (new_alter_pair)) - old_alter) * old_alter
96                       < Rational (0)))
97                 {
98                   restore = scm_cons (scm_car (s), restore);
99                 }
100             }
101 
102           if (scm_is_pair (restore))
103             {
104               cancellation_ = make_item ("KeyCancellation",
105                                          key_event_
106                                          ? key_event_->self_scm () : SCM_EOL);
107 
108               set_property (cancellation_, "alteration-alist", restore);
109               set_property (cancellation_, "c0-position",
110                             get_property (this, "middleCClefPosition"));
111             }
112         }
113 
114       set_property (item_, "alteration-alist", scm_reverse (key));
115     }
116 
117   if (!is_default)
118     {
119       SCM visibility = get_property (this, "explicitKeySignatureVisibility");
120       set_property (item_, "break-visibility", visibility);
121       set_property (item_, "non-default", SCM_BOOL_T);
122     }
123 }
124 
125 void
listen_key_change(Stream_event * ev)126 Key_engraver::listen_key_change (Stream_event *ev)
127 {
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_);
131 }
132 
133 void
acknowledge_clef(Grob_info)134 Key_engraver::acknowledge_clef (Grob_info /* info */)
135 {
136   SCM c = get_property (this, "createKeyOnClefChange");
137   if (from_scm<bool> (c))
138     create_key (false);
139 }
140 
141 void
acknowledge_bar_line(Grob_info)142 Key_engraver::acknowledge_bar_line (Grob_info /* info */)
143 {
144   create_key (true);
145 }
146 
147 void
process_music()148 Key_engraver::process_music ()
149 {
150   if (key_event_
151       || !scm_is_eq (get_property (this, "lastKeyAlterations"),
152                      get_property (this, "keyAlterations")))
153     create_key (false);
154 }
155 
156 void
stop_translation_timestep()157 Key_engraver::stop_translation_timestep ()
158 {
159   item_ = 0;
160   set_property (context (), "lastKeyAlterations", get_property (this, "keyAlterations"));
161   cancellation_ = 0;
162   key_event_ = 0;
163 }
164 
165 void
read_event(Stream_event const * r)166 Key_engraver::read_event (Stream_event const *r)
167 {
168   SCM p = get_property (r, "pitch-alist");
169   if (!scm_is_pair (p))
170     return;
171 
172   SCM accs = SCM_EOL;
173 
174   SCM alist = scm_list_copy (p);
175   SCM order = get_property (this, "keyAlterationOrder");
176   for (SCM s = order;
177        scm_is_pair (s) && scm_is_pair (alist); s = scm_cdr (s))
178     {
179       SCM head = scm_member (scm_car (s), alist);
180 
181       if (scm_is_pair (head))
182         {
183           accs = scm_cons (scm_car (head), accs);
184           alist = scm_delete_x (scm_car (head), alist);
185         }
186     }
187 
188   if (scm_is_pair (alist))
189     {
190       bool warn = false;
191       for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
192         if (from_scm<Rational> (scm_cdar (s)))
193           {
194             warn = true;
195             accs = scm_cons (scm_car (s), accs);
196           }
197 
198       if (warn)
199         r->warning (_ ("Incomplete keyAlterationOrder for key signature"));
200     }
201 
202   set_property (context (), "keyAlterations", scm_reverse_x (accs, SCM_EOL));
203   set_property (context (), "tonic",
204                 get_property (r, "tonic"));
205 }
206 
207 void
initialize()208 Key_engraver::initialize ()
209 {
210   set_property (context (), "keyAlterations", SCM_EOL);
211   set_property (context (), "lastKeyAlterations", SCM_EOL);
212 
213   Pitch p;
214   set_property (context (), "tonic", p.smobbed_copy ());
215 }
216 
217 void
boot()218 Key_engraver::boot ()
219 {
220   ADD_LISTENER (Key_engraver, key_change);
221   ADD_ACKNOWLEDGER (Key_engraver, clef);
222   ADD_ACKNOWLEDGER (Key_engraver, bar_line);
223 }
224 
225 ADD_TRANSLATOR (Key_engraver,
226                 /* doc */
227                 "Engrave a key signature.",
228 
229                 /* create */
230                 "KeyCancellation "
231                 "KeySignature ",
232 
233                 /* read */
234                 "createKeyOnClefChange "
235                 "explicitKeySignatureVisibility "
236                 "extraNatural "
237                 "keyAlterationOrder "
238                 "keyAlterations "
239                 "lastKeyAlterations "
240                 "printKeyCancellation "
241                 "middleCClefPosition ",
242 
243                 /* write */
244                 "keyAlterations "
245                 "lastKeyAlterations "
246                 "tonic "
247                );
248