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"
16 #include "staff-symbol-referencer.hh"
17 #include "group-interface.hh"
20 Side_position::add_support (Score_element*me, Score_element*e)
22 Pointer_group_interface (me, "side-support-elements").add_element (e);
28 Side_position::get_direction (Score_element*me)
30 SCM d = me->get_elt_property ("direction");
32 return to_dir (d) ? to_dir (d) : DOWN;
34 Direction relative_dir = UP;
35 SCM reldir = me->get_elt_property ("side-relative-direction"); // should use a lambda.
38 relative_dir = to_dir (reldir);
41 SCM other_elt = me->get_elt_property ("direction-source");
42 Score_element * e = unsmob_element(other_elt);
45 return (Direction)(relative_dir * Side_position::get_direction (e));
52 Callback that does the aligning. Puts the element next to the support
55 MAKE_SCHEME_CALLBACK(Side_position,side_position,2);
57 Side_position::side_position (SCM element_smob, SCM axis)
59 Score_element *me = unsmob_element (element_smob);
60 Axis a = (Axis) gh_scm2int (axis);
62 Score_element *common = me->parent_l (a);
63 SCM support = me->get_elt_property ("side-support-elements");
64 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
66 Score_element * e = unsmob_element (gh_car (s));
68 common = common->common_refpoint (e, a);
72 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
75 Score_element * e = unsmob_element ( gh_car (s));
78 Real coord = e->relative_coordinate (common, a);
80 dim.unite (coord + e->extent (a));
89 Direction dir = Side_position::get_direction (me);
91 Real off = me->parent_l (a)->relative_coordinate (common, a);
92 SCM minimum = me->remove_elt_property ("minimum-space");
94 Real total_off = dim[dir] + off;
95 SCM padding = me->remove_elt_property ("padding");
96 if (gh_number_p (padding))
98 total_off += gh_scm2double (padding) * dir;
100 if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
102 total_off = gh_scm2double (minimum) * dir;
104 if (fabs (total_off) > 100 CM)
105 programming_error ("Huh ? Improbable staff side dim.");
107 return gh_double2scm (total_off);
111 callback that centers the element on itself
113 MAKE_SCHEME_CALLBACK(Side_position,aligned_on_self,2);
115 Side_position::aligned_on_self (SCM element_smob, SCM axis)
117 Score_element *me = unsmob_element (element_smob);
118 Axis a = (Axis) gh_scm2int (axis);
119 String s ("self-alignment-");
121 s += (a == X_AXIS) ? "X" : "Y";
123 SCM align (me->get_elt_property (s.ch_C()));
124 if (gh_number_p (align))
126 Interval ext(me->extent (a));
130 programming_error ("I'm empty. Can't align on self");
131 return gh_double2scm (0.0);
135 Real lambda = (0.5 - gh_scm2double (align) / 2.0);
136 return gh_double2scm (- (lambda * ext[LEFT] + (1 - lambda) * ext[RIGHT]));
139 else if (unsmob_element (align))
141 return gh_double2scm (- unsmob_element (align)->relative_coordinate (me, a));
143 return gh_double2scm (0.0);
149 directed_round (Real f, Direction d)
158 Callback that quantises in staff-spaces, rounding in the direction
159 of the elements "direction" elt property.
161 Only rounds when we're inside the staff, as determined by
162 Staff_symbol_referencer::staff_radius() */
163 MAKE_SCHEME_CALLBACK(Side_position,quantised_position,2);
165 Side_position::quantised_position (SCM element_smob, SCM )
167 Score_element *me = unsmob_element (element_smob);
170 Direction d = Side_position::get_direction (me);
172 if (Staff_symbol_referencer::has_interface (me))
174 Real p = Staff_symbol_referencer::position_f (me);
175 Real rp = directed_round (p, d);
176 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
179 if (abs (ip) < rad && Staff_symbol_referencer::on_staffline (me,ip))
185 return gh_double2scm ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
187 return gh_double2scm (0.0);
191 Position next to support, taking into account my own dimensions and padding.
193 MAKE_SCHEME_CALLBACK(Side_position,aligned_side,2);
195 Side_position::aligned_side (SCM element_smob, SCM axis)
197 Score_element *me = unsmob_element (element_smob);
198 Axis a = (Axis) gh_scm2int (axis);
200 Direction d = Side_position::get_direction (me);
201 Real o = gh_scm2double (side_position (element_smob,axis));
203 Interval iv = me->extent (a);
209 SCM pad = me->get_elt_property ("padding");
210 if (gh_number_p (pad))
211 o += d *gh_scm2double (pad) ;
213 return gh_double2scm (o);
217 Position centered on parent.
219 MAKE_SCHEME_CALLBACK(Side_position,centered_on_parent,2);
221 Side_position::centered_on_parent (SCM element_smob, SCM axis)
223 Score_element *me = unsmob_element (element_smob);
224 Axis a = (Axis) gh_scm2int (axis);
225 Score_element *him = me->parent_l (a);
227 return gh_double2scm (him->extent (a).center ());
232 Side_position::add_staff_support (Score_element*me)
234 Score_element* st = Staff_symbol_referencer::staff_symbol_l (me);
242 Side_position::set_axis (Score_element*me, Axis a)
244 if (!me->has_offset_callback_b (Side_position_aligned_side_proc, a))
245 me->add_offset_callback (Side_position_aligned_side_proc, a);
250 // ugh. doesn't cactch all variants.
252 Side_position::get_axis (Score_element*me)
254 if (me->has_offset_callback_b (Side_position_aligned_side_proc, X_AXIS)
255 || me->has_offset_callback_b (Side_position_aligned_side_proc , X_AXIS))
263 Side_position::set_direction (Score_element*me, Direction d)
265 me->set_elt_property ("direction", gh_int2scm (d));
269 Side_position::set_minimum_space (Score_element*me, Real m)
271 me->set_elt_property ("minimum-space", gh_double2scm (m));
275 Side_position::set_padding (Score_element*me, Real p)
277 me->set_elt_property ("padding", gh_double2scm (p));
281 Side_position::has_interface (Score_element*me)
283 return me->has_interface (ly_symbol2scm ("side-position-interface"));
287 Side_position::supported_b (Score_element*me)
289 SCM s = me->get_elt_property ("side-support-elements");