2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "engraver.hh"
22 #include "international.hh"
24 #include "rhythmic-head.hh"
25 #include "script-interface.hh"
26 #include "self-alignment-interface.hh"
27 #include "side-position-interface.hh"
29 #include "stream-event.hh"
32 #include "translator.icc"
38 Stream_event *note_event_;
39 Stream_event *finger_event_;
40 bool follow_into_staff_;
47 note_event_ = finger_event_ = 0;
48 follow_into_staff_ = false;
53 operator < (Finger_tuple const &a, Finger_tuple const &b)
55 return a.position_ < b.position_;
58 class New_fingering_engraver : public Engraver
60 vector<Finger_tuple> fingerings_;
61 vector<Finger_tuple> stroke_fingerings_;
62 vector<Finger_tuple> articulations_;
63 vector<Finger_tuple> string_numbers_;
65 vector<Grob *> heads_;
66 vector<Grob *> accidentals_;
71 TRANSLATOR_DECLARATIONS (New_fingering_engraver);
73 void stop_translation_timestep ();
74 void acknowledge_rhythmic_head (Grob_info);
75 void acknowledge_inline_accidental (Grob_info);
76 void acknowledge_stem (Grob_info);
77 void add_fingering (Grob *, SCM,
78 vector<Finger_tuple> *,
79 Stream_event *, Stream_event *);
80 void add_script (Grob *, Stream_event *, Stream_event *);
81 void add_string (Grob *, Stream_event *, Stream_event *);
82 void position_scripts (SCM orientations, vector<Finger_tuple> *);
86 New_fingering_engraver::acknowledge_inline_accidental (Grob_info inf)
88 accidentals_.push_back (inf.grob ());
92 New_fingering_engraver::acknowledge_rhythmic_head (Grob_info inf)
94 Stream_event *note_ev = inf.event_cause ();
98 SCM arts = note_ev->get_property ("articulations");
100 for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
102 Stream_event *ev = unsmob<Stream_event> (scm_car (s));
107 if (ev->in_event_class ("fingering-event"))
108 add_fingering (inf.grob (),
109 ly_symbol2scm ("Fingering"),
112 else if (ev->in_event_class ("text-script-event"))
113 ev->origin ()->warning (_ ("cannot add text scripts to individual note heads"));
114 else if (ev->in_event_class ("script-event"))
115 add_script (inf.grob (), ev, note_ev);
116 else if (ev->in_event_class ("string-number-event"))
118 // String numbers are used in calculating harmonics even
119 // when we don't want them displayed. So don't make space
120 // for them if 'stencil is #f
121 Grob *g = make_item ("StringNumber", ev->self_scm ());
122 if (scm_is_true (g->get_property ("stencil")))
123 add_fingering (inf.grob (),
124 ly_symbol2scm ("StringNumber"), &string_numbers_,
126 g->suicide (); // Kill grob created to check stencil
128 else if (ev->in_event_class ("stroke-finger-event"))
129 add_fingering (inf.grob (),
130 ly_symbol2scm ("StrokeFinger"), &stroke_fingerings_,
132 else if (ev->in_event_class ("harmonic-event"))
134 inf.grob ()->set_property ("style", ly_symbol2scm ("harmonic"));
135 Grob *d = unsmob<Grob> (inf.grob ()->get_object ("dot"));
136 if (d && !to_boolean (get_property ("harmonicDots")))
141 heads_.push_back (inf.grob ());
145 New_fingering_engraver::acknowledge_stem (Grob_info inf)
151 New_fingering_engraver::add_script (Grob *head,
153 Stream_event * /* note */)
157 Grob *g = make_item ("Script", event->self_scm ());
158 make_script_from_event (g, context (),
159 event->get_property ("articulation-type"), 0);
161 ft.script_->set_parent (head, X_AXIS);
163 SCM forced_dir = event->get_property ("direction");
164 if (to_dir (forced_dir))
165 ft.script_->set_property ("direction", forced_dir);
167 articulations_.push_back (ft);
171 New_fingering_engraver::add_fingering (Grob *head,
173 vector<Finger_tuple> *tuple_vector,
175 Stream_event *hevent)
179 ft.script_ = internal_make_item (grob_sym, event->self_scm (),
180 ly_symbol2string (grob_sym).c_str (),
181 __FILE__, __LINE__, __FUNCTION__);
183 Side_position_interface::add_support (ft.script_, head);
185 ft.finger_event_ = event;
186 ft.note_event_ = hevent;
189 tuple_vector->push_back (ft);
193 New_fingering_engraver::position_scripts (SCM orientations,
194 vector<Finger_tuple> *scripts)
196 for (vsize i = 0; i < scripts->size (); i++)
199 Side_position_interface::add_support (scripts->at (i).script_, stem_);
200 if (Grob *flag = unsmob<Grob> (stem_->get_object ("flag")))
201 Side_position_interface::add_support (scripts->at (i).script_, flag);
205 This is not extremely elegant, but we have to do a little
206 formatting here, because the parent/child relations should be
207 known before we move on to the next time step.
209 A more sophisticated approach would be to set both X and Y parents
210 to the note head, and write a more flexible function for
211 positioning the fingerings, setting both X and Y coordinates.
213 for (vsize i = 0; i < scripts->size (); i++)
214 (*scripts)[i].position_ = scm_to_int ((*scripts)[i].head_->get_property ("staff-position"));
216 for (vsize i = scripts->size (); i--;)
217 for (vsize j = heads_.size (); j--;)
218 Side_position_interface::add_support ((*scripts)[i].script_, heads_[j]);
220 vector<Finger_tuple> up, down, horiz;
221 for (vsize i = scripts->size (); i--;)
223 SCM d = (*scripts)[i].finger_event_->get_property ("direction");
226 ((to_dir (d) == UP) ? up : down).push_back ((*scripts)[i]);
227 scripts->erase (scripts->begin () + i);
231 vector_sort (*scripts, less<Finger_tuple> ());
233 bool up_p = scm_is_true (scm_c_memq (ly_symbol2scm ("up"), orientations));
234 bool down_p = scm_is_true (scm_c_memq (ly_symbol2scm ("down"), orientations));
235 bool left_p = scm_is_true (scm_c_memq (ly_symbol2scm ("left"), orientations));
236 bool right_p = scm_is_true (scm_c_memq (ly_symbol2scm ("right"), orientations));
237 Direction hordir = (right_p) ? RIGHT : LEFT;
238 if (left_p || right_p)
240 if (up_p && !up.size () && scripts->size ())
242 up.push_back (scripts->back ());
243 scripts->pop_back ();
246 if (down_p && !down.size () && scripts->size ())
248 down.push_back ((*scripts)[0]);
249 scripts->erase (scripts->begin ());
252 horiz.insert (horiz.end (), scripts->begin (), scripts->end ());
254 else if (up_p && down_p)
256 int center = scripts->size () / 2;
257 down.insert (down.end (), scripts->begin (), scripts->begin () + center);
258 up.insert (up.end (), scripts->begin () + center, scripts->end ());
262 up.insert (up.end (), scripts->begin (), scripts->end ());
269 warning (_ ("no placement found for fingerings"));
270 warning (_ ("placing below"));
272 down.insert (down.end (), scripts->begin (), scripts->end ());
276 for (vsize i = 0; i < horiz.size (); i++)
278 Finger_tuple ft = horiz[i];
279 Grob *f = ft.script_;
280 f->set_parent (ft.head_, X_AXIS);
281 f->set_parent (ft.head_, Y_AXIS);
282 f->set_property ("avoid-slur", ly_symbol2scm ("inside"));
284 && unsmob<Grob> (ft.head_->get_object ("accidental-grob")))
285 Side_position_interface::add_support (f,
286 unsmob<Grob> (ft.head_->get_object ("accidental-grob")));
287 else if (unsmob<Grob> (ft.head_->get_object ("dot")))
288 Side_position_interface::add_support (f,
289 unsmob<Grob> (ft.head_->get_object ("dot")));
291 Self_alignment_interface::set_aligned_on_parent (f, Y_AXIS);
292 Side_position_interface::set_axis (f, X_AXIS);
294 f->set_property ("direction", scm_from_int (hordir));
297 Drul_array< vector<Finger_tuple> > vertical (down, up);
298 for (DOWN_and_UP (d))
300 for (vsize i = 0; i < vertical[d].size (); i++)
302 Finger_tuple ft = vertical[d][i];
303 Grob *f = ft.script_;
304 int finger_prio = robust_scm2int (f->get_property ("script-priority"), 200);
305 f->set_parent (ft.head_, X_AXIS);
306 f->set_property ("script-priority",
307 scm_from_int (finger_prio + d * ft.position_));
309 Self_alignment_interface::set_aligned_on_parent (f, X_AXIS);
310 Side_position_interface::set_axis (f, Y_AXIS);
312 f->set_property ("direction", scm_from_int (d));
318 New_fingering_engraver::stop_translation_timestep ()
326 New_fingering_engraver::position_all ()
328 if (fingerings_.size ())
330 position_scripts (get_property ("fingeringOrientations"),
332 fingerings_.clear ();
335 if (string_numbers_.size ())
337 position_scripts (get_property ("stringNumberOrientations"),
339 string_numbers_.clear ();
342 if (stroke_fingerings_.size ())
344 position_scripts (get_property ("strokeFingerOrientations"),
345 &stroke_fingerings_);
346 stroke_fingerings_.clear ();
349 for (vsize i = articulations_.size (); i--;)
351 Grob *script = articulations_[i].script_;
353 for (vsize j = 0; j < accidentals_.size (); j++)
354 Side_position_interface::add_support (script, accidentals_[j]);
356 accidentals_.resize (0);
357 for (vsize j = heads_.size (); j--;)
358 Side_position_interface::add_support (script, heads_[j]);
360 if (stem_ && to_dir (script->get_property ("side-relative-direction")))
361 script->set_object ("direction-source", stem_->self_scm ());
363 if (stem_ && to_boolean (script->get_property ("add-stem-support")))
364 Side_position_interface::add_support (script, stem_);
366 articulations_.clear ();
369 New_fingering_engraver::New_fingering_engraver ()
376 New_fingering_engraver::boot ()
378 ADD_ACKNOWLEDGER (New_fingering_engraver, rhythmic_head);
379 ADD_ACKNOWLEDGER (New_fingering_engraver, inline_accidental);
380 ADD_ACKNOWLEDGER (New_fingering_engraver, stem);
383 ADD_TRANSLATOR (New_fingering_engraver,
385 "Create fingering scripts for notes in a new chord. This"
386 " engraver is ill-named, since it also takes care of"
387 " articulations and harmonic note heads.",
396 "fingeringOrientations "
398 "strokeFingerOrientations "
399 "stringNumberOrientations ",