2 side-position-interface.cc -- implement Side_position_interface
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "side-position-interface.hh"
11 #include <cmath> // ceil.
16 #include "directional-element-interface.hh"
20 #include "note-head.hh"
21 #include "pointer-group-interface.hh"
22 #include "staff-symbol-referencer.hh"
23 #include "staff-symbol.hh"
25 #include "string-convert.hh"
29 Side_position_interface::add_support (Grob
*me
, Grob
*e
)
31 Pointer_group_interface::add_unordered_grob (me
, ly_symbol2scm ("side-support-elements"), e
);
35 Side_position_interface::get_direction (Grob
*me
)
37 Direction relative_dir
= Direction (1);
38 SCM reldir
= me
->get_property ("side-relative-direction");
39 if (is_direction (reldir
))
40 relative_dir
= to_dir (reldir
);
42 SCM other_elt
= me
->get_object ("direction-source");
43 Grob
*e
= unsmob_grob (other_elt
);
45 return (Direction
) (relative_dir
* get_grob_direction (e
));
50 /* Put the element next to the support, optionally taking in
51 account the extent of the support.
53 Does not take into account the extent of ME.
56 Side_position_interface::general_side_position (Grob
*me
, Axis a
, bool use_extents
,
57 bool include_my_extent
,
58 bool pure
, int start
, int end
,
61 Real ss
= Staff_symbol_referencer::staff_space (me
);
63 extract_grob_set (me
, "side-support-elements", support
);
65 Grob
*common
= common_refpoint_of_array (support
, me
->get_parent (a
), a
);
66 Grob
*staff_symbol
= Staff_symbol_referencer::get_staff_symbol (me
);
70 && scm_is_number (me
->get_property ("staff-padding"))
71 && !to_boolean (me
->get_property ("quantize-position"));
74 Interval staff_extents
;
77 common
= staff_symbol
->common_refpoint (common
, Y_AXIS
);
78 staff_extents
= staff_symbol
->maybe_pure_extent (common
, Y_AXIS
, pure
, start
, end
);
81 dim
.unite (staff_extents
);
84 Direction dir
= get_grob_direction (me
);
86 for (vsize i
= 0; i
< support
.size (); i
++)
90 // In the case of a stem, we will find a note head as well
91 // ignoring the stem solves cyclic dependencies if the stem is
92 // attached to a cross-staff beam.
94 && Stem::has_interface (e
)
95 && dir
== - get_grob_direction (e
))
100 dim
.unite (e
->maybe_pure_extent (common
, a
, pure
, start
, end
));
103 Real x
= e
->maybe_pure_coordinate (common
, a
, pure
, start
, end
);
104 dim
.unite (Interval (x
, x
));
109 dim
= Interval (0, 0);
111 Real off
= me
->get_parent (a
)->maybe_pure_coordinate (common
, a
, pure
, start
, end
);
112 Real minimum_space
= ss
* robust_scm2double (me
->get_property ("minimum-space"), -1);
114 Real total_off
= dim
.linear_combination (dir
) - off
;
115 total_off
+= dir
* ss
* robust_scm2double (me
->get_property ("padding"), 0);
117 if (minimum_space
>= 0
119 && total_off
* dir
< minimum_space
)
120 total_off
= minimum_space
* dir
;
122 if (include_my_extent
)
124 Interval iv
= me
->maybe_pure_extent (me
, a
, pure
, start
, end
);
129 programming_error ("direction unknown, but aligned-side wanted");
132 total_off
+= -iv
[-dir
];
137 total_off
= dir
* max (dir
* total_off
,
138 dir
* (*current_offset
));
141 /* FIXME: 1000 should relate to paper size. */
142 if (fabs (total_off
) > 1000)
145 = String_convert::form_string ("Improbable offset for grob %s: %f",
146 me
->name ().c_str (), total_off
);
148 programming_error (msg
);
149 if (strict_infinity_checking
)
150 scm_misc_error (__FUNCTION__
, "Improbable offset.", SCM_EOL
);
152 return scm_from_double (total_off
);
156 MAKE_SCHEME_CALLBACK (Side_position_interface
, y_aligned_on_support_refpoints
, 1);
158 Side_position_interface::y_aligned_on_support_refpoints (SCM smob
)
160 return general_side_position (unsmob_grob (smob
), Y_AXIS
, false, false, false, 0, 0, 0);
163 MAKE_SCHEME_CALLBACK (Side_position_interface
, pure_y_aligned_on_support_refpoints
, 3);
165 Side_position_interface::pure_y_aligned_on_support_refpoints (SCM smob
, SCM start
, SCM end
)
167 return general_side_position (unsmob_grob (smob
), Y_AXIS
, false, false,
168 true, scm_to_int (start
), scm_to_int (end
), 0);
173 Position next to support, taking into account my own dimensions and padding.
176 axis_aligned_side_helper (SCM smob
, Axis a
, bool pure
, int start
, int end
, SCM current_off_scm
)
179 Real
*current_off_ptr
= 0;
180 if (scm_is_number (current_off_scm
))
182 r
= scm_to_double (current_off_scm
);
183 current_off_ptr
= &r
;
186 return Side_position_interface::aligned_side (unsmob_grob (smob
), a
, pure
, start
, end
, current_off_ptr
);
190 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface
, x_aligned_side
, 2, 1, "");
192 Side_position_interface::x_aligned_side (SCM smob
, SCM current_off
)
194 return axis_aligned_side_helper (smob
, X_AXIS
, false, 0, 0, current_off
);
197 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface
, y_aligned_side
, 2, 1, "");
199 Side_position_interface::y_aligned_side (SCM smob
, SCM current_off
)
201 return axis_aligned_side_helper (smob
, Y_AXIS
, false, 0, 0, current_off
);
204 MAKE_SCHEME_CALLBACK_WITH_OPTARGS (Side_position_interface
, pure_y_aligned_side
, 4, 1, "");
206 Side_position_interface::pure_y_aligned_side (SCM smob
, SCM start
, SCM end
, SCM cur_off
)
208 return axis_aligned_side_helper (smob
, Y_AXIS
, true,
214 MAKE_SCHEME_CALLBACK (Side_position_interface
, calc_cross_staff
, 1)
216 Side_position_interface::calc_cross_staff (SCM smob
)
218 Grob
*me
= unsmob_grob (smob
);
219 extract_grob_set (me
, "side-support-elements", elts
);
221 for (vsize i
= 0; i
< elts
.size (); i
++)
222 if (to_boolean (elts
[i
]->get_property ("cross-staff")))
225 Grob
*common
= common_refpoint_of_array (elts
, me
->get_parent (Y_AXIS
), Y_AXIS
);
226 return scm_from_bool (common
!= me
->get_parent (Y_AXIS
));
230 Side_position_interface::aligned_side (Grob
*me
, Axis a
, bool pure
, int start
, int end
,
233 Direction dir
= get_grob_direction (me
);
235 Real o
= scm_to_double (general_side_position (me
, a
, true, true, pure
, start
, end
, current_off
));
238 Maintain a minimum distance to the staff. This is similar to side
239 position with padding, but it will put adjoining objects on a row if
240 stuff sticks out of the staff a little.
242 Grob
*staff
= Staff_symbol_referencer::get_staff_symbol (me
);
243 if (staff
&& a
== Y_AXIS
)
245 if (to_boolean (me
->get_property ("quantize-position")))
247 Grob
*common
= me
->common_refpoint (staff
, Y_AXIS
);
248 Real my_off
= me
->get_parent (Y_AXIS
)->maybe_pure_coordinate (common
, Y_AXIS
, pure
, start
, end
);
249 Real staff_off
= staff
->maybe_pure_coordinate (common
, Y_AXIS
, pure
, start
, end
);
250 Real ss
= Staff_symbol::staff_space (staff
);
251 Real position
= 2 * (my_off
+ o
- staff_off
) / ss
;
252 Real rounded
= directed_round (position
, dir
);
253 Grob
*head
= me
->get_parent (X_AXIS
);
255 if (fabs (position
) <= 2 * Staff_symbol_referencer::staff_radius (me
) + 1
256 /* In case of a ledger lines, quantize even if we're outside the staff. */
257 || (Note_head::has_interface (head
)
259 && abs (Staff_symbol_referencer::get_position (head
)) > abs (position
)))
261 o
+= (rounded
- position
) * 0.5 * ss
;
262 if (Staff_symbol_referencer::on_line (me
, int (rounded
)))
266 else if (scm_is_number (me
->get_property ("staff-padding")) && dir
)
268 Interval iv
= me
->maybe_pure_extent (me
, a
, pure
, start
, end
);
271 = Staff_symbol_referencer::staff_space (me
)
272 * scm_to_double (me
->get_property ("staff-padding"));
274 Grob
*common
= me
->common_refpoint (staff
, Y_AXIS
);
276 Interval staff_size
= staff
->maybe_pure_extent (common
, Y_AXIS
, pure
, start
, end
);
277 Real diff
= dir
*staff_size
[dir
] + padding
- dir
* (o
+ iv
[-dir
]);
278 o
+= dir
* max (diff
, 0.0);
281 return scm_from_double (o
);
285 Side_position_interface::set_axis (Grob
*me
, Axis a
)
287 if (!scm_is_number (me
->get_property ("side-axis")))
289 me
->set_property ("side-axis", scm_from_int (a
));
290 chain_offset_callback (me
,
292 ? x_aligned_side_proc
293 : y_aligned_side_proc
,
299 Side_position_interface::get_axis (Grob
*me
)
301 if (scm_is_number (me
->get_property ("side-axis")))
302 return Axis (scm_to_int (me
->get_property ("side-axis")));
304 string msg
= String_convert::form_string ("side-axis not set for grob %s.",
305 me
->name ().c_str ());
306 me
->programming_error (msg
);
310 ADD_INTERFACE (Side_position_interface
,
311 "Position a victim object (this one) next to other objects"
312 " (the support). The property @code{direction} signifies where"
313 " to put the victim object relative to the support (left or"
314 " right, up or down?)\n"
316 "The routine also takes the size of the staff into account if"
317 " @code{staff-padding} is set. If undefined, the staff symbol"
327 "side-relative-direction "
328 "side-support-elements "