]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* lily/include/lily-guile.hh: many new ly_ functions. Thanks to
[lilypond.git] / lily / stem-engraver.cc
1 /*
2   stem-grav.cc -- implement Stem_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "staff-symbol-referencer.hh"
10 #include "rhythmic-head.hh"
11 #include "stem.hh"
12 #include "event.hh"
13 #include "misc.hh"
14 #include "stem-tremolo.hh"
15 #include "item.hh"
16 #include "context.hh"
17
18 #include "engraver.hh"
19
20
21
22 /**
23   Make stems upon receiving noteheads.
24  */
25 class Stem_engraver : public Engraver
26 {
27   TRANSLATOR_DECLARATIONS (Stem_engraver);
28 protected:
29   virtual void acknowledge_grob (Grob_info);
30   virtual void stop_translation_timestep ();
31   virtual bool try_music (Music*);
32   
33 private:
34   Grob  *stem_;
35   Grob *tremolo_;
36   Music *rhythmic_ev_;
37   Music* tremolo_ev_;
38 };
39
40 Stem_engraver::Stem_engraver ()
41 {
42   tremolo_ev_ = 0;
43   stem_ = 0;
44   tremolo_ = 0;
45   rhythmic_ev_ =0;
46 }
47
48
49 void
50 Stem_engraver::acknowledge_grob (Grob_info i)
51 {
52   Grob* h = i.grob_;
53   if (Rhythmic_head::has_interface (h))
54     {
55       if (Rhythmic_head::get_stem (h))
56         return;
57
58       /* Reverted to the old method so chord tremolos work again. /MB 
59       */
60       int duration_log = 0;
61
62       Music * m = i.music_cause ();
63       if (m->is_mus_type ("rhythmic-event"))
64         duration_log = unsmob_duration (m->get_property ("duration"))-> duration_log (); 
65       
66       if (!stem_) 
67         {
68           stem_ = make_item ("Stem");
69
70           stem_->set_property ("duration-log", scm_int2num (duration_log));
71
72           if (tremolo_ev_)
73             {
74               /*
75                 Stem tremolo is never applied to a note by default,
76                 is must me evuested.  But there is a default for the
77                 tremolo value:
78
79                    c4:8 c c:
80
81                 the first and last (quarter) note bothe get one tremolo flag.
82                */
83               int requested_type = ly_scm2int (tremolo_ev_->get_property ("tremolo-type"));
84               SCM f = get_property ("tremoloFlags");
85               if (!requested_type)
86                 if (ly_number_p (f))
87                   requested_type = ly_scm2int (f);
88                 else
89                   requested_type = 8; 
90               else
91                 daddy_context_->set_property ("tremoloFlags", scm_int2num (requested_type));
92
93               int tremolo_flags = intlog2 (requested_type) - 2
94                 - (duration_log > 2 ? duration_log - 2 : 0);
95               if (tremolo_flags <= 0)
96                 {
97                   tremolo_ev_->origin ()->warning (_("tremolo duration is too long"));
98                   tremolo_flags = 0;
99                 }
100
101               if (tremolo_flags)
102                 {
103                   tremolo_ = make_item ("StemTremolo");
104                   announce_grob (tremolo_, tremolo_ev_->self_scm ());
105
106                   /*
107                     The number of tremolo flags is the number of flags of
108                     the tremolo-type minus the number of flags of the note
109                     itself.
110                    */
111                   tremolo_->set_property ("flag-count",
112                                                scm_int2num (tremolo_flags));
113                   tremolo_->set_parent (stem_, X_AXIS);
114                   stem_->set_property ("tremolo-flag", tremolo_->self_scm ());
115                   tremolo_->set_property ("stem",
116                                           stem_->self_scm ());
117                 }
118             }
119
120           /*
121             We announce the cause of the head as cause of the stem.
122             The stem needs a rhythmic structure to fit it into a beam.  */
123           announce_grob (stem_, i.music_cause ()->self_scm ());
124         }
125
126       if (Stem::duration_log (stem_) != duration_log)
127         {
128           i.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::duration_log (stem_))
129                                                 + _f ("Don't you want polyphonic voices instead?")
130                                                 );
131         }
132
133       Stem::add_head (stem_,h);
134     }
135 }
136
137 void
138 Stem_engraver::stop_translation_timestep ()
139 {
140   if (tremolo_)
141     {
142       typeset_grob (tremolo_);
143       tremolo_ = 0;
144     }
145
146   if (stem_)
147     {
148       /*
149         toDO: junk these properties.
150        */
151       SCM prop = get_property ("stemLeftBeamCount");
152       if (ly_number_p (prop))
153         {
154           Stem::set_beaming (stem_,ly_scm2int (prop),LEFT);
155           daddy_context_->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
156         }
157       prop = get_property ("stemRightBeamCount");
158       if (ly_number_p (prop))
159         {
160           Stem::set_beaming (stem_,ly_scm2int (prop), RIGHT);
161           daddy_context_->unset_property (ly_symbol2scm ("stemRightBeamCount"));
162         }
163
164       typeset_grob (stem_);
165       stem_ = 0;
166     }
167
168
169   tremolo_ev_ = 0;
170 }
171
172 bool
173 Stem_engraver::try_music (Music* r)
174 {
175   if (r->is_mus_type ("tremolo-event"))
176     {
177       tremolo_ev_ = r;
178       return true;
179     }
180   return false;
181 }
182
183 ENTER_DESCRIPTION (Stem_engraver,
184 /* descr */       "Create stems and single-stem tremolos.  It also works together with "
185 "the beam engraver for overriding beaming.",
186 /* creats*/       "Stem StemTremolo",
187 /* accepts */     "tremolo-event",
188 /* acks  */      "rhythmic-head-interface",
189 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
190 /* write */       "");