]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
Merge branch 'master' of ssh://jeancharlesm@git.sv.gnu.org/srv/git/lilypond
[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--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "staff-symbol-referencer.hh"
10
11 #include "staff-symbol.hh"
12 #include "grob.hh"
13 #include "output-def.hh"
14 #include "libc-extension.hh"
15
16 int
17 Staff_symbol_referencer::line_count (Grob *me)
18 {
19   Grob *st = get_staff_symbol (me);
20   return st ? Staff_symbol::line_count (st) : 0;
21 }
22
23 bool
24 Staff_symbol_referencer::on_line (Grob *me)
25 {
26   return on_line (me, (int) rint (get_position (me)));
27 }
28
29 bool
30 Staff_symbol_referencer::on_staff_line (Grob *me)
31 {
32   return on_staff_line (me, (int) rint (get_position (me)));
33 }
34
35 bool
36 Staff_symbol_referencer::on_line (Grob *me, int pos)
37 {
38   return Staff_symbol::on_line (me, pos);
39 }
40
41 bool
42 Staff_symbol_referencer::on_staff_line (Grob *me, int pos)
43 {
44   return on_line (me, pos) && abs (pos) <= 2 * staff_radius (me);
45 }
46
47 Grob *
48 Staff_symbol_referencer::get_staff_symbol (Grob *me)
49 {
50   if (Staff_symbol::has_interface (me))
51     return me;
52
53   SCM st = me->get_object ("staff-symbol");
54   return unsmob_grob (st);
55 }
56
57 Real
58 Staff_symbol_referencer::staff_space (Grob *me)
59 {
60   Grob *st = get_staff_symbol (me);
61   if (st)
62     return Staff_symbol::staff_space (st);
63   return 1.0;
64 }
65
66 Real
67 Staff_symbol_referencer::line_thickness (Grob *me)
68 {
69   Grob *st = get_staff_symbol (me);
70   if (st)
71     return Staff_symbol::get_line_thickness (st);
72   return me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
73 }
74
75 Real
76 Staff_symbol_referencer::get_position (Grob *me)
77 {
78   Real p = 0.0;
79   Grob *st = get_staff_symbol (me);
80   Grob *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       return p;
88     }
89   else if (!st)
90     return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
91   return robust_scm2double (me->get_property ("staff-position"), p);
92 }
93
94
95 Interval
96 Staff_symbol_referencer::extent_in_staff (Grob *me)
97 {
98   Grob *st = get_staff_symbol (me);
99   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
100
101   Interval retval;
102   if (st && c)
103     {
104       retval  = me->extent (c, Y_AXIS)
105         - st->relative_coordinate (c, Y_AXIS);
106     }
107
108   return retval;
109 }
110
111 int
112 Staff_symbol_referencer::get_rounded_position (Grob *me)
113 {
114   return int (rint (get_position (me)));
115 }
116
117 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
118 SCM
119 Staff_symbol_referencer::callback (SCM smob)
120 {
121   Grob *me = unsmob_grob (smob);
122
123   SCM pos = me->get_property ("staff-position");
124   Real off = 0.0;
125   if (scm_is_number (pos))
126     {
127       Real space = Staff_symbol_referencer::staff_space (me);
128       off = scm_to_double (pos) * space / 2.0;
129     }
130
131   return scm_from_double (off);
132 }
133
134 /*  This sets the position relative to the center of the staff symbol.
135
136 The function is hairy, because it can be called in two situations:
137
138 1. There is no staff yet; we must set staff-position
139
140 2. There is a staff, and perhaps someone even applied a
141 translate_axis (). Then we must compensate for the translation
142
143 In either case, we set a callback to be sure that our new position
144 will be extracted from staff-position */
145
146 void
147 Staff_symbol_referencer::set_position (Grob *me, Real p)
148 {
149   Grob *st = get_staff_symbol (me);
150   Real oldpos = 0.0;
151   if (st && me->common_refpoint (st, Y_AXIS))
152     {
153       oldpos = get_position (me);
154     }
155
156   
157   Real ss = Staff_symbol_referencer::staff_space (me);
158   me->translate_axis ((p  - oldpos) * ss * 0.5, Y_AXIS);
159 }
160
161 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
162 Real
163 Staff_symbol_referencer::staff_radius (Grob *me)
164 {
165   return (line_count (me) - 1) / 2.0;
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 bool
176 position_less (Grob *const &a, Grob *const &b)
177 {
178   return Staff_symbol_referencer::get_position (a)
179     < Staff_symbol_referencer::get_position (b);
180 }
181
182 ADD_INTERFACE (Staff_symbol_referencer,
183                "An object whose Y@tie{}position is meant relative to a staff"
184                " symbol.  These usually"
185                " have @code{Staff_symbol_referencer::callback} in their"
186                " @code{Y-offset-callbacks}.",
187
188                /* properties */
189                "staff-position "
190                );