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.
15 #include "note-head.hh"
18 #include "dimensions.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "group-interface.hh"
21 #include "directional-element-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "string-convert.hh"
28 Side_position_interface::add_support (Grob *me, Grob *e)
30 Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
34 Side_position_interface::get_direction (Grob *me)
36 SCM d = me->get_property ("direction");
37 if (is_direction (d) && to_dir (d))
40 Direction relative_dir = Direction (1);
41 SCM reldir = me->get_property ("side-relative-direction"); // should use a lambda.
42 if (is_direction (reldir))
44 relative_dir = to_dir (reldir);
47 SCM other_elt = me->get_property ("direction-source");
48 Grob *e = unsmob_grob (other_elt);
51 return (Direction) (relative_dir * get_grob_direction (e));
57 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_extents, 2);
59 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
61 Grob *me = unsmob_grob (element_smob);
62 Axis a = (Axis) scm_to_int (axis);
64 return general_side_position (me, a, true);
67 /* Put the element next to the support, optionally taking in
68 account the extent of the support. */
70 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents)
72 Real ss = Staff_symbol_referencer::staff_space (me);
73 SCM support = me->get_property ("side-support-elements");
74 Grob *common = common_refpoint_of_list (support, me->get_parent (a), a);
75 Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
76 bool include_staff = (st
78 && scm_is_number (me->get_property ("staff-padding")));
83 common = st->common_refpoint (common, Y_AXIS);
84 dim = st->extent (common, Y_AXIS);
87 for (SCM s = support; s != SCM_EOL; s = scm_cdr (s))
89 Grob *e = unsmob_grob (scm_car (s));
92 dim.unite (e->extent (common, a));
95 Real x = e->relative_coordinate (common, a);
96 dim.unite (Interval (x, x));
101 dim = Interval (0, 0);
103 Direction dir = Side_position_interface::get_direction (me);
105 Real off = me->get_parent (a)->relative_coordinate (common, a);
106 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
108 Real total_off = dim.linear_combination (dir) - off;
109 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
111 if (minimum_space >= 0
113 && total_off * dir < minimum_space)
114 total_off = minimum_space * dir;
116 /* FIXME: 100CM should relate to paper size. */
117 if (fabs (total_off) > 100 CM)
120 = String_convert::form_string ("Improbable offset for grob %s: %f%s",
121 me->name ().to_str0 (), total_off,
124 programming_error (msg);
126 return scm_make_real (total_off);
132 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_on_support_refpoints, 2);
134 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
136 Grob *me = unsmob_grob (smob);
137 Axis a = (Axis) scm_to_int (axis);
139 return general_side_position (me, a, false);
143 directed_round (Real f, Direction d)
152 Callback that quantises in staff-spaces, rounding in the direction
153 of the elements "direction" elt property.
155 Only rounds when we're inside the staff, as determined by
156 Staff_symbol_referencer::staff_radius () */
157 MAKE_SCHEME_CALLBACK (Side_position_interface, quantised_position, 2);
159 Side_position_interface::quantised_position (SCM element_smob, SCM)
161 Grob *me = unsmob_grob (element_smob);
163 Direction d = Side_position_interface::get_direction (me);
165 Grob *stsym = Staff_symbol_referencer::get_staff_symbol (me);
168 Real p = Staff_symbol_referencer::get_position (me);
169 Real rp = directed_round (p, d);
170 Real rad = Staff_symbol_referencer::staff_radius (me) * 2;
173 Grob *head = me->get_parent (X_AXIS);
175 if (Staff_symbol_referencer::on_staffline (me, ip)
176 && ((abs (ip) <= rad)
177 || (Note_head::has_interface (head)
178 && sign (Staff_symbol_referencer::get_position (head))
185 return scm_make_real ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
187 return scm_make_real (0.0);
191 Position next to support, taking into account my own dimensions and padding.
193 MAKE_SCHEME_CALLBACK (Side_position_interface, aligned_side, 2);
195 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
197 Grob *me = unsmob_grob (element_smob);
198 Axis a = (Axis) scm_to_int (axis);
200 Direction d = Side_position_interface::get_direction (me);
202 Real o = scm_to_double (aligned_on_support_extents (element_smob, axis));
204 Interval iv = me->extent (me, a);
210 programming_error ("direction unknown, but aligned-side wanted");
217 Maintain a minimum distance to the staff. This is similar to side
218 position with padding, but it will put adjoining objects on a row if
219 stuff sticks out of the staff a little.
221 Grob *st = Staff_symbol_referencer::get_staff_symbol (me);
222 if (st && a == Y_AXIS
223 && scm_is_number (me->get_property ("staff-padding")))
226 = Staff_symbol_referencer::staff_space (me)
227 * scm_to_double (me->get_property ("staff-padding"));
229 Grob *common = me->common_refpoint (st, Y_AXIS);
231 Interval staff_size = st->extent (common, Y_AXIS);
232 Real diff = d*staff_size[d] + padding - d * (o + iv[-d]);
233 o += d * max (diff, 0.0);
236 return scm_make_real (o);
240 Side_position_interface::set_axis (Grob *me, Axis a)
242 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
245 // ugh. doesn't catch all variants.
247 Side_position_interface::get_axis (Grob *me)
249 if (me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS)
250 || me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS))
256 ADD_INTERFACE (Side_position_interface, "side-position-interface",
257 "Position a victim object (this one) next to other objects (the "
258 "support). The property @code{direction} signifies where to put the "
259 "victim object relative to the support (left or right, up or down?)\n\n "
260 "The routine also takes the size the staff into account if "
261 "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
262 "staff-padding side-support-elements direction-source "
263 "direction side-relative-direction minimum-space padding");