]> git.donarmstrong.com Git - lilypond.git/blob - lily/fingering-engraver.cc
release: 1.5.30
[lilypond.git] / lily / fingering-engraver.cc
1 /*   
2   fingering-engraver.cc --  implement Fingering_engraver
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1998--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   
8  */
9
10
11 #include "engraver.hh"
12 #include "side-position-interface.hh"
13 #include "item.hh"
14 #include "musical-request.hh"
15 #include "stem.hh"
16 #include "rhythmic-head.hh"
17
18 class Fingering_engraver : public Engraver
19 {
20   Link_array<Music> reqs_;
21   Link_array<Item> fingerings_;
22
23   Link_array<Music> up_reqs_;
24   Link_array<Music> hor_reqs_;
25   Link_array<Music> down_reqs_;
26     
27 public:
28   TRANSLATOR_DECLARATIONS(Fingering_engraver);
29 protected:
30   virtual bool try_music (Music* m);
31   virtual void stop_translation_timestep ();
32   virtual void start_translation_timestep ();
33   virtual void process_music ();
34   virtual void acknowledge_grob (Grob_info);
35
36   void make_script (Direction, Music*,Axis, int);
37 };
38
39 bool
40 Fingering_engraver::try_music (Music *m)
41 {
42   if (dynamic_cast<Text_script_req*> (m))
43     {
44       if (m->get_mus_property ("text-type") != ly_symbol2scm ("finger"))
45         return false;
46       
47       reqs_.push (m);
48       return true;
49     }
50   return false;
51 }
52
53 void
54 Fingering_engraver::acknowledge_grob (Grob_info inf)
55 {
56   
57   if (Stem::has_interface (inf.grob_l_))
58     {
59       for (int i=0; i < fingerings_.size (); i++)
60         {
61           Side_position_interface::add_support (fingerings_[i],inf.grob_l_);
62         }
63     }
64   else if (Rhythmic_head::has_interface (inf.grob_l_))
65     {
66       Music * mc =inf.music_cause ();
67       Pitch * mp = mc? unsmob_pitch (mc->get_mus_property ("pitch")) :0;
68       for (int i=0; i < fingerings_.size (); i++)
69         {
70           Grob*t = fingerings_[i];
71           Side_position_interface::add_support (t,inf.grob_l_);
72           Pitch *fp = unsmob_pitch (t->get_grob_property ("pitch"));
73           if (fp)
74             {
75               if (!mp)
76                 continue;
77         
78               if (*fp == *mp)
79                 {
80                   Axis other = other_axis (Side_position_interface::get_axis (t));
81                   t->set_parent (inf.grob_l_, other);
82                 }
83             }
84           else
85             {
86               if (!t->get_parent (X_AXIS))
87                 t->set_parent (inf.grob_l_, X_AXIS);
88             }
89         }
90     }
91 }
92
93 static int
94 req_compare (Music * const &a,Music * const &b)
95 {
96   Pitch *pa  = unsmob_pitch (a->get_mus_property ("pitch"));
97   Pitch *pb  = unsmob_pitch (b->get_mus_property ("pitch"));
98
99   if (!pa && !pb)
100     return 0;
101   if (pa && !pb)
102     return 1;
103   if (!pa && pb)
104     return -1;
105
106   return Pitch::compare (*pa, *pb);
107 }
108
109 void
110 Fingering_engraver::process_music ()
111 {
112   if (!reqs_.size())
113     return ;
114   
115   Link_array<Music> pitch_sorted_reqs = reqs_;
116   for (int i= pitch_sorted_reqs.size(); i--;)
117     {
118       SCM dir = pitch_sorted_reqs[i]->get_mus_property ("direction");
119       if (isdir_b (dir) && to_dir (dir)) {
120         if (to_dir (dir) == UP)
121           up_reqs_.push (pitch_sorted_reqs[i]);
122         else if (to_dir (dir) == DOWN)
123           down_reqs_ .push (pitch_sorted_reqs[i]);
124         pitch_sorted_reqs.del(i);
125
126         continue ; 
127       }
128       else if (!unsmob_pitch (pitch_sorted_reqs[i]->get_mus_property ("pitch")))
129         {
130           /*
131             chuck out reqs that have no pitch.  We put them over the note by default.
132           */
133           up_reqs_.push (pitch_sorted_reqs [i]);
134           pitch_sorted_reqs.del (i);
135         }
136     }  
137   up_reqs_.reverse ();
138   down_reqs_.reverse ();
139   
140   pitch_sorted_reqs.sort (&req_compare);
141
142   if (to_boolean (get_property ("scriptHorizontal")))
143     {
144 #if 1 // -> 0 for testing horizontal fingerings.
145       
146       down_reqs_.push  ( pitch_sorted_reqs[0]);
147       pitch_sorted_reqs.del (0);
148
149       if (pitch_sorted_reqs.size())
150         {
151           up_reqs_.push (pitch_sorted_reqs.top ());
152           pitch_sorted_reqs.pop();
153         }
154 #endif
155       hor_reqs_ = pitch_sorted_reqs;
156     }
157   else
158     {
159       int sz = pitch_sorted_reqs.size ();
160       down_reqs_.concat (pitch_sorted_reqs.slice(0, (sz + sz%2)/2 ));
161       up_reqs_.concat (pitch_sorted_reqs.slice((sz + sz%2)/2, sz));
162       hor_reqs_ .clear ();
163     }
164
165   for (int i = 0; i < down_reqs_.size();i++)
166     make_script (DOWN,  down_reqs_[i], Y_AXIS, i);
167   for (int i = 0; i < up_reqs_.size();i++)
168     make_script (UP,   up_reqs_[i], Y_AXIS, i);
169   for (int i = 0; i < hor_reqs_.size();i++)
170     make_script (CENTER,  hor_reqs_[i],X_AXIS, i);
171 }
172
173 void
174 Fingering_engraver::make_script (Direction d, Music *r,Axis a,  int i)
175 {
176   Item *fingering = new Item (get_property ("Fingering"));
177
178   Axis other = other_axis (a);
179
180   SCM pitch = r->get_mus_property ("pitch");
181   if (unsmob_pitch (pitch))
182     fingering->set_grob_property ("pitch", pitch);
183   
184   Side_position_interface::set_axis (fingering, a);
185       
186   fingering->add_offset_callback (Side_position_interface::aligned_on_self_proc, other);
187   fingering->add_offset_callback (Side_position_interface::centered_on_parent_proc, other);
188   fingering->set_grob_property ("script-priority",
189                                 gh_int2scm (100 + d* i));
190
191
192   if (!isdir_b (fingering->get_grob_property ("direction")))
193     {
194       if (d)
195         fingering->set_grob_property ("direction", gh_int2scm (d));
196       else
197         fingering->set_grob_property ("direction",  gh_int2scm (RIGHT));
198     }
199   
200   fingering->set_grob_property ("text", r->get_mus_property ("text"));
201   
202   SCM nonempty = get_property ("textNonEmpty");
203   if (to_boolean (nonempty))
204     /*
205       empty fingering: signal that no rods should be applied.  
206     */
207     fingering->set_grob_property ("no-spacing-rods" , SCM_BOOL_F);
208                 
209   announce_grob (fingering, r->self_scm());
210   fingerings_.push (fingering);
211 }
212
213 void
214 Fingering_engraver::stop_translation_timestep ()
215 {
216   if (!fingerings_.size ())
217     return;
218   
219   for (int i=0; i < fingerings_.size (); i++)
220     {
221       Item *ti = fingerings_[i];
222       Side_position_interface::add_staff_support (ti);
223       typeset_grob (ti);
224     }
225   fingerings_.clear ();
226 }
227
228 void
229 Fingering_engraver::start_translation_timestep ()
230 {
231   reqs_.clear ();
232   up_reqs_.clear ();
233   down_reqs_.clear ();
234   hor_reqs_.clear ();
235 }
236
237 Fingering_engraver::Fingering_engraver()
238 {
239
240 }
241
242 ENTER_DESCRIPTION(Fingering_engraver,
243 /* descr */       "Create fingering-scripts",
244 /* creats*/       "Fingering",
245 /* acks  */       "rhythmic-head-interface stem-interface",
246 /* reads */       "scriptHorizontal textNonEmpty",
247 /* write */       "");