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;
26 Side_position_interface::add_support (Score_element*e)
28 SCM sup = elt_l_->get_elt_property ("side-support");
29 elt_l_->set_elt_property ("side-support",
30 gh_cons (e->self_scm_,sup));
36 Side_position_interface::get_direction () const
38 SCM d = elt_l_->get_elt_property ("direction");
40 return to_dir (d) ? to_dir (d) : DOWN;
42 Direction relative_dir = UP;
43 SCM reldir = elt_l_->get_elt_property ("side-relative-direction"); // should use a lambda.
46 relative_dir = to_dir (reldir);
49 SCM other_elt = elt_l_->get_elt_property ("direction-source");
50 Score_element * e = unsmob_element(other_elt);
53 return (Direction)(relative_dir * Side_position_interface (e).get_direction ());
60 Callback that does the aligning. Puts the element next to the support
64 Side_position_interface::side_position (Dimension_cache const * c)
66 Score_element * me = (c->element_l ());
69 Axis axis = c->axis ();
70 Score_element *common = me->parent_l (axis);
71 SCM support = me->get_elt_property ("side-support");
72 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
74 Score_element * e = unsmob_element (gh_car (s));
76 common = common->common_refpoint (e, axis);
79 for (SCM s = support; s != SCM_EOL; s = gh_cdr (s))
82 Score_element * e = unsmob_element ( gh_car (s));
85 Real coord = e->relative_coordinate (common, axis);
87 dim.unite (coord + e->extent (axis));
96 Direction dir = Side_position_interface (me).get_direction ();
98 Real off = me->parent_l (axis)->relative_coordinate (common, axis);
99 SCM minimum = me->remove_elt_property ("minimum-space");
101 Real total_off = dim[dir] + off;
102 SCM padding = me->remove_elt_property ("padding");
103 if (gh_number_p (padding))
105 total_off += gh_scm2double (padding) * dir;
107 if (gh_number_p (minimum) && total_off * dir < gh_scm2double (minimum))
109 total_off = gh_scm2double (minimum) * dir;
111 if (fabs (total_off) > 100 CM)
112 programming_error ("Huh ? Improbable staff side dim.");
118 callback that centers the element on itself
121 Side_position_interface::aligned_on_self (Dimension_cache const *c)
123 String s ("self-alignment-");
124 Axis ax = c->axis ();
125 s += (ax == X_AXIS) ? "X" : "Y";
126 Score_element *elm = c->element_l ();
127 SCM align (elm->get_elt_property (s));
130 Direction d = to_dir (align);
131 Interval ext(elm->extent (ax));
135 programming_error ("I'm empty. Can't align on self");
142 return - ext.center ();
151 directed_round (Real f, Direction d)
160 Callback that quantises in staff-spaces, rounding in the direction
161 of the elements "direction" elt property. */
163 Side_position_interface::quantised_position (Dimension_cache const *c)
165 Score_element * me = (c->element_l ());
166 Side_position_interface s(me);
167 Direction d = s.get_direction ();
168 Staff_symbol_referencer_interface si (me);
170 if (si.has_interface_b ())
172 Real p = si.position_f ();
173 Real rp = directed_round (p, d);
182 return (rp - p) * si.staff_space () / 2.0;
188 Position next to support, taking into account my own dimensions and padding.
191 Side_position_interface::aligned_side (Dimension_cache const *c)
193 Score_element * me = (c->element_l ());
194 Side_position_interface s(me);
195 Direction d = s.get_direction ();
196 Axis ax = c->axis ();
197 Real o = side_position (c);
199 Interval iv = me->extent (ax);
205 SCM pad = me->get_elt_property ("padding");
206 if (gh_number_p (pad))
207 o += d *gh_scm2double (pad) ;
213 Position centered on parent.
216 Side_position_interface::centered_on_parent (Dimension_cache const *c)
219 Score_element *me = c->element_l ();
221 Score_element *him = me->parent_l (a);
223 return him->extent (a).center ();
228 Side_position_interface::add_staff_support ()
230 Staff_symbol_referencer_interface si (elt_l_);
231 if (si.staff_symbol_l ())
233 add_support (si.staff_symbol_l ());
238 Side_position_interface::set_axis (Axis a)
240 // prop transparent ?
241 if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
242 elt_l_->set_elt_property ("side-support" ,SCM_EOL);
244 if (!elt_l_->has_offset_callback_b (aligned_side, a))
245 elt_l_->add_offset_callback (aligned_side, a);
250 Side_position_interface::set_quantised (Axis a)
252 elt_l_->add_offset_callback (quantised_position, a);
256 Side_position_interface::get_axis () const
258 if (elt_l_->has_offset_callback_b (&side_position, X_AXIS)
259 || elt_l_->has_offset_callback_b (&aligned_side , X_AXIS))
267 Side_position_interface::set_direction (Direction d)
269 elt_l_->set_elt_property ("direction", gh_int2scm (d));
273 Side_position_interface::set_minimum_space (Real m)
275 elt_l_->set_elt_property ("minimum-space", gh_double2scm (m));
279 Side_position_interface::set_padding (Real p)
281 elt_l_->set_elt_property ("padding", gh_double2scm (p));
285 Side_position_interface::has_interface_b () const
287 return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
291 Side_position_interface::supported_b () const
293 SCM s =elt_l_->get_elt_property ("side-support");
294 return s != SCM_UNDEFINED && s != SCM_EOL;
298 Side_position_interface
299 side_position (Score_element* e)
301 Side_position_interface si (e);