]> git.donarmstrong.com Git - lilypond.git/blob - lily/grace-engraver.cc
Doc-es: various updates.
[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 (Context *c)
45   : Engraver (c)
46 {
47   grace_settings_ = SCM_EOL;
48   last_moment_ = Moment (Rational (-1, 1));
49 }
50
51 // The iterator should usually come before process_music
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 (unsmob<Context> (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 = find_context_above (context (), context_name);
125           if (c)
126             {
127               SCM cell = Grob_property_info (c, grob).push (sym, val);
128               grace_settings_
129                 = scm_cons (scm_cons2 (c->self_scm (), grob, cell), grace_settings_);
130             }
131           else
132             programming_error ("cannot find context from graceSettings: "
133                                + ly_symbol2string (context_name));
134         }
135     }
136   if (last_moment_ == Rational (-1))
137     {
138       Dispatcher *d = context ()->event_source ();
139       d->add_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
140     }
141   last_moment_ = now;
142 }
143
144 void
145 Grace_engraver::finalize ()
146 {
147   if (last_moment_ != Rational (-1))
148     {
149       Dispatcher *d = context ()->event_source ();
150       d->remove_listener (GET_LISTENER (Grace_engraver, grace_change), ly_symbol2scm ("GraceChange"));
151     }
152 }
153
154 void
155 Grace_engraver::derived_mark () const
156 {
157   scm_gc_mark (grace_settings_);
158   Engraver::derived_mark ();
159 }
160
161 void
162 Grace_engraver::boot ()
163 {
164
165 }
166
167 ADD_TRANSLATOR (Grace_engraver,
168                 /* doc */
169                 "Set font size and other properties for grace notes.",
170
171                 /* create */
172                 "",
173
174                 /* read */
175                 "graceSettings ",
176
177                 /* write */
178                 ""
179                );