2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "side-position-interface.hh"
11 #include <cmath> // ceil.
16 #include "axis-group-interface.hh"
17 #include "directional-element-interface.hh"
21 #include "note-head.hh"
22 #include "pointer-group-interface.hh"
23 #include "staff-symbol-referencer.hh"
24 #include "staff-symbol.hh"
26 #include "string-convert.hh"
31 Side_position_interface::add_support (Grob *me, Grob *e)
33 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
37 Side_position_interface::get_direction (Grob *me)
39 Direction relative_dir = Direction (1);
40 SCM reldir = me->get_property ("side-relative-direction");
41 if (is_direction (reldir))
42 relative_dir = to_dir (reldir);
44 SCM other_elt = me->get_object ("direction-source");
45 Grob *e = unsmob_grob (other_elt);
47 return (Direction) (relative_dir * get_grob_direction (e));
52 /* Put the element next to the support, optionally taking in
53 account the extent of the support.
55 Does not take into account the extent of ME.
58 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
59 bool include_my_extent,
60 bool pure, int start, int end,
63 Real ss = Staff_symbol_referencer::staff_space (me);
65 extract_grob_set (me, "side-support-elements", support);
67 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
68 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
72 && scm_is_number (me->get_property ("staff-padding"))
73 && !to_boolean (me->get_property ("quantize-position"));
76 Interval staff_extents;
79 common = staff_symbol->common_refpoint (common, Y_AXIS);
80 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
83 dim.unite (staff_extents);
86 Direction dir = get_grob_direction (me);
88 for (vsize i = 0; i < support.size (); i++)
92 // In the case of a stem, we will find a note head as well
93 // ignoring the stem solves cyclic dependencies if the stem is
94 // attached to a cross-staff beam.
96 && Stem::has_interface (e)
97 && dir == - get_grob_direction (e))
103 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
106 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
107 dim.unite (Interval (x, x));
113 dim = Interval (0, 0);
115 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
116 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
118 Real total_off = dim.linear_combination (dir) - off;
119 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
121 if (minimum_space >= 0
123 && total_off * dir < minimum_space)
124 total_off = minimum_space * dir;
126 if (include_my_extent)
128 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
133 programming_error ("direction unknown, but aligned-side wanted");
136 total_off += -iv[-dir];
141 total_off = dir * max (dir * total_off,
142 dir * (*current_offset));
145 /* FIXME: 1000 should relate to paper size. */
146 if (fabs (total_off) > 1000)
149 = String_convert::form_string ("Improbable offset for grob %s: %f",
150 me->name ().c_str (), total_off);
152 programming_error (msg);
153 if (strict_infinity_checking)
154 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
156 return scm_from_double (total_off);
160 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
162 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
164 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
167 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
169 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
171 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
172 true, scm_to_int (start), scm_to_int (end), 0);
177 Position next to support, taking into account my own dimensions and padding.
180 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
183 Real *current_off_ptr = 0;
184 if (scm_is_number (current_off_scm))
186 r = scm_to_double (current_off_scm);
187 current_off_ptr = &r;
190 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
194 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
196 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
198 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
201 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
203 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
205 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
208 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
210 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
212 return axis_aligned_side_helper (smob, Y_AXIS, true,
218 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
220 Side_position_interface::calc_cross_staff (SCM smob)
222 Grob *me = unsmob_grob (smob);
223 extract_grob_set (me, "side-support-elements", elts);
225 for (vsize i = 0; i < elts.size (); i++)
226 if (to_boolean (elts[i]->get_property ("cross-staff")))
229 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
230 return scm_from_bool (common != me->get_parent (Y_AXIS));
234 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
237 Direction dir = get_grob_direction (me);
239 Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
242 Maintain a minimum distance to the staff. This is similar to side
243 position with padding, but it will put adjoining objects on a row if
244 stuff sticks out of the staff a little.
246 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
247 if (staff && a == Y_AXIS)
249 if (to_boolean (me->get_property ("quantize-position")))
251 Grob *common = me->common_refpoint (staff, Y_AXIS);
252 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
253 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
254 Real ss = Staff_symbol::staff_space (staff);
255 Real position = 2 * (my_off + o - staff_off) / ss;
256 Real rounded = directed_round (position, dir);
257 Grob *head = me->get_parent (X_AXIS);
259 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
260 /* In case of a ledger lines, quantize even if we're outside the staff. */
261 || (Note_head::has_interface (head)
263 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
265 o += (rounded - position) * 0.5 * ss;
266 if (Staff_symbol_referencer::on_line (me, int (rounded)))
270 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
272 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
275 = Staff_symbol_referencer::staff_space (me)
276 * scm_to_double (me->get_property ("staff-padding"));
278 Grob *common = me->common_refpoint (staff, Y_AXIS);
280 Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
281 Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
282 o += dir * max (diff, 0.0);
285 return scm_from_double (o);
289 Side_position_interface::set_axis (Grob *me, Axis a)
291 if (!scm_is_number (me->get_property ("side-axis")))
293 me->set_property ("side-axis", scm_from_int (a));
294 chain_offset_callback (me,
296 ? x_aligned_side_proc
297 : y_aligned_side_proc,
303 Side_position_interface::get_axis (Grob *me)
305 if (scm_is_number (me->get_property ("side-axis")))
306 return Axis (scm_to_int (me->get_property ("side-axis")));
308 string msg = String_convert::form_string ("side-axis not set for grob %s.",
309 me->name ().c_str ());
310 me->programming_error (msg);
314 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
316 Side_position_interface::move_to_extremal_staff (SCM smob)
318 Grob *me = unsmob_grob (smob);
319 System *sys = dynamic_cast<System*> (me->get_system ());
320 Direction dir = Side_position_interface::get_direction (me);
324 Grob *top_staff = sys->get_extremal_staff (dir, me->extent (sys, X_AXIS));
329 // Only move this grob if it is a direct child of the system. We
330 // are not interested in moving marks from other staves to the top
331 // staff; we only want to move marks from the system to the top
333 if (sys != me->get_parent (Y_AXIS))
336 me->set_parent (top_staff, Y_AXIS);
337 me->flush_extent_cache (Y_AXIS);
338 Axis_group_interface::add_element (top_staff, me);
343 ADD_INTERFACE (Side_position_interface,
344 "Position a victim object (this one) next to other objects"
345 " (the support). The property @code{direction} signifies where"
346 " to put the victim object relative to the support (left or"
347 " right, up or down?)\n"
349 "The routine also takes the size of the staff into account if"
350 " @code{staff-padding} is set. If undefined, the staff symbol"
360 "side-relative-direction "
361 "side-support-elements "