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