2 staff-side.cc -- implement Staff_side_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 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::add_support (Score_element*me, Score_element*e)
21 Pointer_group_interface (me, "side-support-elements").add_element (e);
27 Side_position::get_direction (Score_element*me)
29 SCM d = me->get_elt_property ("direction");
31 return to_dir (d) ? to_dir (d) : DOWN;
33 Direction relative_dir = UP;
34 SCM reldir = me->get_elt_property ("side-relative-direction"); // should use a lambda.
37 relative_dir = to_dir (reldir);
40 SCM other_elt = me->get_elt_property ("direction-source");
41 Score_element * e = unsmob_element(other_elt);
44 return (Direction)(relative_dir * Side_position::get_direction (e));
51 Callback that does the aligning. Puts the element next to the support
54 MAKE_SCHEME_CALLBACK(Side_position,side_position,2);
56 Side_position::side_position (SCM element_smob, SCM axis)
58 Score_element *me = unsmob_element (element_smob);
59 Axis a = (Axis) gh_scm2int (axis);
61 Score_element *common = me->parent_l (a);
62 SCM support = me->get_elt_property ("side-support-elements");
63 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
65 Score_element * e = unsmob_element (gh_car (s));
67 common = common->common_refpoint (e, a);
71 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
74 Score_element * e = unsmob_element ( gh_car (s));
77 Real coord = e->relative_coordinate (common, a);
79 dim.unite (coord + e->extent (a));
88 Direction dir = Side_position::get_direction (me);
90 Real off = me->parent_l (a)->relative_coordinate (common, a);
91 SCM minimum = me->remove_elt_property ("minimum-space");
93 Real total_off = dim[dir] + off;
94 SCM padding = me->remove_elt_property ("padding");
95 if (gh_number_p (padding))
97 total_off += gh_scm2double (padding) * dir;
99 if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
101 total_off = gh_scm2double (minimum) * dir;
103 if (fabs (total_off) > 100 CM)
104 programming_error ("Huh ? Improbable staff side dim.");
106 return gh_double2scm (total_off);
110 callback that centers the element on itself
112 MAKE_SCHEME_CALLBACK(Side_position,aligned_on_self,2);
114 Side_position::aligned_on_self (SCM element_smob, SCM axis)
116 Score_element *me = unsmob_element (element_smob);
117 Axis a = (Axis) gh_scm2int (axis);
118 String s ("self-alignment-");
120 s += (a == X_AXIS) ? "X" : "Y";
122 SCM align (me->get_elt_property (s.ch_C()));
123 if (gh_number_p (align))
125 Interval ext(me->extent (a));
129 programming_error ("I'm empty. Can't align on self");
130 return gh_double2scm (0.0);
134 Real lambda = (0.5 - gh_scm2double (align) / 2.0);
135 return gh_double2scm (- (lambda * ext[LEFT] + (1 - lambda) * ext[RIGHT]));
138 else if (unsmob_element (align))
140 return gh_double2scm (- unsmob_element (align)->relative_coordinate (me, a));
142 return gh_double2scm (0.0);
148 directed_round (Real f, Direction d)
157 Callback that quantises in staff-spaces, rounding in the direction
158 of the elements "direction" elt property.
160 Only rounds when we're inside the staff, as determined by
161 Staff_symbol_referencer::staff_radius() */
162 MAKE_SCHEME_CALLBACK(Side_position,quantised_position,2);
164 Side_position::quantised_position (SCM element_smob, SCM )
166 Score_element *me = unsmob_element (element_smob);
169 Direction d = Side_position::get_direction (me);
171 if (Staff_symbol_referencer::has_interface (me))
173 Real p = Staff_symbol_referencer::position_f (me);
174 Real rp = directed_round (p, d);
175 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
178 if (abs (ip) < rad && Staff_symbol_referencer::on_staffline (me,ip))
184 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
186 return gh_double2scm (0.0);
190 Position next to support, taking into account my own dimensions and padding.
192 MAKE_SCHEME_CALLBACK(Side_position,aligned_side,2);
194 Side_position::aligned_side (SCM element_smob, SCM axis)
196 Score_element *me = unsmob_element (element_smob);
197 Axis a = (Axis) gh_scm2int (axis);
199 Direction d = Side_position::get_direction (me);
200 Real o = gh_scm2double (side_position (element_smob,axis));
202 Interval iv = me->extent (a);
208 SCM pad = me->get_elt_property ("padding");
209 if (gh_number_p (pad))
210 o += d *gh_scm2double (pad) ;
212 return gh_double2scm (o);
216 Position centered on parent.
218 MAKE_SCHEME_CALLBACK(Side_position,centered_on_parent,2);
220 Side_position::centered_on_parent (SCM element_smob, SCM axis)
222 Score_element *me = unsmob_element (element_smob);
223 Axis a = (Axis) gh_scm2int (axis);
224 Score_element *him = me->parent_l (a);
226 return gh_double2scm (him->extent (a).center ());
231 Side_position::add_staff_support (Score_element*me)
233 Score_element* st = Staff_symbol_referencer::staff_symbol_l (me);
241 Side_position::set_axis (Score_element*me, Axis a)
243 me->add_offset_callback (Side_position::aligned_side_proc, a);
248 // ugh. doesn't cactch all variants.
250 Side_position::get_axis (Score_element*me)
252 if (me->has_offset_callback_b (Side_position::aligned_side_proc, X_AXIS)
253 || me->has_offset_callback_b (Side_position::aligned_side_proc , X_AXIS))
261 Side_position::set_direction (Score_element*me, Direction d)
263 me->set_elt_property ("direction", gh_int2scm (d));
267 Side_position::set_minimum_space (Score_element*me, Real m)
269 me->set_elt_property ("minimum-space", gh_double2scm (m));
273 Side_position::set_padding (Score_element*me, Real p)
275 me->set_elt_property ("padding", gh_double2scm (p));
279 Side_position::has_interface (Score_element*me)
281 return me->has_interface (ly_symbol2scm ("side-position-interface"));
285 Side_position::supported_b (Score_element*me)
287 SCM s = me->get_elt_property ("side-support-elements");