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