2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
20 #include "side-position-interface.hh"
22 #include <cmath> // ceil.
29 #include "accidental-interface.hh"
30 #include "accidental-placement.hh"
31 #include "axis-group-interface.hh"
32 #include "directional-element-interface.hh"
34 #include "grob-array.hh"
37 #include "note-head.hh"
38 #include "note-column.hh"
39 #include "pointer-group-interface.hh"
40 #include "skyline-pair.hh"
41 #include "staff-symbol-referencer.hh"
42 #include "staff-symbol.hh"
44 #include "string-convert.hh"
49 Side_position_interface::add_support (Grob *me, Grob *e)
51 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
55 finish_offset (Grob *me, Direction dir, Real total_off, Real *current_offset)
57 Real ss = Staff_symbol_referencer::staff_space (me);
58 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
59 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
61 if (minimum_space >= 0
63 && total_off * dir < minimum_space)
64 total_off = minimum_space * dir;
67 total_off = dir * max (dir * total_off,
68 dir * (*current_offset));
70 /* FIXME: 1000 should relate to paper size. */
71 if (fabs (total_off) > 1000)
74 = String_convert::form_string ("Improbable offset for grob %s: %f",
75 me->name ().c_str (), total_off);
77 programming_error (msg);
78 if (strict_infinity_checking)
79 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
82 return scm_from_double (total_off);
86 get_support_set (Grob *me)
88 // Only slightly kludgy heuristic...
89 // We want to make sure that all AccidentalPlacements'
90 // accidentals make it into the side support
91 extract_grob_set (me, "side-support-elements", proto_support);
94 for (vsize i = 0; i < proto_support.size (); i++)
96 if (Accidental_placement::has_interface (proto_support[i]))
98 Grob *accs = proto_support[i];
99 for (SCM acs = accs->get_object ("accidental-grobs"); scm_is_pair (acs);
101 for (SCM s = scm_cdar (acs); scm_is_pair (s); s = scm_cdr (s))
103 Grob *a = unsmob_grob (scm_car (s));
108 support.insert (proto_support[i]);
113 /* Put the element next to the support, optionally taking in
114 account the extent of the support.
116 Does not take into account the extent of ME.
119 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
120 bool include_my_extent,
121 bool pure, int start, int end,
122 Real *current_offset)
124 set<Grob *> support = get_support_set (me);
126 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
127 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
131 && scm_is_number (me->get_property ("staff-padding"))
132 && !to_boolean (me->get_property ("quantize-position"));
135 Interval staff_extents;
138 common = staff_symbol->common_refpoint (common, Y_AXIS);
139 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
142 dim.unite (staff_extents);
145 Direction dir = get_grob_direction (me);
147 set<Grob *>::iterator it;
149 for (it = support.begin (); it != support.end (); it++)
153 // In the case of a stem, we will find a note head as well
154 // ignoring the stem solves cyclic dependencies if the stem is
155 // attached to a cross-staff beam.
157 && Stem::has_interface (e)
158 && dir == - get_grob_direction (e))
164 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
167 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
168 dim.unite (Interval (x, x));
174 dim = Interval (0, 0);
176 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
178 Real total_off = dim.linear_combination (dir) - off;
179 if (include_my_extent)
181 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
186 programming_error ("direction unknown, but aligned-side wanted");
189 total_off += -iv[-dir];
193 return finish_offset (me, dir, total_off, current_offset);
197 Side_position_interface::skyline_side_position (Grob *me, Axis a,
198 bool pure, int start, int end,
199 Real *current_offset)
201 set<Grob *> support = get_support_set (me);
204 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
205 common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
207 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
208 Direction dir = get_grob_direction (me);
211 Skyline_pair *sp = Skyline_pair::unsmob (me->get_property ("vertical-skylines"));
212 if (sp && a == Y_AXIS && !pure)
214 Skyline_pair copy = Skyline_pair (*sp);
215 copy.shift (me->relative_coordinate (common[X_AXIS], X_AXIS));
216 copy.raise (me->get_parent (Y_AXIS)->relative_coordinate (common[Y_AXIS], Y_AXIS));
222 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
225 off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
226 + me->maybe_pure_extent (me, ax, pure, start, end);
228 off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
231 if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
232 return scm_from_double (0.0);
234 my_dim = Skyline (off, other_axis (a), -dir);
239 && scm_is_number (me->get_property ("staff-padding"))
240 && !to_boolean (me->get_property ("quantize-position"));
243 vector<Skyline_pair> skyps;
244 Real min_h = dir == LEFT ? infinity_f : -infinity_f;
245 set<Grob *>::iterator it;
247 map<Grob *, vector<Grob *> > note_column_map; // for parts of a note column
248 for (it = support.begin (); it != support.end (); it++)
252 // In the case of a stem, we will find a note head as well
253 // ignoring the stem solves cyclic dependencies if the stem is
254 // attached to a cross-staff beam.
256 && Stem::has_interface (e)
257 && dir == - get_grob_direction (e))
262 if (Accidental_interface::has_interface (e))
264 vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
265 boxes.insert (boxes.end (), bs.begin (), bs.end ());
269 if (Note_column::has_interface (e->get_parent (X_AXIS))
270 && to_boolean (me->get_property ("add-stem-support")))
272 note_column_map[e->get_parent (X_AXIS)].push_back (e);
276 Skyline_pair *sp = Skyline_pair::unsmob (e->get_property ("vertical-skylines"));
277 if (sp && a == Y_AXIS && !pure)
279 Skyline_pair copy = Skyline_pair (*sp);
280 copy.shift (e->relative_coordinate (common[X_AXIS], X_AXIS));
281 copy.raise (e->relative_coordinate (common[Y_AXIS], Y_AXIS));
282 skyps.push_back (copy);
286 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
287 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
289 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
293 min_h = minmax (dir, b[a][-dir], min_h);
298 // this loop ensures that parts of a note column will be in the same box
299 // pushes scripts and such over stems instead of just over heads
300 for (map<Grob *, vector<Grob *> >::iterator i = note_column_map.begin (); i != note_column_map.end (); i++)
303 for (vsize j = 0; j < (*i).second.size (); j++)
305 Grob *e = (*i).second[j];
307 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
308 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
310 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
315 if (!big[X_AXIS].is_empty () && !big[Y_AXIS].is_empty ())
316 boxes.push_back (big);
319 Skyline dim (boxes, other_axis (a), dir);
322 Skyline_pair merged (skyps);
323 dim.merge (merged[dir]);
326 dim.set_minimum_height (0.0);
328 dim.set_minimum_height (min_h);
332 Interval staff_extents;
333 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
334 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
335 dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
338 Real dist = dim.distance (my_dim, 0.1); // 0.1 m4g1c value...fix...
339 Real total_off = !isinf (dist) ? dir * dist : 0.0;
341 return finish_offset (me, dir, total_off, current_offset);
344 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
346 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
348 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
351 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
353 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
355 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
356 true, scm_to_int (start), scm_to_int (end), 0);
360 Position next to support, taking into account my own dimensions and padding.
363 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
366 Real *current_off_ptr = 0;
367 if (scm_is_number (current_off_scm))
369 r = scm_to_double (current_off_scm);
370 current_off_ptr = &r;
373 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
376 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
378 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
380 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
383 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
385 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
387 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
390 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
392 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
394 return axis_aligned_side_helper (smob, Y_AXIS, true,
400 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
402 Side_position_interface::calc_cross_staff (SCM smob)
404 Grob *me = unsmob_grob (smob);
405 extract_grob_set (me, "side-support-elements", elts);
407 for (vsize i = 0; i < elts.size (); i++)
408 if (to_boolean (elts[i]->get_property ("cross-staff")))
411 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
412 return scm_from_bool (common != me->get_parent (Y_AXIS));
416 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
419 Direction dir = get_grob_direction (me);
420 bool skyline = to_boolean (me->get_property ("use-skylines"));
422 Real o = scm_to_double (skyline && !pure
423 ? skyline_side_position (me, a, pure, start, end, current_off)
424 : general_side_position (me, a, true, true, pure, start, end, current_off));
427 Maintain a minimum distance to the staff. This is similar to side
428 position with padding, but it will put adjoining objects on a row if
429 stuff sticks out of the staff a little.
431 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
432 if (staff && a == Y_AXIS)
434 if (to_boolean (me->get_property ("quantize-position")))
436 Grob *common = me->common_refpoint (staff, Y_AXIS);
437 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
438 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
439 Real ss = Staff_symbol::staff_space (staff);
440 Real position = 2 * (my_off + o - staff_off) / ss;
441 Real rounded = directed_round (position, dir);
442 Grob *head = me->get_parent (X_AXIS);
444 Interval staff_span = Staff_symbol::line_span (staff);
445 staff_span.widen (1);
446 if (staff_span.contains (position)
447 /* In case of a ledger lines, quantize even if we're outside the staff. */
448 || (Note_head::has_interface (head)
450 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
452 o += (rounded - position) * 0.5 * ss;
453 if (Staff_symbol_referencer::on_line (me, int (rounded)))
457 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
459 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
462 = Staff_symbol_referencer::staff_space (me)
463 * scm_to_double (me->get_property ("staff-padding"));
465 Grob *parent = me->get_parent (Y_AXIS);
466 Grob *common = me->common_refpoint (staff, Y_AXIS);
467 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
468 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
469 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
470 Real diff = (dir * staff_extent[dir] + staff_padding
471 - dir * (o + iv[-dir])
472 + dir * (staff_position - parent_position));
473 o += dir * max (diff, 0.0);
476 return scm_from_double (o);
480 Side_position_interface::set_axis (Grob *me, Axis a)
482 if (!scm_is_number (me->get_property ("side-axis")))
484 me->set_property ("side-axis", scm_from_int (a));
485 chain_offset_callback (me,
487 ? x_aligned_side_proc
488 : y_aligned_side_proc,
494 Side_position_interface::get_axis (Grob *me)
496 if (scm_is_number (me->get_property ("side-axis")))
497 return Axis (scm_to_int (me->get_property ("side-axis")));
499 string msg = String_convert::form_string ("side-axis not set for grob %s.",
500 me->name ().c_str ());
501 me->programming_error (msg);
505 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
507 Side_position_interface::move_to_extremal_staff (SCM smob)
509 Grob *me = unsmob_grob (smob);
510 System *sys = dynamic_cast<System *> (me->get_system ());
511 Direction dir = get_grob_direction (me);
515 Interval iv = me->extent (sys, X_AXIS);
517 Grob *top_staff = sys->get_extremal_staff (dir, iv);
522 // Only move this grob if it is a direct child of the system. We
523 // are not interested in moving marks from other staves to the top
524 // staff; we only want to move marks from the system to the top
526 if (sys != me->get_parent (Y_AXIS))
529 me->set_parent (top_staff, Y_AXIS);
530 me->flush_extent_cache (Y_AXIS);
531 Axis_group_interface::add_element (top_staff, me);
533 // Remove any cross-staff side-support dependencies
534 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
537 vector<Grob *> const &elts = ga->array ();
538 vector<Grob *> new_elts;
539 for (vsize i = 0; i < elts.size (); ++i)
541 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
542 new_elts.push_back (elts[i]);
544 ga->set_array (new_elts);
549 ADD_INTERFACE (Side_position_interface,
550 "Position a victim object (this one) next to other objects"
551 " (the support). The property @code{direction} signifies where"
552 " to put the victim object relative to the support (left or"
553 " right, up or down?)\n"
555 "The routine also takes the size of the staff into account if"
556 " @code{staff-padding} is set. If undefined, the staff symbol"
566 "side-support-elements "