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