]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
Fix some bugs in the dynamic engraver and PostScript backend
[lilypond.git] / lily / chord-tremolo-engraver.cc
1 /*
2   chord-tremolo-engraver.cc -- implement Chord_tremolo_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 2000--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7                  Erik Sandberg <mandolaerik@gmail.com>
8 */
9
10 #include "math.h" // ceil
11
12 #include "beam.hh"
13 #include "engraver-group.hh"
14 #include "international.hh"
15 #include "item.hh"
16 #include "misc.hh"
17 #include "repeated-music.hh"
18 #include "rhythmic-head.hh"
19 #include "spanner.hh"
20 #include "stem-tremolo.hh"
21 #include "stem.hh"
22 #include "warn.hh"
23
24 #include "translator.icc"
25
26 /**
27
28 This acknowledges repeated music with "tremolo" style.  It typesets
29 a beam.
30
31 TODO:
32
33 - perhaps use engraver this to steer other engravers? That would
34 create dependencies between engravers, which is bad.
35
36 - create dots if appropriate.
37
38 - create TremoloBeam iso Beam?
39 */
40 class Chord_tremolo_engraver : public Engraver
41 {
42   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
43 protected:
44   Music *repeat_;
45
46   int flags_;
47   // number of beams for short tremolos
48   int expected_beam_count_;
49   // current direction of beam (first RIGHT, then LEFT)
50   Direction beam_dir_;
51
52   Spanner *beam_;
53 protected:
54   virtual void finalize ();
55   virtual bool try_music (Music *);
56   void process_music ();
57   DECLARE_ACKNOWLEDGER (stem);
58 };
59
60 Chord_tremolo_engraver::Chord_tremolo_engraver ()
61 {
62   beam_ = 0;
63   repeat_ = 0;
64   flags_ = 0;
65   expected_beam_count_ = 0;
66   beam_dir_ = CENTER;
67 }
68
69 bool
70 Chord_tremolo_engraver::try_music (Music *m)
71 {
72   if (m->is_mus_type ("tremolo-span-event"))
73     {
74       Direction span_dir = to_dir (m->get_property ("span-direction"));
75       if (span_dir == START)
76         {
77           repeat_ = m;
78           int type = scm_to_int (m->get_property ("tremolo-type"));
79           /* e.g. 1 for type 8, 2 for type 16 */
80           flags_ = intlog2 (type) - 2;
81           expected_beam_count_ = scm_to_int (m->get_property ("expected-beam-count"));
82           beam_dir_ = RIGHT;
83         }
84       if (span_dir == STOP)
85         {
86           repeat_ = 0;
87           beam_ = 0;
88           expected_beam_count_ = 0;
89           beam_dir_ = CENTER;
90         }
91       return true;
92     }
93   return false;
94 }
95
96 void
97 Chord_tremolo_engraver::process_music ()
98 {
99   if (repeat_ && !beam_)
100     {
101       beam_ = make_spanner ("Beam", repeat_->self_scm ());
102     }
103 }
104
105 void
106 Chord_tremolo_engraver::finalize ()
107 {
108   if (beam_)
109     {
110       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
111       beam_->suicide ();
112     }
113 }
114
115 void
116 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
117 {
118   if (beam_)
119     {
120       Grob *s = info.grob ();
121
122       Stem::set_beaming (s, flags_, beam_dir_);
123
124       if (Stem::duration_log (s) != 1)
125         beam_->set_property ("gap-count", scm_from_int (flags_ - expected_beam_count_));
126
127       if (beam_dir_ == RIGHT)
128         beam_dir_ = LEFT;
129
130       if (info.ultimate_music_cause ()->is_mus_type ("rhythmic-event"))
131         Beam::add_stem (beam_, s);
132       else
133         {
134           string s = _ ("stem must have Rhythmic structure");
135           if (info.music_cause ())
136             info.music_cause ()->origin ()->warning (s);
137           else
138             ::warning (s);
139         }
140     }
141 }
142
143 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
144 ADD_TRANSLATOR (Chord_tremolo_engraver,
145                 /* doc */ "Generates beams for tremolo repeats.",
146                 /* create */ "Beam",
147                 /* accept */ "tremolo-span-event",
148                 /* read */ "",
149                 /* write */ "");