]> git.donarmstrong.com Git - lilypond.git/blob - lily/grace-engraver.cc
Run grand replace for 2015.
[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   DECLARE_LISTENER (grace_change);
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 IMPLEMENT_LISTENER (Grace_engraver, grace_change);
52 void
53 Grace_engraver::grace_change (SCM)
54 {
55   consider_change_grace_settings ();
56 }
57
58 // if we are in grace time already on initialization, it is unlikely
59 // that we'll receive a GraceChange event from the grace iterator yet,
60 // so we want to start into grace mode anyway.  The downside is that
61 // this will get us confused when given something like
62 //
63 // \new Voice { \oneVoice \grace { c''8 8 } g'1 }
64 //
65 // where \grace executes its actions already before \oneVoice, causing
66 // different stem directions.
67
68 void
69 Grace_engraver::start_translation_timestep ()
70 {
71   // Only on startup
72   if (last_moment_ == Rational (-1))
73     consider_change_grace_settings ();
74 }
75
76 // If the grace iterator has moved off to some other context, we might
77 // not get to see the ChangeContext event.  In that case, we still
78 // want to change into or out of grace mode settings as appropriate
79
80 void
81 Grace_engraver::process_music ()
82 {
83   // We may have lost connection to the iterator in which case we
84   // still need to call consider_change_grace_settings in particular
85   // in order to get out of grace mode again
86   if (last_moment_ != now_mom ())
87     consider_change_grace_settings ();
88 }
89
90 void
91 Grace_engraver::consider_change_grace_settings ()
92 {
93   Moment now = now_mom ();
94
95   if (!now.grace_part_)
96     {
97       for (SCM s = grace_settings_; scm_is_pair (s); s = scm_cdr (s))
98         {
99           SCM elt = scm_car (s);
100           SCM context = scm_car (elt);
101           SCM grob = scm_cadr (elt);
102           SCM cell = scm_cddr (elt);
103
104           Grob_property_info (Context::unsmob (context), grob).matched_pop (cell);
105         }
106       grace_settings_ = SCM_EOL;
107     }
108   else if (!last_moment_.grace_part_)
109     {
110       SCM settings = get_property ("graceSettings");
111
112       grace_settings_ = SCM_EOL;
113       for (SCM s = settings; scm_is_pair (s); s = scm_cdr (s))
114         {
115           SCM entry = scm_car (s);
116           SCM context_name = scm_car (entry);
117           SCM grob = scm_cadr (entry);
118           SCM sym = scm_caddr (entry);
119           SCM val = scm_cadr (scm_cddr (entry));
120
121           if (!scm_is_pair (sym))
122             sym = scm_list_1 (sym);
123
124           Context *c = context ();
125           while (c && !c->is_alias (context_name))
126             c = c->get_parent_context ();
127
128           if (c)
129             {
130               SCM cell = Grob_property_info (c, grob).push (sym, val);
131               grace_settings_
132                 = scm_cons (scm_cons2 (c->self_scm (), grob, cell), grace_settings_);
133             }
134           else
135             programming_error ("cannot find context from graceSettings: "
136                                + ly_symbol2string (context_name));
137         }
138     }
139   if (last_moment_ == Rational (-1))
140     {
141       Dispatcher *d = context ()->event_source ();
142       d->add_listener (GET_LISTENER (grace_change), ly_symbol2scm ("GraceChange"));
143     }
144   last_moment_ = now;
145 }
146
147 void
148 Grace_engraver::finalize ()
149 {
150   if (last_moment_ != Rational (-1))
151     {
152       Dispatcher *d = context ()->event_source ();
153       d->remove_listener (GET_LISTENER (grace_change), ly_symbol2scm ("GraceChange"));
154     }
155 }
156
157 void
158 Grace_engraver::derived_mark () const
159 {
160   scm_gc_mark (grace_settings_);
161   Engraver::derived_mark ();
162 }
163
164 ADD_TRANSLATOR (Grace_engraver,
165                 /* doc */
166                 "Set font size and other properties for grace notes.",
167
168                 /* create */
169                 "",
170
171                 /* read */
172                 "graceSettings ",
173
174                 /* write */
175                 ""
176                );