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;
63 total_off = dir * max (dir * total_off,
64 dir * (*current_offset));
66 /* FIXME: 1000 should relate to paper size. */
67 if (fabs (total_off) > 1000)
70 = String_convert::form_string ("Improbable offset for grob %s: %f",
71 me->name ().c_str (), total_off);
73 programming_error (msg);
74 if (strict_infinity_checking)
75 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
78 return scm_from_double (total_off);
81 /* Put the element next to the support, optionally taking in
82 account the extent of the support.
84 Does not take into account the extent of ME.
87 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
88 bool include_my_extent,
89 bool pure, int start, int end,
92 extract_grob_set (me, "side-support-elements", support);
94 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
95 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
99 && scm_is_number (me->get_property ("staff-padding"))
100 && !to_boolean (me->get_property ("quantize-position"));
103 Interval staff_extents;
106 common = staff_symbol->common_refpoint (common, Y_AXIS);
107 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
110 dim.unite (staff_extents);
113 Direction dir = get_grob_direction (me);
115 for (vsize i = 0; i < support.size (); i++)
117 Grob *e = support[i];
119 // In the case of a stem, we will find a note head as well
120 // ignoring the stem solves cyclic dependencies if the stem is
121 // attached to a cross-staff beam.
123 && Stem::has_interface (e)
124 && dir == - get_grob_direction (e))
130 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
133 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
134 dim.unite (Interval (x, x));
140 dim = Interval (0, 0);
142 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
144 Real total_off = dim.linear_combination (dir) - off;
145 if (include_my_extent)
147 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
152 programming_error ("direction unknown, but aligned-side wanted");
155 total_off += -iv[-dir];
159 return finish_offset (me, dir, total_off, current_offset);
163 Side_position_interface::skyline_side_position (Grob *me, Axis a,
164 bool pure, int start, int end,
165 Real *current_offset)
167 extract_grob_set (me, "side-support-elements", support);
170 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
171 common[ax] = common_refpoint_of_array (support, ax == a ? me->get_parent (ax) : me, ax);
173 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
174 Direction dir = get_grob_direction (me);
177 Real my_min_h = dir == LEFT ? -infinity_f : infinity_f;
178 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
181 off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
182 + me->maybe_pure_extent (me, ax, pure, start, end);
184 off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
187 if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
188 return scm_from_double (0.0);
190 my_min_h = off[a][dir];
192 Real skyline_padding = 0.1;
194 Skyline my_dim (off, skyline_padding, other_axis (a), -dir);
195 my_dim.set_minimum_height (my_min_h);
200 && scm_is_number (me->get_property ("staff-padding"))
201 && !to_boolean (me->get_property ("quantize-position"));
204 Real min_h = dir == LEFT ? infinity_f : -infinity_f;
205 for (vsize i = 0; i < support.size (); i++)
207 Grob *e = support[i];
209 // In the case of a stem, we will find a note head as well
210 // ignoring the stem solves cyclic dependencies if the stem is
211 // attached to a cross-staff beam.
213 && Stem::has_interface (e)
214 && dir == - get_grob_direction (e))
219 if (Accidental_interface::has_interface (e))
221 vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
222 boxes.insert (boxes.end (), bs.begin (), bs.end ());
227 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
228 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
230 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
234 min_h = minmax (dir, b[a][-dir], min_h);
239 Skyline dim (boxes, skyline_padding, other_axis (a), dir);
241 dim.set_minimum_height (0.0);
243 dim.set_minimum_height (min_h);
247 Interval staff_extents;
248 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
249 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
250 dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
253 Real total_off = dir * dim.distance (my_dim);
254 return finish_offset (me, dir, total_off, current_offset);
257 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
259 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
261 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
264 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
266 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
268 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
269 true, scm_to_int (start), scm_to_int (end), 0);
273 Position next to support, taking into account my own dimensions and padding.
276 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
279 Real *current_off_ptr = 0;
280 if (scm_is_number (current_off_scm))
282 r = scm_to_double (current_off_scm);
283 current_off_ptr = &r;
286 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
289 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
291 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
293 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
296 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
298 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
300 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
303 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
305 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
307 return axis_aligned_side_helper (smob, Y_AXIS, true,
313 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
315 Side_position_interface::calc_cross_staff (SCM smob)
317 Grob *me = unsmob_grob (smob);
318 extract_grob_set (me, "side-support-elements", elts);
320 for (vsize i = 0; i < elts.size (); i++)
321 if (to_boolean (elts[i]->get_property ("cross-staff")))
324 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
325 return scm_from_bool (common != me->get_parent (Y_AXIS));
329 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
332 Direction dir = get_grob_direction (me);
333 bool skyline = to_boolean (me->get_property ("use-skylines"));
335 Real o = scm_to_double (skyline
336 ? skyline_side_position (me, a, pure, start, end, current_off)
337 : general_side_position (me, a, true, true, pure, start, end, current_off));
340 Maintain a minimum distance to the staff. This is similar to side
341 position with padding, but it will put adjoining objects on a row if
342 stuff sticks out of the staff a little.
344 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
345 if (staff && a == Y_AXIS)
347 if (to_boolean (me->get_property ("quantize-position")))
349 Grob *common = me->common_refpoint (staff, Y_AXIS);
350 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
351 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
352 Real ss = Staff_symbol::staff_space (staff);
353 Real position = 2 * (my_off + o - staff_off) / ss;
354 Real rounded = directed_round (position, dir);
355 Grob *head = me->get_parent (X_AXIS);
357 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
358 /* In case of a ledger lines, quantize even if we're outside the staff. */
359 || (Note_head::has_interface (head)
361 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
363 o += (rounded - position) * 0.5 * ss;
364 if (Staff_symbol_referencer::on_line (me, int (rounded)))
368 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
370 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
373 = Staff_symbol_referencer::staff_space (me)
374 * scm_to_double (me->get_property ("staff-padding"));
376 Grob *parent = me->get_parent (Y_AXIS);
377 Grob *common = me->common_refpoint (staff, Y_AXIS);
378 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
379 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
380 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
381 Real diff = (dir * staff_extent[dir] + staff_padding
382 - dir * (o + iv[-dir])
383 + dir * (staff_position - parent_position));
384 o += dir * max (diff, 0.0);
387 return scm_from_double (o);
391 Side_position_interface::set_axis (Grob *me, Axis a)
393 if (!scm_is_number (me->get_property ("side-axis")))
395 me->set_property ("side-axis", scm_from_int (a));
396 chain_offset_callback (me,
398 ? x_aligned_side_proc
399 : y_aligned_side_proc,
405 Side_position_interface::get_axis (Grob *me)
407 if (scm_is_number (me->get_property ("side-axis")))
408 return Axis (scm_to_int (me->get_property ("side-axis")));
410 string msg = String_convert::form_string ("side-axis not set for grob %s.",
411 me->name ().c_str ());
412 me->programming_error (msg);
416 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
418 Side_position_interface::move_to_extremal_staff (SCM smob)
420 Grob *me = unsmob_grob (smob);
421 System *sys = dynamic_cast<System *> (me->get_system ());
422 Direction dir = get_grob_direction (me);
426 Interval iv = me->extent (sys, X_AXIS);
428 Grob *top_staff = sys->get_extremal_staff (dir, iv);
433 // Only move this grob if it is a direct child of the system. We
434 // are not interested in moving marks from other staves to the top
435 // staff; we only want to move marks from the system to the top
437 if (sys != me->get_parent (Y_AXIS))
440 me->set_parent (top_staff, Y_AXIS);
441 me->flush_extent_cache (Y_AXIS);
442 Axis_group_interface::add_element (top_staff, me);
444 // Remove any cross-staff side-support dependencies
445 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
448 vector<Grob *> const &elts = ga->array ();
449 vector<Grob *> new_elts;
450 for (vsize i = 0; i < elts.size (); ++i)
452 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
453 new_elts.push_back (elts[i]);
455 ga->set_array (new_elts);
460 ADD_INTERFACE (Side_position_interface,
461 "Position a victim object (this one) next to other objects"
462 " (the support). The property @code{direction} signifies where"
463 " to put the victim object relative to the support (left or"
464 " right, up or down?)\n"
466 "The routine also takes the size of the staff into account if"
467 " @code{staff-padding} is set. If undefined, the staff symbol"
476 "side-support-elements "