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