]> git.donarmstrong.com Git - lilypond.git/blob - lily/glissando-engraver.cc
Merge branch 'master' into lilypond/translation
[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   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 IMPLEMENT_TRANSLATOR_LISTENER (Glissando_engraver, glissando);
64 void
65 Glissando_engraver::listen_glissando (Stream_event *ev)
66 {
67   ASSIGN_EVENT_ONCE (event_, ev);
68 }
69
70 void
71 Glissando_engraver::process_music ()
72 {
73   if (event_)
74     start_glissandi = true;
75 }
76
77 void
78 Glissando_engraver::acknowledge_note_column (Grob_info info)
79 {
80   Grob *g = info.grob ();
81   if (stop_glissandi)
82     {
83       extract_grob_set (g, "note-heads", note_heads);
84       int glissando_index = 0;
85       for (vsize i=0; i < note_column_1.size (); i++)
86         {
87           if (note_column_2[i] >= note_heads.size ())
88             {
89               kill_me_.push_back (lines_[i]);
90               announce_end_grob (lines_[i], SCM_EOL);
91             }
92           else
93             {
94               lines_[i]->set_bound (RIGHT, note_heads[note_column_2[i]]);
95               lines_[i]->set_property ("glissando-index", scm_from_int (glissando_index));
96               glissando_index++;
97               announce_end_grob (lines_[i], note_heads[note_column_2[i]]->self_scm ());
98             }
99         }
100       lines_.clear ();
101       note_column_1.clear ();
102       note_column_2.clear ();
103       stop_glissandi = false;
104     }      
105
106   if (start_glissandi)
107     {
108       extract_grob_set (g, "note-heads", note_heads);
109       SCM map = get_property ("glissandoMap");
110       if (map == SCM_EOL)
111         for (vsize i = 0; i < note_heads.size (); i++)
112           {
113             note_column_1.push_back (i);
114             note_column_2.push_back (i);
115           }
116       else
117         for (SCM m = map; scm_is_pair (m); m = scm_cdr (m))
118           {
119             SCM candidate = scm_car (m);
120             if (!scm_is_pair (candidate))
121               continue;
122             int n1 = robust_scm2int (scm_car (candidate), -1);
123             int n2 = robust_scm2int (scm_cdr (candidate), -1);
124             if (n1 < 0 || n2 < 0 || n1 >= note_heads.size ())
125               continue;
126             note_column_1.push_back (vsize (n1));
127             note_column_2.push_back (vsize (n2));
128           }
129       for (vsize i=0; i < note_column_1.size (); i++)
130         {
131           lines_.push_back (make_spanner ("Glissando", event_->self_scm ()));
132           lines_.back ()->set_bound (LEFT, note_heads[note_column_1[i]]);
133         }
134     }
135 }
136
137 void
138 Glissando_engraver::stop_translation_timestep ()
139 {
140
141   if (start_glissandi)
142     {
143       if (stop_glissandi)
144         programming_error ("overwriting glissando");
145       stop_glissandi = true;
146       start_glissandi = false;
147     }
148   event_ = 0;
149 }
150
151 void
152 Glissando_engraver::finalize ()
153 {
154   if (!lines_.empty ())
155     {
156       string msg = _ ("unterminated glissando");
157
158       if (event_)
159         event_->origin ()->warning (msg);
160       else
161         warning (msg);
162
163       for (vsize i=0; i < lines_.size (); i++)
164         lines_[i]->suicide ();
165     }
166
167   for (vsize i=0; i < kill_me_.size (); i++)
168     kill_me_[i]->suicide ();
169 }
170
171 ADD_ACKNOWLEDGER (Glissando_engraver, note_column);
172 ADD_TRANSLATOR (Glissando_engraver,
173                 /* doc */
174                 "Engrave glissandi.",
175
176                 /* create */
177                 "Glissando ",
178
179                 /* read */
180                 "glissandoMap ",
181
182                 /* write */
183                 ""
184                 );