2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2011 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.
27 #include "accidental-interface.hh"
28 #include "axis-group-interface.hh"
29 #include "directional-element-interface.hh"
31 #include "grob-array.hh"
34 #include "note-head.hh"
35 #include "pointer-group-interface.hh"
36 #include "staff-symbol-referencer.hh"
37 #include "staff-symbol.hh"
39 #include "string-convert.hh"
44 Side_position_interface::add_support (Grob *me, Grob *e)
46 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
50 finish_offset (Grob *me, Direction dir, Real total_off, Real *current_offset)
52 Real ss = Staff_symbol_referencer::staff_space (me);
53 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
54 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
56 if (minimum_space >= 0
58 && total_off * dir < minimum_space)
59 total_off = minimum_space * dir;
62 total_off = dir * max (dir * total_off,
63 dir * (*current_offset));
65 /* FIXME: 1000 should relate to paper size. */
66 if (fabs (total_off) > 1000)
69 = String_convert::form_string ("Improbable offset for grob %s: %f",
70 me->name ().c_str (), total_off);
72 programming_error (msg);
73 if (strict_infinity_checking)
74 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
77 return scm_from_double (total_off);
80 /* Put the element next to the support, optionally taking in
81 account the extent of the support.
83 Does not take into account the extent of ME.
86 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
87 bool include_my_extent,
88 bool pure, int start, int end,
91 extract_grob_set (me, "side-support-elements", support);
93 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
94 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
98 && scm_is_number (me->get_property ("staff-padding"))
99 && !to_boolean (me->get_property ("quantize-position"));
102 Interval staff_extents;
105 common = staff_symbol->common_refpoint (common, Y_AXIS);
106 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
109 dim.unite (staff_extents);
112 Direction dir = get_grob_direction (me);
114 for (vsize i = 0; i < support.size (); i++)
116 Grob *e = support[i];
118 // In the case of a stem, we will find a note head as well
119 // ignoring the stem solves cyclic dependencies if the stem is
120 // attached to a cross-staff beam.
122 && Stem::has_interface (e)
123 && dir == - get_grob_direction (e))
129 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
132 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
133 dim.unite (Interval (x, x));
139 dim = Interval (0, 0);
141 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
143 Real total_off = dim.linear_combination (dir) - off;
144 if (include_my_extent)
146 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
151 programming_error ("direction unknown, but aligned-side wanted");
154 total_off += -iv[-dir];
158 return finish_offset (me, dir, total_off, current_offset);
162 Side_position_interface::skyline_side_position (Grob *me, Axis a,
163 bool pure, int start, int end,
164 Real *current_offset)
166 extract_grob_set (me, "side-support-elements", support);
169 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
170 common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
172 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
173 Direction dir = get_grob_direction (me);
176 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
179 off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
180 + me->maybe_pure_extent (me, ax, pure, start, end);
182 off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
185 if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
186 return scm_from_double (0.0);
188 Real skyline_padding = 0.1;
190 Skyline my_dim (off, skyline_padding, other_axis (a), -dir);
195 && scm_is_number (me->get_property ("staff-padding"))
196 && !to_boolean (me->get_property ("quantize-position"));
199 Real min_h = dir == LEFT ? infinity_f : -infinity_f;
200 for (vsize i = 0; i < support.size (); i++)
202 Grob *e = support[i];
204 // In the case of a stem, we will find a note head as well
205 // ignoring the stem solves cyclic dependencies if the stem is
206 // attached to a cross-staff beam.
208 && Stem::has_interface (e)
209 && dir == - get_grob_direction (e))
214 if (Accidental_interface::has_interface (e))
216 vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
217 boxes.insert (boxes.end (), bs.begin (), bs.end ());
222 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
223 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
225 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
229 min_h = minmax (dir, b[a][-dir], min_h);
234 Skyline dim (boxes, skyline_padding, other_axis (a), dir);
236 dim.set_minimum_height (0.0);
238 dim.set_minimum_height (min_h);
242 Interval staff_extents;
243 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
244 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
245 dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
248 Real total_off = dir * dim.distance (my_dim);
249 return finish_offset (me, dir, total_off, current_offset);
252 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
254 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
256 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
259 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
261 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
263 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
264 true, scm_to_int (start), scm_to_int (end), 0);
268 Position next to support, taking into account my own dimensions and padding.
271 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
274 Real *current_off_ptr = 0;
275 if (scm_is_number (current_off_scm))
277 r = scm_to_double (current_off_scm);
278 current_off_ptr = &r;
281 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
284 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
286 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
288 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
291 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
293 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
295 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
298 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
300 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
302 return axis_aligned_side_helper (smob, Y_AXIS, true,
308 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
310 Side_position_interface::calc_cross_staff (SCM smob)
312 Grob *me = unsmob_grob (smob);
313 extract_grob_set (me, "side-support-elements", elts);
315 for (vsize i = 0; i < elts.size (); i++)
316 if (to_boolean (elts[i]->get_property ("cross-staff")))
319 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
320 return scm_from_bool (common != me->get_parent (Y_AXIS));
324 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
327 Direction dir = get_grob_direction (me);
328 bool skyline = to_boolean (me->get_property ("use-skylines"));
330 Real o = scm_to_double (skyline
331 ? skyline_side_position (me, a, pure, start, end, current_off)
332 : general_side_position (me, a, true, true, pure, start, end, current_off));
335 Maintain a minimum distance to the staff. This is similar to side
336 position with padding, but it will put adjoining objects on a row if
337 stuff sticks out of the staff a little.
339 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
340 if (staff && a == Y_AXIS)
342 if (to_boolean (me->get_property ("quantize-position")))
344 Grob *common = me->common_refpoint (staff, Y_AXIS);
345 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
346 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
347 Real ss = Staff_symbol::staff_space (staff);
348 Real position = 2 * (my_off + o - staff_off) / ss;
349 Real rounded = directed_round (position, dir);
350 Grob *head = me->get_parent (X_AXIS);
352 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
353 /* In case of a ledger lines, quantize even if we're outside the staff. */
354 || (Note_head::has_interface (head)
356 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
358 o += (rounded - position) * 0.5 * ss;
359 if (Staff_symbol_referencer::on_line (me, int (rounded)))
363 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
365 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
368 = Staff_symbol_referencer::staff_space (me)
369 * scm_to_double (me->get_property ("staff-padding"));
371 Grob *parent = me->get_parent (Y_AXIS);
372 Grob *common = me->common_refpoint (staff, Y_AXIS);
373 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
374 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
375 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
376 Real diff = (dir * staff_extent[dir] + staff_padding
377 - dir * (o + iv[-dir])
378 + dir * (staff_position - parent_position));
379 o += dir * max (diff, 0.0);
382 return scm_from_double (o);
386 Side_position_interface::set_axis (Grob *me, Axis a)
388 if (!scm_is_number (me->get_property ("side-axis")))
390 me->set_property ("side-axis", scm_from_int (a));
391 chain_offset_callback (me,
393 ? x_aligned_side_proc
394 : y_aligned_side_proc,
400 Side_position_interface::get_axis (Grob *me)
402 if (scm_is_number (me->get_property ("side-axis")))
403 return Axis (scm_to_int (me->get_property ("side-axis")));
405 string msg = String_convert::form_string ("side-axis not set for grob %s.",
406 me->name ().c_str ());
407 me->programming_error (msg);
411 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
413 Side_position_interface::move_to_extremal_staff (SCM smob)
415 Grob *me = unsmob_grob (smob);
416 System *sys = dynamic_cast<System *> (me->get_system ());
417 Direction dir = get_grob_direction (me);
421 Interval iv = me->extent (sys, X_AXIS);
423 Grob *top_staff = sys->get_extremal_staff (dir, iv);
428 // Only move this grob if it is a direct child of the system. We
429 // are not interested in moving marks from other staves to the top
430 // staff; we only want to move marks from the system to the top
432 if (sys != me->get_parent (Y_AXIS))
435 me->set_parent (top_staff, Y_AXIS);
436 me->flush_extent_cache (Y_AXIS);
437 Axis_group_interface::add_element (top_staff, me);
439 // Remove any cross-staff side-support dependencies
440 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
443 vector<Grob *> const &elts = ga->array ();
444 vector<Grob *> new_elts;
445 for (vsize i = 0; i < elts.size (); ++i)
447 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
448 new_elts.push_back (elts[i]);
450 ga->set_array (new_elts);
455 ADD_INTERFACE (Side_position_interface,
456 "Position a victim object (this one) next to other objects"
457 " (the support). The property @code{direction} signifies where"
458 " to put the victim object relative to the support (left or"
459 " right, up or down?)\n"
461 "The routine also takes the size of the staff into account if"
462 " @code{staff-padding} is set. If undefined, the staff symbol"
471 "side-support-elements "