* stepmake/stepmake/metafont-rules.make: backport 1.7 fixes.
[lilypond.git] / lily / staff-symbol-referencer.cc
blobfd7bcd2791d870ed90639298ca32bf4fec188a01
1 /*
2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2002 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 = get_staff_symbol (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 (get_position (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::get_staff_symbol (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 = get_staff_symbol (me);
53 if (st)
54 return Staff_symbol::staff_space (st);
56 return 1.0;
59 Real
60 Staff_symbol_referencer::get_position (Grob*me)
62 Real p =0.0;
63 Grob * st = get_staff_symbol (me);
64 Grob * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
65 if (st && c)
67 Real y = me->relative_coordinate (c, Y_AXIS)
68 - st->relative_coordinate (c, Y_AXIS);
70 p += 2.0 * y / Staff_symbol::staff_space (st);
72 else
74 SCM pos = me->get_grob_property ("staff-position");
75 if (gh_number_p (pos))
76 return gh_scm2double (pos);
79 return p;
85 should use offset callback!
87 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
88 SCM
89 Staff_symbol_referencer::callback (SCM element_smob, SCM)
91 Grob *me = unsmob_grob (element_smob);
94 SCM pos = me->get_grob_property ("staff-position");
95 Real off =0.0;
96 if (gh_number_p (pos))
98 Real space = Staff_symbol_referencer::staff_space (me);
99 off = gh_scm2double (pos) * space/2.0;
100 me->set_grob_property ("staff-position", gh_double2scm (0.0));
103 return gh_double2scm (off);
108 This sets the position relative to the center of the staff symbol.
110 The function is hairy, because it can be callled in two situations:
112 1. There is no staff yet; we must set staff-position
114 2. There is a staff, and perhaps someone even applied a
115 translate_axis (). Then we must compensate for the translation
117 In either case, we set a callback to be sure that our new position
118 will be extracted from staff-position
121 void
122 Staff_symbol_referencer::set_position (Grob*me,Real p)
124 Grob * st = get_staff_symbol (me);
125 if (st && me->common_refpoint (st, Y_AXIS))
127 Real oldpos = get_position (me);
128 me->set_grob_property ("staff-position", gh_double2scm (p - oldpos));
130 else
132 me->set_grob_property ("staff-position",
133 gh_double2scm (p));
137 if (me->has_offset_callback_b (Staff_symbol_referencer::callback_proc, Y_AXIS))
138 return ;
140 me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
144 half of the height, in staff space.
146 Real
147 Staff_symbol_referencer::staff_radius (Grob*me)
149 return (line_count (me) -1) / 2;
154 compare_position (Grob *const &a, Grob * const &b)
156 return sign (Staff_symbol_referencer::get_position ((Grob*)a) -
157 Staff_symbol_referencer::get_position ((Grob*)b));
163 #define has_interface ugly_hack
164 ADD_INTERFACE (Staff_symbol_referencer,"staff-symbol-referencer-interface",
165 "Object whose Y position is meaning with reference to a staff
166 symbol. Objects that have this interface should include
167 Staff_symbol_referencer::callback in their Y-offset-callback.
169 "staff-position");