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