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 "axis-group-interface.hh"
28 #include "directional-element-interface.hh"
30 #include "grob-array.hh"
33 #include "note-head.hh"
34 #include "pointer-group-interface.hh"
35 #include "staff-symbol-referencer.hh"
36 #include "staff-symbol.hh"
38 #include "string-convert.hh"
43 Side_position_interface::add_support (Grob *me, Grob *e)
45 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
48 /* Put the element next to the support, optionally taking in
49 account the extent of the support.
51 Does not take into account the extent of ME.
54 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
55 bool include_my_extent,
56 bool pure, int start, int end,
59 Real ss = Staff_symbol_referencer::staff_space (me);
61 extract_grob_set (me, "side-support-elements", support);
63 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
64 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
68 && scm_is_number (me->get_property ("staff-padding"))
69 && !to_boolean (me->get_property ("quantize-position"));
72 Interval staff_extents;
75 common = staff_symbol->common_refpoint (common, Y_AXIS);
76 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
79 dim.unite (staff_extents);
82 Direction dir = get_grob_direction (me);
84 for (vsize i = 0; i < support.size (); i++)
88 // In the case of a stem, we will find a note head as well
89 // ignoring the stem solves cyclic dependencies if the stem is
90 // attached to a cross-staff beam.
92 && Stem::has_interface (e)
93 && dir == - get_grob_direction (e))
99 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
102 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
103 dim.unite (Interval (x, x));
109 dim = Interval (0, 0);
111 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
112 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
114 Real total_off = dim.linear_combination (dir) - off;
115 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
117 if (minimum_space >= 0
119 && total_off * dir < minimum_space)
120 total_off = minimum_space * dir;
122 if (include_my_extent)
124 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
129 programming_error ("direction unknown, but aligned-side wanted");
132 total_off += -iv[-dir];
137 total_off = dir * max (dir * total_off,
138 dir * (*current_offset));
140 /* FIXME: 1000 should relate to paper size. */
141 if (fabs (total_off) > 1000)
144 = String_convert::form_string ("Improbable offset for grob %s: %f",
145 me->name ().c_str (), total_off);
147 programming_error (msg);
148 if (strict_infinity_checking)
149 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
152 return scm_from_double (total_off);
155 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
157 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
159 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
162 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
164 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
166 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
167 true, scm_to_int (start), scm_to_int (end), 0);
171 Position next to support, taking into account my own dimensions and padding.
174 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
177 Real *current_off_ptr = 0;
178 if (scm_is_number (current_off_scm))
180 r = scm_to_double (current_off_scm);
181 current_off_ptr = &r;
184 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
187 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
189 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
191 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
194 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
196 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
198 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
201 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
203 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
205 return axis_aligned_side_helper (smob, Y_AXIS, true,
211 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
213 Side_position_interface::calc_cross_staff (SCM smob)
215 Grob *me = unsmob_grob (smob);
216 extract_grob_set (me, "side-support-elements", elts);
218 for (vsize i = 0; i < elts.size (); i++)
219 if (to_boolean (elts[i]->get_property ("cross-staff")))
222 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
223 return scm_from_bool (common != me->get_parent (Y_AXIS));
227 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
230 Direction dir = get_grob_direction (me);
232 Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
235 Maintain a minimum distance to the staff. This is similar to side
236 position with padding, but it will put adjoining objects on a row if
237 stuff sticks out of the staff a little.
239 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
240 if (staff && a == Y_AXIS)
242 if (to_boolean (me->get_property ("quantize-position")))
244 Grob *common = me->common_refpoint (staff, Y_AXIS);
245 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
246 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
247 Real ss = Staff_symbol::staff_space (staff);
248 Real position = 2 * (my_off + o - staff_off) / ss;
249 Real rounded = directed_round (position, dir);
250 Grob *head = me->get_parent (X_AXIS);
252 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
253 /* In case of a ledger lines, quantize even if we're outside the staff. */
254 || (Note_head::has_interface (head)
256 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
258 o += (rounded - position) * 0.5 * ss;
259 if (Staff_symbol_referencer::on_line (me, int (rounded)))
263 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
265 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
268 = Staff_symbol_referencer::staff_space (me)
269 * scm_to_double (me->get_property ("staff-padding"));
271 Grob *parent = me->get_parent (Y_AXIS);
272 Grob *common = me->common_refpoint (staff, Y_AXIS);
273 Real parent_position = parent->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
274 Real staff_position = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
275 Interval staff_extent = staff->maybe_pure_extent (staff, a, pure, start, end);
276 Real diff = (dir * staff_extent[dir] + staff_padding
277 - dir * (o + iv[-dir])
278 + dir * (staff_position - parent_position));
279 o += dir * max (diff, 0.0);
282 return scm_from_double (o);
286 Side_position_interface::set_axis (Grob *me, Axis a)
288 if (!scm_is_number (me->get_property ("side-axis")))
290 me->set_property ("side-axis", scm_from_int (a));
291 chain_offset_callback (me,
293 ? x_aligned_side_proc
294 : y_aligned_side_proc,
300 Side_position_interface::get_axis (Grob *me)
302 if (scm_is_number (me->get_property ("side-axis")))
303 return Axis (scm_to_int (me->get_property ("side-axis")));
305 string msg = String_convert::form_string ("side-axis not set for grob %s.",
306 me->name ().c_str ());
307 me->programming_error (msg);
311 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
313 Side_position_interface::move_to_extremal_staff (SCM smob)
315 Grob *me = unsmob_grob (smob);
316 System *sys = dynamic_cast<System *> (me->get_system ());
317 Direction dir = get_grob_direction (me);
321 Interval iv = me->extent (sys, X_AXIS);
323 Grob *top_staff = sys->get_extremal_staff (dir, iv);
328 // Only move this grob if it is a direct child of the system. We
329 // are not interested in moving marks from other staves to the top
330 // staff; we only want to move marks from the system to the top
332 if (sys != me->get_parent (Y_AXIS))
335 me->set_parent (top_staff, Y_AXIS);
336 me->flush_extent_cache (Y_AXIS);
337 Axis_group_interface::add_element (top_staff, me);
339 // Remove any cross-staff side-support dependencies
340 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
343 vector<Grob *> const &elts = ga->array ();
344 vector<Grob *> new_elts;
345 for (vsize i = 0; i < elts.size (); ++i)
347 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
348 new_elts.push_back (elts[i]);
350 ga->set_array (new_elts);
355 ADD_INTERFACE (Side_position_interface,
356 "Position a victim object (this one) next to other objects"
357 " (the support). The property @code{direction} signifies where"
358 " to put the victim object relative to the support (left or"
359 " right, up or down?)\n"
361 "The routine also takes the size of the staff into account if"
362 " @code{staff-padding} is set. If undefined, the staff symbol"
371 "side-support-elements "