]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
Keep a staff alive with multiple layers
[lilypond.git] / lily / script-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
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 "context.hh"
23 #include "directional-element-interface.hh"
24 #include "international.hh"
25 #include "note-column.hh"
26 #include "paper-column.hh"
27 #include "rhythmic-head.hh"
28 #include "script-interface.hh"
29 #include "side-position-interface.hh"
30 #include "slur.hh"
31 #include "staff-symbol-referencer.hh"
32 #include "stem.hh"
33 #include "stream-event.hh"
34 #include "warn.hh"
35
36 #include "translator.icc"
37
38 struct Script_tuple
39 {
40   Stream_event *event_;
41   Grob *script_;
42   Script_tuple ()
43   {
44     event_ = 0;
45     script_ = 0;
46   }
47 };
48
49 class Script_engraver : public Engraver
50 {
51   vector<Script_tuple> scripts_;
52
53 protected:
54   void stop_translation_timestep ();
55   void process_music ();
56
57   void listen_articulation (Stream_event *);
58   void acknowledge_rhythmic_head (Grob_info);
59   void acknowledge_stem (Grob_info);
60   void acknowledge_stem_tremolo (Grob_info);
61   void acknowledge_tie (Grob_info);
62   void acknowledge_end_tie (Grob_info);
63   void acknowledge_note_column (Grob_info);
64   void acknowledge_inline_accidental (Grob_info);
65
66 public:
67   TRANSLATOR_DECLARATIONS (Script_engraver);
68 };
69
70 Script_engraver::Script_engraver ()
71 {
72 }
73
74 void
75 Script_engraver::listen_articulation (Stream_event *ev)
76 {
77   /* Discard double articulations for part-combining.  */
78   for (vsize i = 0; i < scripts_.size (); i++)
79     if (ly_is_equal (scripts_[i].event_
80                      ->get_property ("articulation-type"),
81                      ev->get_property ("articulation-type")))
82       return;
83
84   Script_tuple t;
85   t.event_ = ev;
86   scripts_.push_back (t);
87 }
88
89 void
90 copy_property (Grob *g, SCM sym, SCM alist)
91 {
92   if (scm_is_null (g->get_property (sym)))
93     {
94       SCM entry = scm_assoc (sym, alist);
95       if (scm_is_pair (entry))
96         g->set_property (sym, scm_cdr (entry));
97     }
98 }
99
100 /* Add the properties, one by one for each Script.  A little memory
101    could be saved by tacking the props onto the Script grob (i.e. make
102    ScriptStaccato , ScriptMarcato, etc. ).
103 */
104 void
105 make_script_from_event (Grob *p, Context *tg,
106                         SCM art_type, int index)
107 {
108   SCM alist = tg->get_property ("scriptDefinitions");
109   SCM art = scm_assoc (art_type, alist);
110
111   if (scm_is_false (art))
112     {
113       /* FIXME: */
114       warning (_ ("do not know how to interpret articulation:"));
115       warning (_ (" scheme encoding: "));
116       scm_write (art_type, scm_current_error_port ());
117       message ("");
118       return;
119     }
120
121   art = scm_cdr (art);
122
123   bool priority_found = false;
124
125   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
126     {
127       SCM sym = scm_caar (s);
128       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
129       if (!ly_is_procedure (type))
130         continue;
131
132       SCM val = scm_cdar (s);
133
134       if (scm_is_eq (sym, ly_symbol2scm ("script-priority")))
135         {
136           priority_found = true;
137           /* Make sure they're in order of user input by adding index i.
138              Don't use the direction in this priority. Smaller means closer
139              to the head.  */
140           int prio = scm_to_int (val) + index;
141
142           val = scm_from_int (prio);
143         }
144
145       SCM preset = p->get_property_data (sym);
146       if (scm_is_null (val)
147           || scm_is_false (scm_call_1 (type, preset)))
148         p->set_property (sym, val);
149     }
150
151   if (!priority_found)
152     {
153       p->set_property ("script-priority",
154                        scm_from_int (index));
155     }
156 }
157
158 void
159 Script_engraver::process_music ()
160 {
161   for (vsize i = 0; i < scripts_.size (); i++)
162     {
163       Stream_event *ev = scripts_[i].event_;
164
165       Grob *p = make_item ("Script", ev->self_scm ());
166
167       make_script_from_event (p, context (),
168                               ev->get_property ("articulation-type"),
169                               i);
170
171       scripts_[i].script_ = p;
172
173       SCM force_dir = ev->get_property ("direction");
174       if (is_direction (force_dir) && to_dir (force_dir))
175         p->set_property ("direction", force_dir);
176     }
177 }
178
179 void
180 Script_engraver::acknowledge_stem (Grob_info info)
181 {
182   for (vsize i = 0; i < scripts_.size (); i++)
183     {
184       Grob *e = scripts_[i].script_;
185
186       if (to_dir (e->get_property ("side-relative-direction")))
187         e->set_object ("direction-source", info.grob ()->self_scm ());
188
189       Side_position_interface::add_support (e, info.grob ());
190     }
191 }
192
193 void
194 Script_engraver::acknowledge_stem_tremolo (Grob_info info)
195 {
196   for (vsize i = 0; i < scripts_.size (); i++)
197     {
198       Grob *e = scripts_[i].script_;
199       Side_position_interface::add_support (e, info.grob ());
200     }
201 }
202
203 void
204 Script_engraver::acknowledge_tie (Grob_info info)
205 {
206   for (vsize i = 0; i < scripts_.size (); i++)
207     {
208       Grob *e = scripts_[i].script_;
209       Side_position_interface::add_support (e, info.grob ());
210     }
211 }
212
213 void
214 Script_engraver::acknowledge_end_tie (Grob_info info)
215 {
216   for (vsize i = 0; i < scripts_.size (); i++)
217     {
218       Grob *e = scripts_[i].script_;
219       Side_position_interface::add_support (e, info.grob ());
220     }
221 }
222
223 void
224 Script_engraver::acknowledge_inline_accidental (Grob_info info)
225 {
226   for (vsize i = 0; i < scripts_.size (); i++)
227     {
228       Grob *e = scripts_[i].script_;
229       Side_position_interface::add_support (e, info.grob ());
230     }
231 }
232
233 void
234 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
235 {
236   if (info.event_cause ())
237     {
238       for (vsize i = 0; i < scripts_.size (); i++)
239         {
240           Grob *e = scripts_[i].script_;
241
242           if (Side_position_interface::get_axis (e) == X_AXIS
243               && !e->get_parent (Y_AXIS))
244             {
245               e->set_parent (info.grob (), Y_AXIS);
246             }
247           Side_position_interface::add_support (e, info.grob ());
248         }
249     }
250 }
251
252 void
253 Script_engraver::acknowledge_note_column (Grob_info info)
254 {
255   /* Make note column the parent of the script.  That is not
256      correct, but due to seconds in a chord, noteheads may be
257      swapped around horizontally.
258
259      As the note head to put it on is not known now, postpone this
260      decision to Script_interface::calc_direction ().  */
261   for (vsize i = 0; i < scripts_.size (); i++)
262     {
263       Grob *e = scripts_[i].script_;
264
265       if (!e->get_parent (X_AXIS)
266           && Side_position_interface::get_axis (e) == Y_AXIS)
267         e->set_parent (info.grob (), X_AXIS);
268     }
269 }
270
271 void
272 Script_engraver::stop_translation_timestep ()
273 {
274   scripts_.clear ();
275 }
276
277
278 void
279 Script_engraver::boot ()
280 {
281   ADD_LISTENER (Script_engraver, articulation);
282   ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
283   ADD_ACKNOWLEDGER (Script_engraver, stem);
284   ADD_ACKNOWLEDGER (Script_engraver, tie);
285   ADD_END_ACKNOWLEDGER (Script_engraver, tie);
286   ADD_ACKNOWLEDGER (Script_engraver, note_column);
287   ADD_ACKNOWLEDGER (Script_engraver, stem_tremolo);
288   ADD_ACKNOWLEDGER (Script_engraver, inline_accidental);
289 }
290
291 ADD_TRANSLATOR (Script_engraver,
292                 /* doc */
293                 "Handle note scripted articulations.",
294
295                 /* create */
296                 "Script ",
297
298                 /* read */
299                 "scriptDefinitions ",
300
301                 /* write */
302                 ""
303                );