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"
19 Side_position_interface::add_support (Grob*me, Grob*e)
21 Pointer_group_interface::add_element (me, "side-support-elements",e);
27 Side_position_interface::get_direction (Grob*me)
29 SCM d = me->get_grob_property ("direction");
30 if (isdir_b (d) && to_dir (d))
33 Direction relative_dir = Direction (1);
34 SCM reldir = me->get_grob_property ("side-relative-direction"); // should use a lambda.
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 * Side_position_interface::get_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 Grob *common = me->parent_l (a);
70 SCM support = me->get_grob_property ("side-support-elements");
71 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
73 Grob * e = unsmob_grob (gh_car (s));
75 common = common->common_refpoint (e, a);
79 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
81 Grob * e = unsmob_grob ( gh_car (s));
84 dim.unite (e->extent (common, a));
87 Real x = e->relative_coordinate (common, a);
88 dim.unite (Interval (x,x));
97 Direction dir = Side_position_interface::get_direction (me);
99 Real off = me->parent_l (a)->relative_coordinate (common, a);
100 SCM minimum = me->remove_grob_property ("minimum-space");
102 Real total_off = dim.linear_combination (dir) + off;
103 SCM padding = me->remove_grob_property ("padding");
104 if (gh_number_p (padding))
106 total_off += gh_scm2double (padding) * dir;
109 if (gh_number_p (minimum)
111 && total_off * dir < gh_scm2double (minimum))
113 total_off = gh_scm2double (minimum) * dir;
116 if (fabs (total_off) > 100 CM)
117 programming_error ("Huh ? Improbable staff side dim.");
119 return gh_double2scm (total_off);
125 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_support_refpoints,2);
127 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
129 Grob *me = unsmob_grob (smob);
130 Axis a = (Axis) gh_scm2int (axis);
132 return general_side_position (me, a, false);
137 callback that centers the element on itself
139 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_on_self,2);
141 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
143 Grob *me = unsmob_grob (element_smob);
144 Axis a = (Axis) gh_scm2int (axis);
145 String s ("self-alignment-");
147 s += (a == X_AXIS) ? "X" : "Y";
149 SCM align (me->get_grob_property (s.ch_C()));
150 if (gh_number_p (align))
152 Interval ext(me->extent (me,a));
156 programming_error ("I'm empty. Can't align on self");
157 return gh_double2scm (0.0);
161 return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
164 else if (unsmob_grob (align))
166 return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me, a));
168 return gh_double2scm (0.0);
174 directed_round (Real f, Direction d)
183 Callback that quantises in staff-spaces, rounding in the direction
184 of the elements "direction" elt property.
186 Only rounds when we're inside the staff, as determined by
187 Staff_symbol_referencer::staff_radius() */
188 MAKE_SCHEME_CALLBACK(Side_position_interface,quantised_position,2);
190 Side_position_interface::quantised_position (SCM element_smob, SCM )
192 Grob *me = unsmob_grob (element_smob);
195 Direction d = Side_position_interface::get_direction (me);
197 if (Staff_symbol_referencer::has_interface (me))
199 Real p = Staff_symbol_referencer::position_f (me);
200 Real rp = directed_round (p, d);
201 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
204 if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
210 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
212 return gh_double2scm (0.0);
216 Position next to support, taking into account my own dimensions and padding.
218 MAKE_SCHEME_CALLBACK(Side_position_interface,aligned_side,2);
220 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
222 Grob *me = unsmob_grob (element_smob);
223 Axis a = (Axis) gh_scm2int (axis);
225 Direction d = Side_position_interface::get_direction (me);
226 Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
228 Interval iv = me->extent (me, a);
234 SCM pad = me->get_grob_property ("padding");
235 if (gh_number_p (pad))
236 o += d *gh_scm2double (pad) ;
238 return gh_double2scm (o);
242 Position centered on parent.
244 MAKE_SCHEME_CALLBACK(Side_position_interface,centered_on_parent,2);
246 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
248 Grob *me = unsmob_grob (element_smob);
249 Axis a = (Axis) gh_scm2int (axis);
250 Grob *him = me->parent_l (a);
252 return gh_double2scm (him->extent (him,a).center ());
257 Side_position_interface::add_staff_support (Grob*me)
259 Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
267 Side_position_interface::set_axis (Grob*me, Axis a)
269 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
274 // ugh. doesn't cactch all variants.
276 Side_position_interface::get_axis (Grob*me)
278 if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
279 || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
287 Side_position_interface::set_direction (Grob*me, Direction d)
289 me->set_grob_property ("direction", gh_int2scm (d));
293 Side_position_interface::set_minimum_space (Grob*me, Real m)
295 me->set_grob_property ("minimum-space", gh_double2scm (m));
299 Side_position_interface::set_padding (Grob*me, Real p)
301 me->set_grob_property ("padding", gh_double2scm (p));
305 Side_position_interface::has_interface (Grob*me)
307 return me->has_interface (ly_symbol2scm ("side-position-interface"));
311 Side_position_interface::supported_b (Grob*me)
313 SCM s = me->get_grob_property ("side-support-elements");