2 staff-side.cc -- implement Staff_side_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 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"
20 Side_position_interface::add_support (Grob*me, Grob*e)
22 Pointer_group_interface::add_element (me, "side-support-elements",e);
28 Side_position_interface::get_direction (Grob*me)
30 SCM d = me->get_grob_property ("direction");
31 if (isdir_b (d) && to_dir (d))
34 Direction relative_dir = Direction (1);
35 SCM reldir = me->get_grob_property ("side-relative-direction"); // should use a lambda.
38 relative_dir = to_dir (reldir);
41 SCM other_elt = me->get_grob_property ("direction-source");
42 Grob * e = unsmob_grob (other_elt);
45 return (Direction) (relative_dir * Directional_element_interface::get (e));
52 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
54 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
56 Grob *me = unsmob_grob (element_smob);
57 Axis a = (Axis) gh_scm2int (axis);
59 return general_side_position (me, a, true);
64 Puts the element next to the support, optionally taking in
65 account the extent of the support.
68 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
70 Grob *common = me->parent_l (a);
71 SCM support = me->get_grob_property ("side-support-elements");
72 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
74 Grob * e = unsmob_grob (gh_car (s));
76 common = common->common_refpoint (e, a);
80 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
82 Grob * e = unsmob_grob (gh_car (s));
85 dim.unite (e->extent (common, a));
88 Real x = e->relative_coordinate (common, a);
89 dim.unite (Interval (x,x));
98 Direction dir = Side_position_interface::get_direction (me);
100 Real off = me->parent_l (a)->relative_coordinate (common, a);
101 SCM minimum = me->remove_grob_property ("minimum-space");
103 Real total_off = dim.linear_combination (dir) + off;
104 SCM padding = me->remove_grob_property ("padding");
105 if (gh_number_p (padding))
107 total_off += gh_scm2double (padding) * dir;
110 if (gh_number_p (minimum)
112 && total_off * dir < gh_scm2double (minimum))
114 total_off = gh_scm2double (minimum) * dir;
117 if (fabs (total_off) > 100 CM)
118 programming_error ("Huh ? Improbable staff side dim.");
120 return gh_double2scm (total_off);
126 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
128 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
130 Grob *me = unsmob_grob (smob);
131 Axis a = (Axis) gh_scm2int (axis);
133 return general_side_position (me, a, false);
138 callback that centers the element on itself
140 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_self,2);
142 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
144 Grob *me = unsmob_grob (element_smob);
145 Axis a = (Axis) gh_scm2int (axis);
146 String s ("self-alignment-");
148 s += (a == X_AXIS) ? "X" : "Y";
150 SCM align (me->get_grob_property (s.ch_C ()));
151 if (gh_number_p (align))
153 Interval ext (me->extent (me,a));
157 programming_error ("I'm empty. Can't align on self");
158 return gh_double2scm (0.0);
162 return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
165 else if (unsmob_grob (align))
167 return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me, a));
169 return gh_double2scm (0.0);
175 directed_round (Real f, Direction d)
184 Callback that quantises in staff-spaces, rounding in the direction
185 of the elements "direction" elt property.
187 Only rounds when we're inside the staff, as determined by
188 Staff_symbol_referencer::staff_radius () */
189 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
191 Side_position_interface::quantised_position (SCM element_smob, SCM)
193 Grob *me = unsmob_grob (element_smob);
196 Direction d = Side_position_interface::get_direction (me);
198 if (Staff_symbol_referencer::has_interface (me))
200 Real p = Staff_symbol_referencer::position_f (me);
201 Real rp = directed_round (p, d);
202 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
205 if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
211 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
213 return gh_double2scm (0.0);
217 Position next to support, taking into account my own dimensions and padding.
219 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
221 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
223 Grob *me = unsmob_grob (element_smob);
224 Axis a = (Axis) gh_scm2int (axis);
226 Direction d = Side_position_interface::get_direction (me);
228 Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
230 Interval iv = me->extent (me, a);
236 programming_error ("Direction unknown, but aligned-side wanted.");
241 SCM pad = me->get_grob_property ("padding");
242 if (gh_number_p (pad))
243 o += d *gh_scm2double (pad) ;
245 return gh_double2scm (o);
249 Position centered on parent.
251 MAKE_SCHEME_CALLBACK (Side_position_interface,centered_on_parent,2);
253 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
255 Grob *me = unsmob_grob (element_smob);
256 Axis a = (Axis) gh_scm2int (axis);
257 Grob *him = me->parent_l (a);
259 return gh_double2scm (him->extent (him,a).center ());
264 Side_position_interface::add_staff_support (Grob*me)
266 Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
274 Side_position_interface::set_axis (Grob*me, Axis a)
276 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
281 // ugh. doesn't cactch all variants.
283 Side_position_interface::get_axis (Grob*me)
285 if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
286 || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
294 Side_position_interface::set_direction (Grob*me, Direction d)
296 me->set_grob_property ("direction", gh_int2scm (d));
300 Side_position_interface::set_minimum_space (Grob*me, Real m)
302 me->set_grob_property ("minimum-space", gh_double2scm (m));
306 Side_position_interface::set_padding (Grob*me, Real p)
308 me->set_grob_property ("padding", gh_double2scm (p));
312 Side_position_interface::has_interface (Grob*me)
314 return me->has_interface (ly_symbol2scm ("side-position-interface"));
318 Side_position_interface::supported_b (Grob*me)
320 SCM s = me->get_grob_property ("side-support-elements");
321 return gh_pair_p (s);