]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
release: 1.3.94
[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--2000 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   Make stems upon receiving noteheads.
21  */
22 class Stem_engraver : public Engraver
23 {
24
25 public:
26   VIRTUAL_COPY_CONS (Translator);
27   Stem_engraver();
28   
29 protected:
30   virtual void do_creation_processing ();
31   virtual void acknowledge_element (Score_element_info);
32   virtual void do_pre_move_processing ();
33   virtual bool do_try_music (Music*);
34   
35 private:
36   int default_tremolo_type_i_;
37   Score_element  *stem_p_;
38   Score_element *tremolo_p_;
39   Rhythmic_req *rhythmic_req_l_;
40   Tremolo_req* tremolo_req_l_;
41 };
42
43 ADD_THIS_TRANSLATOR (Stem_engraver);
44
45 Stem_engraver::Stem_engraver ()
46 {
47   tremolo_req_l_ = 0;
48   stem_p_ = 0;
49   tremolo_p_ = 0;
50   default_tremolo_type_i_ = 16;
51   rhythmic_req_l_ =0;
52 }
53
54 void
55 Stem_engraver::do_creation_processing ()
56 {
57   /*
58     huh, why only at creation time?
59   */
60   SCM prop = get_property ("tremoloFlags");
61   if (gh_number_p(prop)) 
62     {
63       default_tremolo_type_i_  = gh_scm2int (prop);
64     }
65 }
66
67 void
68 Stem_engraver::acknowledge_element(Score_element_info i)
69 {
70   Score_element* h = i.elem_l_;
71   if (Rhythmic_head::has_interface (h))
72     {
73       if (Rhythmic_head::stem_l (h))
74         return;
75       
76       Rhythmic_req * r = dynamic_cast <Rhythmic_req *> (i.req_l_);
77       int duration_log = r->duration_.durlog_i_;      
78       if (!stem_p_) 
79         {
80           stem_p_ = new Item (get_property ("Stem"));
81           Stem::set_interface (stem_p_);
82           Staff_symbol_referencer::set_interface(stem_p_);
83
84           
85           stem_p_->set_elt_property ("duration-log", gh_int2scm (duration_log));
86
87           if (tremolo_req_l_)
88             {
89               /*
90                 Stem tremolo is never applied to a note by default,
91                 is must me requested.  But there is a default for the
92                 tremolo value:
93
94                    c4:8 c c:
95
96                 the first and last (quarter) note bothe get one tremolo flag.
97                */
98               int requested_type = tremolo_req_l_->type_i_;
99               if (!requested_type)
100                 requested_type = default_tremolo_type_i_;
101               else
102                 default_tremolo_type_i_ = requested_type;
103
104               if (requested_type)
105                 {
106                   tremolo_p_ = new Item (get_property ("StemTremolo"));
107                   Stem_tremolo::set_interface (tremolo_p_);
108
109                   announce_element (tremolo_p_, tremolo_req_l_);
110                   /*
111                     The number of tremolo flags is the number of flags of
112                     the tremolo-type minus the number of flags of the note
113                     itself.
114                    */
115                   int tremolo_flags = intlog2 (requested_type) - 2
116                     - (duration_log > 2 ? duration_log - 2 : 0);
117                   if (tremolo_flags < 0)
118                     tremolo_flags = 0;
119                   tremolo_p_->set_elt_property ("tremolo-flags",
120                                                 gh_int2scm (tremolo_flags));
121                 }
122             }
123           announce_element (stem_p_, r);
124         }
125
126       if (Stem::flag_i (stem_p_) != duration_log)
127         {
128           r->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::flag_i (stem_p_)));
129         }
130
131       Stem::add_head (stem_p_,h);
132     }
133 }
134
135 void
136 Stem_engraver::do_pre_move_processing()
137 {
138   if (tremolo_p_)
139     {
140       Stem_tremolo::set_stem (tremolo_p_, stem_p_);
141       typeset_element (tremolo_p_);
142       tremolo_p_ = 0;
143     }
144
145   if (stem_p_)
146     {
147       SCM prop = get_property ("stemLeftBeamCount");
148       if (gh_number_p(prop))
149         {
150           Stem::set_beaming (stem_p_,gh_scm2int (prop),LEFT);
151           daddy_trans_l_->set_property ("stemLeftBeamCount", SCM_UNDEFINED);
152         }
153       prop = get_property ("stemRightBeamCount");
154       if (gh_number_p(prop))
155         {
156           Stem::set_beaming (stem_p_,gh_scm2int (prop), RIGHT);
157           daddy_trans_l_->set_property ("stemRightBeamCount", SCM_UNDEFINED);
158         }
159
160       
161       // UGH. Should mark non-forced instead.
162
163       /*
164          aargh: I don't get it.  direction is being set (and then set
165          to forced), if we have a Chord_tremolo.
166        */
167       SCM dir = stem_p_->get_elt_property ("direction");
168       if (gh_number_p (dir) && to_dir(dir))
169         {
170           stem_p_->set_elt_property ("dir-forced", SCM_BOOL_T);   
171         }
172
173       typeset_element(stem_p_);
174       stem_p_ = 0;
175     }
176
177
178   tremolo_req_l_ = 0;
179 }
180
181 bool
182 Stem_engraver::do_try_music (Music* r)
183 {
184   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
185     {
186       tremolo_req_l_ = a;
187       return true;
188     }
189   return false;
190 }
191