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"
20 #include "translator.icc"
28 bool follow_into_staff_;
35 note_event_ = finger_event_ = 0;
36 follow_into_staff_ = false;
38 static int compare (Finger_tuple const &c1, Finger_tuple const &c2)
40 return c1.position_- c2.position_;
44 class New_fingering_engraver : public Engraver
46 vector<Finger_tuple> fingerings_;
47 vector<Finger_tuple> articulations_;
48 vector<Finger_tuple> string_numbers_;
54 TRANSLATOR_DECLARATIONS (New_fingering_engraver);
56 void stop_translation_timestep ();
57 DECLARE_ACKNOWLEDGER (rhythmic_head);
58 DECLARE_ACKNOWLEDGER (stem);
59 void add_fingering (Grob *, Music *, Music *);
60 void add_script (Grob *, Music *, Music *);
61 void add_string (Grob *, Music *, Music *);
62 void position_scripts (SCM orientations, vector<Finger_tuple> *);
66 New_fingering_engraver::acknowledge_rhythmic_head (Grob_info inf)
68 Music *note_ev = inf.music_cause ();
72 SCM arts = note_ev->get_property ("articulations");
74 for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
76 Music *m = unsmob_music (scm_car (s));
81 if (m->is_mus_type ("fingering-event"))
82 add_fingering (inf.grob (), m, note_ev);
83 else if (m->is_mus_type ("text-script-event"))
84 m->origin ()->warning (_ ("can't add text scripts to individual note heads"));
85 else if (m->is_mus_type ("script-event"))
86 add_script (inf.grob (), m, note_ev);
87 else if (m->is_mus_type ("string-number-event"))
88 add_string (inf.grob (), m, note_ev);
89 else if (m->is_mus_type ("harmonic-event"))
91 inf.grob ()->set_property ("style", ly_symbol2scm ("harmonic"));
92 Grob *d = unsmob_grob (inf.grob ()->get_object ("dot"));
98 heads_.push_back (inf.grob ());
102 New_fingering_engraver::acknowledge_stem (Grob_info inf)
108 New_fingering_engraver::add_script (Grob *head,
116 Grob *g = make_item ("Script", event->self_scm ());
117 make_script_from_event (g, context (), event->get_property ("articulation-type"), 0);
122 articulations_.push_back (ft);
124 ft.script_->set_parent (head, X_AXIS);
129 New_fingering_engraver::add_fingering (Grob *head,
135 ft.script_ = make_item ("Fingering", event->self_scm ());
137 Side_position_interface::add_support (ft.script_, head);
139 int d = scm_to_int (event->get_property ("digit"));
144 Should add support for thumb. It's a little involved, since
145 the thumb lives in a different font. Maybe it should be moved?
151 music for the softenon children?
153 event->origin ()->warning (_ ("music for the martians."));
155 SCM sstr = scm_number_to_string (scm_from_int (d), scm_from_int (10));
156 ft.script_->set_property ("text", sstr);
158 ft.finger_event_ = event;
159 ft.note_event_ = hevent;
162 fingerings_.push_back (ft);
166 New_fingering_engraver::add_string (Grob *head,
172 ft.script_ = make_item ("StringNumber", event->self_scm ());
174 Side_position_interface::add_support (ft.script_, head);
176 int d = scm_to_int (event->get_property ("string-number"));
178 SCM sstr = scm_number_to_string (scm_from_int (d), scm_from_int (10));
179 ft.script_->set_property ("text", sstr);
181 ft.finger_event_ = event;
182 ft.note_event_ = hevent;
185 string_numbers_.push_back (ft);
189 New_fingering_engraver::position_scripts (SCM orientations,
190 vector<Finger_tuple> *scripts)
192 for (vsize i = 0; i < scripts->size (); i++)
193 if (stem_ && to_boolean (scripts->at (i).script_->get_property ("add-stem-support")))
194 Side_position_interface::add_support (scripts->at (i).script_, stem_);
197 This is not extremely elegant, but we have to do a little
198 formatting here, because the parent/child relations should be
199 known before we move on to the next time step.
201 A more sophisticated approach would be to set both X and Y parents
202 to the note head, and write a more flexible function for
203 positioning the fingerings, setting both X and Y coordinates.
205 for (vsize i = 0; i < scripts->size (); i++)
206 (*scripts)[i].position_ = scm_to_int ((*scripts)[i].head_->get_property ("staff-position"));
208 for (vsize i = scripts->size (); i--;)
209 for (vsize j = heads_.size (); j--;)
210 Side_position_interface::add_support ((*scripts)[i].script_, heads_[j]);
212 vector<Finger_tuple> up, down, horiz;
213 for (vsize i = scripts->size (); i--;)
215 SCM d = (*scripts)[i].finger_event_->get_property ("direction");
218 ((to_dir (d) == UP) ? up : down).push_back ((*scripts)[i]);
219 scripts->erase (scripts->begin () + i);
223 vector_sort (*scripts, Finger_tuple::compare);
225 bool up_p = scm_c_memq (ly_symbol2scm ("up"), orientations) != SCM_BOOL_F;
226 bool down_p = scm_c_memq (ly_symbol2scm ("down"), orientations) != SCM_BOOL_F;
227 bool left_p = scm_c_memq (ly_symbol2scm ("left"), orientations) != SCM_BOOL_F;
228 bool right_p = scm_c_memq (ly_symbol2scm ("right"), orientations) != SCM_BOOL_F;
229 Direction hordir = (right_p) ? RIGHT : LEFT;
230 if (left_p || right_p)
232 if (up_p && !up.size () && scripts->size ())
234 up.push_back (scripts->back ());
235 scripts->pop_back ();
238 if (down_p && !down.size () && scripts->size ())
240 down.push_back ((*scripts)[0]);
241 scripts->erase (scripts->begin ());
244 horiz.insert (horiz.end (), scripts->begin (), scripts->end ());
246 else if (up_p && down_p)
248 int center = scripts->size () / 2;
249 down.insert (down.end (), scripts->begin (), scripts->begin () + center);
250 up.insert (up.end (), scripts->begin () + center, scripts->end ());
254 up.insert (up.end (), scripts->begin (), scripts->end ());
261 warning (_ ("no placement found for fingerings"));
262 warning (_ ("placing below"));
264 down.insert (down.end (), scripts->begin (), scripts->end ());
268 for (vsize i = 0; i < horiz.size (); i++)
270 Finger_tuple ft = horiz[i];
271 Grob *f = ft.script_;
272 f->set_parent (ft.head_, X_AXIS);
273 f->set_parent (ft.head_, Y_AXIS);
275 Self_alignment_interface::set_align_self (f, Y_AXIS);
276 Self_alignment_interface::set_center_parent (f, Y_AXIS);
277 Side_position_interface::set_axis (f, X_AXIS);
279 f->set_property ("direction", scm_from_int (hordir));
282 int finger_prio = 200;
285 Drul_array< vector<Finger_tuple> > vertical (down, up);
288 for (vsize i = 0; i < vertical[d].size (); i++)
290 Finger_tuple ft = vertical[d][i];
291 Grob *f = ft.script_;
292 f->set_parent (ft.head_, X_AXIS);
293 f->set_property ("script-priority",
294 scm_from_int (finger_prio + d * ft.position_));
296 Self_alignment_interface::set_align_self (f, X_AXIS);
297 Self_alignment_interface::set_center_parent (f, X_AXIS);
298 Side_position_interface::set_axis (f, Y_AXIS);
300 f->set_property ("direction", scm_from_int (d));
303 while (flip (&d) != DOWN);
307 New_fingering_engraver::stop_translation_timestep ()
309 if (fingerings_.size ())
311 position_scripts (get_property ("fingeringOrientations"),
313 fingerings_.clear ();
316 if (string_numbers_.size ())
318 position_scripts (get_property ("stringNumberOrientations"),
320 string_numbers_.clear ();
323 for (vsize i = articulations_.size (); i--;)
325 Grob *script = articulations_[i].script_;
327 for (vsize j = heads_.size (); j--;)
328 Side_position_interface::add_support (script, heads_[j]);
330 if (stem_ && to_dir (script->get_property ("side-relative-direction")))
331 script->set_object ("direction-source", stem_->self_scm ());
333 if (stem_ && to_boolean (script->get_property ("add-stem-support")))
334 Side_position_interface::add_support (script, stem_);
341 articulations_.clear ();
344 New_fingering_engraver::New_fingering_engraver ()
348 ADD_ACKNOWLEDGER (New_fingering_engraver, rhythmic_head);
349 ADD_ACKNOWLEDGER (New_fingering_engraver, stem);
351 ADD_TRANSLATOR (New_fingering_engraver,
352 /* doc */ "Create fingering-scripts for notes in a new chord. "
353 "This engraver is ill-named, since it "
354 "also takes care of articulations and harmonic note heads",
355 /* create */ "Fingering",
357 /* read */ "fingeringOrientations",