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