]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond into...
[lilypond.git] / lily / stem-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2012 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
22 #include "context.hh"
23 #include "directional-element-interface.hh"
24 #include "duration.hh"
25 #include "international.hh"
26 #include "item.hh"
27 #include "misc.hh"
28 #include "rhythmic-head.hh"
29 #include "script-interface.hh"
30 #include "staff-symbol-referencer.hh"
31 #include "stem-tremolo.hh"
32 #include "stem.hh"
33 #include "stream-event.hh"
34
35 #include "translator.icc"
36
37 class Stem_engraver : public Engraver
38 {
39   Grob *stem_;
40   Grob *tremolo_;
41   vector <Grob *> maybe_flags_;
42   Stream_event *rhythmic_ev_;
43   Stream_event *tremolo_ev_;
44   bool tuplet_start_;
45
46   TRANSLATOR_DECLARATIONS (Stem_engraver);
47
48 protected:
49   void make_stem (Grob_info, bool);
50
51   DECLARE_TRANSLATOR_LISTENER (tremolo);
52   DECLARE_TRANSLATOR_LISTENER (tuplet_span);
53   DECLARE_ACKNOWLEDGER (rhythmic_head);
54   void stop_translation_timestep ();
55   void finalize ();
56   void kill_unused_flags ();
57 };
58
59 Stem_engraver::Stem_engraver ()
60 {
61   tremolo_ev_ = 0;
62   stem_ = 0;
63   tremolo_ = 0;
64   rhythmic_ev_ = 0;
65   tuplet_start_ = false;
66 }
67
68 void
69 Stem_engraver::make_stem (Grob_info gi, bool tuplet_start)
70 {
71   /* Announce the cause of the head as cause of the stem.  The
72      stem needs a rhythmic structure to fit it into a beam.  */
73   stem_ = make_item ("Stem", gi.grob ()->self_scm ());
74   if (tuplet_start)
75     stem_->set_property ("tuplet-start", SCM_BOOL_T);
76   (void) make_item ("StemStub", gi.grob ()->self_scm ());
77   if (tremolo_ev_)
78     {
79       /* Stem tremolo is never applied to a note by default,
80          it must be requested.  But there is a default for the
81          tremolo value:
82
83          c4:8 c c:
84
85          the first and last (quarter) note both get one tremolo flag.  */
86       int requested_type
87         = robust_scm2int (tremolo_ev_->get_property ("tremolo-type"), 0);
88       SCM f = get_property ("tremoloFlags");
89       if (!requested_type)
90         {
91           if (scm_is_number (f))
92             requested_type = scm_to_int (f);
93           else
94             requested_type = 8;
95         }
96       else
97         context ()->set_property ("tremoloFlags", scm_from_int (requested_type));
98
99       /*
100         we take the duration log from the Event, since the duration-log
101         for a note head is always <= 2.
102       */
103       Stream_event *ev = gi.event_cause ();
104       Duration *dur = unsmob_duration (ev->get_property ("duration"));
105
106       int tremolo_flags = intlog2 (requested_type) - 2
107                           - (dur->duration_log () > 2 ? dur->duration_log () - 2 : 0);
108       if (tremolo_flags <= 0)
109         {
110           tremolo_ev_->origin ()->warning (_ ("tremolo duration is too long"));
111           tremolo_flags = 0;
112         }
113
114       if (tremolo_flags)
115         {
116           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
117
118           /* The number of tremolo flags is the number of flags of the
119              tremolo-type minus the number of flags of the note itself.  */
120           tremolo_->set_property ("flag-count", scm_from_int (tremolo_flags));
121           tremolo_->set_parent (stem_, X_AXIS);
122           stem_->set_object ("tremolo-flag", tremolo_->self_scm ());
123           tremolo_->set_object ("stem", stem_->self_scm ());
124         }
125     }
126 }
127
128 void
129 Stem_engraver::acknowledge_rhythmic_head (Grob_info gi)
130 {
131   if (Rhythmic_head::get_stem (gi.grob ()))
132     return;
133
134   Stream_event *cause = gi.event_cause ();
135   if (!cause)
136     return;
137   Duration *d = unsmob_duration (cause->get_property ("duration"));
138   if (!d)
139     return;
140
141   if (!stem_)
142       make_stem (gi, tuplet_start_);
143       
144
145   int ds = Stem::duration_log (stem_);
146   int dc = d->duration_log ();
147
148   // half notes and quarter notes all have compatible stems.
149   // Longas are done differently (oops?), so we can't unify
150   // them with the other stemmed notes.
151   if (ds == 1)
152     ds = 2;
153   if (dc == 1)
154     dc = 2;
155   // whole notes and brevis both have no stems
156   if (ds == -1)
157     ds = 0;
158   if (dc == -1)
159     dc = 0;
160
161   if (ds != dc)
162     {
163       gi.event_cause ()->origin ()->warning (_f ("adding note head to incompatible stem (type = %d/%d)",
164                                                  ds < 0 ? 1 << -ds : 1,
165                                                  ds > 0 ? 1 << ds : 1));
166       gi.event_cause ()->origin ()->warning (_ ("maybe input should specify polyphonic voices"));
167     }
168
169   Stem::add_head (stem_, gi.grob ());
170
171   if (Stem::is_normal_stem (stem_)
172       && Stem::duration_log (stem_) > 2)
173     {
174       Item *flag = make_item ("Flag", stem_->self_scm ());
175       flag->set_parent (stem_, X_AXIS);
176       stem_->set_object ("flag", flag->self_scm ());
177       maybe_flags_.push_back (flag);
178     }
179   if (tuplet_start_) 
180     stem_->set_property ("tuplet-start", SCM_BOOL_T);
181 }
182
183 void
184 Stem_engraver::kill_unused_flags ()
185 {
186   for (vsize i = 0; i < maybe_flags_.size (); i++)
187     if (unsmob_grob (maybe_flags_[i]->get_parent (X_AXIS)->get_object ("beam")))
188       maybe_flags_[i]->suicide ();
189 }
190
191 void
192 Stem_engraver::finalize ()
193 {
194   kill_unused_flags ();
195 }
196
197 void
198 Stem_engraver::stop_translation_timestep ()
199 {
200   if (scm_is_string (get_property ("whichBar")))
201     kill_unused_flags ();
202
203   tremolo_ = 0;
204   if (stem_)
205     {
206       /* FIXME: junk these properties.  */
207       SCM prop = get_property ("stemLeftBeamCount");
208       if (scm_is_number (prop))
209         {
210           Stem::set_beaming (stem_, scm_to_int (prop), LEFT);
211           context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
212         }
213       prop = get_property ("stemRightBeamCount");
214       if (scm_is_number (prop))
215         {
216           Stem::set_beaming (stem_, scm_to_int (prop), RIGHT);
217           context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
218         }
219       stem_ = 0;
220     }
221   tuplet_start_ = false;
222   tremolo_ev_ = 0;
223 }
224
225 IMPLEMENT_TRANSLATOR_LISTENER (Stem_engraver, tuplet_span);
226 void
227 Stem_engraver::listen_tuplet_span (Stream_event *ev)
228 {
229   Direction dir = to_dir (ev->get_property ("span-direction"));
230   if (dir == START)
231     {
232       // set stem property if stem already exists
233       if (stem_)
234         stem_->set_property ("tuplet-start", SCM_BOOL_T);
235       tuplet_start_ = true;  // stash the value for use in later creation 
236     }
237 }
238
239 IMPLEMENT_TRANSLATOR_LISTENER (Stem_engraver, tremolo);
240 void
241 Stem_engraver::listen_tremolo (Stream_event *ev)
242 {
243   ASSIGN_EVENT_ONCE (tremolo_ev_, ev);
244 }
245
246 ADD_ACKNOWLEDGER (Stem_engraver, rhythmic_head);
247
248 ADD_TRANSLATOR (Stem_engraver,
249                 /* doc */
250                 "Create stems and single-stem tremolos.  It also works"
251                 " together with the beam engraver for overriding beaming.",
252
253                 /* create */
254                 "Stem "
255                 "StemTremolo ",
256
257                 /* read */
258                 "tremoloFlags "
259                 "stemLeftBeamCount "
260                 "stemRightBeamCount "
261                 "whichBar ",
262
263                 /* write */
264                 ""
265                );