]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.5.48
[lilypond.git] / lily / staff-symbol-referencer.cc
1 /*   
2   staff-symbol-referencer.cc -- implement Staff_symbol_referencer
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 1999--2002 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7  */
8
9 #include <math.h>
10
11 #include "staff-symbol-referencer.hh"
12 #include "staff-symbol.hh"
13 #include "paper-def.hh"
14  
15 bool
16 Staff_symbol_referencer::has_interface (Grob*e)
17 {
18   return unsmob_grob (e->get_grob_property ("staff-symbol"))
19     || gh_number_p (e->get_grob_property ("staff-position"));
20 }
21
22 int
23 Staff_symbol_referencer::line_count (Grob*me) 
24 {
25   Grob *st = staff_symbol_l (me);
26   return st  ?  Staff_symbol::line_count (st) : 0;
27 }
28
29 bool
30 Staff_symbol_referencer::on_staffline (Grob*me)
31 {
32   return on_staffline (me, (int) rint (position_f (me)));
33 }
34
35 bool
36 Staff_symbol_referencer::on_staffline (Grob*me, int pos)
37 {
38   int sz = line_count (me)-1;
39   return ((pos + sz) % 2) == 0;
40 }
41
42 Grob*
43 Staff_symbol_referencer::staff_symbol_l (Grob*me) 
44 {
45   SCM st = me->get_grob_property ("staff-symbol");
46   return unsmob_grob (st);
47 }
48
49 Real
50 Staff_symbol_referencer::staff_space (Grob*me) 
51 {
52   Grob * st = staff_symbol_l (me);
53   if (st)
54     return Staff_symbol::staff_space (st);
55
56   return 1.0;
57 }
58
59 Real
60 Staff_symbol_referencer::position_f (Grob*me) 
61 {
62   Real p =0.0;
63   Grob * st = staff_symbol_l (me);
64   Grob * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
65   if (st && c)
66     {
67       Real y = me->relative_coordinate (c, Y_AXIS)
68         - st->relative_coordinate (c, Y_AXIS);
69
70       p += 2.0 * y / Staff_symbol::staff_space (st);
71     }
72   else
73     {
74       SCM pos = me->get_grob_property ("staff-position");
75       if (gh_number_p (pos))
76         return gh_scm2double (pos);
77     }
78   
79   return  p;
80 }
81
82
83
84 /*
85   should use offset callback!
86  */
87 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
88 SCM
89 Staff_symbol_referencer::callback (SCM element_smob, SCM)
90 {
91   Grob *me = unsmob_grob (element_smob);
92
93   
94   SCM pos = me->get_grob_property ("staff-position");
95   Real off =0.0;
96   if (gh_number_p (pos))
97     {
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));
101     }
102
103   return gh_double2scm (off);
104 }
105
106  /*
107   
108   This sets the position relative to the center of the staff symbol.
109  
110   The function is hairy, because it can be callled in two situations:
111
112   1.  There is no staff yet; we must set staff-position
113
114   2.  There is a staff, and perhaps someone even applied a
115   translate_axis (). Then we must compensate for the translation
116   
117   In either case, we set a callback to be sure that our new position
118   will be extracted from staff-position
119   
120  */
121 void
122 Staff_symbol_referencer::set_position (Grob*me,Real p)
123 {
124   Grob * st = staff_symbol_l (me);
125   if (st && me->common_refpoint (st, Y_AXIS))
126     {
127       Real oldpos = position_f (me);
128       me->set_grob_property ("staff-position", gh_double2scm (p - oldpos));
129     }
130   else
131     {
132       me->set_grob_property ("staff-position",
133                             gh_double2scm (p));
134
135     }
136
137   if (me->has_offset_callback_b (Staff_symbol_referencer::callback_proc, Y_AXIS))
138     return ; 
139
140   me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
141 }
142
143 /*
144   half  of the height, in staff space.
145  */
146 Real
147 Staff_symbol_referencer::staff_radius (Grob*me)
148 {
149   return (line_count (me) -1) / 2;
150 }
151
152
153 int
154 compare_position (Grob *const  &a, Grob * const &b)
155 {
156   return sign (Staff_symbol_referencer::position_f ((Grob*)a) - 
157     Staff_symbol_referencer::position_f ((Grob*)b));
158 }
159
160
161
162
163
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.
168 ",
169   "staff-position");