2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
9 #include "staff-symbol-referencer.hh"
12 #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_staffline (Grob *me)
26 return on_staffline (me, (int) rint (get_position (me)));
30 This does not take size into account.
31 maybe rename: on_virtual_staffline, on_staff_or_ledger_line?
34 Staff_symbol_referencer::on_staffline (Grob *me, int pos)
36 int sz = line_count (me) - 1;
37 return ((pos + sz) % 2) == 0;
41 Staff_symbol_referencer::get_staff_symbol (Grob *me)
43 if (Staff_symbol::has_interface (me))
46 SCM st = me->get_object ("staff-symbol");
47 return unsmob_grob (st);
51 Staff_symbol_referencer::staff_space (Grob *me)
53 Grob *st = get_staff_symbol (me);
55 return Staff_symbol::staff_space (st);
60 Staff_symbol_referencer::line_thickness (Grob *me)
62 Grob *st = get_staff_symbol (me);
64 return Staff_symbol::get_line_thickness (st);
65 return me->layout ()->get_dimension (ly_symbol2scm ("linethickness"));
69 Staff_symbol_referencer::get_position (Grob *me)
72 Grob *st = get_staff_symbol (me);
73 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
76 Real y = me->relative_coordinate (c, Y_AXIS)
77 - st->relative_coordinate (c, Y_AXIS);
79 p += 2.0 * y / Staff_symbol::staff_space (st);
83 return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
84 return robust_scm2double (me->get_property ("staff-position"), p);
88 Staff_symbol_referencer::get_rounded_position (Grob *me)
90 return int (rint (get_position (me)));
93 LY_DEFINE (ly_grob_staff_position, "ly:grob-staff-position",
95 "Return the Y-position of @var{sg} relative to the staff.")
97 Grob *g = unsmob_grob (sg);
99 SCM_ASSERT_TYPE (g, sg, SCM_ARG1, __FUNCTION__, "grob");
100 Real pos = Staff_symbol_referencer::get_position (g);
102 if (fabs (rint (pos) -pos) < 1e-6) // ugh.
103 return scm_from_int ((int) my_round (pos));
105 return scm_from_double (pos);
108 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
110 Staff_symbol_referencer::callback (SCM smob)
112 Grob *me = unsmob_grob (smob);
114 SCM pos = me->get_property ("staff-position");
116 if (scm_is_number (pos))
118 Real space = Staff_symbol_referencer::staff_space (me);
119 off = scm_to_double (pos) * space / 2.0;
120 me->set_property ("staff-position", scm_from_int (0));
123 return scm_from_double (off);
126 /* This sets the position relative to the center of the staff symbol.
128 The function is hairy, because it can be called in two situations:
130 1. There is no staff yet; we must set staff-position
132 2. There is a staff, and perhaps someone even applied a
133 translate_axis (). Then we must compensate for the translation
135 In either case, we set a callback to be sure that our new position
136 will be extracted from staff-position */
139 Staff_symbol_referencer::set_position (Grob *me, Real p)
141 Grob *st = get_staff_symbol (me);
143 if (st && me->common_refpoint (st, Y_AXIS))
145 oldpos = get_position (me);
149 Real ss = Staff_symbol_referencer::staff_space (me);
150 me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
153 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
155 Staff_symbol_referencer::staff_radius (Grob *me)
157 return (line_count (me) - 1) / 2.0;
161 compare_position (Grob *const &a, Grob *const &b)
163 return sign (Staff_symbol_referencer::get_position ((Grob *)a)
164 - Staff_symbol_referencer::get_position ((Grob *) b));
167 ADD_INTERFACE (Staff_symbol_referencer, "staff-symbol-referencer-interface",
168 "An object whose Y position is meant relative to a staff "
170 "These usually have @code{Staff_symbol_referencer::callback} "
171 "in their @code{Y-offset-callbacks}. ",