2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "staff-symbol-referencer.hh"
11 #include "staff-symbol.hh"
13 #include "output-def.hh"
14 #include "libc-extension.hh"
17 Staff_symbol_referencer::line_count (Grob *me)
19 Grob *st = get_staff_symbol (me);
20 return st ? Staff_symbol::line_count (st) : 0;
24 Staff_symbol_referencer::on_line (Grob *me)
26 return on_line (me, (int) rint (get_position (me)));
30 Staff_symbol_referencer::on_staff_line (Grob *me)
32 return on_staff_line (me, (int) rint (get_position (me)));
36 Staff_symbol_referencer::on_line (Grob *me, int pos)
38 int sz = line_count (me) - 1;
39 return ((pos + sz) % 2) == 0;
43 Staff_symbol_referencer::on_staff_line (Grob *me, int pos)
45 return on_line (me, pos) && abs (pos) <= 2 * staff_radius (me);
49 Staff_symbol_referencer::get_staff_symbol (Grob *me)
51 if (Staff_symbol::has_interface (me))
54 SCM st = me->get_object ("staff-symbol");
55 return unsmob_grob (st);
59 Staff_symbol_referencer::staff_space (Grob *me)
61 Grob *st = get_staff_symbol (me);
63 return Staff_symbol::staff_space (st);
68 Staff_symbol_referencer::line_thickness (Grob *me)
70 Grob *st = get_staff_symbol (me);
72 return Staff_symbol::get_line_thickness (st);
73 return me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
77 Staff_symbol_referencer::get_position (Grob *me)
80 Grob *st = get_staff_symbol (me);
81 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
84 Real y = me->relative_coordinate (c, Y_AXIS)
85 - st->relative_coordinate (c, Y_AXIS);
87 p += 2.0 * y / Staff_symbol::staff_space (st);
91 return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
92 return robust_scm2double (me->get_property ("staff-position"), p);
97 Staff_symbol_referencer::extent_in_staff (Grob *me)
99 Grob *st = get_staff_symbol (me);
100 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
105 retval = me->extent (c, Y_AXIS)
106 - st->relative_coordinate (c, Y_AXIS);
115 Staff_symbol_referencer::get_rounded_position (Grob *me)
117 return int (rint (get_position (me)));
120 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
122 Staff_symbol_referencer::callback (SCM smob)
124 Grob *me = unsmob_grob (smob);
126 SCM pos = me->get_property ("staff-position");
128 if (scm_is_number (pos))
130 Real space = Staff_symbol_referencer::staff_space (me);
131 off = scm_to_double (pos) * space / 2.0;
134 return scm_from_double (off);
137 /* This sets the position relative to the center of the staff symbol.
139 The function is hairy, because it can be called in two situations:
141 1. There is no staff yet; we must set staff-position
143 2. There is a staff, and perhaps someone even applied a
144 translate_axis (). Then we must compensate for the translation
146 In either case, we set a callback to be sure that our new position
147 will be extracted from staff-position */
150 Staff_symbol_referencer::set_position (Grob *me, Real p)
152 Grob *st = get_staff_symbol (me);
154 if (st && me->common_refpoint (st, Y_AXIS))
156 oldpos = get_position (me);
160 Real ss = Staff_symbol_referencer::staff_space (me);
161 me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
164 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
166 Staff_symbol_referencer::staff_radius (Grob *me)
168 return (line_count (me) - 1) / 2.0;
172 compare_position (Grob *const &a, Grob *const &b)
174 return sign (Staff_symbol_referencer::get_position ((Grob *)a)
175 - Staff_symbol_referencer::get_position ((Grob *) b));
179 position_less (Grob *const &a, Grob *const &b)
181 return Staff_symbol_referencer::get_position (a)
182 < Staff_symbol_referencer::get_position (b);
185 ADD_INTERFACE (Staff_symbol_referencer,
186 "An object whose Y position is meant relative to a staff "
188 "These usually have @code{Staff_symbol_referencer::callback} "
189 "in their @code{Y-offset-callbacks}. ",