]> git.donarmstrong.com Git - lilypond.git/blob - lily/grace-engraver.cc
Fix \lyricsto Staff = "sop" syntax
[lilypond.git] / lily / grace-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2004--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 "engraver.hh"
21 #include "context.hh"
22 #include "dispatcher.hh"
23 #include "listener.hh"
24 #include "warn.hh"
25 #include "grob-properties.hh"
26 #include "stream-event.hh"
27 #include "translator.icc"
28
29 class Grace_engraver : public Engraver
30 {
31   Moment last_moment_;
32   SCM grace_settings_;
33   void consider_change_grace_settings ();
34 protected:
35   virtual void derived_mark () const;
36   virtual void process_music ();
37   virtual void start_translation_timestep ();
38   virtual void finalize ();
39
40   TRANSLATOR_DECLARATIONS (Grace_engraver);
41   void grace_change (SCM);
42 };
43
44 Grace_engraver::Grace_engraver ()
45 {
46   grace_settings_ = SCM_EOL;
47   last_moment_ = Moment (Rational (-1, 1));
48 }
49
50 // The iterator should usually come before process_music
51 void
52 Grace_engraver::grace_change (SCM)
53 {
54   consider_change_grace_settings ();
55 }
56
57 // if we are in grace time already on initialization, it is unlikely
58 // that we'll receive a GraceChange event from the grace iterator yet,
59 // so we want to start into grace mode anyway.  The downside is that
60 // this will get us confused when given something like
61 //
62 // \new Voice { \oneVoice \grace { c''8 8 } g'1 }
63 //
64 // where \grace executes its actions already before \oneVoice, causing
65 // different stem directions.
66
67 void
68 Grace_engraver::start_translation_timestep ()
69 {
70   // Only on startup
71   if (last_moment_ == Rational (-1))
72     consider_change_grace_settings ();
73 }
74
75 // If the grace iterator has moved off to some other context, we might
76 // not get to see the ChangeContext event.  In that case, we still
77 // want to change into or out of grace mode settings as appropriate
78
79 void
80 Grace_engraver::process_music ()
81 {
82   // We may have lost connection to the iterator in which case we
83   // still need to call consider_change_grace_settings in particular
84   // in order to get out of grace mode again
85   if (last_moment_ != now_mom ())
86     consider_change_grace_settings ();
87 }
88
89 void
90 Grace_engraver::consider_change_grace_settings ()
91 {
92   Moment now = now_mom ();
93
94   if (!now.grace_part_)
95     {
96       for (SCM s = grace_settings_; scm_is_pair (s); s = scm_cdr (s))
97         {
98           SCM elt = scm_car (s);
99           SCM context = scm_car (elt);
100           SCM grob = scm_cadr (elt);
101           SCM cell = scm_cddr (elt);
102
103           Grob_property_info (unsmob<Context> (context), grob).matched_pop (cell);
104         }
105       grace_settings_ = SCM_EOL;
106     }
107   else if (!last_moment_.grace_part_)
108     {
109       SCM settings = get_property ("graceSettings");
110
111       grace_settings_ = SCM_EOL;
112       for (SCM s = settings; scm_is_pair (s); s = scm_cdr (s))
113         {
114           SCM entry = scm_car (s);
115           SCM context_name = scm_car (entry);
116           SCM grob = scm_cadr (entry);
117           SCM sym = scm_caddr (entry);
118           SCM val = scm_cadr (scm_cddr (entry));
119
120           if (!scm_is_pair (sym))
121             sym = scm_list_1 (sym);
122
123           Context *c = find_context_above (context (), context_name);
124           if (c)
125             {
126               SCM cell = Grob_property_info (c, grob).push (sym, val);
127               grace_settings_
128                 = scm_cons (scm_cons2 (c->self_scm (), grob, cell), grace_settings_);
129             }
130           else
131             programming_error ("cannot find context from graceSettings: "
132                                + ly_symbol2string (context_name));
133         }
134     }
135   if (last_moment_ == Rational (-1))
136     {
137       Dispatcher *d = context ()->event_source ();
138       d->add_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
139     }
140   last_moment_ = now;
141 }
142
143 void
144 Grace_engraver::finalize ()
145 {
146   if (last_moment_ != Rational (-1))
147     {
148       Dispatcher *d = context ()->event_source ();
149       d->remove_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
150     }
151 }
152
153 void
154 Grace_engraver::derived_mark () const
155 {
156   scm_gc_mark (grace_settings_);
157   Engraver::derived_mark ();
158 }
159
160 void
161 Grace_engraver::boot ()
162 {
163
164 }
165
166 ADD_TRANSLATOR (Grace_engraver,
167                 /* doc */
168                 "Set font size and other properties for grace notes.",
169
170                 /* create */
171                 "",
172
173                 /* read */
174                 "graceSettings ",
175
176                 /* write */
177                 ""
178                );