]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-tremolo-engraver.cc
Issue 4965: Create and use Grob::parent_relative
[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 /**
38
39 This acknowledges repeated music with "tremolo" style.  It typesets
40 a beam.
41
42 TODO:
43
44 - perhaps use engraver this to steer other engravers? That would
45 create dependencies between engravers, which is bad.
46
47 - create dots if appropriate.
48
49 - create TremoloBeam iso Beam?
50 */
51 class Chord_tremolo_engraver : public Engraver
52 {
53   TRANSLATOR_DECLARATIONS (Chord_tremolo_engraver);
54 protected:
55   Stream_event *repeat_;
56
57   Spanner *beam_;
58   // Store the pointer to the previous stem, so we can create a beam if
59   // necessary and end the spanner
60   Grob *previous_stem_;
61
62 protected:
63   virtual void finalize ();
64   void process_music ();
65   void listen_tremolo_span (Stream_event *);
66   void acknowledge_stem (Grob_info);
67 };
68
69 Chord_tremolo_engraver::Chord_tremolo_engraver ()
70 {
71   beam_ = 0;
72   repeat_ = 0;
73   previous_stem_ = 0;
74 }
75
76 void
77 Chord_tremolo_engraver::listen_tremolo_span (Stream_event *ev)
78 {
79   Direction span_dir = to_dir (ev->get_property ("span-direction"));
80   if (span_dir == START)
81     {
82       ASSIGN_EVENT_ONCE (repeat_, ev);
83     }
84   else if (span_dir == STOP)
85     {
86       if (!repeat_)
87         ev->origin ()->warning (_ ("No tremolo to end"));
88       repeat_ = 0;
89       beam_ = 0;
90       previous_stem_ = 0;
91     }
92 }
93
94 void
95 Chord_tremolo_engraver::process_music ()
96 {
97   if (repeat_ && !beam_)
98     {
99       beam_ = make_spanner ("Beam", repeat_->self_scm ());
100     }
101 }
102
103 void
104 Chord_tremolo_engraver::finalize ()
105 {
106   if (beam_)
107     {
108       repeat_->origin ()->warning (_ ("unterminated chord tremolo"));
109       announce_end_grob (beam_, SCM_EOL);
110       beam_->suicide ();
111     }
112 }
113
114 void
115 Chord_tremolo_engraver::acknowledge_stem (Grob_info info)
116 {
117   if (beam_)
118     {
119       int tremolo_type = robust_scm2int (repeat_->get_property ("tremolo-type"), 1);
120       int flags = max (0, intlog2 (tremolo_type) - 2);
121       int repeat_count = robust_scm2int (repeat_->get_property ("repeat-count"), 1);
122       int gap_count = min (flags, intlog2 (repeat_count) + 1);
123
124       Grob *s = info.grob ();
125       if (previous_stem_)
126         {
127           // FIXME: We know that the beam has ended only in listen_tremolo_span
128           //        but then it is too late for Spanner_break_forbid_engraver
129           //        to allow a line break... So, as a nasty hack, announce the
130           //        spanner's end after each note except the first. The only
131           //        "drawback" is that for multi-note tremolos a break would
132           //        theoretically be allowed after the second note (but since
133           //        that note is typically not at a barline, I don't think
134           //        anyone will ever notice!)
135           announce_end_grob (beam_, previous_stem_->self_scm ());
136           // Create the whole beam between previous and current note
137           Stem::set_beaming (previous_stem_, flags, RIGHT);
138           Stem::set_beaming (s, flags, LEFT);
139         }
140
141       if (Stem::duration_log (s) != 1)
142         beam_->set_property ("gap-count", scm_from_int (gap_count));
143
144       if (info.ultimate_event_cause ()->in_event_class ("rhythmic-event"))
145         Beam::add_stem (beam_, s);
146       else
147         {
148           string s = _ ("stem must have Rhythmic structure");
149           if (info.event_cause ())
150             info.event_cause ()->origin ()->warning (s);
151           else
152             ::warning (s);
153         }
154       // Store current grob, so we can possibly end the spanner here (and
155       // reset the beam direction to RIGHT)
156       previous_stem_ = s;
157     }
158 }
159
160 void
161 Chord_tremolo_engraver::boot ()
162 {
163   ADD_LISTENER (Chord_tremolo_engraver, tremolo_span);
164   ADD_ACKNOWLEDGER (Chord_tremolo_engraver, stem);
165 }
166
167 ADD_TRANSLATOR (Chord_tremolo_engraver,
168                 /* doc */
169                 "Generate beams for tremolo repeats.",
170
171                 /* create */
172                 "Beam ",
173
174                 /* read */
175                 "",
176
177                 /* write */
178                 ""
179                );