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