]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
release: 1.3.109
[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_grob (Grob_info);
31   virtual void stop_translation_timestep ();
32   virtual bool try_music (Music*);
33   
34 private:
35   Grob  *stem_p_;
36   Grob *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_grob(Grob_info i)
54 {
55   Grob* h = i.elem_l_;
56   if (Rhythmic_head::has_interface (h))
57     {
58       if (Rhythmic_head::stem_l (h))
59         return;
60       
61       int duration_log  = unsmob_duration (i.req_l_->get_mus_property ("duration"))-> duration_log ();
62             
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_grob_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 = gh_scm2int (tremolo_req_l_->get_mus_property ("tremolo-type"));
84               
85               SCM f = get_property ("tremoloFlags");
86               if (!requested_type && gh_number_p (f))
87                 requested_type = gh_scm2int (f);
88               else
89                 daddy_trans_l_->set_property ("tremoloFlags", gh_int2scm (requested_type));
90
91               if (requested_type)
92                 {
93                   tremolo_p_ = new Item (get_property ("StemTremolo"));
94                   Stem_tremolo::set_interface (tremolo_p_);
95
96                   announce_grob (tremolo_p_, tremolo_req_l_);
97                   /*
98                     The number of tremolo flags is the number of flags of
99                     the tremolo-type minus the number of flags of the note
100                     itself.
101                    */
102                   int tremolo_flags = intlog2 (requested_type) - 2
103                     - (duration_log > 2 ? duration_log - 2 : 0);
104                   if (tremolo_flags < 0)
105                     tremolo_flags = 0;
106                   tremolo_p_->set_grob_property ("tremolo-flags",
107                                                 gh_int2scm (tremolo_flags));
108                 }
109             }
110           announce_grob (stem_p_, i.req_l_);
111         }
112
113       if (Stem::flag_i (stem_p_) != duration_log)
114         {
115           i.req_l_->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::flag_i (stem_p_)));
116         }
117
118       Stem::add_head (stem_p_,h);
119     }
120 }
121
122 void
123 Stem_engraver::stop_translation_timestep()
124 {
125   if (tremolo_p_)
126     {
127       Stem_tremolo::set_stem (tremolo_p_, stem_p_);
128       typeset_grob (tremolo_p_);
129       tremolo_p_ = 0;
130     }
131
132   if (stem_p_)
133     {
134       SCM prop = get_property ("stemLeftBeamCount");
135       if (gh_number_p(prop))
136         {
137           Stem::set_beaming (stem_p_,gh_scm2int (prop),LEFT);
138           daddy_trans_l_->set_property ("stemLeftBeamCount", SCM_UNDEFINED);
139         }
140       prop = get_property ("stemRightBeamCount");
141       if (gh_number_p(prop))
142         {
143           Stem::set_beaming (stem_p_,gh_scm2int (prop), RIGHT);
144           daddy_trans_l_->set_property ("stemRightBeamCount", SCM_UNDEFINED);
145         }
146
147       
148       // UGH. Should mark non-forced instead.
149
150       /*
151          aargh: I don't get it.  direction is being set (and then set
152          to forced), if we have a Chord_tremolo.
153        */
154       SCM dir = stem_p_->get_grob_property ("direction");
155       if (gh_number_p (dir) && to_dir(dir))
156         {
157           stem_p_->set_grob_property ("dir-forced", SCM_BOOL_T);          
158         }
159
160       typeset_grob(stem_p_);
161       stem_p_ = 0;
162     }
163
164
165   tremolo_req_l_ = 0;
166 }
167
168 bool
169 Stem_engraver::try_music (Music* r)
170 {
171   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
172     {
173       tremolo_req_l_ = a;
174       return true;
175     }
176   return false;
177 }
178