]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / chord-tremolo-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5                  Erik Sandberg <mandolaerik@gmail.com>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "beam.hh"
22 #include "engraver-group.hh"
23 #include "international.hh"
24 #include "item.hh"
25 #include "math.h" // ceil
26 #include "misc.hh"
27 #include "repeated-music.hh"
28 #include "rhythmic-head.hh"
29 #include "spanner.hh"
30 #include "stem-tremolo.hh"
31 #include "stem.hh"
32 #include "stream-event.hh"
33 #include "warn.hh"
34
35 #include "translator.icc"
36
37 using std::string;
38
39 /**
40
41 This acknowledges repeated music with "tremolo" style.  It typesets
42 a beam.
43
44 TODO:
45
46 - perhaps use engraver this to steer other engravers? That would
47 create dependencies between engravers, which is bad.
48
49 - create dots if appropriate.
50
51 - create TremoloBeam iso Beam?
52 */
53 class Chord_tremolo_engraver : public Engraver
54 {
55   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
56 protected:
57   Stream_event *repeat_;
58
59   Spanner *beam_;
60   // Store the pointer to the previous stem, so we can create a beam if
61   // necessary and end the spanner
62   Grob *previous_stem_;
63
64 protected:
65   virtual void finalize ();
66   void process_music ();
67   DECLARE_TRANSLATOR_LISTENER (tremolo_span);
68   DECLARE_ACKNOWLEDGER (stem);
69 };
70
71 Chord_tremolo_engraver::Chord_tremolo_engraver ()
72 {
73   beam_ = 0;
74   repeat_ = 0;
75   previous_stem_ = 0;
76 }
77
78 IMPLEMENT_TRANSLATOR_LISTENER (Chord_tremolo_engraver, tremolo_span);
79 void
80 Chord_tremolo_engraver::listen_tremolo_span (Stream_event *ev)
81 {
82   Direction span_dir = to_dir (ev->get_property ("span-direction"));
83   if (span_dir == START)
84     {
85       ASSIGN_EVENT_ONCE (repeat_, ev);
86     }
87   else if (span_dir == STOP)
88     {
89       if (!repeat_)
90         ev->origin ()->warning (_ ("No tremolo to end"));
91       repeat_ = 0;
92       beam_ = 0;
93       previous_stem_ = 0;
94     }
95 }
96
97 void
98 Chord_tremolo_engraver::process_music ()
99 {
100   if (repeat_ && !beam_)
101     {
102       beam_ = make_spanner ("Beam", repeat_->self_scm ());
103     }
104 }
105
106 void
107 Chord_tremolo_engraver::finalize ()
108 {
109   if (beam_)
110     {
111       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
112       announce_end_grob (beam_, SCM_EOL);
113       beam_->suicide ();
114     }
115 }
116
117 void
118 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
119 {
120   if (beam_)
121     {
122       int tremolo_type = robust_scm2int (repeat_->get_property ("tremolo-type"), 1);
123       int flags = max (0, intlog2 (tremolo_type) - 2);
124       int repeat_count = robust_scm2int (repeat_->get_property ("repeat-count"), 1);
125       int gap_count = min (flags, intlog2 (repeat_count) + 1);
126
127       Grob *s = info.grob ();
128       if (previous_stem_)
129         {
130           // FIXME: We know that the beam has ended only in listen_tremolo_span
131           //        but then it is too late for Spanner_break_forbid_engraver
132           //        to allow a line break... So, as a nasty hack, announce the
133           //        spanner's end after each note except the first. The only
134           //        "drawback" is that for multi-note tremolos a break would
135           //        theoretically be allowed after the second note (but since
136           //        that note is typically not at a barline, I don't think
137           //        anyone will ever notice!)
138           announce_end_grob (beam_, previous_stem_->self_scm ());
139           // Create the whole beam between previous and current note
140           Stem::set_beaming (previous_stem_, flags, RIGHT);
141           Stem::set_beaming (s, flags, LEFT);
142         }
143
144       if (Stem::duration_log (s) != 1)
145         beam_->set_property ("gap-count", scm_from_int (gap_count));
146
147       if (info.ultimate_event_cause ()->in_event_class ("rhythmic-event"))
148         Beam::add_stem (beam_, s);
149       else
150         {
151           string s = _ ("stem must have Rhythmic structure");
152           if (info.event_cause ())
153             info.event_cause ()->origin ()->warning (s);
154           else
155             ::warning (s);
156         }
157       // Store current grob, so we can possibly end the spanner here (and
158       // reset the beam direction to RIGHT)
159       previous_stem_ = s;
160     }
161 }
162
163 ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
164 ADD_TRANSLATOR (Chord_tremolo_engraver,
165                 /* doc */
166                 "Generate beams for tremolo repeats.",
167
168                 /* create */
169                 "Beam ",
170
171                 /* read */
172                 "",
173
174                 /* write */
175                 ""
176                );