]> git.donarmstrong.com Git - lilypond.git/blob - lily/side-position-interface.cc
side-position-interface: do not recursively add support
[lilypond.git] / lily / side-position-interface.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "side-position-interface.hh"
21
22 #include <cmath>                // ceil.
23 #include <algorithm>
24 #include <set>
25 #include <map>
26
27 using namespace std;
28
29 #include "accidental-interface.hh"
30 #include "accidental-placement.hh"
31 #include "axis-group-interface.hh"
32 #include "directional-element-interface.hh"
33 #include "grob.hh"
34 #include "grob-array.hh"
35 #include "international.hh"
36 #include "item.hh"
37 #include "main.hh"
38 #include "misc.hh"
39 #include "note-head.hh"
40 #include "note-column.hh"
41 #include "pointer-group-interface.hh"
42 #include "skyline-pair.hh"
43 #include "staff-symbol-referencer.hh"
44 #include "staff-symbol.hh"
45 #include "stem.hh"
46 #include "string-convert.hh"
47 #include "system.hh"
48 #include "warn.hh"
49 #include "unpure-pure-container.hh"
50
51 void
52 Side_position_interface::add_support (Grob *me, Grob *e)
53 {
54   Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
55 }
56
57 set<Grob *>
58 get_support_set (Grob *me)
59 {
60   // Only slightly kludgy heuristic...
61   // We want to make sure that all AccidentalPlacements'
62   // accidentals make it into the side support
63   extract_grob_set (me, "side-support-elements", proto_support);
64   set<Grob *> support;
65
66   for (vsize i = 0; i < proto_support.size (); i++)
67     {
68       if (Accidental_placement::has_interface (proto_support[i]))
69         {
70           Grob *accs = proto_support[i];
71           for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
72                acs = scm_cdr (acs))
73             for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
74               {
75                 Grob *a = unsmob_grob (scm_car (s));
76                 support.insert (a);
77               }
78         }
79       else
80         support.insert (proto_support[i]);
81     }
82   return support;
83 }
84
85 /*
86   Position next to support, taking into account my own dimensions and padding.
87 */
88 SCM
89 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
90 {
91   Real r;
92   Real *current_off_ptr = 0;
93   if (scm_is_number (current_off_scm))
94     {
95       r = scm_to_double (current_off_scm);
96       current_off_ptr = &r;
97     }
98
99   Grob *me = unsmob_grob (smob);
100   // We will only ever want widths of spanners after line breaking
101   // so we can set pure to false
102   if (dynamic_cast<Spanner *> (me) && a == X_AXIS)
103     pure = false;
104
105   return Side_position_interface::aligned_side (me, a, pure, start, end, current_off_ptr);
106 }
107
108 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
109 SCM
110 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
111 {
112   // Because horizontal skylines need vertical heights, we'd trigger
113   // unpure calculations too soon if this were called before line breaking.
114   // So, we always use pure heights.  Given that horizontal skylines are
115   // almost always used before line breaking anyway, this doesn't cause
116   // problems.
117   return axis_aligned_side_helper (smob, X_AXIS, true, 0, 0, current_off);
118 }
119
120 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
121 SCM
122 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
123 {
124   return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
125 }
126
127 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
128 SCM
129 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
130 {
131   return axis_aligned_side_helper (smob, Y_AXIS, true,
132                                    scm_to_int (start),
133                                    scm_to_int (end),
134                                    cur_off);
135 }
136
137 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
138 SCM
139 Side_position_interface::calc_cross_staff (SCM smob)
140 {
141   Grob *me = unsmob_grob (smob);
142   extract_grob_set (me, "side-support-elements", elts);
143
144   Direction my_dir = get_grob_direction (me) ;
145
146   for (vsize i = 0; i < elts.size (); i++)
147     {
148       /*
149         If 'me' is placed relative to any cross-staff element with a
150         'direction callback defined, the placement of 'me' is likely
151         to depend on staff-spacing, thus 'me' should be considered
152         cross-staff.
153       */
154       if (to_boolean (elts[i]->get_property ("cross-staff"))
155            && !is_direction (elts[i]->get_property_data ("direction")))
156         return SCM_BOOL_T;
157
158       /*
159         If elts[i] is cross-staff and is pointing in the same
160         direction as 'me', we assume that the alignment
161         of 'me' is influenced the cross-staffitude of elts[i]
162         and thus we mark 'me' as cross-staff.
163       */
164       if (to_boolean (elts[i]->get_property ("cross-staff"))
165            && my_dir == get_grob_direction (elts[i]))
166         return SCM_BOOL_T;
167     }
168
169   Grob *myvag = Grob::get_vertical_axis_group (me);
170   for (vsize i = 0; i < elts.size (); i++)
171     if (myvag != Grob::get_vertical_axis_group (elts[i]))
172       return SCM_BOOL_T;
173
174   return SCM_BOOL_F;
175 }
176
177 // long function - each stage is clearly marked
178
179 SCM
180 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
181                                        Real *current_off)
182 {
183   Direction dir = get_grob_direction (me);
184
185   set<Grob *> support = get_support_set (me);
186
187   Grob *common[2];
188   for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
189     common[ax] = common_refpoint_of_array (support,
190                                            (ax == a
191                                            ? me->get_parent (ax)
192                                            : me),
193                                            ax);
194
195   Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
196   bool quantize_position = to_boolean (me->get_maybe_pure_property ("quantize-position", pure, start, end));
197   bool me_cross_staff = to_boolean (me->get_property ("cross-staff"));
198
199   bool include_staff
200     = staff_symbol
201       && a == Y_AXIS
202       && scm_is_number (me->get_maybe_pure_property ("staff-padding", pure, start, end))
203       && !quantize_position;
204
205   if (include_staff)
206     common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
207
208   Skyline my_dim;
209   SCM skyp = me->get_maybe_pure_property (a == X_AXIS
210                                           ? "horizontal-skylines"
211                                           : "vertical-skylines",
212                                           pure,
213                                           start,
214                                           end);
215   if (Skyline_pair::unsmob (skyp))
216     {
217       // for spanner pure heights, we don't know horizontal spacing,
218       // so a spanner can never have a meaningful x coordiante
219       // we just give it the parents' coordinate because its
220       // skyline will likely be of infinite width anyway
221       // and we don't want to prematurely trigger H spacing
222       Real xc = a == X_AXIS || (pure && dynamic_cast<Spanner *> (me))
223                 ? me->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
224                 : me->relative_coordinate (common[X_AXIS], X_AXIS);
225       // same here, for X_AXIS spacing, if it's happening, it should only be
226       // before line breaking.  because there is no thing as "pure" x spacing,
227       // we assume that it is all pure
228       Real yc = a == X_AXIS
229                 ? me->pure_relative_y_coordinate (common[Y_AXIS], start, end)
230                 : me->get_parent (Y_AXIS)->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
231       Skyline_pair copy = *Skyline_pair::unsmob (skyp);
232       copy.shift (a == X_AXIS ? yc : xc);
233       copy.raise (a == X_AXIS ? xc : yc);
234       my_dim = copy[-dir];
235     }
236   else
237     me->warning ("cannot find skylines - strange alignment will follow");
238
239
240   vector<Box> boxes;
241   vector<Skyline_pair> skyps;
242   set<Grob *>::iterator it;
243   Real max_raise = -dir * infinity_f;
244   bool aligns_to_cross_staff = false;
245
246   for (it = support.begin (); it != support.end (); it++)
247     {
248       Grob *e = *it;
249
250       bool cross_staff = to_boolean (e->get_property ("cross-staff"));
251       if (a == Y_AXIS
252           && !me_cross_staff // 'me' promised not to adapt to staff-spacing
253           && cross_staff) // but 'e' might move based on staff-pacing
254         continue; // so 'me' may not move in response to 'e'
255
256       if (a == Y_AXIS
257           && Stem::has_interface (e))
258         {
259           // If called as 'pure' we may not force a stem to set its direction,
260           if (pure && !is_direction (e->get_property_data ("direction")))
261             continue;
262           // There is no need to consider stems pointing away.
263           if (dir == -get_grob_direction (e))
264             continue;
265         }
266
267       if (e)
268         {
269            SCM sp = e->get_maybe_pure_property (a == X_AXIS
270                                                 ? "horizontal-skylines"
271                                                 : "vertical-skylines",
272                                                 pure,
273                                                 start,
274                                                 end);
275
276            aligns_to_cross_staff |= cross_staff;
277            if (Skyline_pair::unsmob (sp))
278              {
279                Real xc = pure && dynamic_cast<Spanner *> (e)
280                          ? e->get_parent (X_AXIS)->relative_coordinate (common[X_AXIS], X_AXIS)
281                          : e->relative_coordinate (common[X_AXIS], X_AXIS);
282                // same logic as above
283                // we assume horizontal spacing is always pure
284                Real yc = a == X_AXIS
285                          ? e->pure_relative_y_coordinate (common[Y_AXIS], start, end)
286                          : e->maybe_pure_coordinate (common[Y_AXIS], Y_AXIS, pure, start, end);
287                Skyline_pair copy = *Skyline_pair::unsmob (sp);
288                if (a == Y_AXIS
289                    && Stem::has_interface (e)
290                    && to_boolean (me->get_maybe_pure_property ("add-stem-support", pure, start, end)))
291                  copy[dir].set_minimum_height (copy[dir].max_height ());
292                copy.shift (a == X_AXIS ? yc : xc);
293                copy.raise (a == X_AXIS ? xc : yc);
294                max_raise = minmax (dir, max_raise, a == X_AXIS ? xc : yc);
295                skyps.push_back (copy);
296              }
297            else { /* no warning*/ }
298         }
299     }
300
301   Skyline dim (boxes, other_axis (a), dir);
302   if (skyps.size ())
303     {
304       Skyline_pair merged (skyps);
305       dim.merge (merged[dir]);
306     }
307
308   if (include_staff)
309     {
310       Interval staff_extents;
311       common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
312       staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
313       dim.set_minimum_height (staff_extents[dir]);
314     }
315
316   // Sometimes, we want to side position for grobs but they
317   // don't position against anything.  Some cases where this is true:
318   //   - StanzaNumber if the supporting lyrics are hara-kiri'd
319   //     SystemStartBracket
320   //     InstrumentName
321   // In all these cases, we set the height of the support to 0.
322   // This becomes then like the self-alignment-interface with the
323   // caveat that there is padding added.
324   // TODO: if there is a grob that never has side-support-elements
325   // (like InstrumentName), why are we using this function? Isn't it
326   // overkill? A function like self-alignment-interface with padding
327   // works just fine.
328   // One could even imagine the two interfaces merged, as the only
329   // difference is that in self-alignment-interface we align on the parent
330   // where as here we align on a group of grobs.
331   if (dim.is_empty ())
332     {
333       dim = Skyline (dim.direction ());
334       dim.set_minimum_height (0.0);
335     }
336
337   // Many cross-staff grobs do not have good height estimations.
338   // We give the grob the best chance of not colliding by shifting
339   // it to the maximum height in the case of cross-staff alignment.
340   // This means, in other words, that the old way things were done
341   // (using boxes instead of skylines) is just reactivated for
342   // alignment to cross-staff grobs.
343   if (aligns_to_cross_staff)
344     dim.set_minimum_height (dim.max_height ());
345
346   Real ss = Staff_symbol_referencer::staff_space (me);
347   Real dist = dim.distance (my_dim, robust_scm2double (me->get_maybe_pure_property ("horizon-padding", pure, start, end), 0.0));
348   Real total_off = !isinf (dist) ? dir * dist : 0.0;
349
350   total_off += dir * ss * robust_scm2double (me->get_maybe_pure_property ("padding", pure, start, end), 0.0);
351
352   Real minimum_space = ss * robust_scm2double (me->get_maybe_pure_property ("minimum-space", pure, start, end), -1);
353
354   if (minimum_space >= 0
355       && dir
356       && total_off * dir < minimum_space)
357     total_off = minimum_space * dir;
358
359   if (current_off)
360     total_off = dir * max (dir * total_off,
361                            dir * (*current_off));
362
363   /* FIXME: 1000 should relate to paper size.  */
364   if (fabs (total_off) > 1000)
365     {
366       string msg
367         = String_convert::form_string ("Improbable offset for grob %s: %f",
368                                        me->name ().c_str (), total_off);
369
370       programming_error (msg);
371       if (strict_infinity_checking)
372         scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
373     }
374
375   /*
376     Maintain a minimum distance to the staff. This is similar to side
377     position with padding, but it will put adjoining objects on a row if
378     stuff sticks out of the staff a little.
379   */
380   Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
381   if (staff && a == Y_AXIS)
382     {
383       if (quantize_position)
384         {
385           Grob *common = me->common_refpoint (staff, Y_AXIS);
386           Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
387           Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
388           Real ss = Staff_symbol::staff_space (staff);
389           Real position = 2 * (my_off + total_off - staff_off) / ss;
390           Real rounded = directed_round (position, dir);
391           Grob *head = me->get_parent (X_AXIS);
392
393           Interval staff_span = Staff_symbol::line_span (staff);
394           staff_span.widen (1);
395           if (staff_span.contains (position)
396               /* In case of a ledger lines, quantize even if we're outside the staff. */
397               || (Note_head::has_interface (head)
398
399                   && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
400             {
401               total_off += (rounded - position) * 0.5 * ss;
402               if (Staff_symbol_referencer::on_line (me, int (rounded)))
403                 total_off += dir * 0.5 * ss;
404             }
405         }
406       else if (scm_is_number (me->get_maybe_pure_property ("staff-padding", pure, start, end)) && dir)
407         {
408           Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
409
410           Real staff_padding
411             = Staff_symbol_referencer::staff_space (me)
412               * scm_to_double (me->get_maybe_pure_property ("staff-padding", pure, start, end));
413
414           Grob *parent = me->get_parent (Y_AXIS);
415           Grob *common = me->common_refpoint (staff, Y_AXIS);
416           Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
417           Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
418           Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
419           Real diff = (dir * staff_extent[dir] + staff_padding
420                        - dir * (total_off + iv[-dir])
421                        + dir * (staff_position - parent_position));
422           total_off += dir * max (diff, 0.0);
423         }
424     }
425   return scm_from_double (total_off);
426 }
427
428 void
429 Side_position_interface::set_axis (Grob *me, Axis a)
430 {
431   if (!scm_is_number (me->get_property ("side-axis")))
432     {
433       me->set_property ("side-axis", scm_from_int (a));
434       chain_offset_callback (me,
435                              (a == X_AXIS)
436                              ? x_aligned_side_proc
437                              : ly_make_unpure_pure_container (y_aligned_side_proc, pure_y_aligned_side_proc),
438                              a);
439     }
440 }
441
442 Axis
443 Side_position_interface::get_axis (Grob *me)
444 {
445   if (scm_is_number (me->get_property ("side-axis")))
446     return Axis (scm_to_int (me->get_property ("side-axis")));
447
448   string msg = String_convert::form_string ("side-axis not set for grob %s.",
449                                             me->name ().c_str ());
450   me->programming_error (msg);
451   return NO_AXES;
452 }
453
454 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
455 SCM
456 Side_position_interface::move_to_extremal_staff (SCM smob)
457 {
458   Grob *me = unsmob_grob (smob);
459   System *sys = dynamic_cast<System *> (me->get_system ());
460   Direction dir = get_grob_direction (me);
461   if (dir != DOWN)
462     dir = UP;
463
464   Interval iv = me->extent (sys, X_AXIS);
465   iv.widen (1.0);
466   Grob *top_staff = sys->get_extremal_staff (dir, iv);
467
468   if (!top_staff)
469     return SCM_BOOL_F;
470
471   // Only move this grob if it is a direct child of the system.  We
472   // are not interested in moving marks from other staves to the top
473   // staff; we only want to move marks from the system to the top
474   // staff.
475   if (sys != me->get_parent (Y_AXIS))
476     return SCM_BOOL_F;
477
478   me->set_parent (top_staff, Y_AXIS);
479   me->flush_extent_cache (Y_AXIS);
480   Axis_group_interface::add_element (top_staff, me);
481
482   // Remove any cross-staff side-support dependencies
483   Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
484   if (ga)
485     {
486       vector<Grob *> const &elts = ga->array ();
487       vector<Grob *> new_elts;
488       for (vsize i = 0; i < elts.size (); ++i)
489         {
490           if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
491             new_elts.push_back (elts[i]);
492         }
493       ga->set_array (new_elts);
494     }
495   return SCM_BOOL_T;
496 }
497
498 ADD_INTERFACE (Side_position_interface,
499                "Position a victim object (this one) next to other objects"
500                " (the support).  The property @code{direction} signifies where"
501                " to put the victim object relative to the support (left or"
502                " right, up or down?)\n"
503                "\n"
504                "The routine also takes the size of the staff into account if"
505                " @code{staff-padding} is set.  If undefined, the staff symbol"
506                " is ignored.",
507
508                /* properties */
509                "add-stem-support "
510                "direction "
511                "minimum-space "
512                "horizon-padding "
513                "padding "
514                "quantize-position "
515                "side-axis "
516                "side-support-elements "
517                "slur-padding "
518                "staff-padding "
519                "use-skylines "
520               );