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