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