]> git.donarmstrong.com Git - lilypond.git/blob - lily/slur-scoring.cc
58ad4873d933b704caff41b7ddca647be7bd42ec
[lilypond.git] / lily / slur-scoring.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1996--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5   Jan Nieuwenhuizen <janneke@gnu.org>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "slur-scoring.hh"
22
23 #include <queue>
24
25 #include "axis-group-interface.hh"
26 #include "accidental-interface.hh"
27 #include "beam.hh"
28 #include "clef.hh"
29 #include "directional-element-interface.hh"
30 #include "dots.hh"
31 #include "libc-extension.hh"
32 #include "main.hh"
33 #include "misc.hh"
34 #include "note-column.hh"
35 #include "output-def.hh"
36 #include "paper-column.hh"
37 #include "pitch.hh"
38 #include "pointer-group-interface.hh"
39 #include "slur-configuration.hh"
40 #include "slur.hh"
41 #include "spanner.hh"
42 #include "staff-symbol-referencer.hh"
43 #include "staff-symbol.hh"
44 #include "stem.hh"
45 #include "warn.hh"
46
47 using std::string;
48 using std::vector;
49
50 /*
51   TODO:
52
53   - curve around flag for y coordinate
54
55   - short-cut: try a smaller region first.
56
57   - handle non-visible stems better.
58
59   - try to prune number of scoring criteria
60
61   - take encompass-objects more into account when determining
62   slur shape
63
64   - calculate encompass scoring directly after determining slur shape.
65
66   - optimize.
67 */
68 struct Slur_score_state;
69
70 Slur_score_state::Slur_score_state ()
71 {
72   musical_dy_ = 0.0;
73   valid_ = false;
74   edge_has_beams_ = false;
75   has_same_beam_ = false;
76   is_broken_ = false;
77   dir_ = CENTER;
78   slur_ = 0;
79   common_[X_AXIS] = 0;
80   common_[Y_AXIS] = 0;
81 }
82
83 Slur_score_state::~Slur_score_state ()
84 {
85   junk_pointers (configurations_);
86 }
87
88 /*
89   If a slur is broken across a line break, the direction
90   of the post-break slur must be the same as the pre-break
91   slur.
92 */
93 Direction
94 Slur_score_state::slur_direction () const
95 {
96   Grob *left_neighbor = slur_->broken_neighbor (LEFT);
97
98   if (left_neighbor && left_neighbor->is_live ())
99     return get_grob_direction (left_neighbor);
100
101   Direction dir = get_grob_direction (slur_);
102
103   if (Grob *right_neighbor = slur_->broken_neighbor (RIGHT))
104     set_grob_direction (right_neighbor, dir);
105
106   return dir;
107 }
108
109 Encompass_info
110 Slur_score_state::get_encompass_info (Grob *col) const
111 {
112   Grob *stem = unsmob<Grob> (col->get_object ("stem"));
113   Encompass_info ei;
114
115   if (!stem)
116     {
117       programming_error ("no stem for note column");
118       ei.x_ = col->relative_coordinate (common_[X_AXIS], X_AXIS);
119       ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS],
120                                          Y_AXIS)[dir_];
121       return ei;
122     }
123   Direction stem_dir = get_grob_direction (stem);
124
125   if (Grob *head = Note_column::first_head (col))
126     ei.x_ = head->extent (common_[X_AXIS], X_AXIS).center ();
127   else
128     ei.x_ = col->extent (common_[X_AXIS], X_AXIS).center ();
129
130   Grob *h = Stem::extremal_heads (stem)[Direction (dir_)];
131   if (!h)
132     {
133       ei.head_ = ei.stem_ = col->extent (common_[Y_AXIS], Y_AXIS)[dir_];
134       return ei;
135     }
136
137   ei.head_ = h->extent (common_[Y_AXIS], Y_AXIS)[dir_];
138
139   if ((stem_dir == dir_)
140       && !stem->extent (stem, Y_AXIS).is_empty ())
141     {
142       ei.stem_ = stem->extent (common_[Y_AXIS], Y_AXIS)[dir_];
143       if (Grob *b = Stem::get_beam (stem))
144         ei.stem_ += stem_dir * 0.5 * Beam::get_beam_thickness (b);
145
146       Interval x = stem->extent (common_[X_AXIS], X_AXIS);
147       ei.x_ = x.is_empty ()
148               ? stem->relative_coordinate (common_[X_AXIS], X_AXIS)
149               : x.center ();
150     }
151   else
152     ei.stem_ = ei.head_;
153
154   return ei;
155 }
156
157 Drul_array<Bound_info>
158 Slur_score_state::get_bound_info () const
159 {
160   Drul_array<Bound_info> extremes;
161
162   Direction dir = dir_;
163
164   for (LEFT_and_RIGHT (d))
165     {
166       extremes[d].bound_ = slur_->get_bound (d);
167       if (has_interface<Note_column> (extremes[d].bound_))
168         {
169           extremes[d].note_column_ = extremes[d].bound_;
170           extremes[d].stem_ = Note_column::get_stem (extremes[d].note_column_);
171           extremes[d].flag_ = Note_column::get_flag (extremes[d].note_column_);
172
173           if (extremes[d].stem_)
174             {
175               extremes[d].stem_dir_ = get_grob_direction (extremes[d].stem_);
176
177               for (int a = X_AXIS; a < NO_AXES; a++)
178                 {
179                   Axis ax = Axis (a);
180                   Interval s = extremes[d].stem_->extent (common_[ax], ax);
181                   if (extremes[d].flag_)
182                     s.unite (extremes[d].flag_->extent (common_[ax], ax));
183                   if (s.is_empty ())
184                     {
185                       /*
186                         do not issue warning. This happens for rests and
187                         whole notes.
188                       */
189                       s = Interval (0, 0)
190                           + extremes[d].stem_->relative_coordinate (common_[ax], ax);
191                     }
192                   extremes[d].stem_extent_[ax] = s;
193                 }
194
195               extremes[d].slur_head_
196                 = Stem::extremal_heads (extremes[d].stem_)[dir];
197               if (!extremes[d].slur_head_
198                   && Note_column::has_rests (extremes[d].bound_))
199                 extremes[d].slur_head_ = Note_column::get_rest (extremes[d].bound_);
200               extremes[d].staff_ = Staff_symbol_referencer
201                                    ::get_staff_symbol (extremes[d].stem_);
202               extremes[d].staff_space_ = Staff_symbol_referencer
203                                          ::staff_space (extremes[d].stem_);
204             }
205
206           if (extremes[d].slur_head_)
207             extremes[d].slur_head_x_extent_
208               = extremes[d].slur_head_->extent (common_[X_AXIS], X_AXIS);
209
210         }
211     }
212
213   return extremes;
214 }
215
216 void
217 Slur_score_state::fill (Grob *me)
218 {
219   slur_ = dynamic_cast<Spanner *> (me);
220   columns_
221     = internal_extract_grob_array (me, ly_symbol2scm ("note-columns"));
222
223   if (columns_.empty ())
224     {
225       me->suicide ();
226       return;
227     }
228
229   Slur::replace_breakable_encompass_objects (me);
230   staff_space_ = Staff_symbol_referencer::staff_space (me);
231   line_thickness_ = me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
232   thickness_ = robust_scm2double (me->get_property ("thickness"), 1.0) * line_thickness_;
233
234   dir_ = slur_direction ();
235   parameters_.fill (me);
236
237   extract_grob_set (me, "note-columns", columns);
238   extract_grob_set (me, "encompass-objects", extra_objects);
239
240   Spanner *sp = dynamic_cast<Spanner *> (me);
241
242   for (int i = X_AXIS; i < NO_AXES; i++)
243     {
244       Axis a = (Axis)i;
245       common_[a] = common_refpoint_of_array (columns, me, a);
246       common_[a] = common_refpoint_of_array (extra_objects, common_[a], a);
247
248       for (LEFT_and_RIGHT (d))
249         {
250           /*
251             If bound is not in note-columns, we don't want to know about
252             its Y-position
253           */
254           if (a != Y_AXIS)
255             common_[a] = common_[a]->common_refpoint (sp->get_bound (d), a);
256         }
257     }
258
259   extremes_ = get_bound_info ();
260   is_broken_ = (!extremes_[LEFT].note_column_
261                 || !extremes_[RIGHT].note_column_);
262
263   has_same_beam_
264     = (extremes_[LEFT].stem_ && extremes_[RIGHT].stem_
265        && Stem::get_beam (extremes_[LEFT].stem_) == Stem::get_beam (extremes_[RIGHT].stem_));
266
267   base_attachments_ = get_base_attachments ();
268
269   Drul_array<Real> end_ys
270     = get_y_attachment_range ();
271
272   extra_encompass_infos_ = get_extra_encompass_infos ();
273
274   Interval additional_ys (0.0, 0.0);
275
276   for (vsize i = 0; i < extra_encompass_infos_.size (); i++)
277     {
278       if (extra_encompass_infos_[i].extents_[X_AXIS].is_empty ())
279         continue;
280
281       Real y_place = linear_interpolate (extra_encompass_infos_[i].extents_[X_AXIS].center (),
282                                          base_attachments_[RIGHT][X_AXIS],
283                                          base_attachments_[LEFT][X_AXIS],
284                                          end_ys[RIGHT],
285                                          end_ys[LEFT]);
286       Real encompass_place = extra_encompass_infos_[i].extents_[Y_AXIS][dir_];
287       if (scm_is_eq (extra_encompass_infos_[i].type_,
288                      ly_symbol2scm ("inside"))
289           && minmax (dir_, encompass_place, y_place) == encompass_place
290           && (!extra_encompass_infos_[i].grob_->internal_has_interface (ly_symbol2scm ("key-signature-interface"))
291               && !has_interface<Clef> (extra_encompass_infos_[i].grob_)
292               && !extra_encompass_infos_[i].grob_->internal_has_interface (ly_symbol2scm ("time-signature-interface"))))
293         {
294           for (LEFT_and_RIGHT (d))
295             additional_ys[d] = minmax (dir_,
296                                        additional_ys[d],
297                                        (dir_
298                                         * (parameters_.encompass_object_range_overshoot_
299                                            + (y_place - encompass_place)
300                                            * (normalize (extra_encompass_infos_[i].extents_[X_AXIS].center (),
301                                                          base_attachments_[RIGHT][X_AXIS],
302                                                          base_attachments_[LEFT][X_AXIS])
303                                               + (dir_ == LEFT ? 0 : -1)))));
304         }
305     }
306
307   for (LEFT_and_RIGHT (d))
308     end_ys[d] += additional_ys[d];
309
310   configurations_ = enumerate_attachments (end_ys);
311   for (vsize i = 0; i < columns_.size (); i++)
312     encompass_infos_.push_back (get_encompass_info (columns_[i]));
313
314   valid_ = true;
315
316   musical_dy_ = 0.0;
317   for (LEFT_and_RIGHT (d))
318     {
319       if (!is_broken_
320           && extremes_[d].slur_head_)
321         musical_dy_ += d
322                        * extremes_[d].slur_head_->relative_coordinate (common_[Y_AXIS], Y_AXIS);
323     }
324
325   edge_has_beams_
326     = (extremes_[LEFT].stem_ && Stem::get_beam (extremes_[LEFT].stem_))
327       || (extremes_[RIGHT].stem_ && Stem::get_beam (extremes_[RIGHT].stem_));
328
329   if (is_broken_)
330     musical_dy_ = 0.0;
331 }
332
333 MAKE_SCHEME_CALLBACK (Slur, calc_control_points, 1)
334 SCM
335 Slur::calc_control_points (SCM smob)
336 {
337   Spanner *me = unsmob<Spanner> (smob);
338
339   Slur_score_state state;
340   state.fill (me);
341
342   if (!state.valid_)
343     return SCM_EOL;
344
345   state.generate_curves ();
346
347   SCM end_ys = me->get_property ("positions");
348   SCM inspect_quants = me->get_property ("inspect-quants");
349   bool debug_slurs = to_boolean (me->layout ()
350                                  ->lookup_variable (ly_symbol2scm ("debug-slur-scoring")));
351
352   if (is_number_pair (inspect_quants))
353     {
354       debug_slurs = true;
355       end_ys = inspect_quants;
356     }
357
358   Slur_configuration *best = NULL;
359   if (is_number_pair (end_ys))
360     best = state.get_forced_configuration (ly_scm2interval (end_ys));
361   else
362     best = state.get_best_curve ();
363
364 #if DEBUG_SLUR_SCORING
365   if (debug_slurs)
366     {
367       string total = best->card ();
368       total += to_string (" TOTAL=%.2f idx=%d", best->score (), best->index_);
369       me->set_property ("annotation", ly_string2scm (total));
370     }
371 #endif
372
373   SCM controls = SCM_EOL;
374   for (int i = 4; i--;)
375     {
376       Offset o = best->curve_.control_[i]
377                  - Offset (me->relative_coordinate (state.common_[X_AXIS], X_AXIS),
378                            me->relative_coordinate (state.common_[Y_AXIS], Y_AXIS));
379       controls = scm_cons (ly_offset2scm (o), controls);
380     }
381
382   return controls;
383 }
384
385 Slur_configuration *
386 Slur_score_state::get_forced_configuration (Interval ys) const
387 {
388   Slur_configuration *best = NULL;
389   Real mindist = 1e6;
390   for (vsize i = 0; i < configurations_.size (); i++)
391     {
392       Real d = fabs (configurations_[i]->attachment_[LEFT][Y_AXIS] - ys[LEFT])
393                + fabs (configurations_[i]->attachment_[RIGHT][Y_AXIS] - ys[RIGHT]);
394       if (d < mindist)
395         {
396           best = configurations_[i];
397           mindist = d;
398         }
399     }
400
401   while (!best->done ())
402     best->run_next_scorer (*this);
403
404   if (mindist > 1e5)
405     programming_error ("cannot find quant");
406
407   return best;
408 }
409
410 Slur_configuration *
411 Slur_score_state::get_best_curve () const
412 {
413   std::priority_queue < Slur_configuration *, std::vector<Slur_configuration *>,
414       Slur_configuration_less > queue;
415   for (vsize i = 0; i < configurations_.size (); i++)
416     queue.push (configurations_[i]);
417
418   Slur_configuration *best = NULL;
419   while (true)
420     {
421       best = queue.top ();
422       if (best->done ())
423         break;
424
425       queue.pop ();
426       best->run_next_scorer (*this);
427       queue.push (best);
428     }
429
430   return best;
431 }
432
433 Interval
434 Slur_score_state::breakable_bound_extent (Direction d) const
435 {
436   Grob *col = slur_->get_bound (d)->get_column ();
437   Interval ret;
438   ret.set_empty ();
439
440   extract_grob_set (slur_, "encompass-objects", extra_encompasses);
441
442   for (vsize i = 0; i < extra_encompasses.size (); i++)
443     {
444       Item *item = dynamic_cast<Item *> (extra_encompasses[i]);
445       if (item && col == item->get_column ())
446         ret.unite (robust_relative_extent (item, common_[X_AXIS], X_AXIS));
447     }
448
449   return ret;
450 }
451
452 /*
453   TODO: should analyse encompasses to determine sensible region, and
454   should limit slopes available.
455 */
456
457 Drul_array<Real>
458 Slur_score_state::get_y_attachment_range () const
459 {
460   Drul_array<Real> end_ys;
461   for (LEFT_and_RIGHT (d))
462     {
463       if (extremes_[d].note_column_)
464         {
465           Interval nc_extent = extremes_[d].note_column_
466                                ->extent (common_[Y_AXIS], Y_AXIS);
467           if (nc_extent.is_empty ())
468             slur_->warning ("slur trying to encompass an empty note column.");
469           else
470             end_ys[d] = dir_
471                         * max (max (dir_ * (base_attachments_[d][Y_AXIS]
472                                             + parameters_.region_size_ * dir_),
473                                     dir_ * (dir_ + nc_extent[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
480   return end_ys;
481 }
482
483 bool
484 spanner_less (Spanner *s1, Spanner *s2)
485 {
486   Slice b1, b2;
487   for (LEFT_and_RIGHT (d))
488     {
489       b1[d] = s1->get_bound (d)->get_column ()->get_rank ();
490       b2[d] = s2->get_bound (d)->get_column ()->get_rank ();
491     }
492
493   return b2[LEFT] <= b1[LEFT] && b2[RIGHT] >= b1[RIGHT]
494          && (b2[LEFT] != b1[LEFT] || b2[RIGHT] != b1[RIGHT]);
495 }
496
497 Drul_array<Offset>
498 Slur_score_state::get_base_attachments () const
499 {
500   Drul_array<Offset> base_attachment;
501   for (LEFT_and_RIGHT (d))
502     {
503       Grob *stem = extremes_[d].stem_;
504       Grob *head = extremes_[d].slur_head_;
505
506       Real x = 0.0;
507       Real y = 0.0;
508       if (extremes_[d].note_column_)
509         {
510
511           /*
512             fixme: X coord should also be set in this case.
513           */
514           if (stem
515               && !Stem::is_invisible (stem)
516               && extremes_[d].stem_dir_ == dir_
517               && Stem::get_beaming (stem, -d)
518               && Stem::get_beam (stem)
519               && (!spanner_less (slur_, Stem::get_beam (stem))
520                   || has_same_beam_))
521             y = extremes_[d].stem_extent_[Y_AXIS][dir_];
522           else if (head)
523             y = head->extent (common_[Y_AXIS], Y_AXIS)[dir_];
524           y += dir_ * 0.5 * staff_space_;
525
526           y = move_away_from_staffline (y, head);
527
528           Grob *fh = Note_column::first_head (extremes_[d].note_column_);
529           x
530             = (fh ? fh->extent (common_[X_AXIS], X_AXIS)
531                : extremes_[d].bound_->extent (common_[X_AXIS], X_AXIS))
532               .linear_combination (CENTER);
533         }
534       base_attachment[d] = Offset (x, y);
535     }
536
537   for (LEFT_and_RIGHT (d))
538     {
539       if (!extremes_[d].note_column_)
540         {
541           Real x = 0;
542           Real y = 0;
543
544           Interval ext = breakable_bound_extent (d);
545           if (ext.is_empty ())
546             ext = Axis_group_interface::
547                   generic_bound_extent (extremes_[d].bound_,
548                                         common_[X_AXIS], X_AXIS);
549           x = ext[-d];
550
551           Grob *col = (d == LEFT) ? columns_[0] : columns_.back ();
552
553           if (extremes_[-d].bound_ != col)
554             {
555               y = robust_relative_extent (col, common_[Y_AXIS], Y_AXIS)[dir_];
556               y += dir_ * 0.5 * staff_space_;
557
558               if (get_grob_direction (col) == dir_
559                   && Note_column::get_stem (col)
560                   && !Stem::is_invisible (Note_column::get_stem (col)))
561                 y -= dir_ * 1.5 * staff_space_;
562             }
563           else
564             y = base_attachment[-d][Y_AXIS];
565
566           y = move_away_from_staffline (y, col);
567
568           base_attachment[d] = Offset (x, y);
569         }
570     }
571
572   for (LEFT_and_RIGHT (d))
573     {
574       for (int a = X_AXIS; a < NO_AXES; a++)
575         {
576           Real &b = base_attachment[d][Axis (a)];
577
578           if (isinf (b) || isnan (b))
579             {
580               programming_error ("slur attachment is inf/nan");
581               b = 0.0;
582             }
583         }
584     }
585
586   return base_attachment;
587 }
588
589 Real
590 Slur_score_state::move_away_from_staffline (Real y,
591                                             Grob *on_staff) const
592 {
593   if (!on_staff)
594     return y;
595
596   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (on_staff);
597   if (!staff_symbol)
598     return y;
599
600   Real pos
601     = (y - staff_symbol->relative_coordinate (common_[Y_AXIS],
602                                               Y_AXIS))
603       * 2.0 / staff_space_;
604
605   if (fabs (pos - my_round (pos)) < 0.2
606       && Staff_symbol_referencer::on_staff_line (on_staff, (int) rint (pos)))
607     y += 1.5 * staff_space_ * dir_ / 10;
608
609   return y;
610 }
611
612 vector<Offset>
613 Slur_score_state::generate_avoid_offsets () const
614 {
615   vector<Offset> avoid;
616   vector<Grob *> encompasses = columns_;
617
618   for (vsize i = 0; i < encompasses.size (); i++)
619     {
620       if (extremes_[LEFT].note_column_ == encompasses[i]
621           || extremes_[RIGHT].note_column_ == encompasses[i])
622         continue;
623
624       Encompass_info inf (get_encompass_info (encompasses[i]));
625       Real y = dir_ * (max (dir_ * inf.head_, dir_ * inf.stem_));
626
627       avoid.push_back (Offset (inf.x_, y + dir_ * parameters_.free_head_distance_));
628     }
629
630   extract_grob_set (slur_, "encompass-objects", extra_encompasses);
631   for (vsize i = 0; i < extra_encompasses.size (); i++)
632     {
633       if (has_interface<Slur> (extra_encompasses[i]))
634         {
635           Grob *small_slur = extra_encompasses[i];
636           Bezier b = Slur::get_curve (small_slur);
637
638           Offset z = b.curve_point (0.5);
639           z += Offset (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
640                        small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
641
642           z[Y_AXIS] += dir_ * parameters_.free_slur_distance_;
643           avoid.push_back (z);
644         }
645       else if (scm_is_eq (extra_encompasses[i]->get_property ("avoid-slur"),
646                           ly_symbol2scm ("inside")))
647         {
648           Grob *g = extra_encompasses [i];
649           Interval xe = g->extent (common_[X_AXIS], X_AXIS);
650           Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
651
652           if (!xe.is_empty ()
653               && !ye.is_empty ())
654             avoid.push_back (Offset (xe.center (), ye[dir_]));
655         }
656     }
657   return avoid;
658 }
659
660 void
661 Slur_score_state::generate_curves () const
662 {
663   Real r_0 = robust_scm2double (slur_->get_property ("ratio"), 0.33);
664   Real h_inf = staff_space_ * scm_to_double (slur_->get_property ("height-limit"));
665
666   vector<Offset> avoid = generate_avoid_offsets ();
667   for (vsize i = 0; i < configurations_.size (); i++)
668     configurations_[i]->generate_curve (*this, r_0, h_inf, avoid);
669 }
670
671 vector<Slur_configuration *>
672 Slur_score_state::enumerate_attachments (Drul_array<Real> end_ys) const
673 {
674   vector<Slur_configuration *> scores;
675
676   Drul_array<Offset> os;
677   os[LEFT] = base_attachments_[LEFT];
678   Real minimum_length = staff_space_
679                         * robust_scm2double (slur_->get_property ("minimum-length"), 2.0);
680
681   for (int i = 0; dir_ * os[LEFT][Y_AXIS] <= dir_ * end_ys[LEFT]; i++)
682     {
683       os[RIGHT] = base_attachments_[RIGHT];
684       for (int j = 0; dir_ * os[RIGHT][Y_AXIS] <= dir_ * end_ys[RIGHT]; j++)
685         {
686
687           Drul_array<bool> attach_to_stem (false, false);
688           for (LEFT_and_RIGHT (d))
689             {
690               os[d][X_AXIS] = base_attachments_[d][X_AXIS];
691               if (extremes_[d].stem_
692                   && !Stem::is_invisible (extremes_[d].stem_)
693                   && extremes_[d].stem_dir_ == dir_)
694                 {
695                   Interval stem_y = extremes_[d].stem_extent_[Y_AXIS];
696                   stem_y.widen (0.25 * staff_space_);
697                   if (stem_y.contains (os[d][Y_AXIS]))
698                     {
699                       os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS][-d]
700                                       - d * 0.3;
701                       attach_to_stem[d] = true;
702                     }
703                   else if (dir_ * extremes_[d].stem_extent_[Y_AXIS][dir_]
704                            < dir_ * os[d][Y_AXIS]
705                            && !extremes_[d].stem_extent_[X_AXIS].is_empty ())
706
707                     os[d][X_AXIS] = extremes_[d].stem_extent_[X_AXIS].center ();
708                 }
709             }
710
711           Offset dz;
712           dz = os[RIGHT] - os[LEFT];
713           if (dz[X_AXIS] < minimum_length
714               || fabs (dz[Y_AXIS] / dz[X_AXIS]) > parameters_.max_slope_)
715             {
716               for (LEFT_and_RIGHT (d))
717                 {
718                   if (extremes_[d].slur_head_
719                       && !extremes_[d].slur_head_x_extent_.is_empty ())
720                     {
721                       os[d][X_AXIS] = extremes_[d].slur_head_x_extent_.center ();
722                       attach_to_stem[d] = false;
723                     }
724                 }
725             }
726
727           dz = os[RIGHT] - os[LEFT];
728           for (LEFT_and_RIGHT (d))
729             {
730               if (extremes_[d].slur_head_
731                   && !attach_to_stem[d])
732                 {
733                   /* Horizontally move tilted slurs a little.  Move
734                      more for bigger tilts.
735
736                      TODO: parameter */
737                   os[d][X_AXIS]
738                   -= dir_ * extremes_[d].slur_head_x_extent_.length ()
739                      * sin (dz.arg ()) / 3;
740                 }
741             }
742
743           scores.push_back (Slur_configuration::new_config (os, scores.size ()));
744
745           os[RIGHT][Y_AXIS] += dir_ * staff_space_ / 2;
746         }
747
748       os[LEFT][Y_AXIS] += dir_ * staff_space_ / 2;
749     }
750
751   assert (scores.size () > 0);
752   return scores;
753 }
754
755 vector<Extra_collision_info>
756 Slur_score_state::get_extra_encompass_infos () const
757 {
758   extract_grob_set (slur_, "encompass-objects", encompasses);
759   vector<Extra_collision_info> collision_infos;
760   for (vsize i = encompasses.size (); i--;)
761     {
762       if (has_interface<Slur> (encompasses[i]))
763         {
764           Spanner *small_slur = dynamic_cast<Spanner *> (encompasses[i]);
765           Bezier b = Slur::get_curve (small_slur);
766
767           Offset relative (small_slur->relative_coordinate (common_[X_AXIS], X_AXIS),
768                            small_slur->relative_coordinate (common_[Y_AXIS], Y_AXIS));
769
770           for (int k = 0; k < 3; k++)
771             {
772               Direction hdir = Direction (k - 1);
773
774               /*
775                 Only take bound into account if small slur starts
776                 together with big slur.
777               */
778               if (hdir && small_slur->get_bound (hdir) != slur_->get_bound (hdir))
779                 continue;
780
781               Offset z = b.curve_point (k / 2.0);
782               z += relative;
783
784               Interval yext;
785               yext.set_full ();
786               yext[dir_] = z[Y_AXIS] + dir_ * thickness_ * 1.0;
787
788               Interval xext (-1, 1);
789               xext = xext * (thickness_ * 2) + z[X_AXIS];
790               Extra_collision_info info (small_slur,
791                                          hdir,
792                                          xext,
793                                          yext,
794                                          parameters_.extra_object_collision_penalty_);
795               collision_infos.push_back (info);
796             }
797         }
798       else
799         {
800           Grob *g = encompasses [i];
801           Interval xe = g->extent (common_[X_AXIS], X_AXIS);
802           Interval ye = g->extent (common_[Y_AXIS], Y_AXIS);
803           if (has_interface<Dots> (g))
804             ye.widen (0.2);
805
806           Real xp = 0.0;
807           Real penalty = parameters_.extra_object_collision_penalty_;
808           if (has_interface<Accidental_interface> (g))
809             {
810               penalty = parameters_.accidental_collision_;
811
812               Rational alt = ly_scm2rational (g->get_property ("alteration"));
813               SCM scm_style = g->get_property ("style");
814               if (!scm_is_symbol (scm_style)
815                   && !to_boolean (g->get_property ("parenthesized"))
816                   && !to_boolean (g->get_property ("restore-first")))
817                 {
818                   /* End copy accidental.cc */
819                   if (alt == FLAT_ALTERATION
820                       || alt == DOUBLE_FLAT_ALTERATION)
821                     xp = LEFT;
822                   else if (alt == SHARP_ALTERATION)
823                     xp = 0.5 * dir_;
824                   else if (alt == NATURAL_ALTERATION)
825                     xp = -dir_;
826                 }
827             }
828
829           ye.widen (thickness_ * 0.5);
830           xe.widen (thickness_ * 1.0);
831           Extra_collision_info info (g, xp, xe, ye, penalty);
832           collision_infos.push_back (info);
833         }
834     }
835
836   return collision_infos;
837 }
838
839 Extra_collision_info::Extra_collision_info (Grob *g, Real idx, Interval x, Interval y, Real p)
840 {
841   idx_ = idx;
842   extents_[X_AXIS] = x;
843   extents_[Y_AXIS] = y;
844   penalty_ = p;
845   grob_ = g;
846   type_ = g->get_property ("avoid-slur");
847 }
848
849 Extra_collision_info::Extra_collision_info ()
850 {
851   idx_ = 0.0;
852   penalty_ = 0.;
853   grob_ = 0;
854   type_ = SCM_EOL;
855 }