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_grob (me, ly_symbol2scm ("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->get_parent (a);
73 As this is only used as a callback, this is called only once. We
74 could wipe SIDE-SUPPORT-ELEMENTS after we retrieve it to conserve
75 memory; however -- we should look more into benefits of such actions?
77 The benefit is small, it seems: total GC times taken don't
78 differ. Would this also hamper Generational GC ?
81 SCM support = me->get_grob_property ("side-support-elements");
82 // me->remove_grob_property ("side-support-elements");
83 for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
85 Grob * e = unsmob_grob (ly_car (s));
87 common = common->common_refpoint (e, a);
91 for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
93 Grob * e = unsmob_grob (ly_car (s));
96 dim.unite (e->extent (common, a));
99 Real x = e->relative_coordinate (common, a);
100 dim.unite (Interval (x,x));
106 dim = Interval (0,0);
109 Direction dir = Side_position_interface::get_direction (me);
111 Real off = me->get_parent (a)->relative_coordinate (common, a);
112 SCM minimum = me->remove_grob_property ("minimum-space");
114 Real total_off = dim.linear_combination (dir) + off;
115 SCM padding = me->remove_grob_property ("padding");
116 if (gh_number_p (padding))
118 total_off += gh_scm2double (padding) * dir;
121 if (gh_number_p (minimum)
123 && total_off * dir < gh_scm2double (minimum))
125 total_off = gh_scm2double (minimum) * dir;
128 if (fabs (total_off) > 100 CM)
129 programming_error ("Huh ? Improbable staff side dim.");
131 return gh_double2scm (total_off);
137 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
139 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
141 Grob *me = unsmob_grob (smob);
142 Axis a = (Axis) gh_scm2int (axis);
144 return general_side_position (me, a, false);
149 callback that centers the element on itself
151 Requires that self-alignment-{X,Y} be set.
153 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_self,2);
155 Side_position_interface::aligned_on_self (SCM element_smob, SCM axis)
157 Grob *me = unsmob_grob (element_smob);
158 Axis a = (Axis) gh_scm2int (axis);
159 String s ("self-alignment-");
161 s += (a == X_AXIS) ? "X" : "Y";
163 SCM align (me->get_grob_property (s.ch_C ()));
164 if (gh_number_p (align))
166 Interval ext (me->extent (me,a));
170 programming_error ("I'm empty. Can't align on self");
171 return gh_double2scm (0.0);
175 return gh_double2scm (- ext.linear_combination (gh_scm2double (align)));
178 else if (unsmob_grob (align))
180 return gh_double2scm (- unsmob_grob (align)->relative_coordinate (me, a));
182 return gh_double2scm (0.0);
188 directed_round (Real f, Direction d)
197 Callback that quantises in staff-spaces, rounding in the direction
198 of the elements "direction" elt property.
200 Only rounds when we're inside the staff, as determined by
201 Staff_symbol_referencer::staff_radius () */
202 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
204 Side_position_interface::quantised_position (SCM element_smob, SCM)
206 Grob *me = unsmob_grob (element_smob);
209 Direction d = Side_position_interface::get_direction (me);
211 if (Staff_symbol_referencer::has_interface (me))
213 Real p = Staff_symbol_referencer::position_f (me);
214 Real rp = directed_round (p, d);
215 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
218 if (abs (ip) <= rad && Staff_symbol_referencer::on_staffline (me,ip))
224 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
226 return gh_double2scm (0.0);
230 Position next to support, taking into account my own dimensions and padding.
232 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
234 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
236 Grob *me = unsmob_grob (element_smob);
237 Axis a = (Axis) gh_scm2int (axis);
239 Direction d = Side_position_interface::get_direction (me);
241 Real o = gh_scm2double (aligned_on_support_extents (element_smob,axis));
243 Interval iv = me->extent (me, a);
249 programming_error ("Direction unknown, but aligned-side wanted.");
254 SCM pad = me->get_grob_property ("padding");
255 if (gh_number_p (pad))
256 o += d *gh_scm2double (pad) ;
258 return gh_double2scm (o);
262 Position centered on parent.
264 MAKE_SCHEME_CALLBACK (Side_position_interface,centered_on_parent,2);
266 Side_position_interface::centered_on_parent (SCM element_smob, SCM axis)
268 Grob *me = unsmob_grob (element_smob);
269 Axis a = (Axis) gh_scm2int (axis);
270 Grob *him = me->get_parent (a);
272 return gh_double2scm (him->extent (him,a).center ());
277 Side_position_interface::add_staff_support (Grob*me)
279 Grob* st = Staff_symbol_referencer::staff_symbol_l (me);
280 if (st && get_axis (me) == Y_AXIS)
287 Side_position_interface::set_axis (Grob*me, Axis a)
289 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
294 // ugh. doesn't cactch all variants.
296 Side_position_interface::get_axis (Grob*me)
298 if (me->has_offset_callback_b (Side_position_interface::aligned_side_proc, X_AXIS)
299 || me->has_offset_callback_b (Side_position_interface::aligned_side_proc , X_AXIS))
307 Side_position_interface::set_direction (Grob*me, Direction d)
309 me->set_grob_property ("direction", gh_int2scm (d));
313 Side_position_interface::set_minimum_space (Grob*me, Real m)
315 me->set_grob_property ("minimum-space", gh_double2scm (m));
319 Side_position_interface::set_padding (Grob*me, Real p)
321 me->set_grob_property ("padding", gh_double2scm (p));
325 Side_position_interface::has_interface (Grob*me)
327 return me->has_interface (ly_symbol2scm ("side-position-interface"));
331 Side_position_interface::supported_b (Grob*me)
333 SCM s = me->get_grob_property ("side-support-elements");
334 return gh_pair_p (s);