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 Real off = me->parent_l (axis)->relative_coordinate (common, axis);
99 Direction dir = Side_position_interface (me).get_direction ();
101 SCM pad = me->remove_elt_property ("padding");
102 if (gh_number_p (pad))
104 off += gh_scm2double (pad) * dir;
106 Real total_off = dim[dir] + off;
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 (Dimension_cache const *c)
120 String s ("self-alignment-");
121 Axis ax = c->axis ();
122 s += (ax == X_AXIS) ? "X" : "Y";
123 Score_element *elm = c->element_l ();
124 SCM align (elm->get_elt_property (s));
127 Direction d = to_dir (align);
128 Interval ext(elm->extent (ax));
133 return - ext.center ();
140 directed_round (Real f, Direction d)
149 Callback that quantises in staff-spaces, rounding in the direction
150 of the elements "direction" elt property. */
152 Side_position_interface::quantised_position (Dimension_cache const *c)
154 Score_element * me = (c->element_l ());
155 Side_position_interface s(me);
156 Direction d = s.get_direction ();
157 Staff_symbol_referencer_interface si (me);
159 if (si.has_interface_b ())
161 Real p = si.position_f ();
162 Real rp = directed_round (p, d);
171 return (rp - p) * si.staff_space () / 2.0;
177 Position next to support, taking into account my own dimensions and padding.
180 Side_position_interface::aligned_side (Dimension_cache const *c)
182 Score_element * me = (c->element_l ());
183 Side_position_interface s(me);
184 Direction d = s.get_direction ();
185 Axis ax = c->axis ();
186 Real o = side_position (c);
188 Interval iv = me->extent (ax);
194 SCM pad = me->get_elt_property ("padding");
195 if (gh_number_p (pad))
196 o += d *gh_scm2double (pad) ;
202 Position centered on parent.
205 Side_position_interface::centered_on_parent (Dimension_cache const *c)
208 Score_element *me = c->element_l ();
210 Score_element *him = me->parent_l (a);
212 return him->extent (a).center ();
217 Side_position_interface::add_staff_support ()
219 Staff_symbol_referencer_interface si (elt_l_);
220 if (si.staff_symbol_l ())
222 add_support (si.staff_symbol_l ());
227 Side_position_interface::set_axis (Axis a)
229 // prop transparent ?
230 if (elt_l_->get_elt_property ("side-support") == SCM_UNDEFINED)
231 elt_l_->set_elt_property ("side-support" ,SCM_EOL);
233 elt_l_->dim_cache_[a]->off_callbacks_.push (aligned_side);
238 Side_position_interface::set_quantised (Axis a)
240 Dimension_cache * c = elt_l_->dim_cache_[a];
242 c->off_callbacks_.push (quantised_position);
246 Side_position_interface::get_axis () const
248 Dimension_cache * c = elt_l_->dim_cache_[X_AXIS];
249 for (int i=0 ; i < c->off_callbacks_.size();i ++)
250 if (c->off_callbacks_[i] == side_position
251 ||c->off_callbacks_[i] == aligned_side)
259 Side_position_interface::set_direction (Direction d)
261 elt_l_->set_elt_property ("direction", gh_int2scm (d));
265 Side_position_interface::has_interface_b () const
267 return elt_l_->get_elt_property ("side-support") != SCM_UNDEFINED;
271 Side_position_interface::supported_b () const
273 SCM s =elt_l_->get_elt_property ("side-support");
274 return s != SCM_UNDEFINED && s != SCM_EOL;
278 Side_position_interface
279 side_position (Score_element* e)
281 Side_position_interface si (e);