2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
11 #include "staff-symbol-referencer.hh"
12 #include "staff-symbol.hh"
13 #include "paper-def.hh"
14 #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 Staff_symbol_referencer::on_staffline (Grob *me, int pos)
33 int sz = line_count (me) - 1;
34 return ((pos + sz) % 2) == 0;
38 Staff_symbol_referencer::get_staff_symbol (Grob *me)
40 SCM st = me->get_property ("staff-symbol");
41 return unsmob_grob (st);
45 Staff_symbol_referencer::staff_space (Grob *me)
47 Grob *st = get_staff_symbol (me);
49 return Staff_symbol::staff_space (st);
54 Staff_symbol_referencer::line_thickness (Grob *me)
56 Grob *st = get_staff_symbol (me);
58 return Staff_symbol::get_line_thickness (st);
59 return me->get_paper ()->get_dimension (ly_symbol2scm ("linethickness"));
63 Staff_symbol_referencer::get_position (Grob *me)
66 Grob *st = get_staff_symbol (me);
67 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
70 Real y = me->relative_coordinate (c, Y_AXIS)
71 - st->relative_coordinate (c, Y_AXIS);
73 p += 2.0 * y / Staff_symbol::staff_space (st);
77 return robust_scm2double (me->get_property ("staff-position"), p);
81 Staff_symbol_referencer::get_rounded_position (Grob*me)
83 return int (rint (get_position (me)));
86 LY_DEFINE (ly_grob_staff_position, "ly:grob-staff-position",
88 "Return the Y-position of @var{sg} relative to the staff.")
90 Grob *g = unsmob_grob (sg);
92 SCM_ASSERT_TYPE (g, sg, SCM_ARG1, __FUNCTION__, "grob");
93 Real pos = Staff_symbol_referencer::get_position (g);
95 if (fabs (rint (pos) -pos) < 1e-6) // ugh.
96 return scm_int2num ((int) my_round (pos));
98 return scm_make_real (pos);
102 /* should use offset callback! */
103 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
105 Staff_symbol_referencer::callback (SCM element_smob, SCM)
107 Grob *me = unsmob_grob (element_smob);
109 SCM pos = me->get_property ("staff-position");
111 if (ly_number_p (pos))
113 Real space = Staff_symbol_referencer::staff_space (me);
114 off = ly_scm2double (pos) * space / 2.0;
115 me->set_property ("staff-position", scm_int2num (0));
118 return scm_make_real (off);
121 /* This sets the position relative to the center of the staff symbol.
123 The function is hairy, because it can be callled in two situations:
125 1. There is no staff yet; we must set staff-position
127 2. There is a staff, and perhaps someone even applied a
128 translate_axis (). Then we must compensate for the translation
130 In either case, we set a callback to be sure that our new position
131 will be extracted from staff-position */
134 Staff_symbol_referencer::set_position (Grob *me, Real p)
136 Grob *st = get_staff_symbol (me);
137 if (st && me->common_refpoint (st, Y_AXIS))
139 Real oldpos = get_position (me);
140 me->set_property ("staff-position", scm_make_real (p - oldpos));
143 me->set_property ("staff-position", scm_make_real (p));
145 if (!me->has_offset_callback (Staff_symbol_referencer::callback_proc,
147 me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
150 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
152 Staff_symbol_referencer::staff_radius (Grob *me)
154 return (line_count (me) - 1) / 2.0;
158 compare_position (Grob *const &a, Grob *const &b)
160 return sign (Staff_symbol_referencer::get_position ((Grob*)a)
161 - Staff_symbol_referencer::get_position ((Grob*) b));
164 ADD_INTERFACE (Staff_symbol_referencer,"staff-symbol-referencer-interface",
165 "An object whose Y position is meant relative to a staff "
167 "These usually have @code{Staff_symbol_referencer::callback} "
168 "in their @code{Y-offset-callbacks}. "