]> git.donarmstrong.com Git - lilypond.git/blob - lily/glissando-engraver.cc
Issue 4997/2: Use Preinit class in Scheme_engraver
[lilypond.git] / lily / glissando-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "international.hh"
23 #include "pointer-group-interface.hh"
24 #include "rhythmic-head.hh"
25 #include "spanner.hh"
26 #include "stream-event.hh"
27 #include "warn.hh"
28 #include "item.hh"
29
30 #include "translator.icc"
31
32 class Glissando_engraver : public Engraver
33 {
34 public:
35   TRANSLATOR_DECLARATIONS (Glissando_engraver);
36
37 protected:
38   void listen_glissando (Stream_event *);
39   void acknowledge_note_column (Grob_info);
40   virtual void finalize ();
41
42   void stop_translation_timestep ();
43   void process_music ();
44
45 private:
46   vector<Spanner *> lines_;
47   vector<Spanner *> kill_me_;
48   bool start_glissandi_;
49   bool stop_glissandi_;
50
51   Stream_event *event_;
52   vector<vsize> note_column_1;
53   vector<vsize> note_column_2;
54 };
55
56 Glissando_engraver::Glissando_engraver ()
57 {
58   event_ = 0;
59   start_glissandi_ = false;
60   stop_glissandi_ = false;
61 }
62
63 void
64 Glissando_engraver::listen_glissando (Stream_event *ev)
65 {
66   ASSIGN_EVENT_ONCE (event_, ev);
67 }
68
69 void
70 Glissando_engraver::process_music ()
71 {
72   if (event_)
73     start_glissandi_ = true;
74 }
75
76 void
77 Glissando_engraver::acknowledge_note_column (Grob_info info)
78 {
79   Grob *g = info.grob ();
80   if (to_boolean (g->get_property ("glissando-skip")))
81     return;
82
83   if (stop_glissandi_)
84     {
85       extract_grob_set (g, "note-heads", note_heads);
86       int glissando_index = 0;
87       for (vsize i = 0; i < note_column_1.size (); i++)
88         {
89           if (note_column_2[i] >= note_heads.size ())
90             {
91               kill_me_.push_back (lines_[i]);
92               announce_end_grob (lines_[i], SCM_EOL);
93             }
94           else
95             {
96               lines_[i]->set_bound (RIGHT, note_heads[note_column_2[i]]);
97               lines_[i]->set_property ("glissando-index", scm_from_int (glissando_index));
98               glissando_index++;
99               announce_end_grob (lines_[i], note_heads[note_column_2[i]]->self_scm ());
100             }
101         }
102       lines_.clear ();
103       note_column_1.clear ();
104       note_column_2.clear ();
105       stop_glissandi_ = false;
106     }
107
108   if (start_glissandi_)
109     {
110       extract_grob_set (g, "note-heads", note_heads);
111       SCM map = get_property ("glissandoMap");
112       if (scm_is_null (map))
113         for (vsize i = 0; i < note_heads.size (); i++)
114           {
115             note_column_1.push_back (i);
116             note_column_2.push_back (i);
117           }
118       else
119         for (SCM m = map; scm_is_pair (m); m = scm_cdr (m))
120           {
121             SCM candidate = scm_car (m);
122             if (!scm_is_pair (candidate))
123               continue;
124             int n1 = robust_scm2int (scm_car (candidate), -1);
125             int n2 = robust_scm2int (scm_cdr (candidate), -1);
126             if ((n1 < 0) || (n2 < 0) || (size_t (n1) >= note_heads.size ()))
127               continue;
128             note_column_1.push_back (vsize (n1));
129             note_column_2.push_back (vsize (n2));
130           }
131       for (vsize i = 0; i < note_column_1.size (); i++)
132         {
133           lines_.push_back (make_spanner ("Glissando", event_->self_scm ()));
134           lines_.back ()->set_bound (LEFT, note_heads[note_column_1[i]]);
135         }
136     }
137 }
138
139 void
140 Glissando_engraver::stop_translation_timestep ()
141 {
142   if (start_glissandi_)
143     {
144       if (stop_glissandi_)
145         programming_error ("overwriting glissando");
146       stop_glissandi_ = true;
147       start_glissandi_ = false;
148     }
149   event_ = 0;
150 }
151
152 void
153 Glissando_engraver::finalize ()
154 {
155   if (!lines_.empty ())
156     {
157       string msg = _ ("unterminated glissando");
158
159       if (event_)
160         event_->origin ()->warning (msg);
161       else
162         warning (msg);
163
164       for (vsize i = 0; i < lines_.size (); i++)
165         lines_[i]->suicide ();
166     }
167
168   for (vsize i = 0; i < kill_me_.size (); i++)
169     kill_me_[i]->suicide ();
170 }
171
172 void
173 Glissando_engraver::boot ()
174 {
175   ADD_LISTENER (Glissando_engraver, glissando);
176   ADD_ACKNOWLEDGER (Glissando_engraver, note_column);
177 }
178
179 ADD_TRANSLATOR (Glissando_engraver,
180                 /* doc */
181                 "Engrave glissandi.",
182
183                 /* create */
184                 "Glissando ",
185
186                 /* read */
187                 "glissandoMap ",
188
189                 /* write */
190                 ""
191                );