2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1999--2012 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 Grob *st = get_staff_symbol (me);
45 return st ? Staff_symbol::on_line (st, pos, false) : false;
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)
79 return internal_get_position (me, false);
83 Staff_symbol_referencer::pure_get_position (Grob *me)
85 return internal_get_position (me, true);
89 Staff_symbol_referencer::internal_get_position (Grob *me, bool pure)
92 Grob *st = get_staff_symbol (me);
93 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
97 ? me->pure_relative_y_coordinate (c, 0, INT_MAX)
98 : me->relative_coordinate (c, Y_AXIS))
99 - st->relative_coordinate (c, Y_AXIS);
100 Real space = Staff_symbol::staff_space (st);
101 p = (space == 0) ? 0 : 2.0 * y / space;
105 return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
106 return robust_scm2double (me->get_property ("staff-position"), p);
110 Staff_symbol_referencer::extent_in_staff (Grob *me)
112 Grob *st = get_staff_symbol (me);
113 Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
118 retval = me->extent (c, Y_AXIS)
119 - st->relative_coordinate (c, Y_AXIS);
126 Staff_symbol_referencer::get_rounded_position (Grob *me)
128 return int (rint (get_position (me)));
132 Staff_symbol_referencer::pure_get_rounded_position (Grob *me)
134 return int (rint (pure_get_position (me)));
137 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
139 Staff_symbol_referencer::callback (SCM smob)
141 Grob *me = unsmob_grob (smob);
143 SCM pos = me->get_property ("staff-position");
145 if (scm_is_number (pos))
147 Real space = Staff_symbol_referencer::staff_space (me);
148 off = scm_to_double (pos) * space / 2.0;
151 return scm_from_double (off);
154 /* This sets the position relative to the center of the staff symbol.
156 The function is hairy, because it can be called in two situations:
158 1. There is no staff yet; we must set staff-position
160 2. There is a staff, and perhaps someone even applied a
161 translate_axis (). Then we must compensate for the translation
163 In either case, we set a callback to be sure that our new position
164 will be extracted from staff-position */
167 Staff_symbol_referencer::set_position (Grob *me, Real p)
169 internal_set_position (me, p, false);
173 Staff_symbol_referencer::pure_set_position (Grob *me, Real p)
175 internal_set_position (me, p, true);
179 Staff_symbol_referencer::internal_set_position (Grob *me, Real p, bool pure)
181 Grob *st = get_staff_symbol (me);
183 if (st && me->common_refpoint (st, Y_AXIS))
185 oldpos = pure ? pure_get_position (me) : get_position (me);
188 Real ss = Staff_symbol_referencer::staff_space (me);
189 me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
193 Staff_symbol_referencer::staff_span (Grob *me)
197 if (Grob *symb = get_staff_symbol (me))
198 result = Staff_symbol::line_span (symb);
203 Staff_symbol_referencer::staff_radius (Grob *me)
206 line_span is measured in pitch steps, not in staff spaces
208 return staff_span (me).length () / 4.0;
212 compare_position (Grob *const &a, Grob *const &b)
214 return sign (Staff_symbol_referencer::get_position ((Grob *)a)
215 - Staff_symbol_referencer::get_position ((Grob *) b));
219 position_less (Grob *const &a, Grob *const &b)
221 return Staff_symbol_referencer::get_position (a)
222 < Staff_symbol_referencer::get_position (b);
226 pure_position_less (Grob *const &a, Grob *const &b)
228 return Staff_symbol_referencer::pure_get_position (a)
229 < Staff_symbol_referencer::pure_get_position (b);
232 ADD_INTERFACE (Staff_symbol_referencer,
233 "An object whose Y@tie{}position is meant relative to a staff"
234 " symbol. These usually"
235 " have @code{Staff_symbol_referencer::callback} in their"
236 " @code{Y-offset-callbacks}.",