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