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