]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
cb3a007d53ed6f3e160e713ce5ec78373a5bbedc
[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
96 make_script_from_event (Grob *p,  Context *tg,
97                         SCM art_type, int index)
98 {
99   SCM alist = tg->get_property ("scriptDefinitions");
100   SCM art = scm_assoc (art_type, alist);
101
102   if (art == SCM_BOOL_F)
103     {
104       /* FIXME: */
105       warning (_ ("don't know how to interpret articulation: "));
106       warning (_ ("scheme encoding: "));
107       scm_write (art_type, scm_current_error_port ());
108       message ("");
109       return;
110     }
111
112   art = scm_cdr (art);
113
114   bool priority_found = false;
115
116   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
117     {
118       SCM sym = scm_caar (s);
119       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
120       if (!ly_is_procedure (type))
121         continue;
122
123       SCM val = scm_cdar (s);
124
125       if (sym == ly_symbol2scm ("script-priority"))
126         {
127           priority_found = true;
128           /* Make sure they're in order of user input by adding index i.
129              Don't use the direction in this priority. Smaller means closer
130              to the head.  */
131           int prio = scm_to_int (val) + index;
132
133           val = scm_from_int (prio);
134         }
135
136       SCM preset = p->get_property_data (sym);
137       if (val == SCM_EOL
138           || scm_call_1 (type, preset) == SCM_BOOL_F)
139         p->internal_set_property (sym, val);
140     }
141
142   if (!priority_found)
143     {
144       p->set_property ("script-priority",
145                        scm_from_int (index));
146     }
147
148   Side_position_interface::set_axis (p, Y_AXIS);
149 }
150
151 void
152 Script_engraver::process_music ()
153 {
154   for (vsize i = 0; i < scripts_.size (); i++)
155     {
156       Stream_event *ev = scripts_[i].event_;
157
158       Grob *p = make_item ("Script", ev->self_scm ());
159
160       make_script_from_event (p, context (),
161                               ev->get_property ("articulation-type"),
162                               i);
163
164       scripts_[i].script_ = p;
165
166       SCM force_dir = ev->get_property ("direction");
167       if (is_direction (force_dir) && to_dir (force_dir))
168         p->set_property ("direction", force_dir);
169     }
170 }
171
172 void
173 Script_engraver::acknowledge_stem (Grob_info info)
174 {
175   int script_count = scripts_.size ();
176   for (int i = 0; i < script_count; i++)
177     {
178       Grob *e = scripts_[i].script_;
179
180       if (to_dir (e->get_property ("side-relative-direction")))
181         e->set_object ("direction-source", info.grob ()->self_scm ());
182
183       Side_position_interface::add_support (e, info.grob ());
184     }
185 }
186
187 void
188 Script_engraver::acknowledge_stem_tremolo (Grob_info info)
189 {
190   int script_count = scripts_.size ();
191   for (int i = 0; i < script_count; i++)
192     {
193       Grob *e = scripts_[i].script_;
194       Side_position_interface::add_support (e, info.grob ());
195     }
196 }
197
198
199 void
200 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
201 {
202   if (info.event_cause ())
203     {
204       for (vsize i = 0; i < scripts_.size (); i++)
205         {
206           Grob *e = scripts_[i].script_;
207
208           if (Side_position_interface::get_axis (e) == X_AXIS
209               && !e->get_parent (Y_AXIS))
210             {
211               e->set_parent (info.grob (), Y_AXIS);
212             }
213           Side_position_interface::add_support (e, info.grob ());
214         }
215     }
216 }
217
218 void
219 Script_engraver::acknowledge_note_column (Grob_info info)
220 {
221   /* Make note column the parent of the script.  That is not
222      correct, but due to seconds in a chord, noteheads may be
223      swapped around horizontally.
224
225      As the note head to put it on is not known now, postpone this
226      decision to Script_interface::calc_direction ().  */
227   for (vsize i = 0; i < scripts_.size (); i++)
228     {
229       Grob *e = scripts_[i].script_;
230
231       if (!e->get_parent (X_AXIS)
232           && Side_position_interface::get_axis (e) == Y_AXIS)
233         e->set_parent (info.grob (), X_AXIS);
234     }
235 }
236
237 void
238 Script_engraver::acknowledge_slur (Grob_info info)
239 {
240   slur_ = info.spanner ();
241 }
242
243 void
244 Script_engraver::stop_translation_timestep ()
245 {
246   scripts_.clear ();
247 }
248
249 ADD_ACKNOWLEDGER (Script_engraver, slur);
250 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
251 ADD_ACKNOWLEDGER (Script_engraver, stem);
252 ADD_ACKNOWLEDGER (Script_engraver, note_column);
253 ADD_ACKNOWLEDGER (Script_engraver, stem_tremolo);
254
255 ADD_TRANSLATOR (Script_engraver,
256                 /* doc */ "Handles note scripted articulations.",
257                 /* create */ "Script",
258                 /* accept */ "script-event articulation-event",
259                 /* read */ "scriptDefinitions",
260                 /* write */ "");