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