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