]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
Merge branch 'lilypond/translation' into staging
[lilypond.git] / lily / staff-symbol-referencer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2012 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "staff-symbol-referencer.hh"
21
22 #include "staff-symbol.hh"
23 #include "grob.hh"
24 #include "output-def.hh"
25 #include "libc-extension.hh"
26
27 int
28 Staff_symbol_referencer::line_count (Grob *me)
29 {
30   Grob *st = get_staff_symbol (me);
31   return st ? Staff_symbol::line_count (st) : 0;
32 }
33
34 bool
35 Staff_symbol_referencer::on_line (Grob *me, int pos)
36 {
37   Grob *st = get_staff_symbol (me);
38   return st ? Staff_symbol::on_line (st, pos) : false;
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   return internal_get_position (me, false);
79 }
80
81 Real
82 Staff_symbol_referencer::pure_get_position (Grob *me)
83 {
84   return internal_get_position (me, true);
85 }
86
87 Real
88 Staff_symbol_referencer::internal_get_position (Grob *me, bool pure)
89 {
90   Real p = 0.0;
91   Grob *st = get_staff_symbol (me);
92   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
93   if (st && c)
94     {
95       Real y = (pure
96                 ? me->pure_relative_y_coordinate (c, 0, INT_MAX)
97                 : me->relative_coordinate (c, Y_AXIS))
98                - st->relative_coordinate (c, Y_AXIS);
99       Real space = Staff_symbol::staff_space (st);
100       p = (space == 0) ? 0 : 2.0 * y / space;
101       return p;
102     }
103   else if (!st)
104     return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
105   return robust_scm2double (me->get_property ("staff-position"), p);
106 }
107
108 Interval
109 Staff_symbol_referencer::extent_in_staff (Grob *me)
110 {
111   Grob *st = get_staff_symbol (me);
112   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
113
114   Interval retval;
115   if (st && c)
116     {
117       retval = me->extent (c, Y_AXIS)
118                - st->relative_coordinate (c, Y_AXIS);
119     }
120
121   return retval;
122 }
123
124 int
125 Staff_symbol_referencer::get_rounded_position (Grob *me)
126 {
127   return int (rint (get_position (me)));
128 }
129
130 int
131 Staff_symbol_referencer::pure_get_rounded_position (Grob *me)
132 {
133   return int (rint (pure_get_position (me)));
134 }
135
136 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
137 SCM
138 Staff_symbol_referencer::callback (SCM smob)
139 {
140   Grob *me = unsmob_grob (smob);
141
142   SCM pos = me->get_property ("staff-position");
143   Real off = 0.0;
144   if (scm_is_number (pos))
145     {
146       Real space = Staff_symbol_referencer::staff_space (me);
147       off = scm_to_double (pos) * space / 2.0;
148     }
149
150   return scm_from_double (off);
151 }
152
153 /*  This sets the position relative to the center of the staff symbol.
154
155 The function is hairy, because it can be called in two situations:
156
157 1. There is no staff yet; we must set staff-position
158
159 2. There is a staff, and perhaps someone even applied a
160 translate_axis (). Then we must compensate for the translation
161
162 In either case, we set a callback to be sure that our new position
163 will be extracted from staff-position */
164
165 void
166 Staff_symbol_referencer::set_position (Grob *me, Real p)
167 {
168   internal_set_position (me, p, false);
169 }
170
171 void
172 Staff_symbol_referencer::pure_set_position (Grob *me, Real p)
173 {
174   internal_set_position (me, p, true);
175 }
176
177 void
178 Staff_symbol_referencer::internal_set_position (Grob *me, Real p, bool pure)
179 {
180   Grob *st = get_staff_symbol (me);
181   Real oldpos = 0.0;
182   if (st && me->common_refpoint (st, Y_AXIS))
183     {
184       oldpos = pure ? pure_get_position (me) : get_position (me);
185     }
186
187   Real ss = Staff_symbol_referencer::staff_space (me);
188   me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
189 }
190
191 /* Half of the height, in staff space, i.e. 2.0 for a normal staff. */
192 Real
193 Staff_symbol_referencer::staff_radius (Grob *me)
194 {
195   return (line_count (me) - 1) / 2.0;
196 }
197
198 int
199 compare_position (Grob *const &a, Grob *const &b)
200 {
201   return sign (Staff_symbol_referencer::get_position ((Grob *)a)
202                - Staff_symbol_referencer::get_position ((Grob *) b));
203 }
204
205 bool
206 position_less (Grob *const &a, Grob *const &b)
207 {
208   return Staff_symbol_referencer::get_position (a)
209          < Staff_symbol_referencer::get_position (b);
210 }
211
212 bool
213 pure_position_less (Grob *const &a, Grob *const &b)
214 {
215   return Staff_symbol_referencer::pure_get_position (a)
216          < Staff_symbol_referencer::pure_get_position (b);
217 }
218
219 ADD_INTERFACE (Staff_symbol_referencer,
220                "An object whose Y@tie{}position is meant relative to a staff"
221                " symbol.  These usually"
222                " have @code{Staff_symbol_referencer::callback} in their"
223                " @code{Y-offset-callbacks}.",
224
225                /* properties */
226                "staff-position "
227               );