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