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