2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "staff-symbol-referencer.hh"
22 #include "staff-symbol.hh"
24 #include "output-def.hh"
25 #include "libc-extension.hh"
28 Staff_symbol_referencer::line_count (Grob *me)
30 Grob *st = get_staff_symbol (me);
31 return st ? Staff_symbol::line_count (st) : 0;
35 Staff_symbol_referencer::on_line (Grob *me, int pos)
37 Grob *st = get_staff_symbol (me);
38 return st ? Staff_symbol::on_line (st, pos) : false;
42 Staff_symbol_referencer::on_staff_line (Grob *me, int pos)
44 return on_line (me, pos) && abs (pos) <= 2 * staff_radius (me);
48 Staff_symbol_referencer::get_staff_symbol (Grob *me)
50 if (Staff_symbol::has_interface (me))
53 SCM st = me->get_object ("staff-symbol");
54 return unsmob_grob (st);
58 Staff_symbol_referencer::staff_space (Grob *me)
60 Grob *st = get_staff_symbol (me);
62 return Staff_symbol::staff_space (st);
67 Staff_symbol_referencer::line_thickness (Grob *me)
69 Grob *st = get_staff_symbol (me);
71 return Staff_symbol::get_line_thickness (st);
72 return me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
76 Staff_symbol_referencer::get_position (Grob *me)
79 Grob *st = get_staff_symbol (me);
80 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
83 Real y = me->relative_coordinate (c, Y_AXIS)
84 - st->relative_coordinate (c, Y_AXIS);
85 Real space = Staff_symbol::staff_space (st);
86 p = (space == 0) ? 0 : 2.0 * y / space;
90 return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
91 return robust_scm2double (me->get_property ("staff-position"), p);
95 Staff_symbol_referencer::extent_in_staff (Grob *me)
97 Grob *st = get_staff_symbol (me);
98 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
103 retval = me->extent (c, Y_AXIS)
104 - st->relative_coordinate (c, Y_AXIS);
111 Staff_symbol_referencer::get_rounded_position (Grob *me)
113 return int (rint (get_position (me)));
116 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
118 Staff_symbol_referencer::callback (SCM smob)
120 Grob *me = unsmob_grob (smob);
122 SCM pos = me->get_property ("staff-position");
124 if (scm_is_number (pos))
126 Real space = Staff_symbol_referencer::staff_space (me);
127 off = scm_to_double (pos) * space / 2.0;
130 return scm_from_double (off);
133 /* This sets the position relative to the center of the staff symbol.
135 The function is hairy, because it can be called in two situations:
137 1. There is no staff yet; we must set staff-position
139 2. There is a staff, and perhaps someone even applied a
140 translate_axis (). Then we must compensate for the translation
142 In either case, we set a callback to be sure that our new position
143 will be extracted from staff-position */
146 Staff_symbol_referencer::set_position (Grob *me, Real p)
148 Grob *st = get_staff_symbol (me);
150 if (st && me->common_refpoint (st, Y_AXIS))
152 oldpos = get_position (me);
155 Real ss = Staff_symbol_referencer::staff_space (me);
156 me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
159 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
161 Staff_symbol_referencer::staff_radius (Grob *me)
163 return (line_count (me) - 1) / 2.0;
167 compare_position (Grob *const &a, Grob *const &b)
169 return sign (Staff_symbol_referencer::get_position ((Grob *)a)
170 - Staff_symbol_referencer::get_position ((Grob *) b));
174 position_less (Grob *const &a, Grob *const &b)
176 return Staff_symbol_referencer::get_position (a)
177 < Staff_symbol_referencer::get_position (b);
180 ADD_INTERFACE (Staff_symbol_referencer,
181 "An object whose Y@tie{}position is meant relative to a staff"
182 " symbol. These usually"
183 " have @code{Staff_symbol_referencer::callback} in their"
184 " @code{Y-offset-callbacks}.",