]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
2003 -> 2004
[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--2004 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 #include "libc-extension.hh" 
15
16
17 int
18 Staff_symbol_referencer::line_count (Grob*me) 
19 {
20   Grob *st = get_staff_symbol (me);
21   return st  ?  Staff_symbol::line_count (st) : 0;
22 }
23
24 bool
25 Staff_symbol_referencer::on_staffline (Grob*me)
26 {
27   return on_staffline (me, (int) rint (get_position (me)));
28 }
29
30 bool
31 Staff_symbol_referencer::on_staffline (Grob*me, int pos)
32 {
33   int sz = line_count (me)-1;
34   return ((pos + sz) % 2) == 0;
35 }
36
37 Grob*
38 Staff_symbol_referencer::get_staff_symbol (Grob*me) 
39 {
40   SCM st = me->get_grob_property ("staff-symbol");
41   return unsmob_grob (st);
42 }
43
44 Real
45 Staff_symbol_referencer::staff_space (Grob*me) 
46 {
47   Grob * st = get_staff_symbol (me);
48   if (st)
49     return Staff_symbol::staff_space (st);
50
51   return 1.0;
52 }
53
54 Real
55 Staff_symbol_referencer::line_thickness (Grob*me) 
56 {
57   Grob * st = get_staff_symbol (me);
58   if (st)
59     return Staff_symbol::get_line_thickness (st);
60
61   return me->get_paper ()->get_realvar (ly_symbol2scm ("linethickness"));
62 }
63
64 Real
65 Staff_symbol_referencer::get_position (Grob*me) 
66 {
67   Real p =0.0;
68   Grob * st = get_staff_symbol (me);
69   Grob * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
70   if (st && c)
71     {
72       Real y = me->relative_coordinate (c, Y_AXIS)
73         - st->relative_coordinate (c, Y_AXIS);
74
75       p += 2.0 * y / Staff_symbol::staff_space (st);
76       return p;
77     }
78
79   return robust_scm2double ( me->get_grob_property ("staff-position"), p);
80 }
81
82
83 LY_DEFINE(ly_grob_staff_position,
84           "ly:grob-staff-position",
85           1, 0,0, (SCM sg),
86           "Return the Y-position of this grob relative to the staff.")
87 {
88   Grob * g = unsmob_grob (sg);
89
90   SCM_ASSERT_TYPE (g, sg, SCM_ARG1, __FUNCTION__, "grob");
91   Real pos = Staff_symbol_referencer::get_position (g);
92
93   if (fabs (rint (pos) -pos) < 1e-6) // ugh.
94     return gh_int2scm ((int) my_round (pos));
95   else
96     return gh_double2scm (pos);
97 }
98
99
100 /*
101   should use offset callback!
102  */
103 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer,callback,2);
104 SCM
105 Staff_symbol_referencer::callback (SCM element_smob, SCM)
106 {
107   Grob *me = unsmob_grob (element_smob);
108
109   SCM pos = me->get_grob_property ("staff-position");
110   Real off =0.0;
111   if (gh_number_p (pos))
112     {
113       Real space = Staff_symbol_referencer::staff_space (me);
114       off = gh_scm2double (pos) * space/2.0;
115       me->set_grob_property ("staff-position", gh_int2scm (0));
116     }
117
118   return gh_double2scm (off);
119 }
120
121  /*
122   
123   This sets the position relative to the center of the staff symbol.
124  
125   The function is hairy, because it can be callled in two situations:
126
127   1.  There is no staff yet; we must set staff-position
128
129   2.  There is a staff, and perhaps someone even applied a
130   translate_axis (). Then we must compensate for the translation
131   
132   In either case, we set a callback to be sure that our new position
133   will be extracted from staff-position
134   
135  */
136 void
137 Staff_symbol_referencer::set_position (Grob*me,Real p)
138 {
139   Grob * st = get_staff_symbol (me);
140   if (st && me->common_refpoint (st, Y_AXIS))
141     {
142       Real oldpos = get_position (me);
143       me->set_grob_property ("staff-position", gh_double2scm (p - oldpos));
144     }
145   else
146     {
147       me->set_grob_property ("staff-position",
148                             gh_double2scm (p));
149
150     }
151
152   if (me->has_offset_callback_b (Staff_symbol_referencer::callback_proc, Y_AXIS))
153     return ; 
154
155   me->add_offset_callback (Staff_symbol_referencer::callback_proc, Y_AXIS);
156 }
157
158 /*
159   Half of the height, in staff space, i.e. 2.0 for a normal staff. 
160 */
161 Real
162 Staff_symbol_referencer::staff_radius (Grob*me)
163 {
164   return (line_count (me) -1) / 2.0;
165 }
166
167
168 int
169 compare_position (Grob *const  &a, Grob * const &b)
170 {
171   return sign (Staff_symbol_referencer::get_position ((Grob*)a) - 
172     Staff_symbol_referencer::get_position ((Grob*)b));
173 }
174
175 ADD_INTERFACE (Staff_symbol_referencer,"staff-symbol-referencer-interface",
176   "Object whose Y position is meaning with reference to a staff "
177 "symbol. Objects that have this interface should include "
178 "Staff_symbol_referencer::callback in their Y-offset-callback. "
179 ,
180   "staff-position");