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