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