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"
19 #include "grob-array.hh"
22 #include "note-head.hh"
23 #include "pointer-group-interface.hh"
24 #include "staff-symbol-referencer.hh"
25 #include "staff-symbol.hh"
27 #include "string-convert.hh"
32 Side_position_interface::add_support (Grob *me, Grob *e)
34 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
38 Side_position_interface::get_direction (Grob *me)
40 Direction relative_dir = Direction (1);
41 SCM reldir = me->get_property ("side-relative-direction");
42 if (is_direction (reldir))
43 relative_dir = to_dir (reldir);
45 SCM other_elt = me->get_object ("direction-source");
46 Grob *e = unsmob_grob (other_elt);
48 return (Direction) (relative_dir * get_grob_direction (e));
53 /* Put the element next to the support, optionally taking in
54 account the extent of the support.
56 Does not take into account the extent of ME.
59 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
60 bool include_my_extent,
61 bool pure, int start, int end,
64 Real ss = Staff_symbol_referencer::staff_space (me);
66 extract_grob_set (me, "side-support-elements", support);
68 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
69 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
73 && scm_is_number (me->get_property ("staff-padding"))
74 && !to_boolean (me->get_property ("quantize-position"));
77 Interval staff_extents;
80 common = staff_symbol->common_refpoint (common, Y_AXIS);
81 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
84 dim.unite (staff_extents);
87 Direction dir = get_grob_direction (me);
89 for (vsize i = 0; i < support.size (); i++)
93 // In the case of a stem, we will find a note head as well
94 // ignoring the stem solves cyclic dependencies if the stem is
95 // attached to a cross-staff beam.
97 && Stem::has_interface (e)
98 && dir == - get_grob_direction (e))
104 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
107 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
108 dim.unite (Interval (x, x));
114 dim = Interval (0, 0);
116 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
117 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
119 Real total_off = dim.linear_combination (dir) - off;
120 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
122 if (minimum_space >= 0
124 && total_off * dir < minimum_space)
125 total_off = minimum_space * dir;
127 if (include_my_extent)
129 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
134 programming_error ("direction unknown, but aligned-side wanted");
137 total_off += -iv[-dir];
142 total_off = dir * max (dir * total_off,
143 dir * (*current_offset));
146 /* FIXME: 1000 should relate to paper size. */
147 if (fabs (total_off) > 1000)
150 = String_convert::form_string ("Improbable offset for grob %s: %f",
151 me->name ().c_str (), total_off);
153 programming_error (msg);
154 if (strict_infinity_checking)
155 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
157 return scm_from_double (total_off);
161 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
163 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
165 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
168 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
170 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
172 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
173 true, scm_to_int (start), scm_to_int (end), 0);
178 Position next to support, taking into account my own dimensions and padding.
181 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
184 Real *current_off_ptr = 0;
185 if (scm_is_number (current_off_scm))
187 r = scm_to_double (current_off_scm);
188 current_off_ptr = &r;
191 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
195 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
197 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
199 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
202 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
204 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
206 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
209 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
211 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
213 return axis_aligned_side_helper (smob, Y_AXIS, true,
219 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
221 Side_position_interface::calc_cross_staff (SCM smob)
223 Grob *me = unsmob_grob (smob);
224 extract_grob_set (me, "side-support-elements", elts);
226 for (vsize i = 0; i < elts.size (); i++)
227 if (to_boolean (elts[i]->get_property ("cross-staff")))
230 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
231 return scm_from_bool (common != me->get_parent (Y_AXIS));
235 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
238 Direction dir = get_grob_direction (me);
240 Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
243 Maintain a minimum distance to the staff. This is similar to side
244 position with padding, but it will put adjoining objects on a row if
245 stuff sticks out of the staff a little.
247 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
248 if (staff && a == Y_AXIS)
250 if (to_boolean (me->get_property ("quantize-position")))
252 Grob *common = me->common_refpoint (staff, Y_AXIS);
253 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
254 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
255 Real ss = Staff_symbol::staff_space (staff);
256 Real position = 2 * (my_off + o - staff_off) / ss;
257 Real rounded = directed_round (position, dir);
258 Grob *head = me->get_parent (X_AXIS);
260 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
261 /* In case of a ledger lines, quantize even if we're outside the staff. */
262 || (Note_head::has_interface (head)
264 && abs (Staff_symbol_referencer::get_position (head)) > abs (position)))
266 o += (rounded - position) * 0.5 * ss;
267 if (Staff_symbol_referencer::on_line (me, int (rounded)))
271 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
273 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
276 = Staff_symbol_referencer::staff_space (me)
277 * scm_to_double (me->get_property ("staff-padding"));
279 Grob *common = me->common_refpoint (staff, Y_AXIS);
281 Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
282 Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
283 o += dir * max (diff, 0.0);
286 return scm_from_double (o);
290 Side_position_interface::set_axis (Grob *me, Axis a)
292 if (!scm_is_number (me->get_property ("side-axis")))
294 me->set_property ("side-axis", scm_from_int (a));
295 chain_offset_callback (me,
297 ? x_aligned_side_proc
298 : y_aligned_side_proc,
304 Side_position_interface::get_axis (Grob *me)
306 if (scm_is_number (me->get_property ("side-axis")))
307 return Axis (scm_to_int (me->get_property ("side-axis")));
309 string msg = String_convert::form_string ("side-axis not set for grob %s.",
310 me->name ().c_str ());
311 me->programming_error (msg);
315 MAKE_SCHEME_CALLBACK (Side_position_interface, move_to_extremal_staff, 1);
317 Side_position_interface::move_to_extremal_staff (SCM smob)
319 Grob *me = unsmob_grob (smob);
320 System *sys = dynamic_cast<System*> (me->get_system ());
321 Direction dir = Side_position_interface::get_direction (me);
325 Interval iv = me->extent (sys, X_AXIS);
327 Grob *top_staff = sys->get_extremal_staff (dir, iv);
332 // Only move this grob if it is a direct child of the system. We
333 // are not interested in moving marks from other staves to the top
334 // staff; we only want to move marks from the system to the top
336 if (sys != me->get_parent (Y_AXIS))
339 me->set_parent (top_staff, Y_AXIS);
340 me->flush_extent_cache (Y_AXIS);
341 Axis_group_interface::add_element (top_staff, me);
343 // Remove any cross-staff side-support dependencies
344 Grob_array *ga = unsmob_grob_array (me->get_object ("side-support-elements"));
347 vector<Grob*> const& elts = ga->array ();
348 vector<Grob*> new_elts;
349 for (vsize i = 0; i < elts.size (); ++i)
351 if (me->common_refpoint (elts[i], Y_AXIS) == top_staff)
352 new_elts.push_back (elts[i]);
354 ga->set_array (new_elts);
360 ADD_INTERFACE (Side_position_interface,
361 "Position a victim object (this one) next to other objects"
362 " (the support). The property @code{direction} signifies where"
363 " to put the victim object relative to the support (left or"
364 " right, up or down?)\n"
366 "The routine also takes the size of the staff into account if"
367 " @code{staff-padding} is set. If undefined, the staff symbol"
377 "side-relative-direction "
378 "side-support-elements "