]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
release: 1.3.68
[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
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   Stem *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   if (Rhythmic_head * h = dynamic_cast<Rhythmic_head *> (i.elem_l_))
71     {
72       if (h->stem_l ())
73         return;
74       
75       Rhythmic_req * r = dynamic_cast <Rhythmic_req *> (i.req_l_);
76       int duration_log = r->duration_.durlog_i_;      
77       if (!stem_p_) 
78         {
79           stem_p_ = new Stem (get_property ("basicStemProperties"));
80           Staff_symbol_referencer_interface::set_interface(stem_p_);
81
82           
83           stem_p_->set_elt_property ("duration-log", gh_int2scm (duration_log));
84
85           if (tremolo_req_l_)
86             {
87               /*
88                 Stem tremolo is never applied to a note by default,
89                 is must me requested.  But there is a default for the
90                 tremolo value:
91
92                    c4:8 c c:
93
94                 the first and last (quarter) note bothe get one tremolo flag.
95                */
96               int requested_type = tremolo_req_l_->type_i_;
97               if (!requested_type)
98                 requested_type = default_tremolo_type_i_;
99               else
100                 default_tremolo_type_i_ = requested_type;
101
102               if (requested_type)
103                 {
104                   tremolo_p_ = new Item (get_property ("basicStemTremoloProperties"));
105                   Stem_tremolo::set_interface (tremolo_p_);
106
107                   announce_element (Score_element_info (tremolo_p_, tremolo_req_l_));
108                   /*
109                     The number of tremolo flags is the number of flags of
110                     the tremolo-type minus the number of flags of the note
111                     itself.
112                    */
113                   int tremolo_flags = intlog2 (requested_type) - 2
114                     - (duration_log > 2 ? duration_log - 2 : 0);
115                   if (tremolo_flags < 0)
116                     tremolo_flags = 0;
117                   tremolo_p_->set_elt_property ("tremolo-flags",
118                                                 gh_int2scm (tremolo_flags));
119                 }
120             }
121           announce_element (Score_element_info (stem_p_, r));
122         }
123
124       if (stem_p_->flag_i () != duration_log)
125         {
126           r->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  stem_p_->flag_i ()));
127         }
128
129       stem_p_->add_head (h);
130     }
131 }
132
133 void
134 Stem_engraver::do_pre_move_processing()
135 {
136   if (tremolo_p_)
137     {
138       Stem_tremolo::set_stem (tremolo_p_, stem_p_);
139       typeset_element (tremolo_p_);
140       tremolo_p_ = 0;
141     }
142
143   if (stem_p_)
144     {
145       SCM prop = get_property ("stemLeftBeamCount");
146       if (gh_number_p(prop))
147         {
148           stem_p_->set_beaming (gh_scm2int (prop),LEFT);
149           daddy_trans_l_->set_property ("stemLeftBeamCount", SCM_UNDEFINED);
150         }
151       prop = get_property ("stemRightBeamCount");
152       if (gh_number_p(prop))
153         {
154           stem_p_->set_beaming (gh_scm2int (prop), RIGHT);
155           daddy_trans_l_->set_property ("stemRightBeamCount", SCM_UNDEFINED);
156         }
157
158       
159       // UGH. Should mark non-forced instead.
160
161       /*
162          aargh: I don't get it.  direction is being set (and then set
163          to forced), if we have a Chord_tremolo.
164        */
165       SCM dir = stem_p_->get_elt_property ("direction");
166       if (gh_number_p (dir) && to_dir(dir))
167         {
168           stem_p_->set_elt_property ("dir-forced", SCM_BOOL_T);   
169         }
170
171       typeset_element(stem_p_);
172       stem_p_ = 0;
173     }
174
175
176   tremolo_req_l_ = 0;
177 }
178
179 bool
180 Stem_engraver::do_try_music (Music* r)
181 {
182   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
183     {
184       tremolo_req_l_ = a;
185       return true;
186     }
187   return false;
188 }
189