]> git.donarmstrong.com Git - lilypond.git/blob - lily/new-fingering-engraver.cc
(position_scripts): don't crash
[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--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10 #include "warn.hh"
11 #include "engraver.hh"
12 #include "side-position-interface.hh"
13 #include "item.hh"
14 #include "event.hh"
15 #include "stem.hh"
16 #include "rhythmic-head.hh"
17 #include "self-alignment-interface.hh"
18 #include "script.hh"
19 #include "stem.hh"
20
21 struct Finger_tuple
22 {
23   Grob *head_;
24   Grob *script_;
25   Music *note_event_;
26   Music *finger_event_;
27   SCM description_;
28   int position_;
29
30   Finger_tuple ()
31   {
32     position_ = 0;
33     head_ = script_ = 0;
34     note_event_ = finger_event_ = 0;
35     description_ = SCM_EOL;
36   }
37   static int compare (Finger_tuple const & c1, Finger_tuple const & c2)
38   {
39     return c1.position_-  c2.position_;
40   }
41                
42 };
43
44 class New_fingering_engraver : public Engraver
45 {
46   Array<Finger_tuple> fingerings_;
47   Array<Finger_tuple> articulations_;
48   Link_array<Grob> heads_;
49   Grob *stem_;
50   
51 public:
52   TRANSLATOR_DECLARATIONS(New_fingering_engraver);
53 protected:
54   virtual void stop_translation_timestep ();
55   virtual void acknowledge_grob (Grob_info);
56   void add_fingering (Grob*, Music*,Music*);
57   void add_script  (Grob*, Music*,Music*);
58   void position_scripts();
59 };
60
61 void
62 New_fingering_engraver::acknowledge_grob (Grob_info inf)
63 {
64   if (Rhythmic_head::has_interface (inf.grob_))
65     {
66       Music * note_ev =inf.music_cause ();
67
68       SCM arts = note_ev->get_mus_property ("articulations");
69
70       for (SCM s = arts; gh_pair_p (s); s = gh_cdr  (s))
71         {
72           Music * m = unsmob_music (gh_car (s));
73
74           if (!m)
75             continue;
76           
77
78           if (m->is_mus_type ("fingering-event"))
79             {
80               add_fingering (inf.grob_ , m, note_ev);
81             }
82           else if (m->is_mus_type ("script-event"))
83             {
84               add_script (inf.grob_, m, note_ev);
85             }
86         }
87
88       heads_.push (inf.grob_);
89     }
90   else if (Stem::has_interface (inf.grob_))
91     {
92       stem_ = inf.grob_;
93     }
94 }
95
96 extern Grob *make_script_from_event (SCM * descr, Translator_group*tg, Music * event,
97                               int index);
98 void
99 New_fingering_engraver::add_script (Grob * head,
100                                     Music * event,
101                                     Music * head_event)
102 {
103   Finger_tuple ft ;
104
105   ft.script_ =make_script_from_event (&ft.description_, daddy_trans_, event, 0);
106
107   articulations_.push (ft);
108   announce_grob (ft.script_, event->self_scm ());
109   
110  
111   ft.script_->set_parent (head, X_AXIS);
112 }
113                                     
114                                     
115
116 void
117 New_fingering_engraver::add_fingering (Grob * head,
118                                        Music * event,
119                                        Music *hevent)
120 {
121   Finger_tuple ft;
122
123   ft.script_ = new Item (get_property ("Fingering"));
124   announce_grob (ft.script_, event->self_scm());
125   
126   Side_position_interface::add_support (ft.script_, head);
127
128   int d = gh_scm2int ( event->get_mus_property ("digit"));
129   
130   /*
131     TODO:
132     
133     Should add support for thumb.  It's a little involved, since
134     the thumb lives in a different font. Maybe it should be moved?
135     
136    */
137   if (d > 5)
138     {
139       /*
140         music for the softenon children? 
141        */
142       event->origin()->warning (_("music for the martians."));
143     }
144   SCM sstr = scm_number_to_string (gh_int2scm (d), gh_int2scm (10)) ;
145   ft.script_->set_grob_property ("text", sstr);
146        
147   ft.finger_event_ = event;
148   ft.note_event_ = hevent;
149   ft.head_ = head;
150
151   fingerings_.push (ft);
152 }
153
154 void
155 New_fingering_engraver::position_scripts ()
156 {
157
158   /*
159     This is not extremely elegant, but we have to do a little
160     formatting here, because the parent/child relations should be
161     known before we move on to the next time step.
162
163     A more sophisticated approach would be to set both X and Y parents
164     to the note head, and write a more flexible function for
165     positioning the fingerings, setting both X and Y coordinates.
166   */
167   for (int i = 0; i < fingerings_.size(); i++)
168     {      
169       fingerings_[i].position_ = gh_scm2int (fingerings_[i].head_ -> get_grob_property( "staff-position"));
170     }
171
172   SCM fhd = get_property ("fingerHorizontalDirection");
173
174   for (int i = fingerings_.size(); i--;)
175     for (int j = heads_.size() ; j--;)
176       Side_position_interface::add_support (fingerings_[i].script_, heads_[j]);
177     
178   Array<Finger_tuple> up, down, horiz;
179   for (int i = fingerings_.size(); i--;)
180     {
181       SCM d = fingerings_[i].finger_event_->get_mus_property ("direction");
182       if (to_dir (d))
183         {
184           if (to_dir (d) == UP)
185             {
186               up.push (fingerings_[i]);
187             }
188           else
189             down.push (fingerings_[i]);
190           fingerings_.del (i);
191         }
192     }
193   
194   fingerings_.sort (&Finger_tuple::compare);
195   
196   if (ly_dir_p (fhd))
197     {
198       if (!up.size())
199         up.push (fingerings_.pop());
200       if (fingerings_.size () && !down.size())
201         {
202           down.push (fingerings_[0]);
203           fingerings_.del(0);
204         }
205
206       horiz.concat (fingerings_);
207     }
208   else
209     {
210       int center = fingerings_.size() / 2;
211       down.concat (fingerings_.slice (0,center));
212       up.concat (fingerings_.slice (center, fingerings_.size()));
213     }
214
215   for (int i = 0; i < horiz.size(); i++)
216     {
217       Finger_tuple ft = horiz[i];
218       Grob* f = ft.script_;
219       f->set_parent (ft.head_, X_AXIS);
220       f->set_parent (ft.head_, Y_AXIS);
221       f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, Y_AXIS);
222       f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, Y_AXIS);
223       f->add_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS);
224
225       f->set_grob_property ("direction", fhd);
226       typeset_grob (f);
227     }
228
229   int finger_prio = 200;
230   for (int i = 0; i < up.size(); i++)
231     {
232       Finger_tuple ft = up[i];
233       Grob* f = ft.script_;
234       f->set_parent (ft.head_, X_AXIS);
235       f->set_grob_property ("script-priority",
236                             gh_int2scm (finger_prio + i));
237       f->add_offset_callback (Side_position_interface::aligned_side_proc, Y_AXIS);
238       f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, X_AXIS);
239       f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, X_AXIS);
240       
241       f->set_grob_property ("direction", gh_int2scm (UP));
242
243       Side_position_interface::add_staff_support (f);
244       typeset_grob (f);
245     }
246   
247   for (int i = 0; i < down.size(); i++)
248     {
249       Finger_tuple ft = down[i];
250       Grob* f = ft.script_;
251       f->set_parent (ft.head_, X_AXIS);
252       f->set_grob_property ("script-priority",
253                             gh_int2scm (finger_prio + down.size() - i));
254
255       f->add_offset_callback (Self_alignment_interface::centered_on_parent_proc, X_AXIS);
256       f->add_offset_callback (Self_alignment_interface::aligned_on_self_proc, X_AXIS);
257       f->add_offset_callback (Side_position_interface::aligned_side_proc, Y_AXIS);
258       f->set_grob_property ("direction", gh_int2scm (DOWN));
259       Side_position_interface::add_staff_support (f);
260       typeset_grob (f);
261     }
262 }
263
264 void
265 New_fingering_engraver::stop_translation_timestep ()
266 {
267   if (fingerings_.size ())
268     {
269       position_scripts();
270       fingerings_.clear ();
271     }
272   
273   for (int i =  articulations_.size(); i--;)
274     {
275       Grob *sc = articulations_[i].script_;
276    
277       for (int j = heads_.size() ; j--;)
278         Side_position_interface::add_support (sc, heads_[j]);
279
280       if (stem_ && to_dir (sc->get_grob_property ("side-relative-direction")))
281         sc->set_grob_property ("direction-source", stem_->self_scm ());
282       
283       SCM follow = scm_assoc (ly_symbol2scm ("follow-into-staff"), articulations_[i].description_);
284       if (gh_pair_p (follow) && to_boolean (gh_cdr (follow)))
285         sc->add_offset_callback (Side_position_interface::quantised_position_proc, Y_AXIS);
286       else
287         Side_position_interface::add_staff_support (sc);
288       typeset_grob (sc);
289     }
290
291   stem_ = 0;
292   heads_.clear ();
293   articulations_.clear();
294 }
295
296
297 New_fingering_engraver::New_fingering_engraver()
298 {
299   stem_ = 0;  
300 }
301
302 ENTER_DESCRIPTION(New_fingering_engraver,
303 /* descr */       "Create fingering-scripts for notes in a new chord.",
304 /* creats*/       "Fingering",
305 /* accepts */     "",
306 /* acks  */       "rhythmic-head-interface stem-interface",
307 /* reads */       "fingerHorizontalDirection",
308 /* write */       "");