]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
*** empty log message ***
[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--2003 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 "translator-group.hh"
17 #include "engraver.hh"
18
19
20
21 /**
22   Make stems upon receiving noteheads.
23  */
24 class Stem_engraver : public Engraver
25 {
26   TRANSLATOR_DECLARATIONS(Stem_engraver);
27 protected:
28   virtual void acknowledge_grob (Grob_info);
29   virtual void stop_translation_timestep ();
30   virtual bool try_music (Music*);
31   
32 private:
33   Grob  *stem_;
34   Grob *tremolo_;
35   Music *rhythmic_req_;
36   Music* tremolo_req_;
37 };
38
39 Stem_engraver::Stem_engraver ()
40 {
41   tremolo_req_ = 0;
42   stem_ = 0;
43   tremolo_ = 0;
44   rhythmic_req_ =0;
45 }
46
47
48 void
49 Stem_engraver::acknowledge_grob (Grob_info i)
50 {
51   Grob* h = i.grob_;
52   if (Rhythmic_head::has_interface (h))
53     {
54       if (Rhythmic_head::get_stem (h))
55         return;
56
57       /* Reverted to the old method so chord tremolos work again. /MB 
58       */
59       int duration_log = 0;
60
61       Music * m = i.music_cause ();
62       if (m->is_mus_type ("rhythmic-event"))
63         duration_log = unsmob_duration (m->get_mus_property ("duration"))-> duration_log (); 
64       
65       if (!stem_) 
66         {
67           stem_ = new Item (get_property ("Stem"));
68
69           stem_->set_grob_property ("duration-log", gh_int2scm (duration_log));
70
71           if (tremolo_req_)
72             {
73               /*
74                 Stem tremolo is never applied to a note by default,
75                 is must me 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 bothe get one tremolo flag.
81                */
82               int requested_type = gh_scm2int (tremolo_req_->get_mus_property ("tremolo-type"));
83               SCM f = get_property ("tremoloFlags");
84               if (!requested_type)
85                 if (gh_number_p (f))
86                   requested_type = gh_scm2int (f);
87                 else
88                   requested_type = 8; 
89               else
90                 daddy_trans_->set_property ("tremoloFlags", gh_int2scm (requested_type));
91
92               int tremolo_flags = intlog2 (requested_type) - 2
93                 - (duration_log > 2 ? duration_log - 2 : 0);
94               if (tremolo_flags <= 0)
95                 {
96                   tremolo_req_->origin()->warning (_("tremolo duration is too long"));
97                   tremolo_flags = 0;
98                 }
99
100               if (tremolo_flags)
101                 {
102                   tremolo_ = new Item (get_property ("StemTremolo"));
103                   announce_grob(tremolo_, tremolo_req_->self_scm());
104
105                   /*
106                     The number of tremolo flags is the number of flags of
107                     the tremolo-type minus the number of flags of the note
108                     itself.
109                    */
110                           tremolo_->set_grob_property ("flag-count",
111                                                 gh_int2scm (tremolo_flags));
112                   tremolo_->set_parent (stem_, X_AXIS);
113                 }
114             }
115
116           /*
117             We announce the cause of the head as cause of the stem.
118             The stem needs a rhythmic structure to fit it into a beam.  */
119           announce_grob(stem_, i.music_cause ()->self_scm());
120         }
121
122       if (Stem::duration_log (stem_) != duration_log)
123         {
124           i.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::duration_log (stem_)));
125         }
126
127       Stem::add_head (stem_,h);
128     }
129 }
130
131 void
132 Stem_engraver::stop_translation_timestep ()
133 {
134   if (tremolo_)
135     {
136       Stem_tremolo::set_stem (tremolo_, stem_);
137       typeset_grob (tremolo_);
138       tremolo_ = 0;
139     }
140
141   if (stem_)
142     {
143       SCM prop = get_property ("stemLeftBeamCount");
144       if (gh_number_p (prop))
145         {
146           Stem::set_beaming (stem_,gh_scm2int (prop),LEFT);
147           daddy_trans_->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
148         }
149       prop = get_property ("stemRightBeamCount");
150       if (gh_number_p (prop))
151         {
152           Stem::set_beaming (stem_,gh_scm2int (prop), RIGHT);
153           daddy_trans_->unset_property (ly_symbol2scm ("stemRightBeamCount"));
154         }
155
156       typeset_grob (stem_);
157       stem_ = 0;
158     }
159
160
161   tremolo_req_ = 0;
162 }
163
164 bool
165 Stem_engraver::try_music (Music* r)
166 {
167   if (r->is_mus_type ("tremolo-event"))
168     {
169       tremolo_req_ = r;
170       return true;
171     }
172   return false;
173 }
174
175 ENTER_DESCRIPTION(Stem_engraver,
176 /* descr */       "Create stems and single-stem tremolos.  It also works together with
177 the beam engraver for overriding beaming.",
178 /* creats*/       "Stem StemTremolo",
179 /* accepts */     "tremolo-event",
180 /* acks  */      "rhythmic-head-interface",
181 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
182 /* write */       "");