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"
12 #include "staff-symbol.hh"
15 #include "dimensions.hh"
16 #include "dimension-cache.hh"
17 #include "staff-symbol-referencer.hh"
19 Side_position_interface::Side_position_interface (Score_element const *e)
21 elt_l_ = (Score_element*)e;
25 Side_position_interface::add_support (Score_element*e)
27 SCM sup = elt_l_->get_elt_property ("side-support");
28 elt_l_->set_elt_property ("side-support",
29 gh_cons (e->self_scm_,sup));
35 Side_position_interface::get_direction () const
37 SCM d = elt_l_->get_elt_property ("direction");
39 return to_dir (d) ? to_dir (d) : DOWN;
41 Direction relative_dir = UP;
42 SCM reldir = elt_l_->get_elt_property ("side-relative-direction"); // should use a lambda.
45 relative_dir = to_dir (reldir);
48 SCM other_elt = elt_l_->get_elt_property ("direction-source");
49 Score_element * e = unsmob_element(other_elt);
52 return (Direction)(relative_dir * Side_position_interface (e).get_direction ());
59 Callback that does the aligning. Puts the element next to the support
63 Side_position_interface::side_position (Score_element const *cme, Axis axis)
65 Score_element* me = (Score_element*)cme;
66 Score_element *common = me->parent_l (axis);
67 SCM support = me->get_elt_property ("side-support");
68 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
70 Score_element * e = unsmob_element (gh_car (s));
72 common = common->common_refpoint (e, axis);
76 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
79 Score_element * e = unsmob_element ( gh_car (s));
82 Real coord = e->relative_coordinate (common, axis);
84 dim.unite (coord + e->extent (axis));
93 Direction dir = Side_position_interface (me).get_direction ();
95 Real off = me->parent_l (axis)->relative_coordinate (common, axis);
96 SCM minimum = me->remove_elt_property ("minimum-space");
98 Real total_off = dim[dir] + off;
99 SCM padding = me->remove_elt_property ("padding");
100 if (gh_number_p (padding))
102 total_off += gh_scm2double (padding) * dir;
104 if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
106 total_off = gh_scm2double (minimum) * dir;
108 if (fabs (total_off) > 100 CM)
109 programming_error ("Huh ? Improbable staff side dim.");
115 callback that centers the element on itself
118 Side_position_interface::aligned_on_self (Score_element const*elm, Axis ax)
120 String s ("self-alignment-");
122 s += (ax == X_AXIS) ? "X" : "Y";
124 SCM align (elm->get_elt_property (s));
127 Direction d = to_dir (align);
128 Interval ext(elm->extent (ax));
132 programming_error ("I'm empty. Can't align on self");
139 return - ext.center ();
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 Side_position_interface::quantised_position (Score_element const *me, Axis a)
162 Side_position_interface s(me);
163 Direction d = s.get_direction ();
164 Staff_symbol_referencer_interface si (me);
166 if (si.has_interface_b ())
168 Real p = si.position_f ();
169 Real rp = directed_round (p, d);
178 return (rp - p) * si.staff_space () / 2.0;
184 Position next to support, taking into account my own dimensions and padding.
187 Side_position_interface::aligned_side (Score_element const*me, Axis ax)
189 Side_position_interface s(me);
190 Direction d = s.get_direction ();
191 Real o = side_position (me,ax);
193 Interval iv = me->extent (ax);
199 SCM pad = me->get_elt_property ("padding");
200 if (gh_number_p (pad))
201 o += d *gh_scm2double (pad) ;
207 Position centered on parent.
210 Side_position_interface::centered_on_parent (Score_element const* me, Axis a)
212 Score_element *him = me->parent_l (a);
214 return him->extent (a).center ();
219 Side_position_interface::add_staff_support ()
221 Staff_symbol_referencer_interface si (elt_l_);
222 if (si.staff_symbol_l ())
224 add_support (si.staff_symbol_l ());
229 Side_position_interface::set_axis (Axis a)
231 // prop transparent ?
232 if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
233 elt_l_->set_elt_property ("side-support" ,SCM_EOL);
235 if (!elt_l_->has_offset_callback_b (aligned_side, a))
236 elt_l_->add_offset_callback (aligned_side, a);
241 Side_position_interface::set_quantised (Axis a)
243 elt_l_->add_offset_callback (quantised_position, a);
247 Side_position_interface::get_axis () const
249 if (elt_l_->has_offset_callback_b (&side_position, X_AXIS)
250 || elt_l_->has_offset_callback_b (&aligned_side , X_AXIS))
258 Side_position_interface::set_direction (Direction d)
260 elt_l_->set_elt_property ("direction", gh_int2scm (d));
264 Side_position_interface::set_minimum_space (Real m)
266 elt_l_->set_elt_property ("minimum-space", gh_double2scm (m));
270 Side_position_interface::set_padding (Real p)
272 elt_l_->set_elt_property ("padding", gh_double2scm (p));
276 Side_position_interface::has_interface_b () const
278 return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
282 Side_position_interface::supported_b () const
284 SCM s =elt_l_->get_elt_property ("side-support");
285 return s != SCM_UNDEFINED && s != SCM_EOL;