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