]> git.donarmstrong.com Git - lilypond.git/blob - lily/note-collision.cc
* lily/text-item.cc (interpret_string): new file, select font with
[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--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9 #include <math.h>
10
11 #include "warn.hh"
12 #include "note-collision.hh"
13 #include "note-column.hh"
14 #include "note-head.hh"
15 #include "rhythmic-head.hh"
16 #include "paper-def.hh"
17 #include "axis-group-interface.hh"
18 #include "item.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) ly_scm2int (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 ( !is_equal (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->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.25;
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       if (Dot_column::has_interface (parent))
272         Side_position_interface::add_support (parent, nu);
273     }
274
275   Direction d = UP;
276   do
277     {
278       for (int i=0; i < clash_groups[d].size (); i++)
279         (*offsets)[d][i] += d * shift_amount;
280     }
281   while ((flip (&d))!= UP);
282 }
283
284 void
285 Note_collision_interface::do_shifts (Grob* me)
286 {
287   Drul_array< Link_array <Grob>  > cg = get_clash_groups (me);
288
289   SCM autos (automatic_shift (me, cg));
290   SCM hand (forced_shift (me));
291   
292   Direction d = UP;
293   Real wid = 0.0;
294   do
295     {
296       if (cg[d].size ())
297         {
298           Grob  *h = cg[d][0];
299           wid = Note_column::first_head (h)->extent (h,X_AXIS).length () ;
300         }
301     }
302   while (flip (&d) != UP);
303   
304   Link_array<Grob> done;
305   for (; is_pair (hand); hand =ly_cdr (hand))
306     {
307       Grob * s = unsmob_grob (ly_caar (hand));
308       Real amount = ly_scm2double (ly_cdar (hand));
309       
310       s->translate_axis (amount *wid, X_AXIS);
311       done.push (s);
312     }
313   for (; is_pair (autos); autos =ly_cdr (autos))
314     {
315       Grob * s = unsmob_grob (ly_caar (autos));
316       Real amount = ly_scm2double (ly_cdar (autos));
317       
318       if (!done.find (s))
319         s->translate_axis (amount * wid, X_AXIS);
320     }
321 }
322
323 Drul_array< Link_array <Grob>  >
324 Note_collision_interface::get_clash_groups (Grob *me)
325 {
326   Drul_array<Link_array<Grob> > clash_groups;
327  
328   SCM s = me->get_property ("elements");
329   for (; is_pair (s); s = ly_cdr (s))
330     {
331       SCM car = ly_car (s);
332
333       Grob * se = unsmob_grob (car);
334       if (Note_column::has_interface (se))
335         clash_groups[Note_column::dir (se)].push (se);
336     }
337   
338   Direction d = UP;
339   do
340     {
341       Link_array<Grob> & clashes (clash_groups[d]);
342       clashes.sort (Note_column::shift_compare);
343     }
344   while ((flip (&d))!= UP);
345
346   return clash_groups;
347 }
348
349 /** This complicated routine moves note columns around horizontally to
350   ensure that notes don't clash.
351
352   This should be put into Scheme.  
353   */
354 SCM
355 Note_collision_interface::automatic_shift (Grob *me,
356                             Drul_array< Link_array <Grob> > 
357                             clash_groups)
358 {
359   Drul_array<Array<int> > shifts;
360   SCM  tups = SCM_EOL;
361
362   
363   Direction d = UP;
364   do
365     {
366       Array<int> & shift (shifts[d]);
367       Link_array<Grob> & clashes (clash_groups[d]);
368
369       for (int i=0; i < clashes.size (); i++)
370         {
371           SCM sh
372             = clashes[i]->get_property ("horizontal-shift");
373
374           if (is_number (sh))
375             shift.push (ly_scm2int (sh));
376           else
377             shift.push (0);
378         }
379       
380       for (int i=1; i < shift.size (); i++)
381         {
382           if (shift[i-1] == shift[i])
383             {
384               clashes[0]->warning (_ ("Too many clashing notecolumns.  Ignoring them."));
385               return tups;
386             }
387         }
388     }
389   while ((flip (&d))!= UP);
390
391   Drul_array< Array < Slice > > extents;
392   Drul_array< Array < Real > > offsets;
393   d = UP;
394   do
395     {
396       for (int i=0; i < clash_groups[d].size (); i++)
397         {
398           Slice s (Note_column::head_positions_interval (clash_groups[d][i]));
399           s[LEFT] --;
400           s[RIGHT]++;
401           extents[d].push (s);
402           offsets[d].push (d * 0.5 * i);
403         }
404     }
405   while ((flip (&d))!= UP);
406
407   /*
408     do horizontal shifts of each direction 
409
410        | 
411       x||
412        x||
413         x|
414    */
415   
416   do
417     {
418       for (int i=1; i < clash_groups[d].size (); i++)
419         {
420           Slice prev =extents[d][i-1];
421           prev.intersect (extents[d][i]);
422           if (prev.length ()> 0 ||
423  (extents[-d].size () && d * (extents[d][i][-d] - extents[-d][0][d]) < 0))
424             for (int j = i; j <  clash_groups[d].size (); j++)
425               offsets[d][j] += d * 0.5;
426         }
427     }   
428   while ((flip (&d))!= UP);
429
430
431   /*
432     Check if chords are meshing
433    */
434
435   check_meshing_chords (me, &offsets, extents, clash_groups);
436   
437   do
438     {
439       for (int i=0; i < clash_groups[d].size (); i++)
440         tups = scm_cons (scm_cons (clash_groups[d][i]->self_scm (),
441                                  scm_make_real (offsets[d][i])),
442                         tups);
443     }
444   while (flip (&d) != UP);
445   return tups;
446 }
447
448
449 SCM
450 Note_collision_interface::forced_shift (Grob *me)
451 {
452   SCM tups = SCM_EOL;
453   
454   SCM s = me->get_property ("elements");
455   for (; is_pair (s); s = ly_cdr (s))
456     {
457       Grob * se = unsmob_grob (ly_car (s));
458
459       SCM force =  se->get_property ("force-hshift");
460       if (is_number (force))
461         {
462           tups = scm_cons (scm_cons (se->self_scm (), force),
463                           tups);
464         }
465     }
466   return tups;
467 }
468
469 void
470 Note_collision_interface::add_column (Grob*me,Grob* ncol)
471 {
472   ncol->add_offset_callback (Note_collision_interface::force_shift_callback_proc, X_AXIS);
473   Axis_group_interface::add_element (me, ncol);
474   me->add_dependency (ncol);
475 }
476
477
478 ADD_INTERFACE (Note_collision_interface, "note-collision-interface",
479                "An object that handles collisions between notes with different stem " 
480                "directions and horizontal shifts. Most of the interesting properties "
481                "are to be set in @ref{note-column-interface}: these are "
482                "@code{force-hshift} and @code{horizontal-shift}."
483
484                ,
485                
486                "merge-differently-dotted merge-differently-headed positioning-done");