]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
Doc: Included/compile.itexi - CG 4.2 - Updated notes on reqs for compiling
[lilypond.git] / lily / stem-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--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
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 using std::vector;
38
39 class Stem_engraver : public Engraver
40 {
41   Grob *stem_;
42   Grob *tremolo_;
43   vector <Grob *> maybe_flags_;
44   Stream_event *rhythmic_ev_;
45   Stream_event *tremolo_ev_;
46   bool tuplet_start_;
47
48   TRANSLATOR_DECLARATIONS (Stem_engraver);
49
50 protected:
51   void make_stem (Grob_info, bool);
52
53   DECLARE_TRANSLATOR_LISTENER (tremolo);
54   DECLARE_TRANSLATOR_LISTENER (tuplet_span);
55   DECLARE_ACKNOWLEDGER (rhythmic_head);
56   void stop_translation_timestep ();
57   void finalize ();
58   void kill_unused_flags ();
59 };
60
61 Stem_engraver::Stem_engraver ()
62 {
63   tremolo_ev_ = 0;
64   stem_ = 0;
65   tremolo_ = 0;
66   rhythmic_ev_ = 0;
67   tuplet_start_ = false;
68 }
69
70 void
71 Stem_engraver::make_stem (Grob_info gi, bool tuplet_start)
72 {
73   /* Announce the cause of the head as cause of the stem.  The
74      stem needs a rhythmic structure to fit it into a beam.  */
75   stem_ = make_item ("Stem", gi.grob ()->self_scm ());
76   if (tuplet_start)
77     stem_->set_property ("tuplet-start", SCM_BOOL_T);
78   (void) make_item ("StemStub", gi.grob ()->self_scm ());
79   if (tremolo_ev_)
80     {
81       /* Stem tremolo is never applied to a note by default,
82          it must be requested.  But there is a default for the
83          tremolo value:
84
85          c4:8 c c:
86
87          the first and last (quarter) note both get one tremolo flag.  */
88       int requested_type
89         = robust_scm2int (tremolo_ev_->get_property ("tremolo-type"), 8);
90
91       /*
92         we take the duration log from the Event, since the duration-log
93         for a note head is always <= 2.
94       */
95       Stream_event *ev = gi.event_cause ();
96       Duration *dur = unsmob<Duration> (ev->get_property ("duration"));
97
98       int tremolo_flags = intlog2 (requested_type) - 2
99                           - (dur->duration_log () > 2 ? dur->duration_log () - 2 : 0);
100       if (tremolo_flags <= 0)
101         {
102           tremolo_ev_->origin ()->warning (_ ("tremolo duration is too long"));
103           tremolo_flags = 0;
104         }
105
106       if (tremolo_flags)
107         {
108           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
109
110           /* The number of tremolo flags is the number of flags of the
111              tremolo-type minus the number of flags of the note itself.  */
112           tremolo_->set_property ("flag-count", scm_from_int (tremolo_flags));
113           tremolo_->set_parent (stem_, X_AXIS);
114           stem_->set_object ("tremolo-flag", tremolo_->self_scm ());
115           tremolo_->set_object ("stem", stem_->self_scm ());
116         }
117     }
118 }
119
120 void
121 Stem_engraver::acknowledge_rhythmic_head (Grob_info gi)
122 {
123   if (Rhythmic_head::get_stem (gi.grob ()))
124     return;
125
126   Stream_event *cause = gi.event_cause ();
127   if (!cause)
128     return;
129   Duration *d = unsmob<Duration> (cause->get_property ("duration"));
130   if (!d)
131     return;
132
133   if (!stem_)
134     make_stem (gi, tuplet_start_);
135
136   int ds = Stem::duration_log (stem_);
137   int dc = d->duration_log ();
138
139   // half notes and quarter notes all have compatible stems.
140   // Longas are done differently (oops?), so we can't unify
141   // them with the other stemmed notes.
142   if (ds == 1)
143     ds = 2;
144   if (dc == 1)
145     dc = 2;
146   // whole notes and brevis both have no stems
147   if (ds == -1)
148     ds = 0;
149   if (dc == -1)
150     dc = 0;
151
152   if (ds != dc)
153     {
154       gi.event_cause ()->origin ()->warning (_f ("adding note head to incompatible stem (type = %d/%d)",
155                                                  ds < 0 ? 1 << -ds : 1,
156                                                  ds > 0 ? 1 << ds : 1));
157       gi.event_cause ()->origin ()->warning (_ ("maybe input should specify polyphonic voices"));
158     }
159
160   Stem::add_head (stem_, gi.grob ());
161
162   if (Stem::is_normal_stem (stem_)
163       && Stem::duration_log (stem_) > 2
164       && !(unsmob<Grob> (stem_->get_object ("flag"))))
165     {
166       Item *flag = make_item ("Flag", stem_->self_scm ());
167       flag->set_parent (stem_, X_AXIS);
168       stem_->set_object ("flag", flag->self_scm ());
169       maybe_flags_.push_back (flag);
170     }
171   if (tuplet_start_)
172     stem_->set_property ("tuplet-start", SCM_BOOL_T);
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   tuplet_start_ = false;
214   tremolo_ev_ = 0;
215 }
216
217 IMPLEMENT_TRANSLATOR_LISTENER (Stem_engraver, tuplet_span);
218 void
219 Stem_engraver::listen_tuplet_span (Stream_event *ev)
220 {
221   Direction dir = to_dir (ev->get_property ("span-direction"));
222   if (dir == START)
223     {
224       // set stem property if stem already exists
225       if (stem_)
226         stem_->set_property ("tuplet-start", SCM_BOOL_T);
227       tuplet_start_ = true;  // stash the value for use in later creation
228     }
229 }
230
231 IMPLEMENT_TRANSLATOR_LISTENER (Stem_engraver, tremolo);
232 void
233 Stem_engraver::listen_tremolo (Stream_event *ev)
234 {
235   ASSIGN_EVENT_ONCE (tremolo_ev_, ev);
236 }
237
238 ADD_ACKNOWLEDGER (Stem_engraver, rhythmic_head);
239
240 ADD_TRANSLATOR (Stem_engraver,
241                 /* doc */
242                 "Create stems, flags and single-stem tremolos.  It also works"
243                 " together with the beam engraver for overriding beaming.",
244
245                 /* create */
246                 "Flag "
247                 "Stem "
248                 "StemStub "
249                 "StemTremolo ",
250
251                 /* read */
252                 "stemLeftBeamCount "
253                 "stemRightBeamCount "
254                 "whichBar ",
255
256                 /* write */
257                 ""
258                );