2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "side-position-interface.hh"
11 #include <cmath> // ceil.
15 #include "note-head.hh"
18 #include "staff-symbol-referencer.hh"
19 #include "pointer-group-interface.hh"
20 #include "directional-element-interface.hh"
21 #include "staff-symbol-referencer.hh"
22 #include "staff-symbol.hh"
23 #include "string-convert.hh"
27 Side_position_interface::add_support (Grob *me, Grob *e)
29 Pointer_group_interface::add_unordered_grob (me, ly_symbol2scm ("side-support-elements"), e);
33 Side_position_interface::get_direction (Grob *me)
35 Direction relative_dir = Direction (1);
36 SCM reldir = me->get_property ("side-relative-direction");
37 if (is_direction (reldir))
38 relative_dir = to_dir (reldir);
40 SCM other_elt = me->get_object ("direction-source");
41 Grob *e = unsmob_grob (other_elt);
43 return (Direction) (relative_dir * get_grob_direction (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 for (vsize i = 0; i < support.size (); i++)
87 dim.unite (e->maybe_pure_extent (common, a, pure, start, end));
90 Real x = e->maybe_pure_coordinate (common, a, pure, start, end);
91 dim.unite (Interval (x, x));
96 dim = Interval (0, 0);
98 Direction dir = get_grob_direction (me);
100 Real off = me->get_parent (a)->maybe_pure_coordinate (common, a, pure, start, end);
101 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
103 Real total_off = dim.linear_combination (dir) - off;
104 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
106 if (minimum_space >= 0
108 && total_off * dir < minimum_space)
109 total_off = minimum_space * dir;
111 if (include_my_extent)
113 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
118 programming_error ("direction unknown, but aligned-side wanted");
121 total_off += -iv[-dir];
126 total_off = dir * max (dir * total_off,
127 dir * (*current_offset));
130 /* FIXME: 1000 should relate to paper size. */
131 if (fabs (total_off) > 1000)
134 = String_convert::form_string ("Improbable offset for grob %s: %f",
135 me->name ().c_str (), total_off);
137 programming_error (msg);
138 if (strict_infinity_checking)
139 scm_misc_error (__FUNCTION__, "Improbable offset.", SCM_EOL);
141 return scm_from_double (total_off);
145 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
147 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
149 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false, false, 0, 0, 0);
152 MAKE_SCHEME_CALLBACK (Side_position_interface, pure_y_aligned_on_support_refpoints, 3);
154 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob, SCM start, SCM end)
156 return general_side_position (unsmob_grob (smob), Y_AXIS, false, false,
157 true, scm_to_int (start), scm_to_int (end), 0);
162 Position next to support, taking into account my own dimensions and padding.
165 axis_aligned_side_helper (SCM smob, Axis a, bool pure, int start, int end, SCM current_off_scm)
168 Real *current_off_ptr = 0;
169 if (scm_is_number (current_off_scm))
171 r = scm_to_double (current_off_scm);
172 current_off_ptr = &r;
175 return Side_position_interface::aligned_side (unsmob_grob (smob), a, pure, start, end, current_off_ptr);
179 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, x_aligned_side, 2, 1);
181 Side_position_interface::x_aligned_side (SCM smob, SCM current_off)
183 return axis_aligned_side_helper (smob, X_AXIS, false, 0, 0, current_off);
186 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, y_aligned_side, 2, 1);
188 Side_position_interface::y_aligned_side (SCM smob, SCM current_off)
190 return axis_aligned_side_helper (smob, Y_AXIS, false, 0, 0, current_off);
193 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface, pure_y_aligned_side, 4, 1);
195 Side_position_interface::pure_y_aligned_side (SCM smob, SCM start, SCM end, SCM cur_off)
197 return axis_aligned_side_helper (smob, Y_AXIS, true,
204 Side_position_interface::aligned_side (Grob *me, Axis a, bool pure, int start, int end,
207 Direction dir = get_grob_direction (me);
209 Real o = scm_to_double (general_side_position (me, a, true, true, pure, start, end, current_off));
212 Maintain a minimum distance to the staff. This is similar to side
213 position with padding, but it will put adjoining objects on a row if
214 stuff sticks out of the staff a little.
216 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
217 if (staff && a == Y_AXIS)
219 if (to_boolean (me->get_property ("quantize-position")))
221 Grob *common = me->common_refpoint (staff, Y_AXIS);
222 Real my_off = me->get_parent (Y_AXIS)->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
223 Real staff_off = staff->maybe_pure_coordinate (common, Y_AXIS, pure, start, end);
224 Real ss = Staff_symbol::staff_space (staff);
225 Real position = 2 * (my_off + o - staff_off) / ss;
226 Real rounded = directed_round (position, dir);
227 Grob *head = me->get_parent (X_AXIS);
229 if (fabs (position) <= 2 * Staff_symbol_referencer::staff_radius (me) + 1
230 || (Note_head::has_interface (head)
231 && sign (Staff_symbol_referencer::get_position (head)) == - dir))
233 o += (rounded - position) * 0.5 * ss;
234 if (Staff_symbol_referencer::on_line (me, int (rounded)))
238 else if (scm_is_number (me->get_property ("staff-padding")))
240 Interval iv = me->maybe_pure_extent (me, a, pure, start, end);
243 = Staff_symbol_referencer::staff_space (me)
244 * scm_to_double (me->get_property ("staff-padding"));
246 Grob *common = me->common_refpoint (staff, Y_AXIS);
248 Interval staff_size = staff->maybe_pure_extent (common, Y_AXIS, pure, start, end);
249 Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
250 o += dir * max (diff, 0.0);
253 return scm_from_double (o);
257 Side_position_interface::set_axis (Grob *me, Axis a)
259 if (!scm_is_number (me->get_property ("side-axis")))
261 me->set_property ("side-axis", scm_from_int (a));
262 chain_offset_callback (me,
264 ? x_aligned_side_proc
265 : y_aligned_side_proc,
270 Side_position_interface::get_axis (Grob *me)
272 if (scm_is_number (me->get_property ("side-axis")))
273 return Axis (scm_to_int (me->get_property ("side-axis")));
275 me->programming_error ("side-axis not set.");
279 ADD_INTERFACE (Side_position_interface, "side-position-interface",
280 "Position a victim object (this one) next to other objects (the "
281 "support). The property @code{direction} signifies where to put the "
282 "victim object relative to the support (left or right, up or down?)\n\n "
283 "The routine also takes the size the staff into account if "
284 "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
293 "side-relative-direction "
294 "side-support-elements "