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