lilypond-1.3.145
[lilypond.git] / lily / staff-symbol-referencer.cc
blob13cf324351f2e5018b50c8c8af524e6340677dc5
1 /*
2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2001 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
9 #include <math.h>
11 #include "staff-symbol-referencer.hh"
12 #include "staff-symbol.hh"
13 #include "paper-def.hh"
15 bool
16 Staff_symbol_referencer::has_interface (Grob*e)
18 return unsmob_grob (e->get_grob_property ("staff-symbol"))
19 || gh_number_p (e->get_grob_property ("staff-position"));
22 int
23 Staff_symbol_referencer::line_count (Grob*me)
25 Grob *st = staff_symbol_l (me);
26 return st ? Staff_symbol::line_count (st) : 0;
29 bool
30 Staff_symbol_referencer::on_staffline (Grob*me)
32 return on_staffline (me, (int) rint (position_f (me)));
35 bool
36 Staff_symbol_referencer::on_staffline (Grob*me, int pos)
38 int sz = line_count (me)-1;
39 return ((pos + sz) % 2) == 0;
42 Grob*
43 Staff_symbol_referencer::staff_symbol_l (Grob*me)
45 SCM st = me->get_grob_property ("staff-symbol");
46 return unsmob_grob (st);
49 Real
50 Staff_symbol_referencer::staff_space (Grob*me)
52 Grob * st = staff_symbol_l (me);
53 if (st)
54 return Staff_symbol::staff_space (st);
57 return 1.0;
61 Real
62 Staff_symbol_referencer::position_f (Grob*me)
64 Real p =0.0;
65 Grob * st = staff_symbol_l (me);
66 Grob * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
67 if (st && c)
69 Real y = me->relative_coordinate (c, Y_AXIS)
70 - st->relative_coordinate (c, Y_AXIS);
72 p += 2.0 * y / Staff_symbol::staff_space (st);
74 else
76 SCM pos = me->get_grob_property ("staff-position");
77 if (gh_number_p (pos))
78 return gh_scm2double (pos);
81 return p;
87 should use offset callback!
89 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
90 SCM
91 Staff_symbol_referencer::callback (SCM element_smob, SCM)
93 Grob *me = unsmob_grob (element_smob);
96 SCM pos = me->get_grob_property ("staff-position");
97 Real off =0.0;
98 if (gh_number_p (pos))
100 Real space = Staff_symbol_referencer::staff_space (me);
101 off = gh_scm2double (pos) * space/2.0;
104 me->set_grob_property ("staff-position", gh_double2scm (0.0));
106 return gh_double2scm (off);
111 This sets the position relative to the center of the staff symbol.
113 The function is hairy, because it can be callled in two situations:
115 1. There is no staff yet; we must set staff-position
117 2. There is a staff, and perhaps someone even applied a
118 translate_axis (). Then we must compensate for the translation
120 In either case, we set a callback to be sure that our new position
121 will be extracted from staff-position
124 void
125 Staff_symbol_referencer::set_position (Grob*me,Real p)
127 Grob * st = staff_symbol_l (me);
128 if (st && me->common_refpoint (st, Y_AXIS))
130 Real oldpos = position_f (me);
131 me->set_grob_property ("staff-position", gh_double2scm (p - oldpos));
133 else
135 me->set_grob_property ("staff-position",
136 gh_double2scm (p));
140 if (me->has_offset_callback_b (Staff_symbol_referencer::callback_proc, Y_AXIS))
141 return ;
143 me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
147 half of the height, in staff space.
149 Real
150 Staff_symbol_referencer::staff_radius (Grob*me)
152 return (line_count (me) -1) / 2;
157 compare_position (Grob *const &a, Grob * const &b)
159 return sign (Staff_symbol_referencer::position_f ((Grob*)a) -
160 Staff_symbol_referencer::position_f ((Grob*)b));
164 void
165 Staff_symbol_referencer::set_interface (Grob * e)
167 if (!gh_number_p (e->get_grob_property ("staff-position")))
168 e->set_grob_property ("staff-position", gh_double2scm (0.0));
170 e->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);