2 fingering-engraver.cc -- implement New_fingering_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "engraver.hh"
12 #include "side-position-interface.hh"
16 #include "rhythmic-head.hh"
17 #include "self-alignment-interface.hh"
34 note_event_ = finger_event_ = 0;
35 description_ = SCM_EOL;
37 static int compare (Finger_tuple const & c1, Finger_tuple const & c2)
39 return c1.position_- c2.position_;
44 class New_fingering_engraver : public Engraver
46 Array<Finger_tuple> fingerings_;
47 Array<Finger_tuple> articulations_;
48 Link_array<Grob> heads_;
52 TRANSLATOR_DECLARATIONS(New_fingering_engraver);
54 virtual void stop_translation_timestep ();
55 virtual void acknowledge_grob (Grob_info);
56 void add_fingering (Grob*, Music*,Music*);
57 void add_script (Grob*, Music*,Music*);
58 void position_scripts();
62 New_fingering_engraver::acknowledge_grob (Grob_info inf)
64 if (Rhythmic_head::has_interface (inf.grob_))
66 Music * note_ev =inf.music_cause ();
68 SCM arts = note_ev->get_mus_property ("articulations");
70 for (SCM s = arts; gh_pair_p (s); s = gh_cdr (s))
72 Music * m = unsmob_music (gh_car (s));
78 if (m->is_mus_type ("fingering-event"))
80 add_fingering (inf.grob_ , m, note_ev);
82 else if (m->is_mus_type ("text-script-event"))
84 m->origin ()->warning ("Can not add text scripts to individual note heads");
86 else if (m->is_mus_type ("script-event"))
88 add_script (inf.grob_, m, note_ev);
92 heads_.push (inf.grob_);
94 else if (Stem::has_interface (inf.grob_))
100 extern void make_script_from_event (Grob *, SCM * descr, Translator_group*tg, Music * event,
103 New_fingering_engraver::add_script (Grob * head,
109 Grob * g= make_item ("Script");
110 make_script_from_event (g, &ft.description_, daddy_trans_, event, 0);
115 articulations_.push (ft);
116 announce_grob (g, event->self_scm ());
118 ft.script_->set_parent (head, X_AXIS);
124 New_fingering_engraver::add_fingering (Grob * head,
130 ft.script_ = make_item ("Fingering");
131 announce_grob (ft.script_, event->self_scm());
133 Side_position_interface::add_support (ft.script_, head);
135 int d = gh_scm2int ( event->get_mus_property ("digit"));
140 Should add support for thumb. It's a little involved, since
141 the thumb lives in a different font. Maybe it should be moved?
147 music for the softenon children?
149 event->origin()->warning (_("music for the martians."));
151 SCM sstr = scm_number_to_string (gh_int2scm (d), gh_int2scm (10)) ;
152 ft.script_->set_grob_property ("text", sstr);
154 ft.finger_event_ = event;
155 ft.note_event_ = hevent;
158 fingerings_.push (ft);
162 New_fingering_engraver::position_scripts ()
166 This is not extremely elegant, but we have to do a little
167 formatting here, because the parent/child relations should be
168 known before we move on to the next time step.
170 A more sophisticated approach would be to set both X and Y parents
171 to the note head, and write a more flexible function for
172 positioning the fingerings, setting both X and Y coordinates.
174 for (int i = 0; i < fingerings_.size(); i++)
176 fingerings_[i].position_ = gh_scm2int (fingerings_[i].head_ -> get_grob_property( "staff-position"));
179 for (int i = fingerings_.size(); i--;)
180 for (int j = heads_.size() ; j--;)
181 Side_position_interface::add_support (fingerings_[i].script_, heads_[j]);
183 Array<Finger_tuple> up, down, horiz;
184 for (int i = fingerings_.size(); i--;)
186 SCM d = fingerings_[i].finger_event_->get_mus_property ("direction");
189 ((to_dir (d) == UP) ? up : down ).push (fingerings_[i]);
194 fingerings_.sort (&Finger_tuple::compare);
195 SCM orientations = get_property ("fingeringOrientations");
197 bool up_p = scm_memq (ly_symbol2scm ("up"), orientations) != SCM_BOOL_F;
198 bool down_p = scm_memq (ly_symbol2scm ("down"), orientations) != SCM_BOOL_F;
199 bool left_p = scm_memq (ly_symbol2scm ("left"), orientations) != SCM_BOOL_F;
200 bool right_p = scm_memq (ly_symbol2scm ("right"), orientations) != SCM_BOOL_F;
201 Direction hordir = (right_p) ? RIGHT : LEFT;
202 if (left_p || right_p)
204 if (up_p && !up.size () && fingerings_.size ())
205 up.push (fingerings_.pop());
207 if (down_p && !down.size () && fingerings_.size())
209 down.push (fingerings_[0]);
213 horiz.concat (fingerings_);
215 else if (up_p && down_p)
217 int center = fingerings_.size() / 2;
218 down.concat (fingerings_.slice (0,center));
219 up.concat (fingerings_.slice (center, fingerings_.size()));
223 up.concat (fingerings_);
224 fingerings_ .clear ();
229 warning(_ ("Fingerings are also not down?! Putting them down anyway."));
230 down.concat (fingerings_);
234 for (int i = 0; i < horiz.size(); i++)
236 Finger_tuple ft = horiz[i];
237 Grob* f = ft.script_;
238 f->set_parent (ft.head_, X_AXIS);
239 f->set_parent (ft.head_, Y_AXIS);
240 f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, Y_AXIS);
241 f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, Y_AXIS);
242 f->add_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS);
244 f->set_grob_property ("direction", gh_int2scm (hordir));
248 int finger_prio = 200;
249 for (int i = 0; i < up.size(); i++)
251 Finger_tuple ft = up[i];
252 Grob* f = ft.script_;
253 f->set_parent (ft.head_, X_AXIS);
254 f->set_grob_property ("script-priority",
255 gh_int2scm (finger_prio + i));
256 f->add_offset_callback (Side_position_interface::aligned_side_proc, Y_AXIS);
257 f->add_offset_callback (Side_position_interface::out_of_staff_proc, Y_AXIS);
258 f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, X_AXIS);
259 f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, X_AXIS);
261 f->set_grob_property ("direction", gh_int2scm (UP));
266 for (int i = 0; i < down.size(); i++)
268 Finger_tuple ft = down[i];
269 Grob* f = ft.script_;
270 f->set_parent (ft.head_, X_AXIS);
271 f->set_grob_property ("script-priority",
272 gh_int2scm (finger_prio + down.size() - i));
274 f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, X_AXIS);
275 f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, X_AXIS);
276 f->add_offset_callback (Side_position_interface::aligned_side_proc, Y_AXIS);
277 f->add_offset_callback (Side_position_interface::out_of_staff_proc, Y_AXIS);
278 f->set_grob_property ("direction", gh_int2scm (DOWN));
284 New_fingering_engraver::stop_translation_timestep ()
286 if (fingerings_.size ())
289 fingerings_.clear ();
292 for (int i = articulations_.size(); i--;)
294 Grob *sc = articulations_[i].script_;
296 for (int j = heads_.size() ; j--;)
297 Side_position_interface::add_support (sc, heads_[j]);
299 if (stem_ && to_dir (sc->get_grob_property ("side-relative-direction")))
300 sc->set_grob_property ("direction-source", stem_->self_scm ());
302 SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"), articulations_[i].description_);
303 if (gh_pair_p (follow) && to_boolean (gh_cdr (follow)))
304 sc->add_offset_callback (Side_position_interface::quantised_position_proc, Y_AXIS);
306 Side_position_interface::add_staff_support (sc);
312 articulations_.clear();
316 New_fingering_engraver::New_fingering_engraver()
321 ENTER_DESCRIPTION(New_fingering_engraver,
322 /* descr */ "Create fingering-scripts for notes in a new chord.",
323 /* creats*/ "Fingering",
325 /* acks */ "rhythmic-head-interface stem-interface",
326 /* reads */ "fingeringOrientations",