2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 #include "side-position-interface.hh"
11 #include <cmath> // ceil.
15 #include "note-head.hh"
18 #include "dimensions.hh"
19 #include "staff-symbol-referencer.hh"
20 #include "pointer-group-interface.hh"
21 #include "directional-element-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "staff-symbol.hh"
24 #include "string-convert.hh"
28 Side_position_interface::add_support (Grob *me, Grob *e)
30 Pointer_group_interface::add_grob (me, ly_symbol2scm ("side-support-elements"), e);
34 Side_position_interface::get_direction (Grob *me)
36 Direction relative_dir = Direction (1);
37 SCM reldir = me->get_property ("side-relative-direction");
38 if (is_direction (reldir))
39 relative_dir = to_dir (reldir);
41 SCM other_elt = me->get_object ("direction-source");
42 Grob *e = unsmob_grob (other_elt);
44 return (Direction) (relative_dir * get_grob_direction (e));
49 /* Put the element next to the support, optionally taking in
50 account the extent of the support. */
52 Side_position_interface::general_side_position (Grob *me, Axis a, bool use_extents)
54 Real ss = Staff_symbol_referencer::staff_space (me);
56 extract_grob_set (me, "side-support-elements", support);
58 Grob *common = common_refpoint_of_array (support, me->get_parent (a), a);
59 Grob *staff_symbol = Staff_symbol_referencer::get_staff_symbol (me);
60 bool include_staff = false;
65 if (scm_is_number (me->get_property ("staff-padding")))
70 Interval staff_extents;
73 common = staff_symbol->common_refpoint (common, Y_AXIS);
74 staff_extents = staff_symbol->extent (common, Y_AXIS);
77 dim.unite (staff_extents);
80 for (int i = 0; i < support.size (); i++)
85 dim.unite (e->extent (common, a));
88 Real x = e->relative_coordinate (common, a);
89 dim.unite (Interval (x, x));
94 dim = Interval (0, 0);
96 Direction dir = get_grob_direction (me);
98 Real off = me->get_parent (a)->relative_coordinate (common, a);
99 Real minimum_space = ss * robust_scm2double (me->get_property ("minimum-space"), -1);
101 Real total_off = dim.linear_combination (dir) - off;
102 total_off += dir * ss * robust_scm2double (me->get_property ("padding"), 0);
104 if (minimum_space >= 0
106 && total_off * dir < minimum_space)
107 total_off = minimum_space * dir;
109 /* FIXME: 100CM should relate to paper size. */
110 if (fabs (total_off) > 100 CM)
113 = String_convert::form_string ("Improbable offset for grob %s: %f%s",
114 me->name ().to_str0 (), total_off,
117 programming_error (msg);
119 return scm_from_double (total_off);
123 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_on_support_refpoints, 1);
125 Side_position_interface::y_aligned_on_support_refpoints (SCM smob)
127 return general_side_position (unsmob_grob (smob), Y_AXIS, false);
133 Position next to support, taking into account my own dimensions and padding.
136 MAKE_SCHEME_CALLBACK (Side_position_interface, x_aligned_side, 1);
138 Side_position_interface::x_aligned_side (SCM smob)
140 return aligned_side (unsmob_grob (smob), X_AXIS);
143 MAKE_SCHEME_CALLBACK (Side_position_interface, y_aligned_side, 1);
145 Side_position_interface::y_aligned_side (SCM smob)
147 return aligned_side (unsmob_grob (smob), Y_AXIS);
151 Side_position_interface::aligned_side (Grob*me, Axis a)
153 Direction dir = get_grob_direction (me);
155 Real o = scm_to_double (general_side_position (me, a, true));
156 Interval iv = me->extent (me, a);
162 programming_error ("direction unknown, but aligned-side wanted");
169 Maintain a minimum distance to the staff. This is similar to side
170 position with padding, but it will put adjoining objects on a row if
171 stuff sticks out of the staff a little.
173 Grob *staff = Staff_symbol_referencer::get_staff_symbol (me);
174 if (staff && a == Y_AXIS)
176 if (scm_is_number (me->get_property ("staff-padding")))
179 = Staff_symbol_referencer::staff_space (me)
180 * scm_to_double (me->get_property ("staff-padding"));
182 Grob *common = me->common_refpoint (staff, Y_AXIS);
184 Interval staff_size = staff->extent (common, Y_AXIS);
185 Real diff = dir*staff_size[dir] + padding - dir * (o + iv[-dir]);
186 o += dir * max (diff, 0.0);
189 if (to_boolean (me->get_property ("quantize-position")))
191 Grob *common = me->common_refpoint (staff, Y_AXIS);
192 Real my_off = me->relative_coordinate (common, Y_AXIS);
193 Real staff_off = staff->relative_coordinate (common, Y_AXIS);
194 Real ss = Staff_symbol::staff_space (staff);
195 Real position = 2 * (my_off + o - staff_off) / ss;
196 Real rounded = directed_round (position, dir);
197 Grob *head = me->get_parent (X_AXIS);
199 if (rounded <= Staff_symbol_referencer::staff_radius (me)
200 || (Note_head::has_interface (head)
201 && sign (Staff_symbol_referencer::get_position (head)) == - dir))
203 o += dir *(rounded - position) * 0.5 * ss;
204 if (Staff_symbol_referencer::on_staffline (me, int (rounded)))
209 return scm_from_double (o);
213 Side_position_interface::set_axis (Grob *me, Axis a)
215 me->internal_set_property (axis_offset_symbol (a),
217 ? x_aligned_side_proc
218 : y_aligned_side_proc);
221 // ugh. doesn't catch all variants.
223 Side_position_interface::get_axis (Grob *me)
225 if (me->get_property_data (ly_symbol2scm ("X-offset"))
226 == Side_position_interface::x_aligned_side_proc)
228 else if (me->get_property_data (ly_symbol2scm ("Y-offset"))
229 == Side_position_interface::y_aligned_side_proc)
235 ADD_INTERFACE (Side_position_interface, "side-position-interface",
236 "Position a victim object (this one) next to other objects (the "
237 "support). The property @code{direction} signifies where to put the "
238 "victim object relative to the support (left or right, up or down?)\n\n "
239 "The routine also takes the size the staff into account if "
240 "@code{staff-padding} is set. If undefined, the staff symbol is ignored.",
247 "side-relative-direction "
248 "side-support-elements "