2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 2004--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "engraver.hh"
22 #include "dispatcher.hh"
23 #include "listener.hh"
25 #include "grob-properties.hh"
26 #include "stream-event.hh"
27 #include "translator.icc"
29 class Grace_engraver : public Engraver
33 void consider_change_grace_settings ();
35 virtual void derived_mark () const;
36 virtual void process_music ();
37 virtual void start_translation_timestep ();
38 virtual void finalize ();
40 TRANSLATOR_DECLARATIONS (Grace_engraver);
41 void grace_change (SCM);
44 Grace_engraver::Grace_engraver ()
46 grace_settings_ = SCM_EOL;
47 last_moment_ = Moment (Rational (-1, 1));
50 // The iterator should usually come before process_music
52 Grace_engraver::grace_change (SCM)
54 consider_change_grace_settings ();
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
62 // \new Voice { \oneVoice \grace { c''8 8 } g'1 }
64 // where \grace executes its actions already before \oneVoice, causing
65 // different stem directions.
68 Grace_engraver::start_translation_timestep ()
71 if (last_moment_ == Rational (-1))
72 consider_change_grace_settings ();
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
80 Grace_engraver::process_music ()
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 ();
90 Grace_engraver::consider_change_grace_settings ()
92 Moment now = now_mom ();
96 for (SCM s = grace_settings_; scm_is_pair (s); s = scm_cdr (s))
98 SCM elt = scm_car (s);
99 SCM context = scm_car (elt);
100 SCM grob = scm_cadr (elt);
101 SCM cell = scm_cddr (elt);
103 Grob_property_info (unsmob<Context> (context), grob).matched_pop (cell);
105 grace_settings_ = SCM_EOL;
107 else if (!last_moment_.grace_part_)
109 SCM settings = get_property ("graceSettings");
111 grace_settings_ = SCM_EOL;
112 for (SCM s = settings; scm_is_pair (s); s = scm_cdr (s))
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));
120 if (!scm_is_pair (sym))
121 sym = scm_list_1 (sym);
123 Context *c = find_context_above (context (), context_name);
126 SCM cell = Grob_property_info (c, grob).push (sym, val);
128 = scm_cons (scm_cons2 (c->self_scm (), grob, cell), grace_settings_);
131 programming_error ("cannot find context from graceSettings: "
132 + ly_symbol2string (context_name));
135 if (last_moment_ == Rational (-1))
137 Dispatcher *d = context ()->event_source ();
138 d->add_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
144 Grace_engraver::finalize ()
146 if (last_moment_ != Rational (-1))
148 Dispatcher *d = context ()->event_source ();
149 d->remove_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
154 Grace_engraver::derived_mark () const
156 scm_gc_mark (grace_settings_);
157 Engraver::derived_mark ();
160 ADD_TRANSLATOR (Grace_engraver,
162 "Set font size and other properties for grace notes.",