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