]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-fingering-engraver.cc
Merge branch 'master' into lilypond/translation
[lilypond.git] / lily / new-fingering-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "international.hh"
23 #include "item.hh"
24 #include "rhythmic-head.hh"
25 #include "script-interface.hh"
26 #include "self-alignment-interface.hh"
27 #include "side-position-interface.hh"
28 #include "stem.hh"
29 #include "stream-event.hh"
30 #include "warn.hh"
31
32 #include "translator.icc"
33
34
35 struct Finger_tuple
36 {
37   Grob *head_;
38   Grob *script_;
39   Stream_event *note_event_;
40   Stream_event *finger_event_;
41   bool follow_into_staff_;
42   int position_;
43
44   Finger_tuple ()
45   {
46     position_ = 0;
47     head_ = script_ = 0;
48     note_event_ = finger_event_ = 0;
49     follow_into_staff_ = false;
50   }
51 };
52
53 bool
54 operator< (Finger_tuple const &a, Finger_tuple const &b)
55 {
56   return a.position_ < b.position_;
57 }
58
59 class New_fingering_engraver : public Engraver
60 {
61   vector<Finger_tuple> fingerings_;
62   vector<Finger_tuple> stroke_fingerings_;
63   vector<Finger_tuple> articulations_;
64   vector<Finger_tuple> string_numbers_;
65
66   vector<Grob *> heads_;
67   Grob *stem_;
68
69   void position_all ();
70 public:
71   TRANSLATOR_DECLARATIONS (New_fingering_engraver);
72 protected:
73   void stop_translation_timestep ();
74   DECLARE_ACKNOWLEDGER (rhythmic_head);
75   DECLARE_ACKNOWLEDGER (stem);
76   void add_fingering (Grob *, SCM,
77                       vector<Finger_tuple> *,
78                       Stream_event *, Stream_event *);
79   void add_script (Grob *, Stream_event *, Stream_event *);
80   void add_string (Grob *, Stream_event *, Stream_event *);
81   void position_scripts (SCM orientations, vector<Finger_tuple> *);
82 };
83
84 void
85 New_fingering_engraver::acknowledge_rhythmic_head (Grob_info inf)
86 {
87   Stream_event *note_ev = inf.event_cause ();
88   if (!note_ev)
89     return;
90
91   SCM arts = note_ev->get_property ("articulations");
92
93   for (SCM s = arts; scm_is_pair (s); s = scm_cdr (s))
94     {
95       Stream_event *ev = unsmob_stream_event (scm_car (s));
96
97       if (!ev)
98         continue;
99
100       if (ev->in_event_class ("fingering-event"))
101         add_fingering (inf.grob (),
102                        ly_symbol2scm ("Fingering"),
103                        &fingerings_,
104                        ev, note_ev);
105       else if (ev->in_event_class ("text-script-event"))
106         ev->origin ()->warning (_ ("cannot add text scripts to individual note heads"));
107       else if (ev->in_event_class ("script-event"))
108         add_script (inf.grob (), ev, note_ev);
109       else if (ev->in_event_class ("string-number-event"))
110         add_fingering (inf.grob (),
111                        ly_symbol2scm ("StringNumber"), &string_numbers_,
112                        ev, note_ev);
113       else if (ev->in_event_class ("stroke-finger-event"))
114         add_fingering (inf.grob (),
115                        ly_symbol2scm ("StrokeFinger"), &stroke_fingerings_,
116                        ev, note_ev);
117       else if (ev->in_event_class ("harmonic-event"))
118         {
119           inf.grob ()->set_property ("style", ly_symbol2scm ("harmonic"));
120           Grob *d = unsmob_grob (inf.grob ()->get_object ("dot"));
121           if (d && !to_boolean (get_property ("harmonicDots")))
122             d->suicide ();
123         }
124     }
125
126   heads_.push_back (inf.grob ());
127 }
128
129 void
130 New_fingering_engraver::acknowledge_stem (Grob_info inf)
131 {
132   stem_ = inf.grob ();
133 }
134
135 void
136 New_fingering_engraver::add_script (Grob *head,
137                                     Stream_event *event,
138                                     Stream_event * /* note */)
139 {
140   Finger_tuple ft;
141
142   Grob *g = make_item ("Script", event->self_scm ());
143   make_script_from_event (g, context (),
144                           event->get_property ("articulation-type"), 0);
145   ft.script_ = g;
146   ft.script_->set_parent (head, X_AXIS);
147
148   SCM forced_dir = event->get_property ("direction");
149   if (to_dir (forced_dir))
150     ft.script_->set_property ("direction", forced_dir);
151
152   articulations_.push_back (ft);
153 }
154
155 void
156 New_fingering_engraver::add_fingering (Grob *head,
157                                        SCM grob_sym,
158                                        vector<Finger_tuple> *tuple_vector,
159                                        Stream_event *event,
160                                        Stream_event *hevent)
161 {
162   Finger_tuple ft;
163
164   ft.script_ = internal_make_item (grob_sym, event->self_scm (),
165                                    ly_symbol2string (grob_sym).c_str (),
166                                    __FILE__, __LINE__, __FUNCTION__);
167
168   Side_position_interface::add_support (ft.script_, head);
169
170   ft.finger_event_ = event;
171   ft.note_event_ = hevent;
172   ft.head_ = head;
173
174   tuple_vector->push_back (ft);
175 }
176
177 void
178 New_fingering_engraver::position_scripts (SCM orientations,
179                                           vector<Finger_tuple> *scripts)
180 {
181   for (vsize i = 0; i < scripts->size (); i++)
182     if (stem_ && to_boolean (scripts->at (i).script_->get_property ("add-stem-support")))
183       Side_position_interface::add_support (scripts->at (i).script_, stem_);
184
185   /*
186     This is not extremely elegant, but we have to do a little
187     formatting here, because the parent/child relations should be
188     known before we move on to the next time step.
189
190     A more sophisticated approach would be to set both X and Y parents
191     to the note head, and write a more flexible function for
192     positioning the fingerings, setting both X and Y coordinates.
193   */
194   for (vsize i = 0; i < scripts->size (); i++)
195     (*scripts)[i].position_ = scm_to_int ((*scripts)[i].head_->get_property ("staff-position"));
196
197   for (vsize i = scripts->size (); i--;)
198     for (vsize j = heads_.size (); j--;)
199       Side_position_interface::add_support ((*scripts)[i].script_, heads_[j]);
200
201   vector<Finger_tuple> up, down, horiz;
202   for (vsize i = scripts->size (); i--;)
203     {
204       SCM d = (*scripts)[i].finger_event_->get_property ("direction");
205       if (to_dir (d))
206         {
207           ((to_dir (d) == UP) ? up : down).push_back ((*scripts)[i]);
208           scripts->erase (scripts->begin () + i);
209         }
210     }
211
212   vector_sort (*scripts, less<Finger_tuple> ());
213
214   bool up_p = scm_c_memq (ly_symbol2scm ("up"), orientations) != SCM_BOOL_F;
215   bool down_p = scm_c_memq (ly_symbol2scm ("down"), orientations) != SCM_BOOL_F;
216   bool left_p = scm_c_memq (ly_symbol2scm ("left"), orientations) != SCM_BOOL_F;
217   bool right_p = scm_c_memq (ly_symbol2scm ("right"), orientations) != SCM_BOOL_F;
218   Direction hordir = (right_p) ? RIGHT : LEFT;
219   if (left_p || right_p)
220     {
221       if (up_p && !up.size () && scripts->size ())
222         {
223           up.push_back (scripts->back ());
224           scripts->pop_back ();
225         }
226
227       if (down_p && !down.size () && scripts->size ())
228         {
229           down.push_back ((*scripts)[0]);
230           scripts->erase (scripts->begin ());
231         }
232
233       horiz.insert (horiz.end (), scripts->begin (), scripts->end ());
234     }
235   else if (up_p && down_p)
236     {
237       int center = scripts->size () / 2;
238       down.insert (down.end (), scripts->begin (), scripts->begin () + center);
239       up.insert (up.end (), scripts->begin () + center, scripts->end ());
240     }
241   else if (up_p)
242     {
243       up.insert (up.end (), scripts->begin (), scripts->end ());
244       scripts->clear ();
245     }
246   else
247     {
248       if (!down_p)
249         {
250           warning (_ ("no placement found for fingerings"));
251           warning (_ ("placing below"));
252         }
253       down.insert (down.end (), scripts->begin (), scripts->end ());
254       scripts->clear ();
255     }
256
257   for (vsize i = 0; i < horiz.size (); i++)
258     {
259       Finger_tuple ft = horiz[i];
260       Grob *f = ft.script_;
261       f->set_parent (ft.head_, X_AXIS);
262       f->set_parent (ft.head_, Y_AXIS);
263       f->set_property ("avoid-slur", SCM_BOOL_F);
264       if (hordir == LEFT
265           && unsmob_grob (ft.head_->get_object ("accidental-grob")))
266         Side_position_interface::add_support (f,
267                                               unsmob_grob (ft.head_->get_object ("accidental-grob")));
268       else if (unsmob_grob (ft.head_->get_object ("dot")))
269         Side_position_interface::add_support (f,
270                                               unsmob_grob (ft.head_->get_object ("dot")));
271
272       Self_alignment_interface::set_align_self (f, Y_AXIS);
273       Self_alignment_interface::set_center_parent (f, Y_AXIS);
274       Side_position_interface::set_axis (f, X_AXIS);
275
276       f->set_property ("direction", scm_from_int (hordir));
277     }
278
279   Direction d = DOWN;
280   Drul_array< vector<Finger_tuple> > vertical (down, up);
281   do
282     {
283       for (vsize i = 0; i < vertical[d].size (); i++)
284         {
285           Finger_tuple ft = vertical[d][i];
286           Grob *f = ft.script_;
287           int finger_prio = robust_scm2int (f->get_property ("script-priority"), 200);
288           f->set_parent (ft.head_, X_AXIS);
289           f->set_property ("script-priority",
290                            scm_from_int (finger_prio + d * ft.position_));
291
292           Self_alignment_interface::set_align_self (f, X_AXIS);
293           Self_alignment_interface::set_center_parent (f, X_AXIS);
294           Side_position_interface::set_axis (f, Y_AXIS);
295
296           f->set_property ("direction", scm_from_int (d));
297         }
298     }
299   while (flip (&d) != DOWN);
300 }
301
302 void
303 New_fingering_engraver::stop_translation_timestep ()
304 {
305   position_all ();
306   stem_ = 0;
307   heads_.clear ();
308 }
309
310 void
311 New_fingering_engraver::position_all ()
312 {
313   if (fingerings_.size ())
314     {
315       position_scripts (get_property ("fingeringOrientations"),
316                         &fingerings_);
317       fingerings_.clear ();
318     }
319
320   if (string_numbers_.size ())
321     {
322       position_scripts (get_property ("stringNumberOrientations"),
323                         &string_numbers_);
324       string_numbers_.clear ();
325     }
326
327   if (stroke_fingerings_.size ())
328     {
329       position_scripts (get_property ("strokeFingerOrientations"),
330                         &stroke_fingerings_);
331       stroke_fingerings_.clear ();
332     }
333
334   for (vsize i = articulations_.size (); i--;)
335     {
336       Grob *script = articulations_[i].script_;
337
338       for (vsize j = heads_.size (); j--;)
339         Side_position_interface::add_support (script, heads_[j]);
340
341       if (stem_ && to_dir (script->get_property ("side-relative-direction")))
342         script->set_object ("direction-source", stem_->self_scm ());
343
344       if (stem_ && to_boolean (script->get_property ("add-stem-support")))
345         Side_position_interface::add_support (script, stem_);
346     }
347   articulations_.clear ();
348 }
349
350 New_fingering_engraver::New_fingering_engraver ()
351 {
352   stem_ = 0;
353 }
354
355 ADD_ACKNOWLEDGER (New_fingering_engraver, rhythmic_head);
356 ADD_ACKNOWLEDGER (New_fingering_engraver, stem);
357
358 ADD_TRANSLATOR (New_fingering_engraver,
359                 /* doc */
360                 "Create fingering scripts for notes in a new chord.  This"
361                 " engraver is ill-named, since it also takes care of"
362                 " articulations and harmonic note heads.",
363
364                 /* create */
365                 "Fingering "
366                 "StringNumber "
367                 "StrokeFinger "
368                 "Script ",
369
370                 /* read */
371                 "fingeringOrientations "
372                 "harmonicDots "
373                 "strokeFingerOrientations "
374                 "stringNumberOrientations ",
375
376                 /* write */
377                 ""
378                 );