]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
Run `make grand-replace'.
[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--2008 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 (_ ("do not 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
146 void
147 Script_engraver::process_music ()
148 {
149   for (vsize i = 0; i < scripts_.size (); i++)
150     {
151       Stream_event *ev = scripts_[i].event_;
152
153       Grob *p = make_item ("Script", ev->self_scm ());
154
155       make_script_from_event (p, context (),
156                               ev->get_property ("articulation-type"),
157                               i);
158
159       scripts_[i].script_ = p;
160
161       SCM force_dir = ev->get_property ("direction");
162       if (is_direction (force_dir) && to_dir (force_dir))
163         p->set_property ("direction", force_dir);
164     }
165 }
166
167 void
168 Script_engraver::acknowledge_stem (Grob_info info)
169 {
170   int script_count = scripts_.size ();
171   for (int i = 0; i < script_count; i++)
172     {
173       Grob *e = scripts_[i].script_;
174
175       if (to_dir (e->get_property ("side-relative-direction")))
176         e->set_object ("direction-source", info.grob ()->self_scm ());
177
178       Side_position_interface::add_support (e, info.grob ());
179     }
180 }
181
182 void
183 Script_engraver::acknowledge_stem_tremolo (Grob_info info)
184 {
185   int script_count = scripts_.size ();
186   for (int i = 0; i < script_count; i++)
187     {
188       Grob *e = scripts_[i].script_;
189       Side_position_interface::add_support (e, info.grob ());
190     }
191 }
192
193
194 void
195 Script_engraver::acknowledge_rhythmic_head (Grob_info info)
196 {
197   if (info.event_cause ())
198     {
199       for (vsize i = 0; i < scripts_.size (); i++)
200         {
201           Grob *e = scripts_[i].script_;
202
203           if (Side_position_interface::get_axis (e) == X_AXIS
204               && !e->get_parent (Y_AXIS))
205             {
206               e->set_parent (info.grob (), Y_AXIS);
207             }
208           Side_position_interface::add_support (e, info.grob ());
209         }
210     }
211 }
212
213 void
214 Script_engraver::acknowledge_note_column (Grob_info info)
215 {
216   /* Make note column the parent of the script.  That is not
217      correct, but due to seconds in a chord, noteheads may be
218      swapped around horizontally.
219
220      As the note head to put it on is not known now, postpone this
221      decision to Script_interface::calc_direction ().  */
222   for (vsize i = 0; i < scripts_.size (); i++)
223     {
224       Grob *e = scripts_[i].script_;
225
226       if (!e->get_parent (X_AXIS)
227           && Side_position_interface::get_axis (e) == Y_AXIS)
228         e->set_parent (info.grob (), X_AXIS);
229     }
230 }
231
232 void
233 Script_engraver::stop_translation_timestep ()
234 {
235   scripts_.clear ();
236 }
237
238 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
239 ADD_ACKNOWLEDGER (Script_engraver, stem);
240 ADD_ACKNOWLEDGER (Script_engraver, note_column);
241 ADD_ACKNOWLEDGER (Script_engraver, stem_tremolo);
242
243 ADD_TRANSLATOR (Script_engraver,
244                 /* doc */
245                 "Handle note scripted articulations.",
246
247                 /* create */
248                 "Script ",
249
250                 /* read */
251                 "scriptDefinitions ",
252
253                 /* write */
254                 ""
255                 );