]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* lily/include/translator.hh (ENTER_DESCRIPTION): add
[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--2002 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
21 /**
22   Make stems upon receiving noteheads.
23  */
24 class Stem_engraver : public Engraver
25 {
26   TRANSLATOR_DECLARATIONS(Stem_engraver);
27 protected:
28   virtual void acknowledge_grob (Grob_info);
29   virtual void stop_translation_timestep ();
30   virtual bool try_music (Music*);
31   
32 private:
33   Grob  *stem_;
34   Grob *tremolo_;
35   Rhythmic_req *rhythmic_req_;
36   Tremolo_req* tremolo_req_;
37 };
38
39 Stem_engraver::Stem_engraver ()
40 {
41   tremolo_req_ = 0;
42   stem_ = 0;
43   tremolo_ = 0;
44   rhythmic_req_ =0;
45 }
46
47
48 void
49 Stem_engraver::acknowledge_grob (Grob_info i)
50 {
51   Grob* h = i.grob_;
52   if (Rhythmic_head::has_interface (h))
53     {
54       if (Rhythmic_head::get_stem (h))
55         return;
56
57       /* Reverted to the old method so chord tremolos work again. /MB 
58       */
59       int duration_log = 0;
60       Rhythmic_req *rhythmic_req = dynamic_cast <Rhythmic_req *> (i.music_cause ()); 
61       if (rhythmic_req)
62         duration_log = unsmob_duration (rhythmic_req->get_mus_property ("duration"))-> duration_log (); 
63       
64       if (!stem_) 
65         {
66           stem_ = new Item (get_property ("Stem"));
67
68           stem_->set_grob_property ("duration-log", gh_int2scm (duration_log));
69
70           if (tremolo_req_)
71             {
72               /*
73                 Stem tremolo is never applied to a note by default,
74                 is must me requested.  But there is a default for the
75                 tremolo value:
76
77                    c4:8 c c:
78
79                 the first and last (quarter) note bothe get one tremolo flag.
80                */
81               int requested_type = gh_scm2int (tremolo_req_->get_mus_property ("tremolo-type"));
82               SCM f = get_property ("tremoloFlags");
83               if (!requested_type && gh_number_p (f))
84                 requested_type = gh_scm2int (f);
85               else
86                 daddy_trans_->set_property ("tremoloFlags", gh_int2scm (requested_type));
87
88               int tremolo_flags = intlog2 (requested_type) - 2
89                 - (duration_log > 2 ? duration_log - 2 : 0);
90               if (tremolo_flags <= 0)
91                 {
92                   tremolo_req_->origin()->warning (_("tremolo duration is too long"));
93                   tremolo_flags = 0;
94                 }
95
96               if (tremolo_flags)
97                 {
98                   tremolo_ = new Item (get_property ("StemTremolo"));
99                   announce_grob(tremolo_, tremolo_req_->self_scm());
100
101                   /*
102                     The number of tremolo flags is the number of flags of
103                     the tremolo-type minus the number of flags of the note
104                     itself.
105                    */
106                           tremolo_->set_grob_property ("flag-count",
107                                                 gh_int2scm (tremolo_flags));
108                   tremolo_->set_parent (stem_, X_AXIS);
109                 }
110             }
111
112           /*
113             We announce the cause of the head as cause of the stem.
114             The stem needs a rhythmic structure to fit it into a beam.  */
115           announce_grob(stem_, i.music_cause ()->self_scm());
116         }
117
118       if (Stem::duration_log (stem_) != duration_log)
119         {
120           i.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 <<  Stem::duration_log (stem_)));
121         }
122
123       Stem::add_head (stem_,h);
124     }
125 }
126
127 void
128 Stem_engraver::stop_translation_timestep ()
129 {
130   if (tremolo_)
131     {
132       Stem_tremolo::set_stem (tremolo_, stem_);
133       typeset_grob (tremolo_);
134       tremolo_ = 0;
135     }
136
137   if (stem_)
138     {
139       SCM prop = get_property ("stemLeftBeamCount");
140       if (gh_number_p (prop))
141         {
142           Stem::set_beaming (stem_,gh_scm2int (prop),LEFT);
143           daddy_trans_->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
144         }
145       prop = get_property ("stemRightBeamCount");
146       if (gh_number_p (prop))
147         {
148           Stem::set_beaming (stem_,gh_scm2int (prop), RIGHT);
149           daddy_trans_->unset_property (ly_symbol2scm ("stemRightBeamCount"));
150         }
151
152       typeset_grob (stem_);
153       stem_ = 0;
154     }
155
156
157   tremolo_req_ = 0;
158 }
159
160 bool
161 Stem_engraver::try_music (Music* r)
162 {
163   if (Tremolo_req* a = dynamic_cast <Tremolo_req *> (r))
164     {
165       tremolo_req_ = a;
166       return true;
167     }
168   return false;
169 }
170
171 ENTER_DESCRIPTION(Stem_engraver,
172 /* descr */       "Create stems and single-stem tremolos.  It also works together with
173 the beam engraver for overriding beaming.",
174 /* creats*/       "Stem StemTremolo",
175 /* accepts */     "general-music",
176 /* acks  */      "rhythmic-head-interface",
177 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
178 /* write */       "");