]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* scripts/convert-ly.py (conv): remove \lyrics from \lyricsto.
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "context.hh"
10 #include "directional-element-interface.hh"
11 #include "engraver.hh"
12 #include "event.hh"
13 #include "item.hh"
14 #include "misc.hh"
15 #include "rhythmic-head.hh"
16 #include "script-interface.hh"
17 #include "staff-symbol-referencer.hh"
18 #include "stem-tremolo.hh"
19 #include "stem.hh"
20
21
22 /**
23   Make stems upon receiving noteheads.
24  */
25 class Stem_engraver : public Engraver
26 {
27   Grob  *stem_;
28   Grob *tremolo_;
29   Music *rhythmic_ev_;
30   Music* tremolo_ev_;
31   TRANSLATOR_DECLARATIONS (Stem_engraver);
32
33 protected:
34   void make_stem (Grob_info);
35   virtual void acknowledge_grob (Grob_info);
36   virtual void stop_translation_timestep ();
37   virtual bool try_music (Music *);
38 };
39
40 Stem_engraver::Stem_engraver ()
41 {
42   tremolo_ev_ = 0;
43   stem_ = 0;
44   tremolo_ = 0;
45   rhythmic_ev_ =0;
46 }
47
48 void
49 Stem_engraver::make_stem (Grob_info gi)
50 {
51   /* Announce the cause of the head as cause of the stem.  The
52      stem needs a rhythmic structure to fit it into a beam.  */
53   stem_ = make_item ("Stem", gi.music_cause ()->self_scm ());
54
55   int duration_log = gi.music_cause ()->duration_log ();
56   stem_->set_property ("duration-log", scm_int2num (duration_log));
57
58   if (tremolo_ev_)
59     {
60       /* Stem tremolo is never applied to a note by default,
61          is must me requested.  But there is a default for the
62          tremolo value:
63
64          c4:8 c c:
65
66          the first and last (quarter) note bothe get one tremolo flag.  */
67       int requested_type
68         = ly_scm2int (tremolo_ev_->get_property ("tremolo-type"));
69       SCM f = get_property ("tremoloFlags");
70       if (!requested_type)
71         {
72           if (ly_c_number_p (f))
73             requested_type = ly_scm2int (f);
74           else
75             requested_type = 8;
76         }
77       else
78         context ()->set_property ("tremoloFlags", scm_int2num (requested_type));
79
80       int tremolo_flags = intlog2 (requested_type) - 2
81         - (duration_log > 2 ? duration_log - 2 : 0);
82       if (tremolo_flags <= 0)
83         {
84           tremolo_ev_->origin ()->warning (_("tremolo duration is too long"));
85           tremolo_flags = 0;
86         }
87
88       if (tremolo_flags)
89         {
90           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
91
92           /* The number of tremolo flags is the number of flags of the
93             tremolo-type minus the number of flags of the note itself.  */
94           tremolo_->set_property ("flag-count", scm_int2num (tremolo_flags));
95           tremolo_->set_parent (stem_, X_AXIS);
96           stem_->set_property ("tremolo-flag", tremolo_->self_scm ());
97           tremolo_->set_property ("stem", stem_->self_scm ());
98         }
99     }
100 }
101
102 void
103 Stem_engraver::acknowledge_grob (Grob_info gi)
104 {
105   if (Rhythmic_head::has_interface (gi.grob_))
106     {
107       if (Rhythmic_head::get_stem (gi.grob_))
108         return;
109
110       Music * cause = gi.music_cause ();
111       if (!cause)
112         return ;
113       
114       if (!stem_)
115         make_stem (gi);
116       
117       int duration_log = cause->duration_log ();
118       if (Stem::duration_log (stem_) != duration_log)
119         {
120           // FIXME: 
121           gi.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 << Stem::duration_log (stem_)));
122           
123           gi.music_cause ()->origin ()->warning (_f ("Don't you want polyphonic voices instead?"));
124         }
125
126       Stem::add_head (stem_, gi.grob_);
127     }
128 }
129
130 void
131 Stem_engraver::stop_translation_timestep ()
132 {
133   tremolo_ = 0;
134   if (stem_)
135     {
136       /* FIXME: junk these properties.  */
137       SCM prop = get_property ("stemLeftBeamCount");
138       if (ly_c_number_p (prop))
139         {
140           Stem::set_beaming (stem_,ly_scm2int (prop),LEFT);
141           context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
142         }
143       prop = get_property ("stemRightBeamCount");
144       if (ly_c_number_p (prop))
145         {
146           Stem::set_beaming (stem_,ly_scm2int (prop), RIGHT);
147           context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
148         }
149       stem_ = 0;
150     }
151   tremolo_ev_ = 0;
152 }
153
154 bool
155 Stem_engraver::try_music (Music *m)
156 {
157   if (m->is_mus_type ("tremolo-event"))
158     {
159       tremolo_ev_ = m;
160       return true;
161     }
162   return false;
163 }
164
165 ENTER_DESCRIPTION (Stem_engraver,
166 /* descr */       "Create stems and single-stem tremolos.  It also works together with "
167 "the beam engraver for overriding beaming.",
168 /* creats*/       "Stem StemTremolo",
169 /* accepts */     "tremolo-event",
170 /* acks  */      "rhythmic-head-interface",
171 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
172 /* write */       "");