]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
release: 1.3.93
[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
16
17
18 void
19 Staff_symbol_referencer::set_interface (Score_element * e)
20 {
21   if (!gh_number_p (e->get_elt_property ("staff-position")))
22       e->set_elt_property ("staff-position", gh_double2scm (0.0));
23       
24   e->add_offset_callback (callback, Y_AXIS);
25 }
26
27 bool
28 Staff_symbol_referencer::has_interface (Score_element*e)
29 {
30   return unsmob_element (e->get_elt_property ("staff-symbol"))
31     || gh_number_p (e->get_elt_property ("staff-position"));
32 }
33
34
35 int
36 Staff_symbol_referencer::line_count (Score_element*me) 
37 {
38   Score_element *st = staff_symbol_l (me);
39   return st  ?  Staff_symbol::line_count (st) : 0;
40 }
41
42 bool
43 Staff_symbol_referencer::on_staffline (Score_element*me)
44 {
45   return on_staffline (me, (int) position_f (me));
46 }
47
48 bool
49 Staff_symbol_referencer::on_staffline (Score_element*me, int pos)
50 {
51   int sz = line_count (me)-1;
52   return ((pos + sz) % 2) == 0;
53 }
54
55 Score_element*
56 Staff_symbol_referencer::staff_symbol_l (Score_element*me) 
57 {
58   SCM st = me->get_elt_property ("staff-symbol");
59   return unsmob_element(st);
60 }
61
62 Real
63 Staff_symbol_referencer::staff_space (Score_element*me) 
64 {
65   Score_element * st = staff_symbol_l (me);
66   if (st)
67     return Staff_symbol::staff_space (st);
68   else if (me->pscore_l_ && me->paper_l ())
69     return me->paper_l ()->get_var ("staffspace");
70  
71   return 0.0;
72 }
73
74
75 Real
76 Staff_symbol_referencer::position_f (Score_element*me) 
77 {
78   Real p =0.0;
79   Score_element * st = staff_symbol_l (me);
80   Score_element * c = st ? me->common_refpoint (st, Y_AXIS) : 0;
81   if (st && c)
82     {
83       Real y = me->relative_coordinate (c, Y_AXIS)
84         - st->relative_coordinate (c, Y_AXIS);
85
86       p += 2.0 * y / Staff_symbol::staff_space (st);
87     }
88   else
89     {
90       SCM pos = me->get_elt_property ("staff-position");
91       if (gh_number_p (pos))
92         return gh_scm2double (pos);
93     }
94   
95   return  p;
96 }
97
98
99
100 /*
101   should use offset callback!
102  */
103 Real
104 Staff_symbol_referencer::callback (Score_element * sc,Axis )
105 {
106   Score_element* me = (Score_element*)sc; // UGH.
107   
108   SCM pos = sc->get_elt_property ("staff-position");
109   Real off =0.0;
110   if (gh_number_p (pos))
111     {
112       Real space = Staff_symbol_referencer::staff_space (sc);
113       off = gh_scm2double (pos) * space/2.0;
114     }
115
116   me->set_elt_property ("staff-position", gh_double2scm (0.0));
117
118   return 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 (Score_element*me,Real p)
138 {
139   Score_element * st = staff_symbol_l (me);
140   if (st && me->common_refpoint(st, Y_AXIS))
141     {
142       Real oldpos = position_f (me);
143       me->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
144     }
145   else
146     {
147       me->set_elt_property ("staff-position",
148                                 gh_double2scm (p));
149
150     }
151
152   if (me->has_offset_callback_b (callback, Y_AXIS))
153     return ; 
154
155   me->add_offset_callback (callback, Y_AXIS);
156 }
157
158 /*
159   half  of the height, in staff space.
160  */
161 Real
162 Staff_symbol_referencer::staff_radius (Score_element*me)
163 {
164   return  (line_count (me) -1) / 2;
165 }
166
167
168 int
169 compare_position (Score_element *const  &a, Score_element * const &b)
170 {
171   return sign (Staff_symbol_referencer::position_f((Score_element*)a) - 
172     Staff_symbol_referencer::position_f((Score_element*)b));
173 }