]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
patch::: 1.3.136.jcn3
[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--2001 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   
57   return 1.0;
58 }
59
60
61 Real
62 Staff_symbol_referencer::position_f (Grob*me) 
63 {
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)
68     {
69       Real y = me->relative_coordinate (c, Y_AXIS)
70         - st->relative_coordinate (c, Y_AXIS);
71
72       p += 2.0 * y / Staff_symbol::staff_space (st);
73     }
74   else
75     {
76       SCM pos = me->get_grob_property ("staff-position");
77       if (gh_number_p (pos))
78         return gh_scm2double (pos);
79     }
80   
81   return  p;
82 }
83
84
85
86 /*
87   should use offset callback!
88  */
89 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
90 SCM
91 Staff_symbol_referencer::callback (SCM element_smob, SCM)
92 {
93   Grob *me = unsmob_grob (element_smob);
94
95   
96   SCM pos = me->get_grob_property ("staff-position");
97   Real off =0.0;
98   if (gh_number_p (pos))
99     {
100       Real space = Staff_symbol_referencer::staff_space (me);
101       off = gh_scm2double (pos) * space/2.0;
102     }
103
104   me->set_grob_property ("staff-position", gh_double2scm (0.0));
105
106   return gh_double2scm (off);
107 }
108
109  /*
110   
111   This sets the position relative to the center of the staff symbol.
112  
113   The function is hairy, because it can be callled in two situations:
114
115   1.  There is no staff yet; we must set staff-position
116
117   2.  There is a staff, and perhaps someone even applied a
118   translate_axis (). Then we must compensate for the translation
119   
120   In either case, we set a callback to be sure that our new position
121   will be extracted from staff-position
122   
123  */
124 void
125 Staff_symbol_referencer::set_position (Grob*me,Real p)
126 {
127   Grob * st = staff_symbol_l (me);
128   if (st && me->common_refpoint (st, Y_AXIS))
129     {
130       Real oldpos = position_f (me);
131       me->set_grob_property ("staff-position", gh_double2scm (p - oldpos));
132     }
133   else
134     {
135       me->set_grob_property ("staff-position",
136                             gh_double2scm (p));
137
138     }
139
140   if (me->has_offset_callback_b (Staff_symbol_referencer::callback_proc, Y_AXIS))
141     return ; 
142
143   me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
144 }
145
146 /*
147   half  of the height, in staff space.
148  */
149 Real
150 Staff_symbol_referencer::staff_radius (Grob*me)
151 {
152   return (line_count (me) -1) / 2;
153 }
154
155
156 int
157 compare_position (Grob *const  &a, Grob * const &b)
158 {
159   return sign (Staff_symbol_referencer::position_f ((Grob*)a) - 
160     Staff_symbol_referencer::position_f ((Grob*)b));
161 }
162
163
164 void
165 Staff_symbol_referencer::set_interface (Grob * e)
166 {
167   if (!gh_number_p (e->get_grob_property ("staff-position")))
168       e->set_grob_property ("staff-position", gh_double2scm (0.0));
169
170   e->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
171 }
172