]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-scoring.cc
new file.
[lilypond.git] / lily / slur-scoring.cc
1 /*
2   slur-scoring.cc -- Score based slur formatting
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1996--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10
11 #include <math.h>
12
13 #include "slur-scoring.hh"
14 #include "libc-extension.hh"
15 #include "slur-configuration.hh"
16 #include "beam.hh"
17 #include "directional-element-interface.hh"
18 #include "group-interface.hh"
19 #include "slur.hh"
20 #include "note-column.hh"
21 #include "output-def.hh"
22 #include "pitch.hh"
23 #include "spanner.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "staff-symbol.hh"
26 #include "stem.hh"
27 #include "warn.hh"
28 #include "paper-column.hh"
29 #include "accidental-interface.hh"
30
31 /*
32   TODO:
33
34   - curve around flag for y coordinate
35
36   - this file is a mess, clean it up
37
38   - short-cut: try a smaller region first.
39
40   - handle non-visible stems better.
41
42   - try to prune number of scoring criteria
43
44   - take encompass-objects more into account when determining
45   slur shape
46
47   - calculate encompass scoring directly after determining slur shape.
48
49   - optimize.
50
51 */
52 struct Slur_score_state;
53
54 Slur_score_state::Slur_score_state()
55 {
56   musical_dy_ = 0.0;
57   valid_ = false;
58   edge_has_beams_ = false;
59   has_same_beam_ = false;
60   is_broken_ = false;
61   dir_ = CENTER;
62   slur_ = 0;
63   common_[X_AXIS] = 0;
64   common_[Y_AXIS] = 0;
65 }
66
67 Slur_score_state::~Slur_score_state ()
68 {
69   junk_pointers (configurations_);
70 }
71
72 Real
73 get_detail (SCM alist, SCM sym)
74 {
75   SCM entry = scm_assq (sym, alist);
76   return robust_scm2double (scm_is_pair (entry)
77                             ? scm_cdr (entry)
78                             : SCM_EOL,
79                             0.0);
80 }
81
82 void
83 Slur_score_parameters::fill (Grob *me)
84 {
85   SCM details = me->get_property ("slur-details");
86
87   region_size_
88     = (int) get_detail (details, ly_symbol2scm ("region-size"));
89   head_encompass_penalty_
90     = get_detail (details, ly_symbol2scm ("head-encompass-penalty"));
91   stem_encompass_penalty_
92     = get_detail (details, ly_symbol2scm ("stem-encompass-penalty"));
93   closeness_factor_
94     = get_detail (details, ly_symbol2scm ("closeness-factor"));
95   edge_attraction_factor_
96     = get_detail (details, ly_symbol2scm ("edge-attraction-factor"));
97   same_slope_penalty_
98     = get_detail (details, ly_symbol2scm ("same-slope-penalty"));
99   steeper_slope_factor_
100     = get_detail (details, ly_symbol2scm ("steeper-slope-factor"));
101   non_horizontal_penalty_
102     = get_detail (details, ly_symbol2scm ("non-horizontal-penalty"));
103   max_slope_
104     = get_detail (details, ly_symbol2scm ("max-slope"));
105   max_slope_factor_
106     = get_detail (details, ly_symbol2scm ("max-slope-factor"));
107   free_head_distance_
108     = get_detail (details, ly_symbol2scm ("free-head-distance"));
109   absolute_closeness_measure_
110     = get_detail (details, ly_symbol2scm ("absolute-closeness-measure"));
111   extra_object_collision_
112     = get_detail (details, ly_symbol2scm ("extra-object-collision"));
113   accidental_collision_
114     = get_detail (details, ly_symbol2scm ("accidental-collision"));
115   extra_encompass_free_distance_
116     = get_detail (details, ly_symbol2scm ("extra-encompass-free-distance"));
117   head_slur_distance_factor_
118     = get_detail (details, ly_symbol2scm ("head-slur-distance-factor"));
119   head_slur_distance_max_ratio_
120     = get_detail (details, ly_symbol2scm ("head-slur-distance-max-ratio"));
121   free_slur_distance_
122     = get_detail (details, ly_symbol2scm ("free-slur-distance"));
123   edge_slope_exponent_
124     = get_detail (details, ly_symbol2scm ("edge-slope-exponent"));
125 }
126
127 Real
128 broken_trend_y (Slur_score_state const &state, Direction hdir)
129 {
130   /* A broken slur should maintain the same vertical trend
131      the unbroken slur would have had.  */
132   Real by = 0.0;
133   if (Spanner *mother = dynamic_cast<Spanner*> (state.slur_->original_))
134     {
135       int k = broken_spanner_index (state.slur_);
136       int j = k + hdir;
137       if (j < 0 || j >= mother->broken_intos_.size ())
138         return by;
139
140       Grob *neighbor = mother->broken_intos_[j];
141       Spanner *common_mother
142         = dynamic_cast<Spanner*> (state.common_[Y_AXIS]->original_);
143       int common_k
144         = broken_spanner_index (dynamic_cast<Spanner*> (state.common_[Y_AXIS]));
145       int common_j = common_k + hdir;
146
147       if (common_j < 0 || common_j >= common_mother->broken_intos_.size ())
148         return by;
149
150       Grob *common_next_system = common_mother->broken_intos_[common_j];
151       
152       SCM last_point =  scm_car (scm_last_pair (neighbor->get_property ("control-points")));
153       
154       return scm_to_double (scm_cdr (last_point))
155         + neighbor->relative_coordinate (common_next_system, Y_AXIS);
156     }
157   return by;
158 }
159
160
161 /*
162   copy slur dir forwards across line break.
163 */
164 void
165 Slur_score_state::set_next_direction () 
166 {
167   if (extremes_[RIGHT].note_column_)
168     return;
169   
170   if (Spanner *mother = dynamic_cast<Spanner*> (slur_->original_))
171     {
172       int k = broken_spanner_index (slur_);
173       int j = k + 1;
174       if (j < 0 || j >= mother->broken_intos_.size ())
175         return;
176
177       Grob *neighbor = mother->broken_intos_[j];
178       set_grob_direction (neighbor, dir_);
179     }
180 }
181
182 Encompass_info
183 Slur_score_state::get_encompass_info (Grob *col) const
184 {
185   Grob *stem = unsmob_grob (col->get_property ("stem"));
186   Encompass_info ei;
187
188   if (!stem)
189     {
190       programming_error ("No stem for note column?");
191       ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
192       ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
193                                          Y_AXIS)[dir_];
194       return ei;
195     }
196   Direction stem_dir = get_grob_direction (stem);
197
198   if (Grob *head = Note_column::first_head (col))
199     ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
200   else
201     ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
202
203   Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
204   if (!h)
205     {
206       ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
207       return ei;
208     }
209
210   ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
211
212   if ((stem_dir == dir_)
213       && !stem->extent (stem, Y_AXIS).is_empty ())
214     {
215       ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
216       if (Grob *b = Stem::get_beam (stem))
217         ei.stem_ += stem_dir * 0.5 * Beam::get_thickness (b);
218
219       Interval x = stem->extent (common_[X_AXIS], X_AXIS);
220       ei.x_ = x.is_empty ()
221         ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
222         : x.center ();
223     }
224   else
225     ei.stem_ = ei.head_;
226
227   return ei;
228 }
229
230
231
232
233 Drul_array<Bound_info>
234 Slur_score_state::get_bound_info () const
235 {
236   Drul_array<Bound_info> extremes;
237
238   Direction d = LEFT;
239   Direction dir = dir_;
240
241   do
242     {
243       extremes[d].bound_ = slur_->get_bound (d);
244       if (Note_column::has_interface (extremes[d].bound_))
245         {
246           extremes[d].note_column_ = extremes[d].bound_;
247           extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
248           extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
249           extremes[d].stem_extent_[X_AXIS]
250             = extremes[d].stem_->extent (common_[X_AXIS], X_AXIS);
251           extremes[d].stem_extent_[Y_AXIS]
252             = extremes[d].stem_->extent (common_[Y_AXIS], Y_AXIS);
253           extremes[d].slur_head_
254             = Stem::extremal_heads (extremes[d].stem_)[dir];
255           if (!extremes[d].slur_head_
256               && Note_column::has_rests (extremes[d].bound_))
257             {
258               extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
259             }
260
261           if (extremes[d].slur_head_)
262             extremes[d].slur_head_extent_
263               = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
264
265           extremes[d].staff_ = Staff_symbol_referencer
266             ::get_staff_symbol (extremes[d].stem_);
267           extremes[d].staff_space_ = Staff_symbol_referencer
268             ::staff_space (extremes[d].stem_);
269         }
270     }
271   while (flip (&d) != LEFT);
272   return extremes;
273 }
274
275 void
276 Slur_score_state::fill (Grob *me)
277 {
278   slur_ = dynamic_cast<Spanner*> (me);
279   columns_ 
280     = Pointer_group_interface__extract_grobs (me, (Grob *) 0, "note-columns");
281   
282   if (columns_.is_empty ())
283     {
284       me->suicide ();
285       return ;
286     }
287
288   staff_space_ = Staff_symbol_referencer::staff_space (me);
289   Real lt = me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
290   thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) *  lt;
291   
292   dir_ = get_grob_direction (me);
293   parameters_.fill (me);
294   
295   SCM eltlist = me->get_property ("note-columns");
296   SCM extra_list = me->get_property ("encompass-objects");
297   Spanner *sp = dynamic_cast<Spanner*> (me);
298
299   for (int i = X_AXIS; i < NO_AXES; i++)
300     {
301       Axis a = (Axis)i;
302       common_[a] = common_refpoint_of_list (eltlist, me, a);
303       common_[a] = common_refpoint_of_list (extra_list, common_[a], a);
304
305       Direction d = LEFT;
306       do {
307         /*
308           If bound is not in note-columns, we don't want to know about
309           its Y-position
310          */
311         if (a != Y_AXIS) 
312           common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
313       }
314       while (flip (&d) != LEFT);
315     }
316
317   extremes_ = get_bound_info ();
318   is_broken_ = (!extremes_[LEFT].note_column_
319                 || !extremes_[RIGHT].note_column_); 
320
321   has_same_beam_ =
322     (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
323      && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
324   
325   base_attachments_ = get_base_attachments ();
326
327   Drul_array<Real> end_ys
328     = get_y_attachment_range ();
329
330   configurations_ = enumerate_attachments ( end_ys);
331   for (int i = 0; i < columns_.size (); i++)
332     encompass_infos_.push (get_encompass_info ( columns_[i]));
333
334   extra_encompass_infos_ = get_extra_encompass_infos ();
335   valid_ = true;
336
337
338   musical_dy_ = 0.0;
339   Direction d = LEFT;
340   do
341     {
342       if (!is_broken_)
343         musical_dy_ += d
344           * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
345     }
346   while (flip (&d) != LEFT);
347   
348   edge_has_beams_
349     = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
350     || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
351
352
353   
354   set_next_direction ();
355
356   if (is_broken_)
357     musical_dy_ = 0.0;
358 }
359
360 void
361 set_slur_control_points (Grob *me)
362 {
363   Slur_score_state state;
364   state.fill (me);
365
366   if (!state.valid_)
367     return;
368   
369   state.generate_curves ();
370
371   SCM end_ys = me->get_property ("positions");
372   Bezier best;
373
374   if (is_number_pair (end_ys))
375     {
376       best = state.configurations_[state.get_closest_index (end_ys)]->curve_;
377     }
378   else
379     {
380       best = state.get_best_curve();
381     }
382
383   SCM controls = SCM_EOL;
384   for (int i = 4; i--;)
385     {
386       Offset o = best.control_[i]
387         - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
388                   me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
389       controls = scm_cons (ly_offset2scm (o), controls);
390     }
391   me->set_property ("control-points", controls);
392 }
393
394
395 Bezier
396 Slur_score_state::get_best_curve ()
397 {
398   for (int i = 0; i < configurations_.size (); i++)
399     {
400       configurations_[i]->score (*this);
401     }
402   
403   Real opt = 1e6;
404   int opt_idx = -1;
405   for (int i = 0; i < configurations_.size (); i++)
406     {
407       if (configurations_[i]->score_ < opt)
408         {
409           opt = configurations_[i]->score_;
410           opt_idx = i;
411         }
412     }
413
414 #if DEBUG_SLUR_SCORING
415   SCM inspect_quants = slur_->get_property ("inspect-quants");
416   if (to_boolean (slur_->get_layout ()
417                   ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")))
418       && scm_is_pair (inspect_quants))
419     opt_idx = get_closest_index (inspect_quants);
420
421   configurations_[opt_idx]->score_card_ += to_string ("=%.2f", opt);  
422   configurations_[opt_idx]->score_card_ += to_string ("i%d", opt_idx);
423
424   // debug quanting
425   slur_->set_property ("quant-score",
426                        scm_makfrom0str (configurations_[opt_idx]->score_card_.to_str0 ()));
427
428 #endif
429
430   return configurations_[opt_idx]->curve_;
431 }
432
433 int
434 Slur_score_state::get_closest_index (SCM inspect_quants) const
435 {
436   Drul_array<Real> ins = ly_scm2interval (inspect_quants);
437
438   int opt_idx = -1;
439   Real mindist = 1e6;
440   for (int i = 0; i < configurations_.size (); i ++)
441     {
442       Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ins[LEFT])
443         + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ins[RIGHT]);
444       if (d < mindist)
445         {
446           opt_idx = i;
447           mindist = d;
448         }
449     }
450   if (mindist > 1e5)
451     programming_error ("Could not find quant.");
452   return opt_idx;
453 }
454
455 /*
456   TODO: should analyse encompasses to determine sensible region, and
457   should limit slopes available.
458  */
459
460 Drul_array<Real>
461 Slur_score_state::get_y_attachment_range () const
462 {
463   Drul_array<Real> end_ys;
464   Direction d = LEFT;
465   do
466     {
467       if (extremes_[d].note_column_)
468         {
469           end_ys[d] = dir_
470             * ((dir_ * (base_attachments_[d][Y_AXIS] +  parameters_.region_size_* dir_))
471                >? (dir_ * (dir_ + extremes_[d].note_column_->extent (common_[Y_AXIS],
472                                                                  Y_AXIS)[dir_]))
473                >? (dir_ * base_attachments_[-d][Y_AXIS]));
474         }
475       else
476         end_ys[d] = base_attachments_[d][Y_AXIS] + parameters_.region_size_ * dir_;
477     }
478   while (flip (&d) != LEFT);
479
480   return end_ys;
481 }
482
483 bool
484 spanner_less (Spanner *s1, Spanner* s2)
485 {
486   Slice b1, b2;
487   Direction d  = LEFT;
488   do
489     {
490       b1[d] = s1->get_bound (d)->get_column ()->rank_;
491       b2[d] = s2->get_bound (d)->get_column ()->rank_;
492     } while (flip (&d) != LEFT);
493
494   return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
495     && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
496 }
497
498
499 Drul_array<Offset>
500 Slur_score_state::get_base_attachments () const
501 {
502   Drul_array<Offset> base_attachment;
503   Direction d = LEFT;
504   do
505     {
506       Grob *stem = extremes_[d].stem_;
507       Grob *head = extremes_[d].slur_head_;
508
509       Real x = 0.0;
510       Real y = 0.0;
511       if (extremes_[d].note_column_)
512         {
513          
514           /*
515             fixme: X coord should also be set in this case.
516            */
517           if (stem
518               && extremes_[d].stem_dir_ == dir_
519               && Stem::get_beaming (stem, -d)
520               && (!spanner_less (slur_, Stem::get_beam (stem))
521                   || has_same_beam_))
522             y = extremes_[d].stem_extent_[Y_AXIS][dir_];
523           else if (head)
524             y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
525           y += dir_ * 0.5 * staff_space_;
526
527
528           y = move_away_from_staffline (y, head); 
529
530           Grob * fh = Note_column::first_head (extremes_[d].note_column_);
531           x =
532          (fh ? fh->extent (common_[X_AXIS], X_AXIS)
533              : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
534             .linear_combination (CENTER);
535         }
536       base_attachment[d] = Offset (x, y);
537
538     } while (flip (&d) != LEFT);
539
540   do
541     {
542       if (!extremes_[d].note_column_)
543         {
544           Real x, y;
545           if (d == RIGHT)
546             {
547               x = extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS)[d];
548             }
549           else
550             {
551               x = slur_->get_broken_left_end_align ();
552             }
553           Grob * col = (d == LEFT) ? columns_[0] : columns_.top();
554               
555           if (extremes_[-d].bound_ != col)
556             {
557               y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
558               y += dir_ * 0.5 * staff_space_;
559               
560               if (get_grob_direction (col) == dir_
561                   && Note_column::get_stem (col)
562                   && !Stem::is_invisible (Note_column::get_stem (col)))
563                 y -= dir_ * 1.5 * staff_space_;
564             }
565           else
566             y = base_attachment[-d][Y_AXIS];
567
568           
569           y = move_away_from_staffline (y, col);
570             
571           base_attachment[d] = Offset (x, y);  
572         }
573     }
574   while (flip (&d) != LEFT);
575
576   return base_attachment;
577 }
578
579 Real
580 Slur_score_state::move_away_from_staffline (Real y,
581                                             Grob *on_staff) const
582 {
583   Real pos
584     = (y - Staff_symbol_referencer::get_staff_symbol (on_staff)->relative_coordinate (common_[Y_AXIS],
585                                                                                Y_AXIS))
586     * 2.0 / staff_space_;
587   
588   if (fabs (pos - my_round (pos)) < 0.2
589       && Staff_symbol_referencer::on_staffline (on_staff, (int) rint (pos))
590       && Staff_symbol_referencer::line_count (on_staff) - 1 >= rint (pos)
591       )
592     y += 1.5 * staff_space_ * dir_ / 10;
593
594   return y;
595 }
596
597 void
598 Slur_score_state::generate_curves () const
599 {
600   Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
601   Real h_inf = staff_space_ *scm_to_double (slur_->get_property ("height-limit"));
602   for (int i = 0; i < configurations_.size (); i++)
603     configurations_[i]->generate_curve (*this, r_0, h_inf);
604 }
605
606
607
608
609 Link_array<Slur_configuration> 
610 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
611 {
612   /*ugh.   */
613   Link_array<Slur_configuration> scores;
614
615
616   Drul_array<Offset> os;
617   os[LEFT] = base_attachments_[LEFT];
618   Real minimum_length = staff_space_
619     * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
620
621   for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
622     {
623       os[RIGHT] = base_attachments_[RIGHT];
624       for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
625         {
626           Slur_configuration s;
627           Direction d = LEFT;
628           Drul_array<bool> attach_to_stem (false, false);
629           do
630             {
631               os[d][X_AXIS] = base_attachments_[d][X_AXIS];
632               if (extremes_[d].stem_
633                   && !Stem::is_invisible (extremes_[d].stem_)
634                   && extremes_[d].stem_dir_ == dir_)
635                 {
636                   Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
637                   stem_y.widen (0.25 * staff_space_);
638                   if (stem_y.contains (os[d][Y_AXIS]))
639                     {
640                       os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
641                         - d * 0.3;
642                       attach_to_stem[d] = true;
643                     }
644                   else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
645                            < dir_ * os[d][Y_AXIS]
646                            && !extremes_[d].stem_extent_[X_AXIS].is_empty ()
647                            )
648                 
649                     os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
650                 }
651             }
652           while (flip (&d) != LEFT);
653
654           Offset dz;    
655           dz = os[RIGHT] - os[LEFT];
656           if (dz[X_AXIS] < minimum_length
657               || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_
658               )
659             {
660               do
661                 {
662                   if (extremes_[d].slur_head_)
663                     {
664                       os[d][X_AXIS] = extremes_[d].slur_head_extent_.center ();
665                       attach_to_stem[d] = false;
666                     }
667                 }
668               while (flip (&d) != LEFT);
669             }
670
671           dz = os[RIGHT] - os[LEFT];
672           do
673             {
674               if (extremes_[d].slur_head_
675                   && !attach_to_stem[d])
676                 {
677                   /* Horizontally move tilted slurs a little.  Move
678                      more for bigger tilts.
679                 
680                      TODO: parameter */
681                   os[d][X_AXIS]
682                     -= dir_ * extremes_[d].slur_head_extent_.length ()
683                     * sin (dz.arg ()) / 3;
684                 }
685             }
686           while (flip (&d) != LEFT);
687         
688           s.attachment_ = os;
689           s.index_ = scores.size ();
690
691           scores.push (new Slur_configuration (s));
692           
693           os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
694         }
695
696       os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
697     }
698
699   assert (scores.size () > 0);
700   return scores;
701 }
702
703
704 Array<Extra_collision_info>
705 Slur_score_state::get_extra_encompass_infos () const
706 {
707   Link_array<Grob> encompasses
708     = Pointer_group_interface__extract_grobs (slur_, (Grob *)0,
709                                               "encompass-objects");
710   Array<Extra_collision_info> collision_infos;
711   for (int i = encompasses.size (); i--; )
712     {
713       if (Slur::has_interface (encompasses[i]))
714         {
715           Spanner * small_slur = dynamic_cast<Spanner*> (encompasses[i]);
716           Bezier b = Slur::get_curve (small_slur);
717
718           Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
719                            small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
720
721           for (int k = 0; k < 3; k++)
722             {
723               Direction hdir =  Direction (k /2 - 1);
724
725               /*
726                 Only take bound into account if small slur starts
727                 together with big slur.
728               */
729               if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
730                 continue;
731         
732               Offset z = b.curve_point (k / 2.0);
733               z += relative;
734
735               Interval yext;
736               yext.set_full ();
737               yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
738
739               Interval xext (-1, 1);
740               xext = xext * (thickness_*2) + z[X_AXIS];
741               Extra_collision_info info (small_slur,
742                                          k - 1.0,
743                                          xext,
744                                          yext,
745                                          parameters_.extra_object_collision_);
746               collision_infos.push (info);
747             }
748         }
749       else
750         {
751           Grob *g = encompasses [i];
752           Interval xe = g->extent (common_[X_AXIS], X_AXIS);
753           Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
754
755           Real xp = 0.0;
756           Real penalty = parameters_.extra_object_collision_;
757           if (Accidental_interface::has_interface (g))
758             {
759               penalty = parameters_.accidental_collision_;
760               /* Begin copy accidental.cc */
761               bool parens = false;
762               if (to_boolean (g->get_property ("cautionary")))
763                 {
764                   SCM cstyle = g->get_property ("cautionary-style");
765                   parens = ly_c_equal_p (cstyle, ly_symbol2scm ("parentheses"));
766                 }
767         
768               SCM accs = g->get_property ("accidentals");
769               SCM scm_style = g->get_property ("style");
770               if (!scm_is_symbol (scm_style)
771                   && !parens
772                   && scm_ilength (accs) == 1)
773                 {
774                   /* End copy accidental.cc */
775                   switch (scm_to_int (scm_car (accs)))
776                     {
777                     case FLAT:
778                     case DOUBLE_FLAT:
779                       xp = LEFT;
780                       break ;
781                     case SHARP:
782                       xp = 0.5 * dir_;
783                       break ;
784                     case NATURAL:
785                       xp = -dir_;
786                       break;
787                     }
788                 }
789             }
790
791           ye.widen (thickness_ * 0.5);
792           xe.widen (thickness_ * 1.0);
793           Extra_collision_info info (g, xp, xe, ye,  penalty);
794           collision_infos.push (info);
795         }
796     }
797
798   return collision_infos;
799 }
800