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