]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
(Module): read fonts from fonts/extension,
[lilypond.git] / lily / note-collision.cc
1 /*
2   collision.cc -- implement Collision
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include "note-collision.hh"
10
11 #include <math.h>
12
13 #include "warn.hh"
14 #include "note-column.hh"
15 #include "note-head.hh"
16 #include "rhythmic-head.hh"
17 #include "output-def.hh"
18 #include "axis-group-interface.hh"
19 #include "stem.hh"
20 #include "side-position-interface.hh"
21 #include "dot-column.hh"
22
23 MAKE_SCHEME_CALLBACK (Note_collision_interface, force_shift_callback, 2);
24
25 SCM
26 Note_collision_interface::force_shift_callback (SCM element_smob, SCM axis)
27 {
28   Grob *me = unsmob_grob (element_smob);
29   Axis a = (Axis) scm_to_int (axis);
30   assert (a == X_AXIS);
31   
32    me = me->get_parent (a);
33
34    if (! to_boolean (me->get_property ("positioning-done")))
35     {
36       me->set_property ("positioning-done", SCM_BOOL_T);
37       do_shifts (me);
38     }
39   
40   return scm_make_real (0.0);
41 }
42
43
44 void
45 check_meshing_chords (Grob *me,
46                       Drul_array< Array < Real > > *offsets,
47                       Drul_array< Array < Slice > > const &extents,
48                       Drul_array<Link_array<Grob> > const &clash_groups)
49         
50 {
51   if (!extents[UP].size () || ! extents[DOWN].size ())
52     return;
53   
54   Grob *cu = clash_groups[UP][0];
55   Grob *cd = clash_groups[DOWN][0];
56
57   /* Every note column should have a stem, but avoid a crash. */
58   if (!Note_column::get_stem (cu) || !Note_column::get_stem (cd))
59     return;
60
61   Grob *nu = Note_column::first_head (cu);
62   Grob *nd = Note_column::first_head (cd);
63
64   Array<int> ups = Stem::note_head_positions (Note_column::get_stem (cu));
65   Array<int> dps = Stem::note_head_positions (Note_column::get_stem (cd));
66
67   /* Too far apart to collide.  */
68   if (ups[0] > dps.top () + 1)
69     return; 
70
71   // FIXME: what's this?
72   bool merge_possible = (ups[0] >= dps[0]) && (ups.top () >= dps.top ());
73
74
75
76   /* Do not merge notes typeset in different style. */
77   if ( !ly_c_equal_p (nu->get_property ("style"),
78                      nd->get_property ("style") ) )
79     merge_possible = false;
80   
81   int upball_type = Note_head::get_balltype (nu);
82   int dnball_type = Note_head::get_balltype (nd);
83   
84   /* Do not merge whole notes (or longer, like breve, longa, maxima).  */
85   if (merge_possible && (upball_type <= 0 || dnball_type <= 0))
86     merge_possible = false;
87
88   if (merge_possible
89       && Rhythmic_head::dot_count (nu) != Rhythmic_head::dot_count (nd)
90       && !to_boolean (me->get_property ("merge-differently-dotted")))
91     merge_possible = false;
92
93   /* Can only merge different heads if merge-differently-headed is
94      set. */
95   if (merge_possible
96       && upball_type != dnball_type
97       && !to_boolean (me->get_property ("merge-differently-headed")))
98     merge_possible = false;
99
100   /* Should never merge quarter and half notes, as this would make
101      them indistinguishable.  */
102   if (merge_possible
103       && ((Rhythmic_head::duration_log (nu) == 1
104            && Rhythmic_head::duration_log (nd) == 2)
105           || (Rhythmic_head::duration_log (nu) == 2
106              && Rhythmic_head::duration_log (nd) == 1)))
107     merge_possible = false;
108
109
110   /*
111     this case (distant half collide), 
112     
113         |
114       x |
115      | x
116      |
117
118    the noteheads may be closer than this case (close half collide)
119
120        |
121        |
122       x 
123      x
124     |
125     |
126     
127    */
128   
129   /* TODO: filter out the 'o's in this configuration, since they're no
130   part in the collision.
131
132      |
133     x|o
134     x|o
135     x
136     
137    */
138   
139   bool close_half_collide = false;
140   bool distant_half_collide = false;  
141   bool full_collide = false;  
142
143   int i = 0, j = 0;
144   while (i < ups.size () && j < dps.size ())
145   {
146     if (abs (ups[i] - dps[j]) == 1)
147       {
148         merge_possible = false;
149         if (ups[i] > dps[j])
150           close_half_collide = true;
151         else
152           distant_half_collide = true;
153       }
154     else if (ups[i]== dps[j])
155       full_collide = true;
156     else if (ups[i] >dps[0] && ups[i] < dps.top ())
157       merge_possible = false;
158     else if (dps[j] >ups[0] && dps[j] < ups.top ())
159       merge_possible = false;
160     
161     if (ups[i] < dps[j])
162       i++;
163     else if (ups[i] > dps[j])
164       j++;
165     else
166       {
167         i++;
168         j++;
169       }
170   }
171
172   full_collide = full_collide || (close_half_collide
173                                   && distant_half_collide);
174   
175   Drul_array<Real> center_note_shifts;
176   center_note_shifts[LEFT] = 0.0;
177   center_note_shifts[RIGHT] = 0.0;
178
179   
180   Real shift_amount = 1;
181
182   bool touch = (ups[0] >= dps.top ());
183   if (touch)
184     shift_amount *= -1;
185
186   /* For full collisions, the right hand head may obscure dots, so
187      make sure the dotted heads go to the right.  */
188   bool stem_to_stem = false;
189   if (full_collide)
190     if (Rhythmic_head::dot_count (nu) > Rhythmic_head::dot_count (nd))
191       shift_amount = 1;
192     else if (Rhythmic_head::dot_count (nu) < Rhythmic_head::dot_count (nd))
193       stem_to_stem = true;
194   
195   if (merge_possible)
196     {
197       shift_amount = 0;
198
199
200       /* If possible, don't wipe any heads. Else, wipe shortest head,
201          or head with smallest amount of dots.  Note: when merging
202          different heads, dots on the smaller one disappear. */
203       Grob *wipe_ball = 0;
204       Grob *dot_wipe_head = nu;
205       
206       if (upball_type == dnball_type)
207         {
208           if (Rhythmic_head::dot_count (nd) < Rhythmic_head::dot_count (nu))
209             {
210               wipe_ball = nd;
211               dot_wipe_head = nd;
212             }
213           else if (Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu))
214             {
215               dot_wipe_head = nu;
216               wipe_ball = nu;
217             }
218           else
219             {
220               dot_wipe_head = nu;
221             }
222         }
223       else if (dnball_type > upball_type)
224         {
225           wipe_ball = nd;
226           dot_wipe_head = nd;
227         }
228       else if (dnball_type < upball_type)
229         {
230           wipe_ball = nu;
231           dot_wipe_head = nu;
232         }
233
234       if (dot_wipe_head)
235         {
236           if (Grob *d = unsmob_grob (dot_wipe_head->get_property ("dot")))
237             d->suicide ();
238         }
239       
240       if (wipe_ball && wipe_ball->is_live ())
241         {
242           wipe_ball->set_property ("transparent", SCM_BOOL_T);
243           wipe_ball->set_property ("stencil", SCM_EOL);
244         }
245     }
246   /* TODO: these numbers are magic; should devise a set of grob props
247      to tune this behavior.  */
248   else if (stem_to_stem)
249     shift_amount = -abs (shift_amount) * 0.65; 
250   else if (close_half_collide && !touch)
251     shift_amount *= 0.52;
252   else if (distant_half_collide && !touch)
253     shift_amount *= 0.4;
254   else if (distant_half_collide || close_half_collide || full_collide)
255     shift_amount *= 0.5;
256   
257   /* we're meshing.  */
258   else if (Rhythmic_head::dot_count (nu) || Rhythmic_head::dot_count (nd))
259     shift_amount *= 0.1;
260   else
261     shift_amount *= 0.17;
262
263   /* For full or close half collisions, the right hand head may
264      obscure dots.  Move dots to the right.  */
265   if (abs (shift_amount) > 1e-6
266       && Rhythmic_head::dot_count (nd) > Rhythmic_head::dot_count (nu)
267       && (full_collide || close_half_collide))
268     {
269       Grob *d = unsmob_grob (nd->get_property ("dot"));
270       Grob *parent = d->get_parent (X_AXIS);
271
272
273       /*
274         FIXME:
275         
276               |
277          x . o
278         |
279
280         
281         the . is put right of o which is erroneous o force-shifted
282         far to the right.
283       */
284       if (Dot_column::has_interface (parent))
285         Side_position_interface::add_support (parent, nu);
286     }
287
288   Direction d = UP;
289   do
290     {
291       for (int i = 0; i < clash_groups[d].size (); i++)
292         (*offsets)[d][i] += d * shift_amount;
293     }
294   while ((flip (&d))!= UP);
295 }
296
297 void
298 Note_collision_interface::do_shifts (Grob* me)
299 {
300   Drul_array< Link_array <Grob>  > cg = get_clash_groups (me);
301
302   SCM autos (automatic_shift (me, cg));
303   SCM hand (forced_shift (me));
304   
305   Direction d = UP;
306   Real wid = 0.0;
307   do
308     {
309       if (cg[d].size ())
310         {
311           Grob  *h = cg[d][0];
312           wid = Note_column::first_head (h)->extent (h, X_AXIS).length () ;
313         }
314     }
315   while (flip (&d) != UP);
316   
317   Link_array<Grob> done;
318   Real left_most = 1e6;
319   
320   Array<Real> amounts;
321   for (; scm_is_pair (hand); hand = scm_cdr (hand))
322     {
323       Grob * s = unsmob_grob (scm_caar (hand));
324       Real amount = scm_to_double (scm_cdar (hand)) * wid;
325
326       done.push (s);
327       amounts.push (amount);
328       if (amount < left_most)
329         left_most = amount;
330       
331     }
332   for (; scm_is_pair (autos); autos = scm_cdr (autos))
333     {
334       Grob * s = unsmob_grob (scm_caar (autos));
335       Real amount = scm_to_double (scm_cdar (autos)) *wid;
336       
337       if (!done.find (s))
338         {
339           done.push (s);
340           amounts.push (amount);
341           if (amount < left_most)
342             left_most = amount;
343         }
344     }
345
346   for (int i = 0; i < amounts.size(); i++)
347     {
348       done[i]->translate_axis (amounts[i] - left_most, X_AXIS);
349     }
350 }
351
352 Drul_array< Link_array <Grob>  >
353 Note_collision_interface::get_clash_groups (Grob *me)
354 {
355   Drul_array<Link_array<Grob> > clash_groups;
356  
357   SCM s = me->get_property ("elements");
358   for (; scm_is_pair (s); s = scm_cdr (s))
359     {
360       SCM car = scm_car (s);
361
362       Grob * se = unsmob_grob (car);
363       if (Note_column::has_interface (se))
364         clash_groups[Note_column::dir (se)].push (se);
365     }
366   
367   Direction d = UP;
368   do
369     {
370       Link_array<Grob> & clashes (clash_groups[d]);
371       clashes.sort (Note_column::shift_compare);
372     }
373   while ((flip (&d))!= UP);
374
375   return clash_groups;
376 }
377
378 /** This complicated routine moves note columns around horizontally to
379   ensure that notes don't clash.
380
381   This should be put into Scheme.  
382   */
383 SCM
384 Note_collision_interface::automatic_shift (Grob *me,
385                             Drul_array< Link_array <Grob> > 
386                             clash_groups)
387 {
388   Drul_array<Array<int> > shifts;
389   SCM  tups = SCM_EOL;
390
391   
392   Direction d = UP;
393   do
394     {
395       Array<int> & shift (shifts[d]);
396       Link_array<Grob> & clashes (clash_groups[d]);
397
398       for (int i = 0; i < clashes.size (); i++)
399         {
400           SCM sh
401             = clashes[i]->get_property ("horizontal-shift");
402
403           if (scm_is_number (sh))
404             shift.push (scm_to_int (sh));
405           else
406             shift.push (0);
407         }
408       
409       for (int i =1; i < shift.size (); i++)
410         {
411           if (shift[i-1] == shift[i])
412             {
413               clashes[0]->warning (_ ("Too many clashing notecolumns.  Ignoring them."));
414               return tups;
415             }
416         }
417     }
418   while ((flip (&d))!= UP);
419
420   Drul_array< Array < Slice > > extents;
421   Drul_array< Array < Real > > offsets;
422   d = UP;
423   do
424     {
425       for (int i = 0; i < clash_groups[d].size (); i++)
426         {
427           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
428           s[LEFT] --;
429           s[RIGHT]++;
430           extents[d].push (s);
431           offsets[d].push (d * 0.5 * i);
432         }
433     }
434   while ((flip (&d))!= UP);
435
436   /*
437     do horizontal shifts of each direction 
438
439        | 
440       x||
441        x||
442         x|
443    */
444   
445   do
446     {
447       for (int i =1; i < clash_groups[d].size (); i++)
448         {
449           Slice prev = extents[d][i-1];
450           prev.intersect (extents[d][i]);
451           if (prev.length ()> 0 ||
452  (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
453             for (int j = i; j <  clash_groups[d].size (); j++)
454               offsets[d][j] += d * 0.5;
455         }
456     }   
457   while ((flip (&d))!= UP);
458
459
460   /*
461     Check if chords are meshing
462    */
463
464   check_meshing_chords (me, &offsets, extents, clash_groups);
465   
466   do
467     {
468       for (int i = 0; i < clash_groups[d].size (); i++)
469         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
470                                  scm_make_real (offsets[d][i])),
471                         tups);
472     }
473   while (flip (&d) != UP);
474   return tups;
475 }
476
477
478 SCM
479 Note_collision_interface::forced_shift (Grob *me)
480 {
481   SCM tups = SCM_EOL;
482   
483   SCM s = me->get_property ("elements");
484   for (; scm_is_pair (s); s = scm_cdr (s))
485     {
486       Grob * se = unsmob_grob (scm_car (s));
487
488       SCM force =  se->get_property ("force-hshift");
489       if (scm_is_number (force))
490         {
491           tups = scm_cons (scm_cons (se->self_scm (), force),
492                           tups);
493         }
494     }
495   return tups;
496 }
497
498 void
499 Note_collision_interface::add_column (Grob*me, Grob* ncol)
500 {
501   ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
502   Axis_group_interface::add_element (me, ncol);
503   me->add_dependency (ncol);
504 }
505
506
507 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
508                "An object that handles collisions between notes with different stem " 
509                "directions and horizontal shifts. Most of the interesting properties "
510                "are to be set in @ref{note-column-interface}: these are "
511                "@code{force-hshift} and @code{horizontal-shift}."
512
513                ,
514                
515                "merge-differently-dotted merge-differently-headed positioning-done");