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::add_element (me, "side-support-elements",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 dim.unite (e->extent (common, a));
86 Direction dir = Side_position::get_direction (me);
88 Real off = me->parent_l (a)->relative_coordinate (common, a);
89 SCM minimum = me->remove_elt_property ("minimum-space");
91 Real total_off = dim[dir] + off;
92 SCM padding = me->remove_elt_property ("padding");
93 if (gh_number_p (padding))
95 total_off += gh_scm2double (padding) * dir;
97 if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
99 total_off = gh_scm2double (minimum) * dir;
101 if (fabs (total_off) > 100 CM)
102 programming_error ("Huh ? Improbable staff side dim.");
104 return gh_double2scm (total_off);
108 callback that centers the element on itself
110 MAKE_SCHEME_CALLBACK(Side_position,aligned_on_self,2);
112 Side_position::aligned_on_self (SCM element_smob, SCM axis)
114 Score_element *me = unsmob_element (element_smob);
115 Axis a = (Axis) gh_scm2int (axis);
116 String s ("self-alignment-");
118 s += (a == X_AXIS) ? "X" : "Y";
120 SCM align (me->get_elt_property (s.ch_C()));
121 if (gh_number_p (align))
123 Interval ext(me->extent (me,a));
127 programming_error ("I'm empty. Can't align on self");
128 return gh_double2scm (0.0);
132 Real lambda = (0.5 - gh_scm2double (align) / 2.0);
133 return gh_double2scm (- (lambda * ext[LEFT] + (1 - lambda) * ext[RIGHT]));
136 else if (unsmob_element (align))
138 return gh_double2scm (- unsmob_element (align)->relative_coordinate (me, a));
140 return gh_double2scm (0.0);
146 directed_round (Real f, Direction d)
155 Callback that quantises in staff-spaces, rounding in the direction
156 of the elements "direction" elt property.
158 Only rounds when we're inside the staff, as determined by
159 Staff_symbol_referencer::staff_radius() */
160 MAKE_SCHEME_CALLBACK(Side_position,quantised_position,2);
162 Side_position::quantised_position (SCM element_smob, SCM )
164 Score_element *me = unsmob_element (element_smob);
167 Direction d = Side_position::get_direction (me);
169 if (Staff_symbol_referencer::has_interface (me))
171 Real p = Staff_symbol_referencer::position_f (me);
172 Real rp = directed_round (p, d);
173 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
176 if (abs (ip) < rad && Staff_symbol_referencer::on_staffline (me,ip))
182 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
184 return gh_double2scm (0.0);
188 Position next to support, taking into account my own dimensions and padding.
190 MAKE_SCHEME_CALLBACK(Side_position,aligned_side,2);
192 Side_position::aligned_side (SCM element_smob, SCM axis)
194 Score_element *me = unsmob_element (element_smob);
195 Axis a = (Axis) gh_scm2int (axis);
197 Direction d = Side_position::get_direction (me);
198 Real o = gh_scm2double (side_position (element_smob,axis));
200 Interval iv = me->extent (me, a);
206 SCM pad = me->get_elt_property ("padding");
207 if (gh_number_p (pad))
208 o += d *gh_scm2double (pad) ;
210 return gh_double2scm (o);
214 Position centered on parent.
216 MAKE_SCHEME_CALLBACK(Side_position,centered_on_parent,2);
218 Side_position::centered_on_parent (SCM element_smob, SCM axis)
220 Score_element *me = unsmob_element (element_smob);
221 Axis a = (Axis) gh_scm2int (axis);
222 Score_element *him = me->parent_l (a);
224 return gh_double2scm (him->extent (him,a).center ());
229 Side_position::add_staff_support (Score_element*me)
231 Score_element* st = Staff_symbol_referencer::staff_symbol_l (me);
239 Side_position::set_axis (Score_element*me, Axis a)
241 me->add_offset_callback (Side_position::aligned_side_proc, a);
246 // ugh. doesn't cactch all variants.
248 Side_position::get_axis (Score_element*me)
250 if (me->has_offset_callback_b (Side_position::aligned_side_proc, X_AXIS)
251 || me->has_offset_callback_b (Side_position::aligned_side_proc , X_AXIS))
259 Side_position::set_direction (Score_element*me, Direction d)
261 me->set_elt_property ("direction", gh_int2scm (d));
265 Side_position::set_minimum_space (Score_element*me, Real m)
267 me->set_elt_property ("minimum-space", gh_double2scm (m));
271 Side_position::set_padding (Score_element*me, Real p)
273 me->set_elt_property ("padding", gh_double2scm (p));
277 Side_position::has_interface (Score_element*me)
279 return me->has_interface (ly_symbol2scm ("side-position-interface"));
283 Side_position::supported_b (Score_element*me)
285 SCM s = me->get_elt_property ("side-support-elements");