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