2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "side-position-interface.hh"
11 #include <cmath> // ceil.
15 #include "directional-element-interface.hh"
19 #include "note-head.hh"
20 #include "pointer-group-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "staff-symbol.hh"
24 #include "string-convert.hh"
28 Side_position_interface::add_support (Grob *me, Grob *e)
30 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
34 Side_position_interface::get_direction (Grob *me)
36 Direction relative_dir = Direction (1);
37 SCM reldir = me->get_property ("side-relative-direction");
38 if (is_direction (reldir))
39 relative_dir = to_dir (reldir);
41 SCM other_elt = me->get_object ("direction-source");
42 Grob *e = unsmob_grob (other_elt);
44 return (Direction) (relative_dir * get_grob_direction (e));
49 /* Put the element next to the support, optionally taking in
50 account the extent of the support.
52 Does not take into account the extent of ME.
55 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents,
56 bool include_my_extent,
57 bool pure, int start, int end,
60 Real ss = Staff_symbol_referencer::staff_space (me);
62 extract_grob_set (me, "side-support-elements", support);
64 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
65 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
69 && scm_is_number (me->get_property ("staff-padding"))
70 && !to_boolean (me->get_property ("quantize-position"));
73 Interval staff_extents;
76 common = staff_symbol->common_refpoint (common, Y_AXIS);
77 staff_extents = staff_symbol->maybe_pure_extent (common, Y_AXIS, pure, start, end);
80 dim.unite (staff_extents);
83 Direction dir = get_grob_direction (me);
85 for (vsize i = 0; i < support.size (); i++)
89 // In the case of a stem, we will find a note head as well
90 // ignoring the stem solves cyclic dependencies if the stem is
91 // attached to a cross-staff beam.
93 && Stem::has_interface (e)
94 && 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));
108 dim = Interval (0, 0);
110 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
111 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
113 Real total_off = dim.linear_combination (dir) - off;
114 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
116 if (minimum_space >= 0
118 && total_off * dir < minimum_space)
119 total_off = minimum_space * dir;
121 if (include_my_extent)
123 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
128 programming_error ("direction unknown, but aligned-side wanted");
131 total_off += -iv[-dir];
136 total_off = dir * max (dir * total_off,
137 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);
151 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);
172 Position next to support, taking into account my own dimensions and padding.
175 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
178 Real *current_off_ptr = 0;
179 if (scm_is_number (current_off_scm))
181 r = scm_to_double (current_off_scm);
182 current_off_ptr = &r;
185 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
189 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1, "");
191 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
193 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
196 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1, "");
198 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
200 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
203 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1, "");
205 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
207 return axis_aligned_side_helper (smob, Y_AXIS, true,
213 MAKE_SCHEME_CALLBACK (Side_position_interface, calc_cross_staff, 1)
215 Side_position_interface::calc_cross_staff (SCM smob)
217 Grob *me = unsmob_grob (smob);
218 extract_grob_set (me, "side-support-elements", elts);
220 Grob *common = common_refpoint_of_array (elts, me->get_parent (Y_AXIS), Y_AXIS);
221 return scm_from_bool (common != me->get_parent (Y_AXIS));
225 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
228 Direction dir = get_grob_direction (me);
230 Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
233 Maintain a minimum distance to the staff. This is similar to side
234 position with padding, but it will put adjoining objects on a row if
235 stuff sticks out of the staff a little.
237 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
238 if (staff && a == Y_AXIS)
240 if (to_boolean (me->get_property ("quantize-position")))
242 Grob *common = me->common_refpoint (staff, Y_AXIS);
243 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
244 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
245 Real ss = Staff_symbol::staff_space (staff);
246 Real position = 2 * (my_off + o - staff_off) / ss;
247 Real rounded = directed_round (position, dir);
248 Grob *head = me->get_parent (X_AXIS);
250 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
251 || (Note_head::has_interface (head)
252 && sign (Staff_symbol_referencer::get_position (head)) == - dir))
254 o += (rounded - position) * 0.5 * ss;
255 if (Staff_symbol_referencer::on_line (me, int (rounded)))
259 else if (scm_is_number (me->get_property ("staff-padding")) && dir)
261 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
264 = Staff_symbol_referencer::staff_space (me)
265 * scm_to_double (me->get_property ("staff-padding"));
267 Grob *common = me->common_refpoint (staff, Y_AXIS);
269 Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
270 Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
271 o += dir * max (diff, 0.0);
274 return scm_from_double (o);
278 Side_position_interface::set_axis (Grob *me, Axis a)
280 if (!scm_is_number (me->get_property ("side-axis")))
282 me->set_property ("side-axis", scm_from_int (a));
283 chain_offset_callback (me,
285 ? x_aligned_side_proc
286 : y_aligned_side_proc,
291 Side_position_interface::get_axis (Grob *me)
293 if (scm_is_number (me->get_property ("side-axis")))
294 return Axis (scm_to_int (me->get_property ("side-axis")));
296 me->programming_error ("side-axis not set.");
300 ADD_INTERFACE (Side_position_interface,
301 "Position a victim object (this one) next to other objects (the "
302 "support). The property @code{direction} signifies where to put the "
303 "victim object relative to the support (left or right, up or down?)\n\n "
304 "The routine also takes the size the staff into account if "
305 "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
314 "side-relative-direction "
315 "side-support-elements "