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 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
180 off[ax] = me->get_parent (ax)->maybe_pure_coordinate (common[ax], ax, pure, start, end)
181 + me->maybe_pure_extent (me, ax, pure, start, end);
183 off[ax] = me->maybe_pure_extent (common[ax], ax, pure, start, end);
186 if (off[X_AXIS].is_empty () || off[Y_AXIS].is_empty ())
187 return scm_from_double (0.0);
189 Real skyline_padding = 0.1;
191 Skyline my_dim (off, skyline_padding, other_axis (a), -dir);
196 && scm_is_number (me->get_property ("staff-padding"))
197 && !to_boolean (me->get_property ("quantize-position"));
200 Real min_h = dir == LEFT ? infinity_f : -infinity_f;
201 for (vsize i = 0; i < support.size (); i++)
203 Grob *e = support[i];
205 // In the case of a stem, we will find a note head as well
206 // ignoring the stem solves cyclic dependencies if the stem is
207 // attached to a cross-staff beam.
209 && Stem::has_interface (e)
210 && dir == - get_grob_direction (e))
215 if (Accidental_interface::has_interface (e))
217 vector<Box> bs = Accidental_interface::accurate_boxes (e, common);
218 boxes.insert (boxes.end (), bs.begin (), bs.end ());
223 for (Axis ax = X_AXIS; ax < NO_AXES; incr (ax))
224 b[ax] = e->maybe_pure_extent (common[ax], ax, pure, start, end);
226 if (b[X_AXIS].is_empty () || b[Y_AXIS].is_empty ())
230 min_h = minmax (dir, b[a][-dir], min_h);
235 Skyline dim (boxes, skyline_padding, other_axis (a), dir);
237 dim.set_minimum_height (0.0);
239 dim.set_minimum_height (min_h);
243 Interval staff_extents;
244 common[Y_AXIS] = staff_symbol->common_refpoint (common[Y_AXIS], Y_AXIS);
245 staff_extents = staff_symbol->maybe_pure_extent (common[Y_AXIS], Y_AXIS, pure, start, end);
246 dim.set_minimum_height (minmax (dir, min_h, staff_extents[dir]));
249 Real total_off = dir * dim.distance (my_dim);
250 return finish_offset (me, dir, total_off, current_offset);
253 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
255 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
257 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
260 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
262 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
264 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
265 true, scm_to_int (start), scm_to_int (end), 0);
269 Position next to support, taking into account my own dimensions and padding.
272 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
275 Real *current_off_ptr = 0;
276 if (scm_is_number (current_off_scm))
278 r = scm_to_double (current_off_scm);
279 current_off_ptr = &r;
282 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
285 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
287 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
289 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
292 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
294 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
296 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
299 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
301 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
303 return axis_aligned_side_helper (smob, Y_AXIS, true,
309 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
311 Side_position_interface::calc_cross_staff (SCM smob)
313 Grob *me = unsmob_grob (smob);
314 extract_grob_set (me, "side-support-elements", elts);
316 for (vsize i = 0; i < elts.size (); i++)
317 if (to_boolean (elts[i]->get_property ("cross-staff")))
320 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
321 return scm_from_bool (common != me->get_parent (Y_AXIS));
325 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
328 Direction dir = get_grob_direction (me);
329 bool skyline = to_boolean (me->get_property ("use-skylines"));
331 Real o = scm_to_double (skyline
332 ? skyline_side_position (me, a, pure, start, end, current_off)
333 : general_side_position (me, a, true, true, pure, start, end, current_off));
336 Maintain a minimum distance to the staff. This is similar to side
337 position with padding, but it will put adjoining objects on a row if
338 stuff sticks out of the staff a little.
340 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
341 if (staff && a == Y_AXIS)
343 if (to_boolean (me->get_property ("quantize-position")))
345 Grob *common = me->common_refpoint (staff, Y_AXIS);
346 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
347 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
348 Real ss = Staff_symbol::staff_space (staff);
349 Real position = 2 * (my_off + o - staff_off) / ss;
350 Real rounded = directed_round (position, dir);
351 Grob *head = me->get_parent (X_AXIS);
353 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
354 /* In case of a ledger lines, quantize even if we're outside the staff. */
355 || (Note_head::has_interface (head)
357 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
359 o += (rounded - position) * 0.5 * ss;
360 if (Staff_symbol_referencer::on_line (me, int (rounded)))
364 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
366 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
369 = Staff_symbol_referencer::staff_space (me)
370 * scm_to_double (me->get_property ("staff-padding"));
372 Grob *parent = me->get_parent (Y_AXIS);
373 Grob *common = me->common_refpoint (staff, Y_AXIS);
374 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
375 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
376 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
377 Real diff = (dir * staff_extent[dir] + staff_padding
378 - dir * (o + iv[-dir])
379 + dir * (staff_position - parent_position));
380 o += dir * max (diff, 0.0);
383 return scm_from_double (o);
387 Side_position_interface::set_axis (Grob *me, Axis a)
389 if (!scm_is_number (me->get_property ("side-axis")))
391 me->set_property ("side-axis", scm_from_int (a));
392 chain_offset_callback (me,
394 ? x_aligned_side_proc
395 : y_aligned_side_proc,
401 Side_position_interface::get_axis (Grob *me)
403 if (scm_is_number (me->get_property ("side-axis")))
404 return Axis (scm_to_int (me->get_property ("side-axis")));
406 string msg = String_convert::form_string ("side-axis not set for grob %s.",
407 me->name ().c_str ());
408 me->programming_error (msg);
412 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
414 Side_position_interface::move_to_extremal_staff (SCM smob)
416 Grob *me = unsmob_grob (smob);
417 System *sys = dynamic_cast<System *> (me->get_system ());
418 Direction dir = get_grob_direction (me);
422 Interval iv = me->extent (sys, X_AXIS);
424 Grob *top_staff = sys->get_extremal_staff (dir, iv);
429 // Only move this grob if it is a direct child of the system. We
430 // are not interested in moving marks from other staves to the top
431 // staff; we only want to move marks from the system to the top
433 if (sys != me->get_parent (Y_AXIS))
436 me->set_parent (top_staff, Y_AXIS);
437 me->flush_extent_cache (Y_AXIS);
438 Axis_group_interface::add_element (top_staff, me);
440 // Remove any cross-staff side-support dependencies
441 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
444 vector<Grob *> const &elts = ga->array ();
445 vector<Grob *> new_elts;
446 for (vsize i = 0; i < elts.size (); ++i)
448 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
449 new_elts.push_back (elts[i]);
451 ga->set_array (new_elts);
456 ADD_INTERFACE (Side_position_interface,
457 "Position a victim object (this one) next to other objects"
458 " (the support). The property @code{direction} signifies where"
459 " to put the victim object relative to the support (left or"
460 " right, up or down?)\n"
462 "The routine also takes the size of the staff into account if"
463 " @code{staff-padding} is set. If undefined, the staff symbol"
472 "side-support-elements "