]> git.donarmstrong.com Git - lilypond.git/blob - lily/glissando-engraver.cc
Adds a regtest for glissando index.
[lilypond.git] / lily / glissando-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2000--2011 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   DECLARE_TRANSLATOR_LISTENER (glissando);
39   DECLARE_ACKNOWLEDGER (note_column);
40   virtual void finalize ();
41
42   void stop_translation_timestep ();
43   void process_music ();
44
45 private:
46   vector<Spanner *> lines_;
47   bool start_glissandi;
48   bool stop_glissandi;
49
50   Stream_event *event_;
51   vector<vsize> note_column_1;
52   vector<vsize> note_column_2;
53 };
54
55 Glissando_engraver::Glissando_engraver ()
56 {
57   event_ = 0;
58   start_glissandi = false;
59   stop_glissandi = false;
60 }
61
62 IMPLEMENT_TRANSLATOR_LISTENER (Glissando_engraver, glissando);
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 (start_glissandi)
81     {
82       extract_grob_set (g, "note-heads", note_heads);
83       SCM map = get_property ("glissandoMap");
84       if (map == SCM_EOL)
85         for (vsize i = 0; i < note_heads.size (); i++)
86           {
87             note_column_1.push_back (i);
88             note_column_2.push_back (i);
89           }
90       else
91         for (SCM m = map; scm_is_pair (m); m = scm_cdr (m))
92           {
93             SCM candidate = scm_car (m);
94             if (!scm_is_pair (candidate))
95               continue;
96             int n1 = robust_scm2int (scm_car (candidate), -1);
97             int n2 = robust_scm2int (scm_cdr (candidate), -1);
98             if (n1 < 0 || n2 < 0 || n1 >= note_heads.size ())
99               continue;
100             note_column_1.push_back (vsize (n1));
101             note_column_2.push_back (vsize (n2));
102           }
103       for (vsize i=0; i < note_column_1.size (); i++)
104         {
105           lines_.push_back (make_spanner ("Glissando", event_->self_scm ()));
106           lines_.back ()->set_bound (LEFT, note_heads[note_column_1[i]]);
107         }
108     }
109
110   if (stop_glissandi)
111     {
112       extract_grob_set (g, "note-heads", note_heads);
113       int glissando_index = 0;
114       for (vsize i=0; i < note_column_1.size (); i++)
115         {
116           if (note_column_2[i] >= note_heads.size ())
117             lines_[i]->suicide ();
118           else
119             {
120               lines_[i]->set_bound (RIGHT, note_heads[note_column_2[i]]);
121               lines_[i]->set_property ("glissando-index", scm_from_int (glissando_index));
122               glissando_index++;
123               announce_end_grob (lines_[i], note_heads[note_column_2[i]]->self_scm ());
124             }
125         }
126       lines_.clear ();
127       note_column_1.clear ();
128       note_column_2.clear ();
129       stop_glissandi = false;
130     }      
131 }
132
133 void
134 Glissando_engraver::stop_translation_timestep ()
135 {
136
137   if (start_glissandi)
138     {
139       if (stop_glissandi)
140         programming_error ("overwriting glissando");
141       stop_glissandi = true;
142     }
143
144   start_glissandi = false;
145   event_ = 0;
146 }
147
148 void
149 Glissando_engraver::finalize ()
150 {
151   if (!lines_.empty ())
152     {
153       string msg = _ ("unterminated glissando");
154
155       if (event_)
156         event_->origin ()->warning (msg);
157       else
158         warning (msg);
159
160       for (vsize i=0; i < lines_.size (); i++)
161         lines_[i]->suicide ();
162     }
163 }
164
165 ADD_ACKNOWLEDGER (Glissando_engraver, note_column);
166 ADD_TRANSLATOR (Glissando_engraver,
167                 /* doc */
168                 "Engrave glissandi.",
169
170                 /* create */
171                 "Glissando ",
172
173                 /* read */
174                 "glissandoMap ",
175
176                 /* write */
177                 ""
178                 );