]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
release: 1.5.24
[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--2001 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 "musical-request.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_p_;
34   Grob *tremolo_p_;
35   Rhythmic_req *rhythmic_req_l_;
36   Tremolo_req* tremolo_req_l_;
37 };
38
39 Stem_engraver::Stem_engraver ()
40 {
41   tremolo_req_l_ = 0;
42   stem_p_ = 0;
43   tremolo_p_ = 0;
44   rhythmic_req_l_ =0;
45 }
46
47
48 void
49 Stem_engraver::acknowledge_grob (Grob_info i)
50 {
51   Grob* h = i.grob_l_;
52   if (Rhythmic_head::has_interface (h))
53     {
54       if (Rhythmic_head::stem_l (h))
55         return;
56
57       /*
58         We take the duration-log of the head; this is because
59         auto-tieing does strange things with the rhythmics.
60        */
61       int duration_log  =gh_scm2int (h->get_grob_property ("duration-log"));
62       if (!stem_p_) 
63         {
64           stem_p_ = new Item (get_property ("Stem"));
65           Stem::set_interface (stem_p_);
66           Staff_symbol_referencer::set_interface (stem_p_);
67           
68           stem_p_->set_grob_property ("duration-log", gh_int2scm (duration_log));
69
70           if (tremolo_req_l_)
71             {
72               /*
73                 Stem tremolo is never applied to a note by default,
74                 is must me 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 bothe get one tremolo flag.
80                */
81               int requested_type = gh_scm2int (tremolo_req_l_->get_mus_property ("tremolo-type"));
82               
83               SCM f = get_property ("tremoloFlags");
84               if (!requested_type && gh_number_p (f))
85                 requested_type = gh_scm2int (f);
86               else
87                 daddy_trans_l_->set_property ("tremoloFlags", gh_int2scm (requested_type));
88
89               if (requested_type)
90                 {
91                   tremolo_p_ = new Item (get_property ("StemTremolo"));
92                   Stem_tremolo::set_interface (tremolo_p_);
93
94                   announce_grob (tremolo_p_, tremolo_req_l_);
95                   /*
96                     The number of tremolo flags is the number of flags of
97                     the tremolo-type minus the number of flags of the note
98                     itself.
99                    */
100                   int tremolo_flags = intlog2 (requested_type) - 2
101                     - (duration_log > 2 ? duration_log - 2 : 0);
102                   if (tremolo_flags < 0)
103                     tremolo_flags = 0;
104                   tremolo_p_->set_grob_property ("tremolo-flags",
105                                                 gh_int2scm (tremolo_flags));
106                 }
107             }
108           announce_grob (stem_p_, i.music_cause ());
109         }
110
111       if (Stem::flag_i (stem_p_) != duration_log)
112         {
113           i.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::flag_i (stem_p_)));
114         }
115
116       Stem::add_head (stem_p_,h);
117     }
118 }
119
120 void
121 Stem_engraver::stop_translation_timestep ()
122 {
123   if (tremolo_p_)
124     {
125       Stem_tremolo::set_stem (tremolo_p_, stem_p_);
126       typeset_grob (tremolo_p_);
127       tremolo_p_ = 0;
128     }
129
130   if (stem_p_)
131     {
132       SCM prop = get_property ("stemLeftBeamCount");
133       if (gh_number_p (prop))
134         {
135           Stem::set_beaming (stem_p_,gh_scm2int (prop),LEFT);
136           daddy_trans_l_->set_property ("stemLeftBeamCount", SCM_UNDEFINED);
137         }
138       prop = get_property ("stemRightBeamCount");
139       if (gh_number_p (prop))
140         {
141           Stem::set_beaming (stem_p_,gh_scm2int (prop), RIGHT);
142           daddy_trans_l_->set_property ("stemRightBeamCount", SCM_UNDEFINED);
143         }
144
145       
146       // UGH. Should mark non-forced instead.
147       /*
148          aargh: I don't get it.  direction is being set (and then set
149          to forced), if we have a Chord_tremolo.
150        */
151       SCM dir = stem_p_->get_grob_property ("direction");
152       if (gh_number_p (dir) && to_dir (dir))
153         {
154           stem_p_->set_grob_property ("dir-forced", SCM_BOOL_T);          
155         }
156
157       typeset_grob (stem_p_);
158       stem_p_ = 0;
159     }
160
161
162   tremolo_req_l_ = 0;
163 }
164
165 bool
166 Stem_engraver::try_music (Music* r)
167 {
168   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
169     {
170       tremolo_req_l_ = a;
171       return true;
172     }
173   return false;
174 }
175
176 ENTER_DESCRIPTION(Stem_engraver,
177 /* descr */       "Create stems and single-stem tremolos.  It also works together with
178 the beam engraver for overriding beaming.",
179 /* creats*/       "Stem StemTremolo",
180 /* acks  */       "rhythmic-head-interface",
181 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
182 /* write */       "");