new file. (Thanks Hendrik Maryns)
[lilypond.git] / lily / side-position-interface.cc
blob896bbb2b3326dae03c30bf754e6c89a5e9abbf55
1 /*
2 staff-side.cc -- implement Staff_side_element
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8 */
9 #include <math.h> // ceil.
11 #include "note-head.hh"
12 #include "side-position-interface.hh"
13 #include "warn.hh"
14 #include "warn.hh"
15 #include "dimensions.hh"
16 #include "staff-symbol-referencer.hh"
17 #include "group-interface.hh"
18 #include "directional-element-interface.hh"
19 #include "staff-symbol-referencer.hh"
21 void
22 Side_position_interface::add_support (Grob*me, Grob*e)
24 Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
27 Direction
28 Side_position_interface::get_direction (Grob*me)
30 SCM d = me->get_property ("direction");
31 if (is_direction (d) && to_dir (d))
32 return to_dir (d);
34 Direction relative_dir = Direction (1);
35 SCM reldir = me->get_property ("side-relative-direction"); // should use a lambda.
36 if (is_direction (reldir))
38 relative_dir = to_dir (reldir);
41 SCM other_elt = me->get_property ("direction-source");
42 Grob * e = unsmob_grob (other_elt);
43 if (e)
45 return (Direction) (relative_dir * get_grob_direction (e));
48 return CENTER;
52 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_extents, 2);
53 SCM
54 Side_position_interface::aligned_on_support_extents (SCM element_smob, SCM axis)
56 Grob *me = unsmob_grob (element_smob);
57 Axis a = (Axis) ly_scm2int (axis);
59 return general_side_position (me, a, true);
64 Puts the element next to the support, optionally taking in
65 account the extent of the support.
67 SCM
68 Side_position_interface::general_side_position (Grob * me, Axis a, bool use_extents)
70 Real ss = Staff_symbol_referencer::staff_space (me);
71 SCM support = me->get_property ("side-support-elements");
72 Grob *common = common_refpoint_of_list (support, me->get_parent (a), a);
73 Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
74 bool include_staff = (st
75 && a == Y_AXIS
76 && ly_c_number_p (me->get_property ("staff-padding")));
78 Interval dim;
79 if (include_staff)
81 common = st->common_refpoint (common, Y_AXIS);
82 dim = st->extent (common, Y_AXIS);
85 for (SCM s = support; s != SCM_EOL; s = ly_cdr (s))
87 Grob * e = unsmob_grob (ly_car (s));
88 if (e)
89 if (use_extents)
90 dim.unite (e->extent (common, a));
91 else
93 Real x = e->relative_coordinate (common, a);
94 dim.unite (Interval (x,x));
98 if (dim.is_empty ())
100 dim = Interval (0,0);
103 Direction dir = Side_position_interface::get_direction (me);
105 Real off = me->get_parent (a)->relative_coordinate (common, a);
106 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
108 Real total_off = dim.linear_combination (dir) - off;
109 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
111 if (minimum_space >= 0
112 && dir
113 && total_off * dir < minimum_space)
115 total_off = minimum_space * dir;
118 if (fabs (total_off) > 100 CM)
119 programming_error ("Huh ? Improbable staff side dim.");
124 return scm_make_real (total_off);
128 Cut & paste (ugh.)
130 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_on_support_refpoints,2);
132 Side_position_interface::aligned_on_support_refpoints (SCM smob, SCM axis)
134 Grob *me = unsmob_grob (smob);
135 Axis a = (Axis) ly_scm2int (axis);
137 return general_side_position (me, a, false);
143 Real
144 directed_round (Real f, Direction d)
146 if (d < 0)
147 return floor (f);
148 else
149 return ceil (f);
153 Callback that quantises in staff-spaces, rounding in the direction
154 of the elements "direction" elt property.
156 Only rounds when we're inside the staff, as determined by
157 Staff_symbol_referencer::staff_radius () */
158 MAKE_SCHEME_CALLBACK (Side_position_interface,quantised_position,2);
160 Side_position_interface::quantised_position (SCM element_smob, SCM)
162 Grob *me = unsmob_grob (element_smob);
164 Direction d = Side_position_interface::get_direction (me);
166 Grob * stsym = Staff_symbol_referencer::get_staff_symbol (me);
167 if (stsym)
169 Real p = Staff_symbol_referencer::get_position (me);
170 Real rp = directed_round (p, d);
171 Real rad = Staff_symbol_referencer::staff_radius (me) *2 ;
172 int ip = int (rp);
174 Grob *head = me->get_parent (X_AXIS);
176 if (Staff_symbol_referencer::on_staffline (me,ip)
177 && ((abs (ip) <= rad)
178 || (Note_head::has_interface (head)
179 && sign (Staff_symbol_referencer::get_position (head))
180 == -d)
183 ip += d;
184 rp += d;
187 return scm_make_real ((rp - p) * Staff_symbol_referencer::staff_space (me) / 2.0);
189 return scm_make_real (0.0);
193 Position next to support, taking into account my own dimensions and padding.
195 MAKE_SCHEME_CALLBACK (Side_position_interface,aligned_side,2);
197 Side_position_interface::aligned_side (SCM element_smob, SCM axis)
199 Grob *me = unsmob_grob (element_smob);
200 Axis a = (Axis) ly_scm2int (axis);
202 Direction d = Side_position_interface::get_direction (me);
204 Real o = ly_scm2double (aligned_on_support_extents (element_smob,axis));
206 Interval iv = me->extent (me, a);
208 if (!iv.is_empty ())
210 if (!d)
212 programming_error ("Direction unknown, but aligned-side wanted.");
213 d = DOWN;
215 o += - iv[-d];
219 Maintain a minimum distance to the staff. This is similar to side
220 position with padding, but it will put adjoining objects on a row if
221 stuff sticks out of the staff a little.
223 Grob * st = Staff_symbol_referencer::get_staff_symbol (me);
224 if (st && a == Y_AXIS
225 && ly_c_number_p (me->get_property ("staff-padding")))
227 Real padding=
228 Staff_symbol_referencer::staff_space (me)
229 * ly_scm2double (me->get_property ("staff-padding"));
231 Grob *common = me->common_refpoint (st, Y_AXIS);
233 Interval staff_size = st->extent (common, Y_AXIS);
234 Interval me_ext = me->extent (common, a);
235 Real diff = d*staff_size[d] + padding - d*(o + iv[-d]);
236 o += (d* (diff >? 0));
239 return scm_make_real (o);
243 void
244 Side_position_interface::set_axis (Grob*me, Axis a)
246 me->add_offset_callback (Side_position_interface::aligned_side_proc, a);
251 // ugh. doesn't catch all variants.
252 Axis
253 Side_position_interface::get_axis (Grob*me)
255 if (me->has_offset_callback (Side_position_interface::aligned_side_proc, X_AXIS)
256 || me->has_offset_callback (Side_position_interface::aligned_side_proc , X_AXIS))
257 return X_AXIS;
259 return Y_AXIS;
263 ADD_INTERFACE (Side_position_interface,"side-position-interface",
264 "Position a victim object (this one) next to other objects (the "
265 "support). The property @code{direction} signifies where to put the "
266 "victim object relative to the support (left or right, up or down?)\n\n "
267 "The routine also takes the size the staff into account if "
268 "@code{staff-padding} is set. If undefined, the staff symbol is ignored."
270 "staff-padding side-support-elements direction-source "
271 "direction side-relative-direction minimum-space padding");