]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
b4a2351ea3348aa9a3e69596f87f607b468bf1ec
[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 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 Real
91 Staff_symbol_referencer::callback (Score_element * sc,Axis )
92 {
93   Score_element* me = (Score_element*)sc; // UGH.
94   
95   SCM pos = sc->get_elt_property ("staff-position");
96   Real off =0.0;
97   if (gh_number_p (pos))
98     {
99       Real space = Staff_symbol_referencer::staff_space (sc);
100       off = gh_scm2double (pos) * space/2.0;
101     }
102
103   me->set_elt_property ("staff-position", gh_double2scm (0.0));
104
105   return off;
106 }
107
108 /*
109   
110   This sets the position relative to the center of the staff symbol.
111
112   The function is hairy, because it can be callled in two situations:
113
114   1.  There is no staff yet; we must set staff-position
115
116   2.  There is a staff, and perhaps someone even applied a
117   translate_axis (). Then we must compensate for the translation
118   
119   In either case, we set a callback to be sure that our new position
120   will be extracted from staff-position
121   
122  */
123 void
124 Staff_symbol_referencer::set_position (Score_element*me,Real p)
125 {
126   Score_element * st = staff_symbol_l (me);
127   if (st && me->common_refpoint(st, Y_AXIS))
128     {
129       Real oldpos = position_f (me);
130       me->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
131     }
132   else
133     {
134       me->set_elt_property ("staff-position",
135                                 gh_double2scm (p));
136
137     }
138
139   if (me->has_offset_callback_b (callback, Y_AXIS))
140     return ; 
141
142   me->add_offset_callback (callback, Y_AXIS);
143 }
144
145 /*
146   half  of the height, in staff space.
147  */
148 Real
149 Staff_symbol_referencer::staff_radius (Score_element*me)
150 {
151   return  (line_count (me) -1) / 2;
152 }
153
154
155 int
156 compare_position (Score_element *const  &a, Score_element * const &b)
157 {
158   return sign (Staff_symbol_referencer::position_f((Score_element*)a) - 
159     Staff_symbol_referencer::position_f((Score_element*)b));
160 }