lilypond-1.3.74
[lilypond.git] / lily / staff-symbol-referencer.cc
blob85b52f5aabc69a605ee96a33f28e9dea22aad621
1 /*
2 staff-symbol-referencer.cc -- implement Staff_symbol_referencer
4 source file of the GNU LilyPond music typesetter
6 (c) 1999--2000 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"
18 void
19 Staff_symbol_referencer::set_interface (Score_element * e)
21 if (!gh_number_p (e->get_elt_property ("staff-position")))
22 e->set_elt_property ("staff-position", gh_double2scm (0.0));
24 e->add_offset_callback (callback, Y_AXIS);
27 bool
28 Staff_symbol_referencer::has_interface (Score_element*e)
30 return unsmob_element (e->get_elt_property ("staff-symbol"))
31 || gh_number_p (e->get_elt_property ("staff-position"));
35 int
36 Staff_symbol_referencer::line_count (Score_element*me)
38 Score_element *st = staff_symbol_l (me);
39 return st ? Staff_symbol::line_count (st) : 0;
42 Score_element*
43 Staff_symbol_referencer::staff_symbol_l (Score_element*me)
45 SCM st = me->get_elt_property ("staff-symbol");
46 return unsmob_element(st);
49 Real
50 Staff_symbol_referencer::staff_space (Score_element*me)
52 Score_element * st = staff_symbol_l (me);
53 if (st)
54 return Staff_symbol::staff_space (st);
55 else if (me->pscore_l_ && me->paper_l ())
56 return me->paper_l ()->get_var ("interline");
58 return 0.0;
62 Real
63 Staff_symbol_referencer::position_f (Score_element*me)
65 Real p =0.0;
66 Score_element * st = staff_symbol_l (me);
67 Score_element * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
68 if (st && c)
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);
75 else
77 SCM pos = me->get_elt_property ("staff-position");
78 if (gh_number_p (pos))
79 return gh_scm2double (pos);
82 return p;
88 should use offset callback!
90 Real
91 Staff_symbol_referencer::callback (Score_element * sc,Axis )
93 Score_element* me = (Score_element*)sc; // UGH.
95 SCM pos = sc->get_elt_property ("staff-position");
96 Real off =0.0;
97 if (gh_number_p (pos))
99 Real space = Staff_symbol_referencer::staff_space (sc);
100 off = gh_scm2double (pos) * space/2.0;
103 me->set_elt_property ("staff-position", gh_double2scm (0.0));
105 return off;
110 This sets the position relative to the center of the staff symbol.
112 The function is hairy, because it can be callled in two situations:
114 1. There is no staff yet; we must set staff-position
116 2. There is a staff, and perhaps someone even applied a
117 translate_axis (). Then we must compensate for the translation
119 In either case, we set a callback to be sure that our new position
120 will be extracted from staff-position
123 void
124 Staff_symbol_referencer::set_position (Score_element*me,Real p)
126 Score_element * st = staff_symbol_l (me);
127 if (st && me->common_refpoint(st, Y_AXIS))
129 Real oldpos = position_f (me);
130 me->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
132 else
134 me->set_elt_property ("staff-position",
135 gh_double2scm (p));
139 if (me->has_offset_callback_b (callback, Y_AXIS))
140 return ;
142 me->add_offset_callback (callback, Y_AXIS);
146 half of the height, in staff space.
148 Real
149 Staff_symbol_referencer::staff_radius (Score_element*me)
151 return (line_count (me) -1) / 2;
156 compare_position (Score_element *const &a, Score_element * const &b)
158 return sign (Staff_symbol_referencer::position_f((Score_element*)a) -
159 Staff_symbol_referencer::position_f((Score_element*)b));