2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "side-position-interface.hh"
11 #include <math.h> // ceil.
14 #include "note-head.hh"
17 #include "dimensions.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 "string-convert.hh"
27 Side_position_interface::add_support (Grob *me, Grob *e)
29 Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
33 Side_position_interface::get_direction (Grob *me)
35 SCM d = me->get_property ("direction");
36 if (is_direction (d) && to_dir (d))
39 Direction relative_dir = Direction (1);
40 SCM reldir = me->get_property ("side-relative-direction"); // should use a lambda.
41 if (is_direction (reldir))
43 relative_dir = to_dir (reldir);
46 SCM other_elt = me->get_object ("direction-source");
47 Grob *e = unsmob_grob (other_elt);
50 return (Direction) (relative_dir * get_grob_direction (e));
56 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_extents, 2);
58 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
60 Grob *me = unsmob_grob (element_smob);
61 Axis a = (Axis) scm_to_int (axis);
63 return general_side_position (me, a, true);
66 /* Put the element next to the support, optionally taking in
67 account the extent of the support. */
69 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents)
71 Real ss = Staff_symbol_referencer::staff_space (me);
73 extract_grob_set (me, "side-support-elements", support);
75 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
76 Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
77 bool include_staff = (st
79 && scm_is_number (me->get_property ("staff-padding")));
84 common = st->common_refpoint (common, Y_AXIS);
85 dim = st->extent (common, Y_AXIS);
88 for (int i = 0; i < support.size (); i++)
93 dim.unite (e->extent (common, a));
96 Real x = e->relative_coordinate (common, a);
97 dim.unite (Interval (x, x));
102 dim = Interval (0, 0);
104 Direction dir = Side_position_interface::get_direction (me);
106 Real off = me->get_parent (a)->relative_coordinate (common, a);
107 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
109 Real total_off = dim.linear_combination (dir) - off;
110 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
112 if (minimum_space >= 0
114 && total_off * dir < minimum_space)
115 total_off = minimum_space * dir;
117 /* FIXME: 100CM should relate to paper size. */
118 if (fabs (total_off) > 100 CM)
121 = String_convert::form_string ("Improbable offset for grob %s: %f%s",
122 me->name ().to_str0 (), total_off,
125 programming_error (msg);
127 return scm_from_double (total_off);
133 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_refpoints, 2);
135 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
137 Grob *me = unsmob_grob (smob);
138 Axis a = (Axis) scm_to_int (axis);
140 return general_side_position (me, a, false);
144 directed_round (Real f, Direction d)
153 Callback that quantises in staff-spaces, rounding in the direction
154 of the elements "direction" elt property.
156 Only rounds when we're inside the staff, as determined by
157 Staff_symbol_referencer::staff_radius () */
158 MAKE_SCHEME_CALLBACK (Side_position_interface, quantised_position, 2);
160 Side_position_interface::quantised_position (SCM element_smob, SCM)
162 Grob *me = unsmob_grob (element_smob);
164 Direction d = Side_position_interface::get_direction (me);
166 Grob *stsym = Staff_symbol_referencer::get_staff_symbol (me);
169 Real p = Staff_symbol_referencer::get_position (me);
170 Real rp = directed_round (p, d);
171 Real rad = Staff_symbol_referencer::staff_radius (me) * 2;
174 Grob *head = me->get_parent (X_AXIS);
176 if (Staff_symbol_referencer::on_staffline (me, ip)
177 && ((abs (ip) <= rad)
178 || (Note_head::has_interface (head)
179 && sign (Staff_symbol_referencer::get_position (head))
186 return scm_from_double ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
188 return scm_from_double (0.0);
192 Position next to support, taking into account my own dimensions and padding.
194 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_side, 2);
196 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
198 Grob *me = unsmob_grob (element_smob);
199 Axis a = (Axis) scm_to_int (axis);
201 Direction d = Side_position_interface::get_direction (me);
203 Real o = scm_to_double (aligned_on_support_extents (element_smob, axis));
205 Interval iv = me->extent (me, a);
211 programming_error ("direction unknown, but aligned-side wanted");
218 Maintain a minimum distance to the staff. This is similar to side
219 position with padding, but it will put adjoining objects on a row if
220 stuff sticks out of the staff a little.
222 Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
223 if (st && a == Y_AXIS
224 && scm_is_number (me->get_property ("staff-padding")))
227 = Staff_symbol_referencer::staff_space (me)
228 * scm_to_double (me->get_property ("staff-padding"));
230 Grob *common = me->common_refpoint (st, Y_AXIS);
232 Interval staff_size = st->extent (common, Y_AXIS);
233 Real diff = d*staff_size[d] + padding - d * (o + iv[-d]);
234 o += d * max (diff, 0.0);
237 return scm_from_double (o);
241 Side_position_interface::set_axis (Grob *me, Axis a)
243 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
246 // ugh. doesn't catch all variants.
248 Side_position_interface::get_axis (Grob *me)
250 if (me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS)
251 || me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS))
257 ADD_INTERFACE (Side_position_interface, "side-position-interface",
258 "Position a victim object (this one) next to other objects (the "
259 "support). The property @code{direction} signifies where to put the "
260 "victim object relative to the support (left or right, up or down?)\n\n "
261 "The routine also takes the size the staff into account if "
262 "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
267 "side-relative-direction "
268 "side-support-elements "