]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
6baa67bfc05d8d20536fc4b18d88afe63499780d
[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--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "context.hh"
10 #include "directional-element-interface.hh"
11 #include "engraver.hh"
12 #include "slur.hh"
13 #include "note-column.hh"
14 #include "paper-column.hh"
15 #include "rhythmic-head.hh"
16 #include "script-interface.hh"
17 #include "side-position-interface.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "stem.hh"
20 #include "warn.hh"
21
22 struct Script_tuple
23 {
24   Music *event_;
25   Grob *script_;
26   Script_tuple ()
27   {
28     event_ = 0;
29     script_ = 0;
30   }
31 };
32
33 class Script_engraver : public Engraver
34 {
35   Array<Script_tuple> scripts_;
36   Spanner *slur_;
37
38 protected:
39   virtual bool try_music (Music *);
40   void stop_translation_timestep ();
41   void process_music ();
42
43   DECLARE_ACKNOWLEDGER (slur);
44   DECLARE_ACKNOWLEDGER (rhythmic_head);
45   DECLARE_ACKNOWLEDGER (stem);
46   DECLARE_ACKNOWLEDGER (stem_tremolo);
47   DECLARE_ACKNOWLEDGER (note_column);
48
49 public:
50   TRANSLATOR_DECLARATIONS (Script_engraver);
51 };
52
53 Script_engraver::Script_engraver ()
54 {
55   slur_ = 0;
56 }
57
58 bool
59 Script_engraver::try_music (Music *m)
60 {
61   if (m->is_mus_type ("articulation-event"))
62     {
63       /* Discard double articulations for part-combining.  */
64       int script_count = scripts_.size ();
65       for (int i = 0; i < script_count; i++)
66         if (ly_is_equal (scripts_[i].event_
67                          ->get_property ("articulation-type"),
68                          m->get_property ("articulation-type")))
69           return true;
70
71       Script_tuple t;
72       t.event_ = m;
73       scripts_.push (t);
74       return true;
75     }
76   return false;
77 }
78
79 void
80 copy_property (Grob *g, SCM sym, SCM alist)
81 {
82   if (g->internal_get_property (sym) == SCM_EOL)
83     {
84       SCM entry = scm_assoc (sym, alist);
85       if (scm_is_pair (entry))
86         g->internal_set_property (sym, scm_cdr (entry));
87     }
88 }
89
90 /* Add the properties, one by one for each Script.  A little memory
91    could be saved by tacking the props onto the Script grob (i.e. make
92    ScriptStaccato , ScriptMarcato, etc. ).
93 */
94 void make_script_from_event (Grob *p,  Context *tg,
95                              SCM art_type, int index)
96 {
97   SCM alist = tg->get_property ("scriptDefinitions");
98   SCM art = scm_assoc (art_type, alist);
99
100   if (art == SCM_BOOL_F)
101     {
102       /* FIXME: */
103       warning (_ ("don't know how to interpret articulation: "));
104       warning (_ ("scheme encoding: "));
105       scm_write (art_type, scm_current_error_port ());
106       message ("");
107       return;
108     }
109
110   art = scm_cdr (art);
111
112   bool priority_found = false;
113
114   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
115     {
116       SCM sym = scm_caar (s);
117       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
118       if (!ly_is_procedure (type))
119         continue;
120
121       SCM val = scm_cdar (s);
122
123       if (sym == ly_symbol2scm ("script-priority"))
124         {
125           priority_found = true;
126           /* Make sure they're in order of user input by adding index i.
127              Don't use the direction in this priority. Smaller means closer
128              to the head.  */
129           int prio = scm_to_int (val) + index;
130
131           val = scm_from_int (prio);
132         }
133
134       SCM preset = p->get_property_data (sym);
135       if (scm_call_1 (type, preset) == SCM_BOOL_F)
136         p->internal_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 (int i = 0; i < scripts_.size (); i++)
152     {
153       Music *music = scripts_[i].event_;
154
155       Grob *p = make_item ("Script", music->self_scm ());
156
157       make_script_from_event (p, context (),
158                               music->get_property ("articulation-type"),
159                               i);
160
161       scripts_[i].script_ = p;
162
163       SCM force_dir = music->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.music_cause ())
200     {
201       for (int 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 (int 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::acknowledge_slur (Grob_info info)
236 {
237   slur_ = info.spanner ();
238 }
239
240 void
241 Script_engraver::stop_translation_timestep ()
242 {
243   scripts_.clear ();
244 }
245
246 #include "translator.icc"
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 */ "");