1 /*
2 This file is part of LilyPond, the GNU music typesetter.
3
4 Copyright (C) 2000--2020 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 using std::string;
33 using std::vector;
34
35 class Glissando_engraver : public Engraver
36 {
37 public:
38 TRANSLATOR_DECLARATIONS (Glissando_engraver);
39
40 protected:
41 void listen_glissando (Stream_event *);
42 void acknowledge_note_column (Grob_info);
43 void finalize () override;
44
45 void stop_translation_timestep ();
46 void process_music ();
47
48 private:
49 vector<Spanner *> lines_;
50 vector<Spanner *> kill_me_;
51 bool start_glissandi_;
52 bool stop_glissandi_;
53
54 Stream_event *event_;
55 vector<vsize> note_column_1;
56 vector<vsize> note_column_2;
57 };
58
Glissando_engraver(Context * c)59 Glissando_engraver::Glissando_engraver (Context *c)
60 : Engraver (c)
61 {
62 event_ = 0;
63 start_glissandi_ = false;
64 stop_glissandi_ = false;
65 }
66
67 void
listen_glissando(Stream_event * ev)68 Glissando_engraver::listen_glissando (Stream_event *ev)
69 {
70 ASSIGN_EVENT_ONCE (event_, ev);
71 }
72
73 void
process_music()74 Glissando_engraver::process_music ()
75 {
76 if (event_)
77 start_glissandi_ = true;
78 }
79
80 void
acknowledge_note_column(Grob_info info)81 Glissando_engraver::acknowledge_note_column (Grob_info info)
82 {
83 Grob *g = info.grob ();
84 if (from_scm<bool> (get_property (g, "glissando-skip")))
85 return;
86
87 if (stop_glissandi_)
88 {
89 extract_grob_set (g, "note-heads", note_heads);
90 int glissando_index = 0;
91 for (vsize i = 0; i < note_column_1.size (); i++)
92 {
93 if (note_column_2[i] >= note_heads.size ())
94 {
95 kill_me_.push_back (lines_[i]);
96 announce_end_grob (lines_[i], SCM_EOL);
97 }
98 else
99 {
100 lines_[i]->set_bound (RIGHT, note_heads[note_column_2[i]]);
101 set_property (lines_[i], "glissando-index", to_scm (glissando_index));
102 glissando_index++;
103 announce_end_grob (lines_[i], note_heads[note_column_2[i]]->self_scm ());
104 }
105 }
106 lines_.clear ();
107 note_column_1.clear ();
108 note_column_2.clear ();
109 stop_glissandi_ = false;
110 }
111
112 if (start_glissandi_)
113 {
114 extract_grob_set (g, "note-heads", note_heads);
115 SCM map = get_property (this, "glissandoMap");
116 if (scm_is_null (map))
117 for (vsize i = 0; i < note_heads.size (); i++)
118 {
119 note_column_1.push_back (i);
120 note_column_2.push_back (i);
121 }
122 else
123 for (SCM m = map; scm_is_pair (m); m = scm_cdr (m))
124 {
125 SCM candidate = scm_car (m);
126 if (!scm_is_pair (candidate))
127 continue;
128 int n1 = from_scm (scm_car (candidate), -1);
129 int n2 = from_scm (scm_cdr (candidate), -1);
130 if ((n1 < 0) || (n2 < 0) || (size_t (n1) >= note_heads.size ()))
131 continue;
132 note_column_1.push_back (vsize (n1));
133 note_column_2.push_back (vsize (n2));
134 }
135 for (vsize i = 0; i < note_column_1.size (); i++)
136 {
137 lines_.push_back (make_spanner ("Glissando", event_->self_scm ()));
138 lines_.back ()->set_bound (LEFT, note_heads[note_column_1[i]]);
139 }
140 }
141 }
142
143 void
stop_translation_timestep()144 Glissando_engraver::stop_translation_timestep ()
145 {
146 if (start_glissandi_)
147 {
148 if (stop_glissandi_)
149 programming_error ("overwriting glissando");
150 stop_glissandi_ = true;
151 start_glissandi_ = false;
152 }
153 event_ = 0;
154 }
155
156 void
finalize()157 Glissando_engraver::finalize ()
158 {
159 if (!lines_.empty ())
160 {
161 string msg = _ ("unterminated glissando");
162
163 if (event_)
164 event_->warning (msg);
165 else
166 warning (msg);
167
168 for (vsize i = 0; i < lines_.size (); i++)
169 lines_[i]->suicide ();
170 }
171
172 for (vsize i = 0; i < kill_me_.size (); i++)
173 kill_me_[i]->suicide ();
174 }
175
176 void
boot()177 Glissando_engraver::boot ()
178 {
179 ADD_LISTENER (Glissando_engraver, glissando);
180 ADD_ACKNOWLEDGER (Glissando_engraver, note_column);
181 }
182
183 ADD_TRANSLATOR (Glissando_engraver,
184 /* doc */
185 "Engrave glissandi.",
186
187 /* create */
188 "Glissando ",
189
190 /* read */
191 "glissandoMap ",
192
193 /* write */
194 ""
195 );
196