]> git.donarmstrong.com Git - lilypond.git/blob - lily/script-engraver.cc
26d72f361deba3737a82b79ace6b38c9f5a3a2ca
[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@cs.uu.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   bool follow_into_staff_;
27   Script_tuple ()
28   {
29     follow_into_staff_ = false;
30     event_ = 0;
31     script_ = 0;
32   }
33 };
34
35 class Script_engraver : public Engraver
36 {
37   Array<Script_tuple> scripts_;
38   Spanner *slur_;
39
40 protected:
41   virtual bool try_music (Music *);
42   void stop_translation_timestep ();
43   void process_music ();
44
45   DECLARE_ACKNOWLEDGER (slur);
46   DECLARE_ACKNOWLEDGER (rhythmic_head);
47   DECLARE_ACKNOWLEDGER (stem);
48   DECLARE_ACKNOWLEDGER (note_column);
49
50 public:
51   TRANSLATOR_DECLARATIONS (Script_engraver);
52 };
53
54 Script_engraver::Script_engraver ()
55 {
56   slur_ = 0;
57 }
58
59 bool
60 Script_engraver::try_music (Music *m)
61 {
62   if (m->is_mus_type ("articulation-event"))
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                          m->get_property ("articulation-type")))
70           return true;
71
72       Script_tuple t;
73       t.event_ = m;
74       scripts_.push (t);
75       return true;
76     }
77   return false;
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, bool *follow, 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   SCM follow_scm = scm_assoc (ly_symbol2scm ("follow-into-staff"),
114                               art);
115
116   *follow = scm_is_pair (follow_scm) && to_boolean (scm_cdr (follow_scm));
117   bool priority_found = false;
118
119   for (SCM s = art; scm_is_pair (s); s = scm_cdr (s))
120     {
121       SCM sym = scm_caar (s);
122       SCM type = scm_object_property (sym, ly_symbol2scm ("backend-type?"));
123       if (!ly_is_procedure (type))
124         continue;
125
126       SCM val = scm_cdar (s);
127
128       if (sym == ly_symbol2scm ("script-priority"))
129         {
130           priority_found = true;
131           /* Make sure they're in order of user input by adding index i.
132              Don't use the direction in this priority. Smaller means closer
133              to the head.  */
134           int prio = scm_to_int (val) + index;
135
136           val = scm_from_int (prio);
137         }
138       if (p->get_property_data (sym) == SCM_EOL)
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 (int i = 0; i < scripts_.size (); i++)
155     {
156       Music *music = scripts_[i].event_;
157
158       Grob *p = make_item ("Script", music->self_scm ());
159
160       make_script_from_event (p, &scripts_[i].follow_into_staff_, context (),
161                               music->get_property ("articulation-type"),
162                               i);
163
164       scripts_[i].script_ = p;
165
166       SCM force_dir = music->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_rhythmic_head (Grob_info info)
189 {
190   if (info.music_cause ())
191     {
192       for (int i = 0; i < scripts_.size (); i++)
193         {
194           Grob *e = scripts_[i].script_;
195
196           if (Side_position_interface::get_axis (e) == X_AXIS
197               && !e->get_parent (Y_AXIS))
198             {
199               e->set_parent (info.grob (), Y_AXIS);
200             }
201           Side_position_interface::add_support (e, info.grob ());
202         }
203     }
204 }
205
206 void
207 Script_engraver::acknowledge_note_column (Grob_info info)
208 {
209   /* Make note column the parent of the script.  That is not
210      correct, but due to seconds in a chord, noteheads may be
211      swapped around horizontally.
212
213      As the note head to put it on is not known now, postpone this
214      decision to Script_interface::calc_direction ().  */
215   */
216   for (int i = 0; i < scripts_.size (); i++)
217     {
218       Grob *e = scripts_[i].script_;
219
220       if (!e->get_parent (X_AXIS)
221           && Side_position_interface::get_axis (e) == Y_AXIS)
222         e->set_parent (info.grob (), X_AXIS);
223     }
224 }
225
226 void
227 Script_engraver::acknowledge_slur (Grob_info info)
228 {
229   slur_ = info.spanner ();
230 }
231
232 void
233 Script_engraver::stop_translation_timestep ()
234 {
235   int script_count = scripts_.size ();
236   for (int i = 0; i < script_count; i++)
237     if (scripts_[i].follow_into_staff_)
238       {
239         Grob *sc = scripts_[i].script_;
240         sc->add_offset_callback (Side_position_interface
241                                  ::quantised_position_proc, Y_AXIS);
242         sc->set_property ("staff-padding", SCM_EOL);
243       }
244
245   scripts_.clear ();
246 }
247
248 #include "translator.icc"
249
250 ADD_ACKNOWLEDGER (Script_engraver, slur);
251 ADD_ACKNOWLEDGER (Script_engraver, rhythmic_head);
252 ADD_ACKNOWLEDGER (Script_engraver, stem);
253 ADD_ACKNOWLEDGER (Script_engraver, note_column);
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 */ "");