2 fingering-engraver.cc -- implement New_fingering_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
11 #include "international.hh"
12 #include "rhythmic-head.hh"
13 #include "script-interface.hh"
14 #include "self-alignment-interface.hh"
15 #include "side-position-interface.hh"
17 #include "stream-event.hh"
20 #include "translator.icc"
28 Stream_event *note_event_;
29 Stream_event *finger_event_;
30 bool follow_into_staff_;
37 note_event_ = finger_event_ = 0;
38 follow_into_staff_ = false;
43 operator< (Finger_tuple const &a, Finger_tuple const &b)
45 return a.position_ < b.position_;
48 class New_fingering_engraver : public Engraver
50 vector<Finger_tuple> fingerings_;
51 vector<Finger_tuple> stroke_fingerings_;
52 vector<Finger_tuple> articulations_;
53 vector<Finger_tuple> string_numbers_;
60 TRANSLATOR_DECLARATIONS (New_fingering_engraver);
62 void stop_translation_timestep ();
63 DECLARE_ACKNOWLEDGER (rhythmic_head);
64 DECLARE_ACKNOWLEDGER (stem);
65 void add_fingering (Grob *, SCM,
66 vector<Finger_tuple> *,
67 Stream_event *, Stream_event *);
68 void add_script (Grob *, Stream_event *, Stream_event *);
69 void add_string (Grob *, Stream_event *, Stream_event *);
70 void position_scripts (SCM orientations, vector<Finger_tuple> *);
74 New_fingering_engraver::acknowledge_rhythmic_head (Grob_info inf)
76 Stream_event *note_ev = inf.event_cause ();
80 SCM arts = note_ev->get_property ("articulations");
82 for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
84 Stream_event *ev = unsmob_stream_event (scm_car (s));
89 if (ev->in_event_class ("fingering-event"))
90 add_fingering (inf.grob (),
91 ly_symbol2scm ("Fingering"),
94 else if (ev->in_event_class ("text-script-event"))
95 ev->origin ()->warning (_ ("can't add text scripts to individual note heads"));
96 else if (ev->in_event_class ("script-event"))
97 add_script (inf.grob (), ev, note_ev);
98 else if (ev->in_event_class ("string-number-event"))
99 add_fingering (inf.grob (),
100 ly_symbol2scm ("StringNumber"), &string_numbers_,
102 else if (ev->in_event_class ("stroke-finger-event"))
103 add_fingering (inf.grob (),
104 ly_symbol2scm ("StrokeFinger"), &stroke_fingerings_,
106 else if (ev->in_event_class ("harmonic-event"))
108 inf.grob ()->set_property ("style", ly_symbol2scm ("harmonic"));
109 Grob *d = unsmob_grob (inf.grob ()->get_object ("dot"));
115 heads_.push_back (inf.grob ());
119 New_fingering_engraver::acknowledge_stem (Grob_info inf)
125 New_fingering_engraver::add_script (Grob *head,
133 Grob *g = make_item ("Script", event->self_scm ());
134 make_script_from_event (g, context (),
135 event->get_property ("articulation-type"), 0);
137 ft.script_->set_parent (head, X_AXIS);
139 articulations_.push_back (ft);
143 New_fingering_engraver::add_fingering (Grob *head,
145 vector<Finger_tuple> *tuple_vector,
147 Stream_event *hevent)
151 ft.script_ = internal_make_item (grob_sym, event->self_scm (),
152 ly_symbol2string (grob_sym).c_str (),
153 __FILE__, __LINE__, __FUNCTION__
156 Side_position_interface::add_support (ft.script_, head);
158 ft.finger_event_ = event;
159 ft.note_event_ = hevent;
162 tuple_vector->push_back (ft);
166 New_fingering_engraver::position_scripts (SCM orientations,
167 vector<Finger_tuple> *scripts)
169 for (vsize i = 0; i < scripts->size (); i++)
170 if (stem_ && to_boolean (scripts->at (i).script_->get_property ("add-stem-support")))
171 Side_position_interface::add_support (scripts->at (i).script_, stem_);
174 This is not extremely elegant, but we have to do a little
175 formatting here, because the parent/child relations should be
176 known before we move on to the next time step.
178 A more sophisticated approach would be to set both X and Y parents
179 to the note head, and write a more flexible function for
180 positioning the fingerings, setting both X and Y coordinates.
182 for (vsize i = 0; i < scripts->size (); i++)
183 (*scripts)[i].position_ = scm_to_int ((*scripts)[i].head_->get_property ("staff-position"));
185 for (vsize i = scripts->size (); i--;)
186 for (vsize j = heads_.size (); j--;)
187 Side_position_interface::add_support ((*scripts)[i].script_, heads_[j]);
189 vector<Finger_tuple> up, down, horiz;
190 for (vsize i = scripts->size (); i--;)
192 SCM d = (*scripts)[i].finger_event_->get_property ("direction");
195 ((to_dir (d) == UP) ? up : down).push_back ((*scripts)[i]);
196 scripts->erase (scripts->begin () + i);
200 vector_sort (*scripts, less<Finger_tuple> ());
202 bool up_p = scm_c_memq (ly_symbol2scm ("up"), orientations) != SCM_BOOL_F;
203 bool down_p = scm_c_memq (ly_symbol2scm ("down"), orientations) != SCM_BOOL_F;
204 bool left_p = scm_c_memq (ly_symbol2scm ("left"), orientations) != SCM_BOOL_F;
205 bool right_p = scm_c_memq (ly_symbol2scm ("right"), orientations) != SCM_BOOL_F;
206 Direction hordir = (right_p) ? RIGHT : LEFT;
207 if (left_p || right_p)
209 if (up_p && !up.size () && scripts->size ())
211 up.push_back (scripts->back ());
212 scripts->pop_back ();
215 if (down_p && !down.size () && scripts->size ())
217 down.push_back ((*scripts)[0]);
218 scripts->erase (scripts->begin ());
221 horiz.insert (horiz.end (), scripts->begin (), scripts->end ());
223 else if (up_p && down_p)
225 int center = scripts->size () / 2;
226 down.insert (down.end (), scripts->begin (), scripts->begin () + center);
227 up.insert (up.end (), scripts->begin () + center, scripts->end ());
231 up.insert (up.end (), scripts->begin (), scripts->end ());
238 warning (_ ("no placement found for fingerings"));
239 warning (_ ("placing below"));
241 down.insert (down.end (), scripts->begin (), scripts->end ());
245 for (vsize i = 0; i < horiz.size (); i++)
247 Finger_tuple ft = horiz[i];
248 Grob *f = ft.script_;
249 f->set_parent (ft.head_, X_AXIS);
250 f->set_parent (ft.head_, Y_AXIS);
252 Self_alignment_interface::set_align_self (f, Y_AXIS);
253 Self_alignment_interface::set_center_parent (f, Y_AXIS);
254 Side_position_interface::set_axis (f, X_AXIS);
256 f->set_property ("direction", scm_from_int (hordir));
259 int finger_prio = 200;
262 Drul_array< vector<Finger_tuple> > vertical (down, up);
265 for (vsize i = 0; i < vertical[d].size (); i++)
267 Finger_tuple ft = vertical[d][i];
268 Grob *f = ft.script_;
269 f->set_parent (ft.head_, X_AXIS);
270 f->set_property ("script-priority",
271 scm_from_int (finger_prio + d * ft.position_));
273 Self_alignment_interface::set_align_self (f, X_AXIS);
274 Self_alignment_interface::set_center_parent (f, X_AXIS);
275 Side_position_interface::set_axis (f, Y_AXIS);
277 f->set_property ("direction", scm_from_int (d));
280 while (flip (&d) != DOWN);
284 New_fingering_engraver::stop_translation_timestep ()
293 New_fingering_engraver::position_all ()
295 if (fingerings_.size ())
297 position_scripts (get_property ("fingeringOrientations"),
299 fingerings_.clear ();
302 if (string_numbers_.size ())
304 position_scripts (get_property ("stringNumberOrientations"),
306 string_numbers_.clear ();
309 if (stroke_fingerings_.size ())
311 position_scripts (get_property ("strokeFingerOrientations"),
312 &stroke_fingerings_);
313 stroke_fingerings_.clear ();
316 for (vsize i = articulations_.size (); i--;)
318 Grob *script = articulations_[i].script_;
320 for (vsize j = heads_.size (); j--;)
321 Side_position_interface::add_support (script, heads_[j]);
323 if (stem_ && to_dir (script->get_property ("side-relative-direction")))
324 script->set_object ("direction-source", stem_->self_scm ());
326 if (stem_ && to_boolean (script->get_property ("add-stem-support")))
327 Side_position_interface::add_support (script, stem_);
329 articulations_.clear ();
332 New_fingering_engraver::New_fingering_engraver ()
338 ADD_ACKNOWLEDGER (New_fingering_engraver, rhythmic_head);
339 ADD_ACKNOWLEDGER (New_fingering_engraver, stem);
341 ADD_TRANSLATOR (New_fingering_engraver,
342 /* doc */ "Create fingering-scripts for notes in a new chord. "
343 "This engraver is ill-named, since it "
344 "also takes care of articulations and harmonic note heads",
353 "fingeringOrientations "
354 "strokeFingerOrientations "
355 "stringNumberOrientations "