2 fingering-engraver.cc -- implement New_fingering_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #include "engraver.hh"
11 #include "side-position-interface.hh"
13 #include "rhythmic-head.hh"
14 #include "self-alignment-interface.hh"
15 #include "script-interface.hh"
18 #include "translator.icc"
26 bool follow_into_staff_;
33 note_event_ = finger_event_ = 0;
34 follow_into_staff_ = false;
36 static int compare (Finger_tuple const &c1, Finger_tuple const &c2)
38 return c1.position_- c2.position_;
42 class New_fingering_engraver : public Engraver
44 Array<Finger_tuple> fingerings_;
45 Array<Finger_tuple> articulations_;
46 Array<Finger_tuple> string_numbers_;
48 Link_array<Grob> heads_;
52 TRANSLATOR_DECLARATIONS (New_fingering_engraver);
54 void stop_translation_timestep ();
55 DECLARE_ACKNOWLEDGER (rhythmic_head);
56 DECLARE_ACKNOWLEDGER (stem);
57 void add_fingering (Grob *, Music *, Music *);
58 void add_script (Grob *, Music *, Music *);
59 void add_string (Grob *, Music *, Music *);
60 void position_scripts (SCM orientations, Array<Finger_tuple> *);
64 New_fingering_engraver::acknowledge_rhythmic_head (Grob_info inf)
66 Music *note_ev = inf.music_cause ();
70 SCM arts = note_ev->get_property ("articulations");
72 for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
74 Music *m = unsmob_music (scm_car (s));
79 if (m->is_mus_type ("fingering-event"))
80 add_fingering (inf.grob (), m, note_ev);
81 else if (m->is_mus_type ("text-script-event"))
82 m->origin ()->warning (_ ("can't add text scripts to individual note heads"));
83 else if (m->is_mus_type ("script-event"))
84 add_script (inf.grob (), m, note_ev);
85 else if (m->is_mus_type ("string-number-event"))
86 add_string (inf.grob (), m, note_ev);
87 else if (m->is_mus_type ("harmonic-event"))
89 inf.grob ()->set_property ("style", ly_symbol2scm ("harmonic"));
90 Grob *d = unsmob_grob (inf.grob ()->get_object ("dot"));
96 heads_.push (inf.grob ());
100 New_fingering_engraver::acknowledge_stem (Grob_info inf)
106 New_fingering_engraver::add_script (Grob *head,
114 Grob *g = make_item ("Script", event->self_scm ());
115 make_script_from_event (g, context (), event->get_property ("articulation-type"), 0);
120 articulations_.push (ft);
122 ft.script_->set_parent (head, X_AXIS);
127 New_fingering_engraver::add_fingering (Grob *head,
133 ft.script_ = make_item ("Fingering", event->self_scm ());
135 Side_position_interface::add_support (ft.script_, head);
137 int d = scm_to_int (event->get_property ("digit"));
142 Should add support for thumb. It's a little involved, since
143 the thumb lives in a different font. Maybe it should be moved?
149 music for the softenon children?
151 event->origin ()->warning (_ ("music for the martians."));
153 SCM sstr = scm_number_to_string (scm_from_int (d), scm_from_int (10));
154 ft.script_->set_property ("text", sstr);
156 ft.finger_event_ = event;
157 ft.note_event_ = hevent;
160 fingerings_.push (ft);
164 New_fingering_engraver::add_string (Grob *head,
170 ft.script_ = make_item ("StringNumber", event->self_scm ());
172 Side_position_interface::add_support (ft.script_, head);
174 int d = scm_to_int (event->get_property ("string-number"));
176 SCM sstr = scm_number_to_string (scm_from_int (d), scm_from_int (10));
177 ft.script_->set_property ("text", sstr);
179 ft.finger_event_ = event;
180 ft.note_event_ = hevent;
183 string_numbers_.push (ft);
187 New_fingering_engraver::position_scripts (SCM orientations,
188 Array<Finger_tuple> *scripts)
190 for (int i = 0; i < scripts->size (); i++)
191 if (stem_ && to_boolean (scripts->elem (i).script_->get_property ("add-stem-support")))
192 Side_position_interface::add_support (scripts->elem (i).script_, stem_);
195 This is not extremely elegant, but we have to do a little
196 formatting here, because the parent/child relations should be
197 known before we move on to the next time step.
199 A more sophisticated approach would be to set both X and Y parents
200 to the note head, and write a more flexible function for
201 positioning the fingerings, setting both X and Y coordinates.
203 for (int i = 0; i < scripts->size (); i++)
204 (*scripts)[i].position_ = scm_to_int ((*scripts)[i].head_->get_property ("staff-position"));
206 for (int i = scripts->size (); i--;)
207 for (int j = heads_.size (); j--;)
208 Side_position_interface::add_support ((*scripts)[i].script_, heads_[j]);
210 Array<Finger_tuple> up, down, horiz;
211 for (int i = scripts->size (); i--;)
213 SCM d = (*scripts)[i].finger_event_->get_property ("direction");
216 ((to_dir (d) == UP) ? up : down).push ((*scripts)[i]);
221 scripts->sort (&Finger_tuple::compare);
223 bool up_p = scm_c_memq (ly_symbol2scm ("up"), orientations) != SCM_BOOL_F;
224 bool down_p = scm_c_memq (ly_symbol2scm ("down"), orientations) != SCM_BOOL_F;
225 bool left_p = scm_c_memq (ly_symbol2scm ("left"), orientations) != SCM_BOOL_F;
226 bool right_p = scm_c_memq (ly_symbol2scm ("right"), orientations) != SCM_BOOL_F;
227 Direction hordir = (right_p) ? RIGHT : LEFT;
228 if (left_p || right_p)
230 if (up_p && !up.size () && scripts->size ())
231 up.push (scripts->pop ());
233 if (down_p && !down.size () && scripts->size ())
235 down.push ((*scripts)[0]);
239 horiz.concat (*scripts);
241 else if (up_p && down_p)
243 int center = scripts->size () / 2;
244 down.concat (scripts->slice (0, center));
245 up.concat (scripts->slice (center, scripts->size ()));
249 up.concat (*scripts);
256 warning (_ ("no placement found for fingerings"));
257 warning (_ ("placing below"));
259 down.concat (*scripts);
263 for (int i = 0; i < horiz.size (); i++)
265 Finger_tuple ft = horiz[i];
266 Grob *f = ft.script_;
267 f->set_parent (ft.head_, X_AXIS);
268 f->set_parent (ft.head_, Y_AXIS);
270 Self_alignment_interface::set_align_self (f, Y_AXIS);
271 Self_alignment_interface::set_center_parent (f, Y_AXIS);
272 Side_position_interface::set_axis (f, X_AXIS);
274 f->set_property ("direction", scm_from_int (hordir));
277 int finger_prio = 200;
280 Drul_array< Array<Finger_tuple> > vertical (down, up);
283 for (int i = 0; i < vertical[d].size (); i++)
285 Finger_tuple ft = vertical[d][i];
286 Grob *f = ft.script_;
287 f->set_parent (ft.head_, X_AXIS);
288 f->set_property ("script-priority",
289 scm_from_int (finger_prio + d * ft.position_));
291 Self_alignment_interface::set_align_self (f, X_AXIS);
292 Self_alignment_interface::set_center_parent (f, X_AXIS);
293 Side_position_interface::set_axis (f, Y_AXIS);
295 f->set_property ("direction", scm_from_int (d));
298 while (flip (&d) != DOWN);
302 New_fingering_engraver::stop_translation_timestep ()
304 if (fingerings_.size ())
306 position_scripts (get_property ("fingeringOrientations"),
308 fingerings_.clear ();
311 if (string_numbers_.size ())
313 position_scripts (get_property ("stringNumberOrientations"),
315 string_numbers_.clear ();
318 for (int i = articulations_.size (); i--;)
320 Grob *script = articulations_[i].script_;
322 for (int j = heads_.size (); j--;)
323 Side_position_interface::add_support (script, heads_[j]);
325 if (stem_ && to_dir (script->get_property ("side-relative-direction")))
326 script->set_object ("direction-source", stem_->self_scm ());
328 if (stem_ && to_boolean (script->get_property ("add-stem-support")))
329 Side_position_interface::add_support (script, stem_);
336 articulations_.clear ();
339 New_fingering_engraver::New_fingering_engraver ()
343 ADD_ACKNOWLEDGER (New_fingering_engraver, rhythmic_head);
344 ADD_ACKNOWLEDGER (New_fingering_engraver, stem);
346 ADD_TRANSLATOR (New_fingering_engraver,
347 /* doc */ "Create fingering-scripts for notes in a new chord. "
348 "This engraver is ill-named, since it "
349 "also takes care of articulations and harmonic note heads",
350 /* create */ "Fingering",
352 /* read */ "fingeringOrientations",