]> git.donarmstrong.com Git - lilypond.git/blob - lily/staff-symbol-referencer.cc
Web-ja: update introduction
[lilypond.git] / lily / staff-symbol-referencer.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1999--2015 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   Grob *st = get_staff_symbol (me);
45   return st ? Staff_symbol::on_line (st, pos, false) : false;
46 }
47
48 Grob *
49 Staff_symbol_referencer::get_staff_symbol (Grob *me)
50 {
51   if (has_interface<Staff_symbol> (me))
52     return me;
53
54   SCM st = me->get_object ("staff-symbol");
55   return unsmob<Grob> (st);
56 }
57
58 Real
59 Staff_symbol_referencer::staff_space (Grob *me)
60 {
61   Grob *st = get_staff_symbol (me);
62   if (st)
63     return Staff_symbol::staff_space (st);
64   return 1.0;
65 }
66
67 Real
68 Staff_symbol_referencer::line_thickness (Grob *me)
69 {
70   Grob *st = get_staff_symbol (me);
71   if (st)
72     return Staff_symbol::get_line_thickness (st);
73   return me->layout ()->get_dimension (ly_symbol2scm ("line-thickness"));
74 }
75
76 Real
77 Staff_symbol_referencer::get_position (Grob *me)
78 {
79   return internal_get_position (me, false);
80 }
81
82 Real
83 Staff_symbol_referencer::pure_get_position (Grob *me)
84 {
85   return internal_get_position (me, true);
86 }
87
88 Real
89 Staff_symbol_referencer::internal_get_position (Grob *me, bool pure)
90 {
91   Real p = 0.0;
92   Grob *st = get_staff_symbol (me);
93   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
94   if (st && c)
95     {
96       Real y = (pure
97                 ? me->pure_relative_y_coordinate (c, 0, INT_MAX)
98                 : me->relative_coordinate (c, Y_AXIS))
99                - st->relative_coordinate (c, Y_AXIS);
100       Real space = Staff_symbol::staff_space (st);
101       p = (space == 0) ? 0 : 2.0 * y / space;
102       return p;
103     }
104   else if (!st)
105     return me->relative_coordinate (me->get_parent (Y_AXIS), Y_AXIS) * 2;
106   return robust_scm2double (me->get_property ("staff-position"), p);
107 }
108
109 Interval
110 Staff_symbol_referencer::extent_in_staff (Grob *me)
111 {
112   Grob *st = get_staff_symbol (me);
113   Grob *c = st ? me->common_refpoint (st, Y_AXIS) : 0;
114
115   Interval retval;
116   if (st && c)
117     {
118       retval = me->extent (c, Y_AXIS)
119                - st->relative_coordinate (c, Y_AXIS);
120     }
121
122   return retval;
123 }
124
125 int
126 Staff_symbol_referencer::get_rounded_position (Grob *me)
127 {
128   return int (rint (get_position (me)));
129 }
130
131 int
132 Staff_symbol_referencer::pure_get_rounded_position (Grob *me)
133 {
134   return int (rint (pure_get_position (me)));
135 }
136
137 MAKE_SCHEME_CALLBACK (Staff_symbol_referencer, callback, 1);
138 SCM
139 Staff_symbol_referencer::callback (SCM smob)
140 {
141   Grob *me = unsmob<Grob> (smob);
142
143   SCM pos = me->get_property ("staff-position");
144   Real off = 0.0;
145   if (scm_is_number (pos))
146     {
147       Real space = Staff_symbol_referencer::staff_space (me);
148       off = scm_to_double (pos) * space / 2.0;
149     }
150
151   return scm_from_double (off);
152 }
153
154 /*  This sets the position relative to the center of the staff symbol.
155
156 The function is hairy, because it can be called in two situations:
157
158 1. There is no staff yet; we must set staff-position
159
160 2. There is a staff, and perhaps someone even applied a
161 translate_axis (). Then we must compensate for the translation
162
163 In either case, we set a callback to be sure that our new position
164 will be extracted from staff-position */
165
166 void
167 Staff_symbol_referencer::set_position (Grob *me, Real p)
168 {
169   internal_set_position (me, p, false);
170 }
171
172 void
173 Staff_symbol_referencer::pure_set_position (Grob *me, Real p)
174 {
175   internal_set_position (me, p, true);
176 }
177
178 void
179 Staff_symbol_referencer::internal_set_position (Grob *me, Real p, bool pure)
180 {
181   Grob *st = get_staff_symbol (me);
182   Real oldpos = 0.0;
183   if (st && me->common_refpoint (st, Y_AXIS))
184     {
185       oldpos = pure ? pure_get_position (me) : get_position (me);
186     }
187
188   Real ss = Staff_symbol_referencer::staff_space (me);
189   me->translate_axis ((p - oldpos) * ss * 0.5, Y_AXIS);
190 }
191
192 Interval
193 Staff_symbol_referencer::staff_span (Grob *me)
194 {
195   Interval result;
196   if (me)
197     if (Grob *symb = get_staff_symbol (me))
198       result = Staff_symbol::line_span (symb);
199   return result;
200 }
201
202 Real
203 Staff_symbol_referencer::staff_radius (Grob *me)
204 {
205   /*
206     line_span is measured in pitch steps, not in staff spaces
207   */
208   return staff_span (me).length () / 4.0;
209 }
210
211 int
212 compare_position (Grob *const &a, Grob *const &b)
213 {
214   return sign (Staff_symbol_referencer::get_position ((Grob *)a)
215                - Staff_symbol_referencer::get_position ((Grob *) b));
216 }
217
218 bool
219 position_less (Grob *const &a, Grob *const &b)
220 {
221   return Staff_symbol_referencer::get_position (a)
222          < Staff_symbol_referencer::get_position (b);
223 }
224
225 bool
226 pure_position_less (Grob *const &a, Grob *const &b)
227 {
228   return Staff_symbol_referencer::pure_get_position (a)
229          < Staff_symbol_referencer::pure_get_position (b);
230 }
231
232 ADD_INTERFACE (Staff_symbol_referencer,
233                "An object whose Y@tie{}position is meant relative to a staff"
234                " symbol.  These usually"
235                " have @code{Staff_symbol_referencer::callback} in their"
236                " @code{Y-offset-callbacks}.",
237
238                /* properties */
239                "staff-position "
240               );