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.");
113 return gh_double2scm (total_off);
119 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
121 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
123 Grob *me = unsmob_grob (smob);
124 Axis a = (Axis) gh_scm2int (axis);
126 return general_side_position (me, a, false);
133 directed_round (Real f, Direction d)
142 Callback that quantises in staff-spaces, rounding in the direction
143 of the elements "direction" elt property.
145 Only rounds when we're inside the staff, as determined by
146 Staff_symbol_referencer::staff_radius () */
147 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
149 Side_position_interface::quantised_position (SCM element_smob, SCM)
151 Grob *me = unsmob_grob (element_smob);
153 Direction d = Side_position_interface::get_direction (me);
155 Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
158 Real p = Staff_symbol_referencer::get_position (me);
159 Real rp = directed_round (p, d);
160 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
163 if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
169 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
171 return gh_double2scm (0.0);
175 Position next to support, taking into account my own dimensions and padding.
177 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
179 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
181 Grob *me = unsmob_grob (element_smob);
182 Axis a = (Axis) gh_scm2int (axis);
184 Direction d = Side_position_interface::get_direction (me);
186 Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
188 Interval iv = me->extent (me, a);
194 programming_error ("Direction unknown, but aligned-side wanted.");
201 Maintain a minimum distance to the staff. This is similar to side
202 position with padding, but it will put adjoining objects on a row if
203 stuff sticks out of the staff a little.
205 Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
206 if (st && a == Y_AXIS
207 && gh_number_p (me->get_grob_property ("staff-padding")))
210 Staff_symbol_referencer::staff_space (me)
211 * gh_scm2double (me->get_grob_property ("staff-padding"));
213 Grob *common = me->common_refpoint (st, Y_AXIS);
215 Interval staff_size = st->extent (common, Y_AXIS);
216 Interval me_ext = me->extent (common, a);
217 Real diff = d*staff_size[d] + padding - d*(o + iv[-d]);
218 o += (d* (diff >? 0));
221 return gh_double2scm (o);
226 Side_position_interface::set_axis (Grob*me, Axis a)
228 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
233 // ugh. doesn't cactch all variants.
235 Side_position_interface::get_axis (Grob*me)
237 if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
238 || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
248 ADD_INTERFACE (Side_position_interface,"side-position-interface",
249 "Position a victim object (this one) next to other objects (the "
250 "support). In this case, the property @code{direction} signifies where to put the "
251 "victim object relative to the support (left or right, up or down?)\n\n "
252 "The routine puts objects at a distance of the staff if the property "
253 "@code{staff-padding} is defined. If undefined, the staff symbol is ignored."
255 "staff-padding side-support-elements direction-source "
256 "direction side-relative-direction minimum-space padding");