2 staff-side.cc -- implement Staff_side_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include <math.h> // ceil.
11 #include "side-position-interface.hh"
14 #include "dimensions.hh"
15 #include "staff-symbol-referencer.hh"
16 #include "group-interface.hh"
17 #include "directional-element-interface.hh"
18 #include "staff-symbol-referencer.hh"
21 Side_position_interface::add_support (Grob*me, Grob*e)
23 Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
27 Side_position_interface::get_direction (Grob*me)
29 SCM d = me->get_grob_property ("direction");
30 if (is_direction (d) && to_dir (d))
33 Direction relative_dir = Direction (1);
34 SCM reldir = me->get_grob_property ("side-relative-direction"); // should use a lambda.
35 if (is_direction (reldir))
37 relative_dir = to_dir (reldir);
40 SCM other_elt = me->get_grob_property ("direction-source");
41 Grob * e = unsmob_grob (other_elt);
44 return (Direction) (relative_dir * get_grob_direction (e));
51 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
53 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
55 Grob *me = unsmob_grob (element_smob);
56 Axis a = (Axis) gh_scm2int (axis);
58 return general_side_position (me, a, true);
63 Puts the element next to the support, optionally taking in
64 account the extent of the support.
67 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
69 Real ss = Staff_symbol_referencer::staff_space (me);
70 SCM support = me->get_grob_property ("side-support-elements");
71 Grob *common = common_refpoint_of_list (support, me->get_parent (a), a);
74 for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
76 Grob * e = unsmob_grob (ly_car (s));
79 dim.unite (e->extent (common, a));
82 Real x = e->relative_coordinate (common, a);
83 dim.unite (Interval (x,x));
92 Direction dir = Side_position_interface::get_direction (me);
94 Real off = me->get_parent (a)->relative_coordinate (common, a);
95 Real minimum_space = ss * robust_scm2double (me->get_grob_property ("minimum-space"), -1);
97 Real total_off = dim.linear_combination (dir) - off;
98 total_off += dir * ss * robust_scm2double (me->get_grob_property ("padding"), 0);
100 if (minimum_space >= 0
102 && total_off * dir < minimum_space)
104 total_off = minimum_space * dir;
107 if (fabs (total_off) > 100 CM)
108 programming_error ("Huh ? Improbable staff side dim.");
110 return gh_double2scm (total_off);
116 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
118 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
120 Grob *me = unsmob_grob (smob);
121 Axis a = (Axis) gh_scm2int (axis);
123 return general_side_position (me, a, false);
130 directed_round (Real f, Direction d)
139 Callback that quantises in staff-spaces, rounding in the direction
140 of the elements "direction" elt property.
142 Only rounds when we're inside the staff, as determined by
143 Staff_symbol_referencer::staff_radius () */
144 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
146 Side_position_interface::quantised_position (SCM element_smob, SCM)
148 Grob *me = unsmob_grob (element_smob);
150 Direction d = Side_position_interface::get_direction (me);
152 Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
155 Real p = Staff_symbol_referencer::get_position (me);
156 Real rp = directed_round (p, d);
157 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
160 if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
166 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
168 return gh_double2scm (0.0);
172 Position next to support, taking into account my own dimensions and padding.
174 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
176 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
178 Grob *me = unsmob_grob (element_smob);
179 Axis a = (Axis) gh_scm2int (axis);
181 Direction d = Side_position_interface::get_direction (me);
183 Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
185 Interval iv = me->extent (me, a);
191 programming_error ("Direction unknown, but aligned-side wanted.");
196 return gh_double2scm (o);
200 Maintain a minimum distance to the staff. This is similar to side
201 position with padding, but it will put adjoining objects on a row if
202 stuff sticks out of the staff a little.
204 MAKE_SCHEME_CALLBACK (Side_position_interface,out_of_staff,2);
206 Side_position_interface::out_of_staff (SCM element_smob, SCM axis)
208 Grob *me = unsmob_grob (element_smob);
209 Axis a = (Axis) gh_scm2int (axis);
211 Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
214 return gh_int2scm (0);
216 SCM scm_padding = me->get_grob_property ("staff-padding");
217 if (!gh_number_p (scm_padding))
218 return gh_int2scm (0);
221 Staff_symbol_referencer::staff_space (me)
222 * gh_scm2double (scm_padding);
224 Grob *common = me->common_refpoint (st, Y_AXIS);
225 Direction d = Side_position_interface::get_direction (me);
226 Interval staff_size = st->extent (common, Y_AXIS);
227 Interval me_ext = me->extent (common, a);
228 Real diff = d*staff_size[d] + padding - d*me_ext[-d];
229 return gh_double2scm (d* (diff >? 0));
233 Side_position_interface::add_staff_support (Grob*me)
235 Grob* st = Staff_symbol_referencer::get_staff_symbol (me);
236 if (st && get_axis (me) == Y_AXIS)
243 Side_position_interface::set_axis (Grob*me, Axis a)
245 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
250 // ugh. doesn't cactch all variants.
252 Side_position_interface::get_axis (Grob*me)
254 if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
255 || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
263 Side_position_interface::set_direction (Grob*me, Direction d)
265 me->set_grob_property ("direction", gh_int2scm (d));
269 Side_position_interface::set_minimum_space (Grob*me, Real m)
271 me->set_grob_property ("minimum-space", gh_double2scm (m));
275 Side_position_interface::set_padding (Grob*me, Real p)
277 me->set_grob_property ("padding", gh_double2scm (p));
282 Side_position_interface::supported_b (Grob*me)
284 SCM s = me->get_grob_property ("side-support-elements");
285 return gh_pair_p (s);
291 ADD_INTERFACE (Side_position_interface,"side-position-interface",
292 "Position a victim object (this one) next to other objects (the "
293 "support). In this case, the property @code{direction} signifies where to put the "
294 "victim object relative to the support (left or right, up or down?)\n\n "
295 "The @code{out_of_staff} routine puts objects at a distance of the staff. If the property "
296 "@code{staff-padding} is not defined, the routine doesn't do anything."
298 "staff-padding side-support-elements direction-source "
299 "direction side-relative-direction minimum-space padding");