]> git.donarmstrong.com Git - lilypond.git/blob - lily/stem-engraver.cc
* scm/define-context-properties.scm (Module): change definition of
[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   
32   TRANSLATOR_DECLARATIONS (Stem_engraver);
33
34 protected:
35   void make_stem (Grob_info);
36   
37   virtual void acknowledge_grob (Grob_info);
38   virtual void stop_translation_timestep ();
39   virtual bool try_music (Music *);
40 };
41
42 Stem_engraver::Stem_engraver ()
43 {
44   tremolo_ev_ = 0;
45   stem_ = 0;
46   tremolo_ = 0;
47   rhythmic_ev_ =0;
48 }
49
50 void
51 Stem_engraver::make_stem (Grob_info gi)
52 {
53   /* Announce the cause of the head as cause of the stem.  The
54      stem needs a rhythmic structure to fit it into a beam.  */
55   stem_ = make_item ("Stem", gi.music_cause ()->self_scm ());
56
57   /*
58     docme: why do we take duration-log from request, not from note
59     head?
60    */
61   int duration_log = gi.music_cause ()->duration_log ();
62   stem_->set_property ("duration-log", scm_int2num (duration_log));
63
64   if (tremolo_ev_)
65     {
66       /* Stem tremolo is never applied to a note by default,
67          is must me requested.  But there is a default for the
68          tremolo value:
69
70          c4:8 c c:
71
72          the first and last (quarter) note bothe get one tremolo flag.  */
73       int requested_type
74         = scm_to_int (tremolo_ev_->get_property ("tremolo-type"));
75       SCM f = get_property ("tremoloFlags");
76       if (!requested_type)
77         {
78           if (scm_is_number (f))
79             requested_type = scm_to_int (f);
80           else
81             requested_type = 8;
82         }
83       else
84         context ()->set_property ("tremoloFlags", scm_int2num (requested_type));
85
86       int tremolo_flags = intlog2 (requested_type) - 2
87         - (duration_log > 2 ? duration_log - 2 : 0);
88       if (tremolo_flags <= 0)
89         {
90           tremolo_ev_->origin ()->warning (_("tremolo duration is too long"));
91           tremolo_flags = 0;
92         }
93
94       if (tremolo_flags)
95         {
96           tremolo_ = make_item ("StemTremolo", tremolo_ev_->self_scm ());
97
98           /* The number of tremolo flags is the number of flags of the
99             tremolo-type minus the number of flags of the note itself.  */
100           tremolo_->set_property ("flag-count", scm_int2num (tremolo_flags));
101           tremolo_->set_parent (stem_, X_AXIS);
102           stem_->set_property ("tremolo-flag", tremolo_->self_scm ());
103           tremolo_->set_property ("stem", stem_->self_scm ());
104         }
105     }
106 }
107
108 void
109 Stem_engraver::acknowledge_grob (Grob_info gi)
110 {
111   if (Rhythmic_head::has_interface (gi.grob_))
112     {
113       if (Rhythmic_head::get_stem (gi.grob_))
114         return;
115
116       Music * cause = gi.music_cause ();
117       if (!cause)
118         return ;
119       
120       if (!stem_)
121         make_stem (gi);
122       
123       int duration_log = cause->duration_log ();
124       if (Stem::duration_log (stem_) != duration_log)
125         {
126           // FIXME: 
127           gi.music_cause ()->origin ()->warning (_f ("Adding note head to incompatible stem (type = %d)", 1 << Stem::duration_log (stem_)));
128           
129           gi.music_cause ()->origin ()->warning (_f ("Don't you want polyphonic voices instead?"));
130         }
131
132       Stem::add_head (stem_, gi.grob_);
133     }
134 }
135
136 void
137 Stem_engraver::stop_translation_timestep ()
138 {
139   tremolo_ = 0;
140   if (stem_)
141     {
142       /* FIXME: junk these properties.  */
143       SCM prop = get_property ("stemLeftBeamCount");
144       if (scm_is_number (prop))
145         {
146           Stem::set_beaming (stem_,scm_to_int (prop),LEFT);
147           context ()->unset_property (ly_symbol2scm ("stemLeftBeamCount"));
148         }
149       prop = get_property ("stemRightBeamCount");
150       if (scm_is_number (prop))
151         {
152           Stem::set_beaming (stem_,scm_to_int (prop), RIGHT);
153           context ()->unset_property (ly_symbol2scm ("stemRightBeamCount"));
154         }
155       stem_ = 0;
156     }
157   tremolo_ev_ = 0;
158 }
159
160 bool
161 Stem_engraver::try_music (Music *m)
162 {
163   if (m->is_mus_type ("tremolo-event"))
164     {
165       tremolo_ev_ = m;
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 */     "tremolo-event",
176 /* acks  */      "rhythmic-head-interface",
177 /* reads */       "tremoloFlags stemLeftBeamCount stemRightBeamCount",
178 /* write */       "");