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