]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
release: 1.3.55
[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 "note-head.hh"
11 #include "stem.hh"
12 #include "musical-request.hh"
13 #include "misc.hh"
14 #include "stem-tremolo.hh"
15 #include "staff-info.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   Stem *stem_p_;
38   Stem_tremolo *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 (SCM_EOL);
80           Staff_symbol_referencer_interface st(stem_p_);
81           st.set_interface ();
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 Stem_tremolo (get_property ("basicStemTremoloProperties"));
105                   announce_element (Score_element_info (tremolo_p_, tremolo_req_l_));
106                   /*
107                     The number of tremolo flags is the number of flags of
108                     the tremolo-type minus the number of flags of the note
109                     itself.
110                    */
111                   int tremolo_flags = intlog2 (requested_type) - 2
112                     - (duration_log > 2 ? duration_log - 2 : 0);
113                   if (tremolo_flags < 0)
114                     tremolo_flags = 0;
115                   tremolo_p_->set_elt_property ("tremolo-flags",
116                                                 gh_int2scm (tremolo_flags));
117                 }
118             }
119           announce_element (Score_element_info (stem_p_, r));
120         }
121
122       if (stem_p_->flag_i () != duration_log)
123         {
124           r->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  stem_p_->flag_i ()));
125         }
126
127       stem_p_->add_head (h);
128     }
129 }
130
131 void
132 Stem_engraver::do_pre_move_processing()
133 {
134   if (tremolo_p_)
135     {
136       tremolo_p_->set_stem (stem_p_);
137       typeset_element (tremolo_p_);
138       tremolo_p_ = 0;
139     }
140
141   if (stem_p_)
142     {
143       SCM prop = get_property ("stemLeftBeamCount");
144       if (gh_number_p(prop))
145         {
146           stem_p_->set_beaming (gh_scm2int (prop),LEFT);
147           daddy_trans_l_->set_property ("stemLeftBeamCount", SCM_UNDEFINED);
148         }
149       prop = get_property ("stemRightBeamCount");
150       if (gh_number_p(prop))
151         {
152           stem_p_->set_beaming (gh_scm2int (prop), RIGHT);
153           daddy_trans_l_->set_property ("stemRightBeamCount", SCM_UNDEFINED);
154         }
155
156       
157       // UGH. Should mark non-forced instead.
158
159       /*
160          aargh: I don't get it.  direction is being set (and then set
161          to forced), if we have a Chord_tremolo.
162        */
163       SCM dir = stem_p_->get_elt_property ("direction");
164       if (gh_number_p (dir) && to_dir(dir))
165         {
166           stem_p_->set_elt_property ("dir-forced", SCM_BOOL_T);   
167         }
168
169       typeset_element(stem_p_);
170       stem_p_ = 0;
171     }
172
173
174   tremolo_req_l_ = 0;
175 }
176
177 bool
178 Stem_engraver::do_try_music (Music* r)
179 {
180   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
181     {
182       tremolo_req_l_ = a;
183       return true;
184     }
185   return false;
186 }
187