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@cs.uu.nl>
9 #include "staff-symbol-referencer.hh"
13 #include "staff-symbol.hh"
14 #include "output-def.hh"
15 #include "libc-extension.hh"
18 Staff_symbol_referencer::line_count (Grob *me)
20 Grob *st = get_staff_symbol (me);
21 return st ? Staff_symbol::line_count (st) : 0;
25 Staff_symbol_referencer::on_staffline (Grob *me)
27 return on_staffline (me, (int) rint (get_position (me)));
31 This does not take size into account.
32 maybe rename: on_virtual_staffline, on_staff_or_ledger_line?
35 Staff_symbol_referencer::on_staffline (Grob *me, int pos)
37 int sz = line_count (me) - 1;
38 return ((pos + sz) % 2) == 0;
42 Staff_symbol_referencer::get_staff_symbol (Grob *me)
44 if (Staff_symbol::has_interface (me))
47 SCM st = me->get_object ("staff-symbol");
48 return unsmob_grob (st);
52 Staff_symbol_referencer::staff_space (Grob *me)
54 Grob *st = get_staff_symbol (me);
56 return Staff_symbol::staff_space (st);
61 Staff_symbol_referencer::line_thickness (Grob *me)
63 Grob *st = get_staff_symbol (me);
65 return Staff_symbol::get_line_thickness (st);
66 return me->get_layout ()->get_dimension (ly_symbol2scm ("linethickness"));
70 Staff_symbol_referencer::get_position (Grob *me)
73 Grob *st = get_staff_symbol (me);
74 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
77 Real y = me->relative_coordinate (c, Y_AXIS)
78 - st->relative_coordinate (c, Y_AXIS);
80 p += 2.0 * y / Staff_symbol::staff_space (st);
85 return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
87 return robust_scm2double (me->get_property ("staff-position"), p);
91 Staff_symbol_referencer::get_rounded_position (Grob *me)
93 return int (rint (get_position (me)));
96 LY_DEFINE (ly_grob_staff_position, "ly:grob-staff-position",
98 "Return the Y-position of @var{sg} relative to the staff.")
100 Grob *g = unsmob_grob (sg);
102 SCM_ASSERT_TYPE (g, sg, SCM_ARG1, __FUNCTION__, "grob");
103 Real pos = Staff_symbol_referencer::get_position (g);
105 if (fabs (rint (pos) -pos) < 1e-6) // ugh.
106 return scm_int2num ((int) my_round (pos));
108 return scm_make_real (pos);
111 /* should use offset callback! */
112 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 2);
114 Staff_symbol_referencer::callback (SCM element_smob, SCM)
116 Grob *me = unsmob_grob (element_smob);
118 SCM pos = me->get_property ("staff-position");
120 if (scm_is_number (pos))
122 Real space = Staff_symbol_referencer::staff_space (me);
123 off = scm_to_double (pos) * space / 2.0;
124 me->set_property ("staff-position", scm_int2num (0));
127 return scm_make_real (off);
130 /* This sets the position relative to the center of the staff symbol.
132 The function is hairy, because it can be called in two situations:
134 1. There is no staff yet; we must set staff-position
136 2. There is a staff, and perhaps someone even applied a
137 translate_axis (). Then we must compensate for the translation
139 In either case, we set a callback to be sure that our new position
140 will be extracted from staff-position */
143 Staff_symbol_referencer::set_position (Grob *me, Real p)
145 Grob *st = get_staff_symbol (me);
146 if (st && me->common_refpoint (st, Y_AXIS))
148 Real oldpos = get_position (me);
149 me->set_property ("staff-position", scm_make_real (p - oldpos));
152 me->set_property ("staff-position", scm_make_real (p));
154 me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
157 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
159 Staff_symbol_referencer::staff_radius (Grob *me)
161 return (line_count (me) - 1) / 2.0;
165 compare_position (Grob *const &a, Grob *const &b)
167 return sign (Staff_symbol_referencer::get_position ((Grob *)a)
168 - Staff_symbol_referencer::get_position ((Grob *) b));
171 ADD_INTERFACE (Staff_symbol_referencer, "staff-symbol-referencer-interface",
172 "An object whose Y position is meant relative to a staff "
174 "These usually have @code{Staff_symbol_referencer::callback} "
175 "in their @code{Y-offset-callbacks}. ",